代码之家  ›  专栏  ›  技术社区  ›  Héctor

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

  •  1
  • Héctor  · 技术社区  · 6 年前

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

    从发票中选择,发票类型为“常规”且接收者为= “CUSTOMER”和(invoiceStatus=“DISPATCHED”或invoiceStatus=“PAYED”)

    我有这个:

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

    该查询返回0个结果,但我知道有许多结果与我正在搜索的匹配。阿法克, must 就像“和”和 should 比如“或者”。我错过了什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   A l w a y s S u n n y    6 年前

    不确定它是否对你有用,但你可以试试看你得到了什么?尽管我做了些改变 match term . 希望这对你有帮助。

    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"}}     
                      ]
                    }}
                  ]
               }
             }
          }
       }
    }