Elasticsearch query with Must (and) Should (or) not producing desired results - elasticsearch

I'm trying to perform a query of X AND (y OR z)
I need to get all the sold properties that the agent was either the listing agent or the selling agent.
With only the bool must I get 9324 results. When I add the bool should, I get the same result set of 9324. The agent with the ID of 140699 should have only about 100 results. I've also tried a bool filter with no success. When replacing the should with a filter, the result is like another bool must, and I only get results where the agent was the listing agent AND the selling agent
GET /property/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"statusCatID": {
"value": "Sold"
}
}
},
{
"range": {
"closingDate": {
"gte": "now-3M"
}
}
}
],
"should": [
{
"term": {
"listAgent1": {
"value": 140699
}
}
},
{
"term": {
"sellingAgent1": {
"value": 140699
}
}
}
]
}
},
"size": 300
}

With your notation you are performing a query like this:
(statuscatid:sold AND closingDate:now-3M OR listAgent1:140699 OR sellingAgent1:140699)
I suggest you to read this official blog post to understand better the bool queries in elastic. If you want a query like this:
(statuscatid:sold AND closingDate:now-3M) AND (listAgent1:140699 OR sellingAgent1:140699)
You should write it in this way:
{
"query": {
"bool": {
"must": [
{
"term": {
"statusCatId": "sold"
}
},
{
"range": {
"closingDate": "now-3M"
}
},
{
"bool": {
"should": [
{
"term": {
"listAgent1": 140699
}
},
{
"term": {
"sellingAgent1": 140699
}
}
]
}
}
]
}
},
"size": 300
}

Related

How do i combine different search parameters in an elasticscearch dsl query?

Good day together,
I have a little problem in Elastic/Kibana. In the Kibana Query Language "KQL" it is possible for me to execute a certain query:
car:* AND coun: * AND doc: (bes* OR *rvr*) AND NOT coun: (SIP OR LUK)
I would like to use this as a filter query using Elasticscearch query DSL. Only I don't get the same result. For this I use the boolean operator. My query looks like this:
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "car"
}
},
{
"exists": {
"field": "coun"
}
}
],
"should": [
{
"wildcard": {
"doc.keyword": {
"value": "bes*"
}
}
},
{
"wildcard": {
"doc.keyword": {
"value": "*rvr*"
}
}
}
],
"must_not": [
{
"term": {
"coun.keyword": "SIP"
}
},
{
"term": {
"coun.keyword": "LUK"
}
}
],
"minimum_should_match": 1
}
}
}
Unfortunately, I do not get the same result. My guess is the "should" operator. But I don't know exactly how to adjust the code.
I would be very grateful for any answer! Thanks a lot!
Problem here, that you putting OR outside AND. Just move should clause inside must. Like this
GET _search
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "car"
}
},
{
"exists": {
"field": "con"
}
},
{
"bool": {
"should": [
{
"wildcard": {
"doc.keyword": {
"value": "bes*"
}
}
},
{
"wildcard": {
"doc.keyword": {
"value": "*rvr*"
}
}
}
]
}
}
],
"must_not": [
{
"term": {
"coun.keyword": "SIP"
}
},
{
"term": {
"coun.keyword": "LUK"
}
}
],
"minimum_should_match": 1
}
}
}

How to return results from elasticsearch after a threshold match

I have two queries as follows:
The first query returns the count of all documents per domain.
The second query returns the count where a field is empty.
Later I filter it in my backend, such that, if for a domain the count of documents missing field value is more than a specific threshold then only consider them else ignore. Could these two queries be combined together, such that I could do the threshold comparison and then return the results.
The first query is as follows:
GET database/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"source": {
"value": "Web"
}
}
}
]
}
},
"aggs": {
"domains": {
"terms": {
"field": "domain_id"
}
}
}
}
The second query just applies a should filter as follows:
GET mapachitl/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"source": {
"value": "Web"
}
}
}
],
"should": [
{
"term": {
"address.city.keyword": {
"value": ""
}
}
},
{
"term": {
"address.zip.keyword": {
"value": ""
}
}
}
],
"minimum_should_match": 1
}
},
"aggs": {
"domains": {
"terms": {
"field": "domain_id"
}
}
}
}
Can I only return those domains where the ratio of documents missing city or zip code is more than 25%? I read about scripting but not sure how can I use it here.

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.

Diference between term and match in Elasticsearch in a bool query

I have a simple document where the _source looks like:
{
"name" : "myProduct",
"label" : "isApiisApi",
"isApi" : 1,
"sold" : 0
}
I've been trying to create a multiple condition query using bool. The only way that I get it working was by using a match query:
{
"query": {
"bool": {
"must": [
{ "term": { "sold": 0 } },
{ "term": { "isApi": 1 } },
{ "match": { "name": "myProduct" } }
]
}
}
}
But why doesn't it work when I use the term query (as the final condition):
{
"query": {
"bool": {
"must": [
{ "term": { "sold": 0 } },
{ "term": { "isApi": 1 } },
{ "term": { "name": "myProduct" } }
]
}
}
}
Tldr;
Elastic text fields upon ingestion passes the data into a analyzer.
By default the standard analyzer is used. Which comes with a token filter named Lowercase.
Your text is indexed in lowercase.
But you are using a term which search for exact match on the indexed data.
In your case myproduct =/= myProduct.
To Reproduce
By default Elastic index, all string like data in two fields.
text
keyword
For exact match you want to use the keyword version.
See below:
POST /72020272/_doc
{
"name" : "myProduct",
"label" : "isApiisApi",
"isApi" : 1,
"sold" : 0
}
GET /72020272/_mapping
GET /72020272/_search
{
"query": {
"bool": {
"must": [
{ "term": { "sold": 0 } },
{ "term": { "isApi": 1 } },
{ "term": { "name": "myProduct" } }
]
}
}
}
GET /72020272/_search
{
"query": {
"bool": {
"must": [
{ "term": { "sold": 0 } },
{ "term": { "isApi": 1 } },
{ "term": { "name.keyword": "myProduct" } }
]
}
}
}

Performance difference between nested bool queries and non nested bool queries in Elastic search

Was wondering if there's a big difference in performance between these two queries which get the same results
{
"query": {
"bool": {
"must": [
"bool": {
"must": [
{
"term": {
"color": "red"
}
},
{
"term": {
"fruit": "strawberry"
}
}
]
}
}
}
}
}
and
{
"query": {
"bool": {
"must": [
{
"term": {
"color": "red"
}
},
{
"term": {
"fruit": "strawberry"
}
}
]
}
}
}
The execution plan of both queries is exactly the same. Add ?explain=true to your URL so you can see how both queries are "explained".
The performance improvement would come from using filter instead of must provided you don't need scoring but only yes/no filtering, i.e.:
{
"query": {
"bool": {
"filter": [ <-- change this
{
"term": {
"color": "red"
}
},
{
"term": {
"fruit": "strawberry"
}
}
]
}
}
}

Resources