Elasticsearch wildcard, regexp, match_phrase, prefix query returning wrong results Elasticsearch wildcard, regexp, match_phrase, prefix query returning wrong results elasticsearch elasticsearch

Elasticsearch wildcard, regexp, match_phrase, prefix query returning wrong results


Since you have not provided any index mapping of yours and as mentioned you are getting method also in the search result. I think that there is some issue with the analyzer that you have set.

One possibility is that you have set ngram tokenizer, that tokenizes the words, and produce token of tho (since all the words have tho present in them)

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

{  "mappings": {    "properties": {      "f1": {        "type": "text"      }    }  }}

Index Data:

{  "f1": "method"}{  "f1": "thought"}{  "f1": "Thomson"}{  "f1": "those"}

Search Query using Wildcard Query:

{  "query": {    "wildcard": {      "f1": {        "value": "tho*"      }    }  }}

Search Query using Prefix Query:

{  "query": {    "prefix": {      "f1": {        "value": "tho"      }    }  }}

Search Query using Regexp query:

{  "query": {    "regexp": {      "f1": {        "value": "tho.*"      }    }  }}

Search QUery using match phrase prefix query:

{  "query": {    "match_phrase_prefix": {      "f1": {        "query": "tho"      }    }  }}

Search Result for all the above 4 queries are

"hits": [      {        "_index": "67673694",        "_type": "_doc",        "_id": "1",        "_score": 1.2039728,        "_source": {          "f1": "thought"        }      },      {        "_index": "67673694",        "_type": "_doc",        "_id": "2",        "_score": 1.2039728,        "_source": {          "f1": "Thomson"        }      },      {        "_index": "67673694",        "_type": "_doc",        "_id": "3",        "_score": 1.2039728,        "_source": {          "f1": "those"        }      }    ]