Search code examples
elasticsearchamazon-elastic-beanstalkelastic-stackelasticsearch-5

How to combine wildcard search with range query within Elasticsearch?


I am trying to combine wildcard with date range in Elasticsearch query but is not giving response based upon the wildcard search. It is returning response with items which have incorrect date range.

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "wildcard": {
                  "hostName": "*abc*"
                }
              },
              {
                "range": {
                  "requestDate": {
                    "gte": "2019-10-01T08:00:00.000Z"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

The index mapping looks as below:

{
  "index_history": {
    "mappings": {
      "applications_datalake": {
        "properties": {
          "query": {
            "properties": {
              "term": {
                "properties": {
                  "server": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "index-data-type": {
        "properties": {
          "attributes": {
            "properties": {
              "wwnListForServer": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "hostName": {
            "type": "keyword"
          },
          "requestDate": {
            "type": "date"
          },
          "requestedBy": {
            "properties": {
              "id": {
                "type": "keyword"
              },
              "name": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

Solution

  • You missed minimum_should_match parameter, Check this out : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html.
    I think your query should looklike this:

    {
      "from": 0,
      "size": 10,
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "wildcard": {
                      "hostName": "*abc*"
                    }
                  },
                  {
                    "range": {
                      "requestDate": {
                        "gte": "2019-10-01T08:00:00.000Z"
                      }
                    }
                  }
                ],
                "minimum_should_match" : 2
              }
            }
          ]
        }
      }
    }
    

    From the documentation :

    You can use the minimum_should_match parameter to specify the number or percentage of should clauses returned documents must match.

    If the bool query includes at least one should clause and no must or filter clauses, the default value is 1. Otherwise, the default value is 0.