Adding NOT conditions

It is possible to add NOT conditions, that is, specifically filtering out certain clauses using the must_not clause in the bool filter. For example, find all of the products in the price range 10 to 20, but they must not be manufactured by encoreThe following query will do just that:

GET /amazon_products/_search
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
}
],
"must_not": [
{
"term": {
"manufacturer.raw": "encore"
}
}
]
}
}
}
}
}

The bool query with the must_not element is useful for negate any query. To negate or apply a NOT filter to the query, it should be wrapped inside the bool with must_not, as follows:

GET /amazon_products/_search
{
"query": {
"bool": {
"must_not": {
.... original query to be negated ...
}
}
}
}

Notice that we do not need to wrap the query in a constant_score query when we are only using must_not to negate a query. The must_not query is always executed in a filter context.

This concludes our understanding of the different types of compound queries. There are more compound queries supported by Elasticsearch. They include the following:

  • Dis Max query
  • Function Score query
  • Boosting query
  • Indices query

Besides the full-text search capabilities of Elasticsearch, we can also model relationships within Elasticsearch. Due to the flat structure of documents, we can easily model a one-to-one type of relationship within a document. Let's see how to model a one-to-many type of relationship in Elasticsearch in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.102.6