degratnik
degratnik

Reputation: 830

Elasticsearch. How to find phrases if query has no spaces

For example, I have a document with a phrase "Star wars" in the name field. I would like to make a search with DSL and query "starwars" and get this document. I am trying to get something like this

    GET _search
{ 
    "query" : { 
        "match_phrase" : {
            "name": {
            "query" : "starwars"
            }
        }

    } 
}

How can I do it with elasticsearch?

Upvotes: 1

Views: 260

Answers (1)

eemp
eemp

Reputation: 1166

I think you would need to update the analyzer on that name field with a custom analyzer that includes the synonym token filter with a synonym for starwars.

The docs on creating a custom analyzer should help you out. Additionally, the standard analyzer is applied by default if you did not specify any analyzer for that name field in your mapping. You can base your custom analyzer on that and add that synonym token filter in that array of filters. Perhaps, give some more thought to how you want the content to be analyzed for the other requirements you have as well as this.

With this analyzer update you should be able to use that query and get the result you expect.

Example:

{
    "filter" : {
        "my_synonym" : {
            "type" : "synonym",
            "synonyms" : [
                "star wars => starwars"
            ]
        }
    },
    "analyzer" : {
        "standard_with_synonym" : {
            "tokenizer" : "standard",
            "filter" : ["standard", "lowercase", "my_synonym", "stop"]
        }
    }
}

Upvotes: 1

Related Questions