Zabi
Zabi

Reputation: 1369

ElasticSearch Reverse Wildcard Search

In ElasticSearch v5.2.2 I can search for "Jo*" using Wildcard and it will match the index value containing "Joseph"

But what if my index also has these values "Joseph","Jo", "Jos", "Jose" and "Josep" and I want to reverse the query.

How can I find "Jo", "Jos", "Jose" and "Josep" in the index using the string "Joseph" as search criteria?

Upvotes: 2

Views: 1207

Answers (1)

Val
Val

Reputation: 217274

That's possible, but you need to create an edgeNGram search analyzer in your index settings.

First create the settings like this. The name field will be indexed with the standard analyzer but searched with your custom prefix_search analyzer instead.

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "prefix_search": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "prefix"
          ]
        }
      },
      "filter": {
        "prefix": {
          "type": "edgeNGram",
          "min_gram": 1,
          "max_gram": 10
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "standard",
          "search_analyzer": "prefix_search"
        }
      }
    }
  }
}

Then if you create a document like this:

PUT test/doc/1
{
   "name": "Jos"
}

You can find it with a query like this one:

POST /test/doc/_search
{
  "query": {
    "match": {
      "name": "Joseph"
    }
  }
} 

Upvotes: 2

Related Questions