Kishore Bandi
Kishore Bandi

Reputation: 5701

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

Upvotes: 3

Views: 2485

Answers (1)

Lax
Lax

Reputation: 1167

When must/must_not present you need to add :

minimum_should_match" : 1

after the should array

Upvotes: 7

Related Questions