簡體   English   中英

ElasticSearch結合了條款和匹配查詢

[英]ElasticSearch combine Terms and Match query

我已經通過控制台解決了我需要的elasticsearch查詢,並且它按預期工作:

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

我現在需要使用巢狀客戶端執行此操作。 我目前已嘗試:

                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));

但是,在fiddler中查看輸出的JSON時,似乎忽略了查詢並使用:

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

如果我刪除了嵌套查詢的match部分,則輸出的JSON DSL使用條款查詢是正確的。

一口氣可以做我想要的嗎?

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)
);

使用基本查詢類型的運算符重載可以進一步縮短此時間,以更簡潔地構造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)
);

兩者都會產生以下查詢

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

另外,對於像terms查詢這樣的terms 級別的查詢,查詢的答案通常是“是”或“否”,例如,該詞是否完全匹配,該數字是否在此數字范圍內,等等。使用這樣的謂詞,您通常不需要這些查詢要執行的相關性評分階段,可以通過在filter上下文(例如bool查詢的filter子句)中執行它們來放棄。 將其與運算符重載放在一起

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)
);

產生

{
  "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