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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With