Vishal Sharma
Vishal Sharma

Reputation: 591

How to boost records with all search terms than OR of all search terms in Elasticsearch?

I have configured the default elasticsearch configuration. When I search for "foo bar" I get results which have just "foo" or "bar" on top and the results with both "foo" and "bar" not on top.

Is there a way to boost the records which are having all the search terms than the others.

Upvotes: 0

Views: 41

Answers (1)

Marco Bonzanini
Marco Bonzanini

Reputation: 766

You can combine the "OR" and "AND" logic as two queries in a single bool query, e.g.

{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "your_field": {
                        "query": "foo bar"
                    }
                }
            },
            "should": {
                "match": {
                    "your_field": {
                        "query": "foo bar",
                        "operator": "and"
                    }
                }
            }
        }
    }
}

where the must part contains the "OR" logic (operator not specified, it defaults to or) to ensure at least one term is matched, while the should part will provide the boost if all the terms are matched, i.e. the "AND" logic.

You can also go further and provide options in case you want to match and exact phrase (with a phrase query), e.g. http://marcobonzanini.com/2015/02/09/phrase-match-and-proximity-search-in-elasticsearch/

Upvotes: 1

Related Questions