Full Text Search/Exact Search using Match and Filters within Elasticsearch

I decided to dig into those two and put it straight.
When we should use Match and when we should use Filters while using Elasticsearch’s query DSL technic.
But first things first:
Elasticsearch(nowadays called “Elastic”) is very powerful search engine which based on Apache Lucene library.
When we search we gotta be aware into two important aspects: “Full text search” and “Exact Search”
Exact search means that we looking for an exact values. For example:
1. “hello” or “Hello” (mind the caps)
2. Search for a specific username
3. Search for specific date
We using Exact search when we know what we are looking for and we basically looking for one result
With Elasticsearch we can achieve this using the Match query within Query DSL:

  GET /_search
    {
        "query": {
            "match": {
                "Background":"I like to dive"
            }
} }

Now pay attention here.
Match is also can be used for Full Text search(which ill explain next) BUT if we use match and we want to get an exact value results we should use it only when we searching for “non full text search fields”

non full text search fields defined as follow:

  • numbers,
  • A boolean
  • not analysed Strings
  • Date

Dig it?

Now, there is another way to look for an exact values in Elasticsearch and thats using Filters.

Filters answers only yes/no questions and therefor are very useful for exact search terms:

GET /_search
    {
        "query": {
            "filtered": {
"filter": {"term":{"Background":"I like to dive"}} }
} }
Using Filters for exact value strings search put you on the “safe” side without over thinking if your String is analysed or not.
Another concerns to choose Filters (over Match) while doing Exact value searching would be:
1. Caching results (Filter does cache results. Useful in case we going to have similar queries at a time)
2. Hitting less number of docs(Since we looking for an exact values we eventually get less docs)
Next, Let’s make Full Text search clear:
Full text search should be used when we desire to search within texts or when we not sure about the result we are expecting.
Big difference that with full text search we going to get bunch of documents ordered by relevance (Using a scoring system which I wont describe here)
As I described in the beginning, Using the Match with Query DSL is the way to perform Full Text search.
Enjoy your searching,
Idan.