Elastic search permutation query

Viewed 304

I want a elastic-search query to search permutation of words from database. eg:- if i enter "ABC" then it should search ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"] all of the combination of words.

1 Answers

What should help you is Terms Set query https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html

The actual implementation is going to be too complicated to put it as an answer here and also too specific to your setup. But if you index your documents in a way described by the link above so that "A", "B" and "C" were tokens of an array you will be able to search by all permutations of those tokens

Another option is going to be using match_phrase query https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html

This will work if by "ABC" you meant actual words and they can be indexed as separate keywords. Then the more of them are matched the higher score the document will get. And words order will not matter so this will mean if you search for "A B C" you will match documents containing "A B C", "B A C", "C B A" etc. By adding slop parameter you'll be able to find documents that match partly, for example the one that contains "A C".

Related