Skip to content Skip to sidebar Skip to footer

How To Search Both Singular And Plural Form Of Word In Elasticsearch?

I am making elastic query using Q object and I have indexed documents, one of the documents contains 'jbl speakers are great', but my query has 'speaker' instead of speakers how ca

Solution 1:

The type of search you are looking for can be achieved by using stemmer token filter at the time of indexing.

Lets see how it work using the example mapping as below:

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "filter": [
            "lowercase",
            "my_stemmer"
          ],
          "tokenizer": "whitespace"
        }
      },
      "filter": {
        "my_stemmer": {
          "type": "stemmer",
          "name": "english"
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "description": {
          "type": "text",
          "analyzer": "my_analyzer",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

For the field description in above mapping we have used analyzer as my_analyzer. This analyzer will apply token filters lowercase and my_stemmer. The my_stemmer will apply english stemming on the input value.

For e.g. if we index a document as below:

{
   "description": "JBL speakers build with perfection"
}

The tokens that will get indexed are:

jbl
speaker
build
with
perfect

Notice speakers is indexed as speaker and perfection as perfect.

Now if you search for speakers or speaker both will match. Similarly, if you search for perfect the above document will match.

Why speakers or perfection will match might be a question arising in your mind. The reason for this is that by default elastic search apply the same analyzer that was used while indexing at the time of searching as well. So if you search for perfection it will be actually searching for perfect and hence the match.

More on stemming.


Post a Comment for "How To Search Both Singular And Plural Form Of Word In Elasticsearch?"