elasticsearch match all words from document in the search query

I finally got it to work but not with a direct method!

This is what I do:

  • Create a clean list of words from the source query, by:
    • change to lower case
    • replacing any special chars and punctuation with space
    • remove duplicate words
  • Search using normal match with OR operator for the words joined as a string
  • Now we will find the best relevant hits in result
  • We take those hits one by one and do a word to word search in php (or whatever programming language you use)
  • This word search will check for all the words of a document from the hits we just found, and match them with the words in source query; such that all words from hit document should be present in the source query string

This worked for me well enough!

Unless someone has a direct method from elasticsearch query language.


The Percolate query should help here. You'd register your documents as queries, making "Nike Free Sparq Mens White" a match query with an AND operator.

Then your query can become a document like one having "Nike Free Sparq 09 - Mens - White/Black/Varsity Red" as content. You should get "Nike Free Sparq Mens White" back, because it matches all terms.

Unfortunately, this won't scale well (e.g. if you have millions of documents, it might get slow).