Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I perform a "begin with" search in Elasticsearch

I'm trying to implement a text searching app using elasticsearch which performs a "begin with" search (like match_phrase_prefix), but I also need to apply some filters such as "stemmer" and "stop" on my analyser.

Here is my sample documents that I want to search against:

  1. The brown fox runs quickly.
  2. Watch! The brown fox is running quickly.
  3. Brown foxes run quickly than yellow foxes.

And my goal is, when I searching for "brown fox", the app should return me 1) and 3)

I tried keyword as my tokeniser with filters of "stemmer" and "stop", it only worked for the last word of my document, for example: "The brown fox runs quickly" will become to "the brown fox runs quick", but I need to tokenise the whole sentence.

like image 493
Jasen Pan Avatar asked Sep 19 '25 13:09

Jasen Pan


1 Answers

You need to apply tokenization, so you will have stemming applied for each word in your sentence.

In settings.analysis.analyzer you could add this custom English analyzer:

{
  "custom_english": {
    "filter": [
      "stopwords",
      "english_possessive_stemmer",
      "lowercase",
      "english_stemmer"
    ],
    "tokenizer": "standard"
  }
}

Then you could add a subfield in your field definition like this in your mappings like this:

{
  "my_field": {
    "type": "keyword",
    "ignore_above": 8191,
    "fields": {
      "en": {
        "analyzer": "custom_english",
        "type": "text"
      }
    }
  }
}

Then you could query like this:

{
  "query": {
    "prefix": {
      "my_field.en": {
        "value": "brown fox"
      }
    }
  }
}

This will match 1, 3. The prefix query makes sure you only match from the beginning of the value. But when you query with brown foxes, then you will also get 1. That's the power of stemming.

like image 200
drjz Avatar answered Sep 23 '25 11:09

drjz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!