简体   繁体   English

ElasticSearch结合了条款和匹配查询

[英]ElasticSearch combine Terms and Match query

I have worked out the elasticsearch query I need via Console, and it works as expected: 我已经通过控制台解决了我需要的elasticsearch查询,并且它按预期工作:

                    "query": {
                    "bool": {
                        "must": [
                        { "terms": { "color": ["red", "blue"]
                            }
                        },
                        { "match": { "availability":   "in stock" }} 
                        ]
                    }
               }

I now need to do this using the nest client. 我现在需要使用巢状客户端执行此操作。 I have currently tried: 我目前已尝试:

                nestClient.Search<Item>(
                s => s.From(query.Page).Size(query.PageSize)
                    .Query(
                        q => q.Bool(
                            b => b.Must(
                                ss => ss.Terms(t => t.Field(f => f.Color).Terms<string>(query.Color)),
                                ss => ss.Match(m => m.Field(f => f.Availability == "in stock"))))).TypedKeys(null));

But when looking at the outputted JSON in fiddler it seems to be ignoring the query and using: 但是,在fiddler中查看输出的JSON时,似乎忽略了查询并使用:

{"from":0,"size":24}

If I remove the match part of the nest query then the outputted JSON DSL is correctly using the Terms query. 如果我删除了嵌套查询的match部分,则输出的JSON DSL使用条款查询是正确的。

Is it possible to do what I am looking for in one pass? 一口气可以做我想要的吗?

The query value for the match query needs to be passed to the .Query(...) method match查询的查询值需要传递给.Query(...)方法

var query = new {
    Page = 0,
    PageSize = 10,
    Color = new [] { "red", "yellow" }
};

nestClient.Search<Item>(s => s
    .From(query.Page)
    .Size(query.PageSize)
    .Query(q => q
        .Bool(b => b
            .Must(
                ss => ss.Terms(t => t.Field(f => f.Color).Terms<string>(query.Color)),
                ss => ss.Match(m => m.Field(f => f.Availability).Query("in stock"))
            )
        )
    )
    .TypedKeys(null)
);

This can be shortened further using operator overloading on the base query type , to construct a bool query more succinctly 使用基本查询类型的运算符重载可以进一步缩短此时间,以更简洁地构造bool查询

nestClient.Search<Item>(s => s
    .From(query.Page)
    .Size(query.PageSize)
    .Query(q => q
        .Terms(t => t.Field(f => f.Color).Terms<string>(query.Color)) && q
        .Match(m => m.Field(f => f.Availability).Query("in stock"))
    )
    .TypedKeys(null)
);

Both will produce the following query 两者都会产生以下查询

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "color": [
              "red",
              "yellow"
            ]
          }
        },
        {
          "match": {
            "availability": {
              "query": "in stock"
            }
          }
        }
      ]
    }
  }
}

Additionally, with term-level queries like terms query, the answer to the query is typically yes or no eg does this term match exactly, is this number in this range of numbers, etc. With predicates like this, you typically don't need the relevancy scoring phase to be executed for these queries, which you can forgo by having them execute in a filter context, such as the filter clause of a bool query. 另外,对于像terms查询这样的terms 级别的查询,查询的答案通常是“是”或“否”,例如,该词是否完全匹配,该数字是否在此数字范围内,等等。使用这样的谓词,您通常不需要这些查询要执行的相关性评分阶段,可以通过在filter上下文(例如bool查询的filter子句)中执行它们来放弃。 Putting this together with operator overloading 将其与运算符重载放在一起

nestClient.Search<Item>(s => s
    .From(query.Page)
    .Size(query.PageSize)
    .Query(q => q
        .Terms(t => t.Field(f => f.Color).Terms<string>(query.Color)) && +q
        .Match(m => m.Field(f => f.Availability).Query("in stock"))
    )
    .TypedKeys(null)
);

which yields 产生

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "color": [
              "red",
              "yellow"
            ]
          }
        }
      ],
      "filter": [
        {
          "match": {
            "availability": {
              "query": "in stock"
            }
          }
        }
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM