skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. First answer is good but for ES v5 using "type":"phrase" will return [WARN ][org.elasticsearch.deprecation.common.ParseField] Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query] in the headers
    So correct query should contain match_phrase:

     {
        "fields": ["title"], 
        "query": {
            "bool": {
                "should": [{
                    "match": {
                        "title": "SEO"
                    }
                }],
                "must": [{
                    "match_phrase": {
                        "content": {
                            "query": "Marketing in social networks"
                        }
                    }
                }]
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search