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


(Bandi Kishore) #1

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": "estellapaul@zillatide.com",
          "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


(Matt Weber) #2

You need to set minimum_should_match value to 1 otherwise BOTH should clauses are optional due to having the must/must_not clauses. If all you had were should clauses, minimum_should_match would not be needed because it defaults to a value of one in that scenario.


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.