【问题标题】:Elasticsearch query with Must (and) Should (or) not producing desired results带有必须(和)应该(或)不产生预期结果的 Elasticsearch 查询
【发布时间】:2020-07-31 04:25:09
【问题描述】:

我正在尝试执行 X AND (y OR z) 的查询 我需要获取代理是上市代理或销售代理的所有已售房产。

只有 bool 我必须得到 9324 个结果。当我添加 bool should 时,我得到相同的结果集 9324。ID 为 140699 的代理应该只有大约 100 个结果。我也尝试过没有成功的布尔过滤器。当用过滤器替换 should 时,结果就像另一个 bool must 一样,我只得到代理是上市代理和销售代理的结果

GET /property/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "statusCatID": {
              "value": "Sold"
            }
          }
        },
        {
          "range": {
            "closingDate": {
              "gte": "now-3M"
            }
          }
        }
      ],
      "should": [
        {
          "term": {
            "listAgent1": {
              "value": 140699
            }
          }
        },
        {
          "term": {
            "sellingAgent1": {
              "value": 140699
            }
          }
        }
      ]
    }
  },
  "size": 300
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    使用您的符号,您正在执行如下查询:

    (statuscatid:sold AND closingDate:now-3M OR listAgent1:140699 OR sellingAgent1:140699)
    

    我建议您阅读this official blog post 以更好地理解弹性中的布尔查询。如果你想要这样的查询:

    (statuscatid:sold AND closingDate:now-3M) AND (listAgent1:140699 OR sellingAgent1:140699)
    

    你应该这样写:

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "statusCatId": "sold"
              }
            },
            {
              "range": {
                "closingDate": "now-3M"
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "term": {
                      "listAgent1": 140699
                    }
                  },
                  {
                    "term": {
                      "sellingAgent1": 140699
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "size": 300
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多