簡體   English   中英

在單個 ElasticSearch 查詢中使用另一個 bool 的多個 match_phrase 條件?

[英]Multiple match_phrase conditions with another bool in a single ElasticSearch query?

我正在嘗試進行 Elasticsearch 查詢,該查詢搜索文本字段(“body”)並返回至少與我提供的兩個多詞短語之一匹配的項目(即:“stack overflow”或“the stackoverflow”)。 我還希望查詢只提供在給定時間戳之后發生的結果,結果按時間排序。

我目前的解決方案如下。 我相信 MUST 工作正常(gte 時間戳),但是帶有兩個 match_phrases 的 BOOL + SHOULD 是不正確的。 我收到以下錯誤:

Unexpected character ('{' (code 123)): was expecting double-quote to start field name

認為這是因為我有兩個 match_phrases 嗎?

是 ES 映射,我正在使用的 ES API 的詳細信息在這里

{"query":
  {"bool":
    {"should":
      [{"match_phrase":
         {"body":"a+phrase"}
       },
       {"match_phrase":
         {"body":"another+phrase"}
       }
      ]
    },
  {"bool":
    {"must":
      [{"range":
        {"created_at:
          {"gte":"thispage"}
        }
       }
      ]}
     }
    },"size":10000,
      "sort":"created_at"
}

我認為您只是在created_at之后遺漏了一個"

{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "created_at": {
                            "gte": "1534004694"
                        }
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "match_phrase": {
                                    "body": "a+phrase"
                                }
                            },
                            {
                                "match_phrase": {
                                    "body": "another+phrase"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "size": 10,
    "sort": "created_at"
}

此外,您可以同時擁有mustshould作為bool object 的屬性,因此這也值得一試。

{
    "query": {
        "bool": {
            "must": {
                "range": {
                    "created_at": {
                        "gte": "1534004694"
                    }
                }
            },
            "should": [
                {
                    "match_phrase": {
                        "body": "a+phrase"
                    }
                },
                {
                    "match_phrase": {
                        "body": "another+phrase"
                    }
                }
            ]
        }
    },
    "size": 10,
    "sort": "created_at"
}

附帶說明一下,Postman 或任何 JSON 格式化程序/驗證程序確實有助於確定錯誤所在。

暫無
暫無

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

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