Will ElasticSearch performance differ if we have multiple nested queries - elasticsearch

In general, if we need to do an OR condition in elasticsearch within one nested type, let's say, we do query like below :
Query 1:
{
"query": {
"bool": {
"must": [{
"nested": {
"query": {
"bool": {
"must": [{
"bool": {
"should": [{
"match": {
"descTags.device": {
"query": "abc"
}
}
}, {
"match": {
"descTags.device": {
"query": "xyz"
}
}
}]
}
}]
}
},
"path": "descTags"
}
}]
}
}
}
The above query has one nested and have OR condition within this one nested type.
Representing below the same query with two nested blobs :
Query 2:
{
"query": {
"bool": {
"must": [{
"bool": {
"should": [{
"bool": {
"must": [{
"nested": {
"query": {
"bool": {
"must": [{
"match": {
"descTags.Modality": {
"query": "abc"
}
}
}]
}
},
"path": "descTags",
}
}]
}
}, {
"bool": {
"must": [{
"nested": {
"query": {
"bool": {
"must": [{
"match": {
"descTags.Modality": {
"query": "xyz"
}
}
}]
}
},
"path": "descTags"
}
}]
}
}]
}
}]
}
}
}
Will there be any difference in performance between query1 and query2 just because we are having nested blob multiple times?
EDIT 1:
As per the below comments (#kamal), I am simplifying my question here..
Will there be any difference in performance if the boolean operation is done between "attributes that belong to same nested type, declaring the nested type only once" vs "attributes that belong to same nested type, declaring the nested type twice, once for each attribute".
like below :
1.
nested :
or :
attribute1
attribute2
vs
2.
or :
nested :
attribute1
nested :
attribute2

Related

How to write query to find data in nested and not nested fields together

I would like to find multiple field search.
So:
GET my_doc/_search
{
"query": {
"multi_match" : {
"query": "text",
"fields": [ "state", "city"]
}
}
}
Generally this query works, but it does not search in the nested fields.
But this one works only for nested fields:
GET my_doc*/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "images",
"score_mode": "max",
"query": {
"bool": {
"must": [
{"match": {"images.description": "Subject" }},
{"match": {"images.Number": "10004" }}
]
}
}
}
}
]
}
}
}
My question is:
How to write query to search everywhere - Just to join my queries together to find in the "images" location too.
I've found solution, just to join queries:
GET my_doc*/_search
{
"query": {
"bool": {
"must": [
{
"multi_match" : {
"query": "LAKE",
"fields": [ "state", "city"]
}
},
{
"nested": {
"path": "images",
"score_mode": "max",
"query": {
"bool": {
"must": [
{"match": {"images.description": "Subject" }},
{"match": {"images.Number": "10004" }}
]
}
}
}
}
]
}
}
}

Elasticsearch must_not inside nested query

My elastic search index having nested fields. and I want to use must, which contains a must_not query in it with a nested query. I have tried a must_not query separately in the following way:
{
"bool": {
"must_not": [{
"nested": {
"path": "fields",
"query": {
"terms": {
"fields.value.raw": [
"200"
]
}
}
}
}]
}
}
above query gives me a valid result but when I was tried this with must query then it will not give me any result.I am using following query:
{
"bool": {
"must": [{
"nested": {
"path": "fields",
"query": {
"bool": {
"must": [{
"match": {
"fields.uid": "number"
}
}, {
"bool": {
"must_not": [{
"nested": {
"path": "fields",
"query": {
"terms": {
"fields.value.raw": [
"200"
]
}
}
}
}]
}
}]
}
}
}
}]
}
}
above query not gives me a valid result. What is wrong in above query?
How I can use a must_not in must with nested query?
You should use must and must_not in the same bool query.
{
"bool": {
"must_not": [{
"nested": {
"path": "fields",
"query": {
"terms": {
"fields.value.raw": [
"200"
]
}
}
}
}],
"must": [{
"match": {
"fields.uid": "number"
}
}]
}
}

passing multiple combination query in elastic search

`"query": {
"function_score": {
"query": {
"bool": {
"must": [],
"should": [],
"filter": [
{
"terms": {
"category": "type-1",
"product": "product-A"
},
"terms": {
"category": "type-2",
"product": "product-B"
}
}
]
}
},
"functions": []
}
},`
I want to pass multiple combination query like above is it possible, what should be the correct query format
in sql my query would be
select * from product where (category='type1' and product=product-A) or (category='type2' and product=product-B) or (category='type3' and product=product-C)
i want to replicate above query
If you want to make a OR statement in a bool query you should is a nested bool query with multiple should clause.
so try :
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [],
"should": [],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"category": "type-1"
}
},
{
"term": {
"product": "product-A"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"category": "type-2"
}
},
{
"term": {
"product": "product-B"
}
}
]
}
}
]
}
}
]
}
}
}
},
"functions": []
}
and if you have no must clause you can move your filters clauses into the main should as only document that matchs at least one of the clause will match.

How to combine more than two filters in elasticsearch?

I want to write query like this for elasticsearch 1.7:
SELECT * FROM prods WHERE
id=1 AND
name='flower' AND
(count_usd=1 OR prise_usd=5) AND
(count_eur=1 OR prise_eur=5)
?
I've read about nested bool ( https://www.elastic.co/guide/en/elasticsearch/guide/1.x/combining-filters.html ) but cannot apply it to my context :-(
You can nest a bool query within a bool query to achieve this kind of filtering:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"term": {"name":"flower"}
}],
"should": [{
"bool": {
"should": [{
"term": {
"count_usd": 1
}
}, {
"term": {
"prise_usd": 5
}
}]
}
}, {
"bool": {
"should": [{
"term": {
"count_eur": 1
}
}, {
"term": {
"prise_eur": 5
}
}]
}
}]
}
}
}
}
}
Note that should clause acts like an OR: at least one of the subclauses in a should must be true to match.

Multiple values in nested elastic search 2 query

I have a nested object named 'bundles', that usually contains more than one object. Using this query I can succesfully query on the id of an object in bundles, but I fail to write a query that can query on multiple id's. Suggestions?
{
"query": {
"nested": {
"path": "bundles",
"query": {
"bool": {
"must": [
{
"match": {
"bundles.id": 43273
}
}
]
}
},
"inner_hits": {}
}
}
}
Perhaps you want "should" instead of "must" in the boolean filter. For example:
{
"query": {
"nested": {
"path": "bundles",
"query": {
"bool": {
"should": [
{
"match": {
"bundles.id": 43273
},
{
"match": {
"bundles.id": 433373
}
}
]
}
}
}
}
}
You could also use terms query if the field can be matched exactly. For example:
{
"query": {
"nested": {
"path": "bundles",
"query": {
"bool": {
"must": [
{
"terms": {
"bundles.id": [1140000000, 114]
}
}
]
}
}
}
}
}'

Resources