簡體   English   中英

Elasticsearch 6.7.0嵌套“bool”,“should”,“must”查詢

[英]Elasticsearch 6.7.0 Nested “bool”, “should”,“must” queries

我正在使用ES_6.7並將Payload發送到其中。

當我為單個查詢發送“Payload”但面對多個問題時,我得到了正確的輸出。

單個查詢的有效負載:PAYLOAD =

  {
    "query": {"bool": {"must": [{"match": {"coreid": {"query": "2"} } }, "match": {"program_id": {"query": "86328" }}}]}}, 
    }

但是當我使用“should”選項復制上面三個輸入的查詢時,面對ERROR:

{"query":{"bool":{"should":
[{"must":[{"match": {"coreid": {"query": "2"} } },
                            {"match": {"program_id": {"query": "86328" }}}

                            ]
                    },

                    {"must":[
                            {"match": {"coreid": {"query": "4"} } },
                            {"match": {"program_id": {"query": "86819" }}}

                            ]
                    },

                    {"must":[
                            {"match": {"coreid": {"query": "5"} } },
                            {"match": {"program_id": {"query": "95142" }}}

                            ]
                    }
            ]       
        }
        }
}

錯誤信息:

“type”:“parsing_exception”,

“reason”:“沒有[查詢]注冊[查詢]”,

“行”:5,

“col”:22,

“地位”:400

有點晚了,但是:用“bool”封裝“必須”,你也可以用“term”代替“match”和“query”。 不確定性能是否會更好,但在我看來它更清潔一點。

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "coreid": "2"
                }
              },
              {
                "term": {
                  "program_id": "35"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "coreid": "758"
                }
              },
              {
                "term": {
                  "program_id": "45678"
                }
              }
            ]
          }
        }

      ]
    }
  }
}

這對我有用。 我在每個級別添加了一個額外的“bool”條件。 我可以對此進行更多優化嗎?

{
"_source" :["coreid", "program_id", "program_name", "university_name", "city", "country"] , 
"query":{"bool": 
        {
            "should":
            [


                    {"bool":{
                        "must":[
                            {"match": {"coreid": {"query": "2"} } },
                            {"match": {"program_id": {"query": "86328" }}}

                            ]
                            }
                    },

                    {"bool":{
                        "must":[
                            {"match": {"coreid": {"query": "59"} } },
                            {"match": {"program_id": {"query": "95142" }}}

                            ]
                            }
                    },

                    {"bool":{
                        "must":[
                            {"match": {"coreid": {"query": "2"} } },
                            {"match": {"program_id": {"query": "86819" }}}

                            ]
                            }
                    }
            ],
            "minimum_should_match" : 1
            }       
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM