Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search keyword using double quotes to get exact match in elasticsearch

If user searches by giving quotes around keyword like "flowers and mulch" then exact matches should be displayed.

I tried using query_string which is almost working but not satisfied with those results.

Can anyone help me out please.

{
    "query": {
        "query_string": {
           "fields": ["body"], 
           "query": "\"flowers and mulch\""
        }
    }
}
like image 702
Siddardha Budige Avatar asked Jul 17 '14 05:07

Siddardha Budige


People also ask

What's the difference between search statement with vs without double quotes?

You may have noticed when searching in databases or using Google, that when you enter your terms surrounded by quotation marks - like, "kidney failure" - you get slightly different results than if you entered the term with no quotes. This is because quotation marks are used for phrase searching.


1 Answers

You should be using phrase_match for exact matches of phrases:

{
    "query": {
        "match_phrase": {
            "body": "flowers and mulch"
        }
    }
}

Phrase matching

In the same way that the match query is the “go-to” query for standard full text search, the match_phrase query is the one you should reach for when you want to find words that are near to each other.

http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/phrase-matching.html

like image 122
John Petrone Avatar answered Sep 26 '22 03:09

John Petrone