How to create an ElasticSearch Query which should not match any of multiple categories - elasticsearch

I'm looking for documents inside elasticSearch which do not match one or the other brand, but fullFill a fix requirement. I'm looking for any entries which are not from Toyota, BMW or Audi. But the entry must be a superEntry (exact match)
The following Query is what I'm working on:
"query": {
"bool": {
"filter": {
"term": {
"superEntry": true
}
},
"must": {
"bool": {
"must_not": [
{
"term": {
"brand": "Toyota"
}
},
{
"term": {
"brand": "BMW"
}
},
{
"term": {
"brand": "Audi"
}
}
]
}
}
}
}
}
Expected: I find any super-entries from any other brand, but not from those 3. The query above still lists me cars from BMW as an example..

Not tested but something like this will help-
{
"size": 10,
"query": {
"bool": {
"must": [
{
"match": {
"superEntry": true
}
}
],
"must_not": [
{
"terms": {
"brand": [
"Toyota",
"BMW",
"Audi"
]
}
}
]
}
}
}

Related

Return parent records even if no child records query matches

(this is actually AWS OpenSearch, which I believe is a fork of Elastic Search 7.x)
So in this contrived example, I have a parent-child relationship between manufacturer and products. I want to return "acme" information and all of the products. Some of the products may be embargoed (not ready to be listed to the public). For a new company, like acme, it only has new embargoed products, so when I run this query, I do not get back the company info. I tried using "min_children": 0, but I still do not get back the manufacturer.
For this query, other manufacturers are returned if they have at least one product that is not embargoed, so it's something about has_child hits not returning any products.
{
"track_total_hits": true,
"query": {
"bool": {
"must": [
{
"has_child": {
"inner_hits": {
"name": "manf_products",
"size": 100
},
"min_children": 0,
"query": {
"bool": {
"should": [
{
"range": {
"embargo_date": {
"lt": "now/s"
}
}
}
]
}
},
"type": "product"
}
},
{
"bool": {
"should": [
{
"term": {
"manuf": {
"value": "acme"
}
}
}
]
}
}
]
}
}
}
The minimum value of min_children is 1. This can give you more info on this.
Below query will return parents and child docs which are not embargoed. It will return parent if no child non embargoed child exists
{
"track_total_hits": true,
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"has_child": {
"inner_hits": {
"name": "manf_products",
"size": 100
},
"query": {
"bool": {
"should": [
{
"range": {
"embargo_date": {
"lt": "now/s"
}
}
}
]
}
},
"type": "product"
}
},
{
"bool": {
"must_not": [
{
"has_child": {
"inner_hits": {
"name": "manf_products",
"size": 100
},
"query": {
"bool": {
"should": [
{
"range": {
"embargo_date": {
"lt": "now/s"
}
}
}
]
}
},
"type": "product"
}
}
]
}
}
],
"filter": [
{
"term": {
"manf": {
"value": "acme"
}
}
}
]
}
}
}

Simplest way to query a elasticsearch index with chained conditions

I have an index of products on which I want to find all the products who fulfill conditions , such as :
((type = "orange" and price > 10) or (type = "apple" and price > 8)) and on_sale=True.
What about
(type = "orange" or type = "apple") and (price <= 25 or on_sale=True) .
You need to combine bool clauss, with "must" and "should".
Find below the required query for the first statement
GET _search
{
"query": {
"bool": {
"must": [
{
"term": {
"on_sale": {
"value": "True"
}
}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"type": {
"value": "orange"
}
}
},
{
"range": {
"price": {
"gte": 10
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"type": {
"value": "apple"
}
}
},
{
"range": {
"price": {
"gte": 8
}
}
}
]
}
}
]
}
}
]
}
}
}
It is just about wrapping "must" or "Should" clauses into one another as required. You need a little bit of practise to figure out how to chain them. But literally any combination can be queried using this kind of syntax.
For the second query:
{
"query": {
"bool": {
"must": [
{
"terms": {
"type": [
"ornage",
"apple"
]
}
},
{
"bool": {
"should": [
{
"term": {
"on_sale": {
"value": "True"
}
}
},
{
"range": {
"price": {
"gte": 10
}
}
}
]
}
}
]
}
}
}
When you need "and" use "MUST", when you need "or" use "SHOULD".
HTH.

ElasticSearch should with nested and bool must_not exists

With the following mapping:
"categories": {
"type": "nested",
"properties": {
"category": {
"type": "integer"
},
"score": {
"type": "float"
}
}
},
I want to use the categories field to return documents that either:
have a score above a threshold in a given category, or
do not have the categories field
This is my query:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"terms": {
"categories.category": [
<id>
]
}
},
{
"range": {
"categories.score": {
"gte": 0.5
}
}
}
]
}
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "categories"
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
It correctly returns documents both with and without the categories field, and orders the results so the ones I want are first, but it doesn't filter the results having score below the 0.5 threshold.
Great question.
That is because categories is not exactly a field from the elasticsearch point of view[a field on which inverted index is created and used for querying/searching] but categories.category and categories.score is.
As a result categories being not found in any document, which is actually true for all the documents, you observe the result what you see.
Modify the query to the below and you'd see your use-case working correctly.
POST <your_index_name>/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"terms": {
"categories.category": [
"100"
]
}
},
{
"range": {
"categories.score": {
"gte": 0.5
}
}
}
]
}
}
}
},
{
"bool": {
"must_not": [ <----- Note this
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "categories.category"
}
},
{
"exists": {
"field": "categories.score"
}
}
]
}
}
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}

Elasticsearch return exact match first then other matches

I have some PageDocuments which I would like to search based on the title, excluding PageDocuments with a path starting with some particular text. This field is analyzed. I would like some fuzziness to help users with spelling mistakes. I need to be able to do partial matches so some would match some text and this is some text.
If I use the following query I don't get an exact match back as the first result because of tf-idf
{
"size": 20,
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "myterm",
"fuzziness": 1
}
}
}
],
"must_not": [
{
"wildcard": {
"path": {
"value": "/test/*"
}
}
}
]
}
}
}
So then I added a not_analyzed version of the title field at title.not_analyzed and tried adding a function score to increase the weighting of an exact match using term.
{
"query": {
"function_score": {
"functions": [
{
"weight": 2,
"filter": {
"fquery": {
"query": {
"term": {
"title.not_analyzed": {
"value": "myterm"
}
}
}
}
}
}
],
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "myterm",
"fuzziness": 1
}
}
}
],
"must_not": [
{
"wildcard": {
"path": {
"value": "/path/*"
}
}
}
]
}
},
"boost_mode": "multiply"
}
}
}
But this gives me the same results. How can I get the exact matches returned first?
We found a solution to this by adding a combination of should and boost.
{
"size": 20,
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "myterm",
"fuzziness": 1
}
}
}
],
"must_not": [
{
"wildcard": {
"path": {
"value": "/path/*"
}
}
}
],
"should": [
{
"term": {
"title": {
"value": "myterm",
"boost": 10
}
}
}
]
}
}
}

How to combine multiple bool queries in elasticsearch

I want to create the equivalent of the following query -
(city = 'New York' AND state = 'NY') AND ((businessName='Java' and businessName='Shop') OR (category='Java' and category = 'Shop'))
I tried different combinations of bool queries using must and should but nothing seems to be working. Can this be done?
How about something like this:
{
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": {
"city": "New york"
}
},
{
"term": {
"state": "NY"
}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"businessName": "Java"
}
},
{
"term": {
"businessName": "Shop"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"category": "Java"
}
},
{
"term": {
"category": "Shop"
}
}
]
}
}
]
}
}
]
}
}
}

Resources