如何在Elasticsearch查询中结合“必须”和“应该”?

问题描述 投票:1回答:1

我需要在Elasticsearch查询DSL中“翻译”这个伪SQL查询:

从invoiceType中选择invoiceType ='REGULAR'和receiver ='CUSTOMER'和(invoiceStatus ='DISPATCHED'或invoiceStatus ='PAYED')

我有这个:

{
  "query": {
    "bool": {
      "must": [
        { "match": { "invoiceType":  "REGULAR" }},
        { "match": { "receiver": "CUSTOMER" }},
        { "bool" : {
            "should": [
                {"match" : {"invoiceStatus": "DISPATCHED"}},
                {"match" : {"invoiceStatus": "PAYED"}}
            ]
          }
        }
      ]
    }
  }
}

该查询返回0结果,但我知道有很多匹配我正在搜索的内容。 AFAIK,must就像'AND'和should就像'OR'。我错过了什么?

elasticsearch
1个回答
0
投票

不确定它是否适合你,但你可以尝试一下,看看你得到了什么?虽然我用matchterm做了一些改变。希望这会帮助你。

GET /invoice/_search
{
   "query" : {
      "constant_score" : {
         "filter" : {
            "bool" : {
              "must" : [
                { "term" : {"invoiceType" : "REGULAR"}}, 
                { "term": { "receiver": "CUSTOMER" }},
                { "bool" : { 
                  "should" : [
                      {"terms": {"invoiceStatus": ["DISPATCHED","PAYED"]}}   
                  ]
                }}
              ]
           }
         }
      }
   }
}

要么

GET /invoice/_search
{
   "query" : {
      "constant_score" : {
         "filter" : {
            "bool" : {
              "must" : [
                { "term" : {"invoiceType" : "REGULAR"}}, 
                { "term": { "receiver": "CUSTOMER" }},
                { "bool" : { 
                  "should" : [
                    {"term": {"invoiceStatus": "DISPATCHED"}},
                    {"term": {"invoiceStatus": "PAYED"}}     
                  ]
                }}
              ]
           }
         }
      }
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.