Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search "should" clause not working as expected when "should", "must" and "must_not" clause are present

I've loaded the data provided here into elastic search.

Now I'm trying to query for Accounts.

  1. Age Must be 38.
  2. State Should not be "ID".
  3. Address Should Contain Either: (This is not Working)
    • The text "lane" OR
    • The a complete text "mill avenue"

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

Response :

{
  "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": "[email protected]",
          "lastname": "Paul"
        },
        "sort": [
          21,
          7004
        ]
      }
    ]
  }
}

You can check the response. The record with address "859 Portal Street" was received and it doesn't contain "lane" or "mill avenue".

Elastic Search Version : 5.1.1

----Edit---- Solution (Thanks to @Lax and @mattweber as posted here):
minimum_should_match is needed if must/must_not is present.

{
  "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"
          }
        }
      }
    }
  }
}
like image 841
Kishore Bandi Avatar asked Dec 18 '25 13:12

Kishore Bandi


1 Answers

When must/must_not present you need to add :

minimum_should_match" : 1

after the should array

like image 122
Lax Avatar answered Dec 21 '25 06:12

Lax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!