Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search exact phrase in string field with ElasticSearch?

I want to search "Marketing in social networks" inside documents. All together. But i continue getting results with words separated. i have the following DSL query:

{
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match": {
                    "content": {
                        "query": "Marketing in social networks",
                        "operator": "and"
                    }
                }
            }]
        }
    }
}

I do not have documents that contain this phrase and title but i also get results(documents) with the words of the phrase to search splitted. I want a strict search. If there is not any document that have this phrase do not retrieve any document or only retrieve documents with that title. Why operator and does not work?

like image 594
Xairo Martínez Avatar asked Dec 08 '22 20:12

Xairo Martínez


1 Answers

Can you try like below using type phrase. See here it says,

query first analyzes the query string to produce a list of terms. It then searches for all the terms, but keeps only documents that contain all of the search terms, in the same positions relative to each other

{
    "fields": ["title"], 
    "query": {
        "bool": {
            "should": [{
                "match": {
                    "title": "SEO"
                }
            }],
            "must": [{
                "match": {
                    "content": {
                        "query": "Marketing in social networks",
                        "type":  "phrase"
                    }
                }
            }]
        }
    }
}

P.S: I haven't tried it yet.

like image 61
Rajind Ruparathna Avatar answered Jan 13 '23 16:01

Rajind Ruparathna