当“should”,“must”和“must_not”子句出现时,Elastic Search“should”子句不能按预期工作

时间:2016-12-29 19:12:14

标签: elasticsearch

我已将here提供的数据加载到弹性搜索中。

现在我正在尝试查询帐户。

  1. 年龄必须是38岁。
  2. 国家不应该是“身份证”。
  3. 地址应包含 :(这不起作用)
    • 文字“lane”或
    • 完整的文字“mill avenue”
  4. 请求:POST - http://localhost:9200/bank/account/_search?pretty

    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "lane" } },
            { "match_phrase": { "address": "mill avenue" } }
          ],
          "must": [
            { "match": { "age": "38" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      },
      "from": 0,
      "size": 1,
      "_source": ["account_number", "balance", "firstname", "lastname", "age", "email", "address", "state"],
      "sort": [
        { "account_number": "asc" },
        { "balance": "desc"}
      ]
    }
    

    回复

    {
      "took": 8,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 36,
        "max_score": null,
        "hits": [
          {
            "_index": "bank",
            "_type": "account",
            "_id": "21",
            "_score": null,
            "_source": {
              "account_number": 21,
              "firstname": "Estella",
              "address": "859 Portal Street",
              "balance": 7004,
              "state": "WV",
              "age": 38,
              "email": "estellapaul@zillatide.com",
              "lastname": "Paul"
            },
            "sort": [
              21,
              7004
            ]
          }
        ]
      }
    }
    

    您可以查看回复。收到地址“859 Portal Street”的记录,不包含“lane”或“mill avenue”

    弹性搜索版本:5.1.1

    ---- ----编辑 解决方案(感谢@Lax和@mattweber发布here):如果必须/ must_not,则需要minimum_should_match

    {
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            { "match": { "address": "avenue" } },
            { "match_phrase": { "address": "Williams Place" } }
          ],
          "must": [
            { "match": { "age": "38" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ],
          "filter": {
            "range": {
              "balance": {
                "gte": 20000,
                "lte": 30000
              }
            }
          }
        }
      },
      "from": 0,
      "size": 1000,
      "_source": ["account_number", "balance", "firstname", "lastname", "age", "email", "address", "state"],
      "sort": [
        { "account_number": "asc" },
        { "balance": "desc"}
      ],
      "aggs": {
        "group_by_state": {
          "terms": {
            "field": "state.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
    

1 个答案:

答案 0 :(得分:2)

必须/必须不存在时,您需要添加:

minimum_should_match" : 1

后面的数组