elasticsearch nested query, more than one one object should meet conditions - elasticsearch

I have some questions about nested query.
Here is my example. The mapping is {"user":"nested"}.The exist data just like this:
{
"user": [
{
"first":"John",
"last":"Smith"
},
{
"first":"Alice",
"last":"White"
}
]
}
How do I create a query to find this document that meets all the conditions:
the first object of user that its "first" is "John" and "last" is "Smith";
the second object of user that its "first" is "Alice" and "last" is "White"

Try with below query :
{
"query":{
"bool":{
"filter":[
{
"bool":{
"must":[
{
"bool":{
"must":[
{
"nested":{
"query":{
"bool":{
"must":[
{
"match_phrase":{
"user.first":{
"query":"John"
}
}
},
{
"match_phrase":{
"user.last":{
"query":"Smith"
}
}
}
]
}
},
"path":"user"
}
},
{
"nested":{
"query":{
"bool":{
"must":[
{
"match_phrase":{
"user.first":{
"query":"Alice"
}
}
},
{
"match_phrase":{
"user.last":{
"query":"White"
}
}
}
]
}
},
"path":"user"
}
}
]
}
}
]
}
}
]
}
}
}

Below query is what you are looking for. You simply need to have two nested queries, one for each conditions you've mentioned, combined in a bool using must clause.
Note that I'm assuming that the fields user.first and user.last are of text type having standard analyzer
POST <your_index_name>
{
"query":{
"bool":{
"must":[
{
"nested":{
"path":"user",
"query":{
"bool":{
"must":[
{
"match":{
"user.first":"john"
}
},
{
"match":{
"user.last":"smith"
}
}
]
}
}
}
},
{
"nested":{
"path":"user",
"query":{
"bool":{
"must":[
{
"match":{
"user.first":"alice"
}
},
{
"match":{
"user.last":"white"
}
}
]
}
}
}
}
]
}
}
}
Hope this helps!

The answer is:
{
"query": {
"bool": {
"must": [
{
"has_parent": {
"parent_type": "doc",
"query": {
"bool": {
"must": [
{
"terms": {
"id": [
713
]
}
},
{
"range": {
"created": {
"lte": "now/d"
}
}
},
{
"range": {
"expires": {
"gte": "now/d"
}
}
}
]
}
}
}
},
{
"nested": {
"path": "prices",
"query": {
"bool": {
"filter": [
{
"term": {
"prices.id_prcknd": 167
}
}
]
}
}
}
},
{
"term": {
"doc_type": "item"
}
},
{
"bool": {
"should": [
{
"term": {
"have_prices": true
}
},
{
"term": {
"is_folder": true
}
}
]
}
}
],
"must_not": {
"exists": {
"field": "folder"
}
}
}
},
"sort": [
{
"is_folder": {
"order": "desc"
}
},
{
"title_low.order": {
"order": "asc"
}
}
],
"size": 1000
}

Related

Elastic Search error in complex bool query

I am trying to make an elasticsearch query where, I need to search for a time frame in the elasticsearch table. I have records which has startime and endtime. And from UI I am giving a starttime and endtime which is time windows for which I need to search files for. Assuming the time window of starttime and endtime in the records is smaller than the time window entered by user, I have created the following query:
{
"_source":["filename","starttime","endtime"],
"sort":[{
"starttime":{"order":"asc"}
}],
"query":{
"bool":{
"should":{
"bool":{
"must":[
"range":{
"starttime":{
"lte":1489602610000
}
},
"range":{
"endtime":{
"gte":1489602610000,
}
}
]
}
},
"should":{
"bool":{
"must":[
"range":{
"starttime":{
"gte":1489602610000
}
},
"range":{
"endtime":{
"lte":1489689000000
}
}
]
}
},
"should":{
"bool":{
"must":[
"range":{
"starttime":{
"lte":1489689000000
}
},
"range":{
"endtime":{
"gte":1489689000000
}
}
]
}
}
}
}
}
I am getting error
"Unexpected character (':' (code 58)): was expecting comma to separate
Array entries\n at [Source:
org.elasticsearch.transport.netty4.ByteBufStreamInput#29263f09; line:
11, column: 33]"
There are several issues with your query:
one dangling comma
more than one bool/should clauses
range queries not properly wrapped inside curly braces
You can find the correct query below:
{
"_source": [
"filename",
"starttime",
"endtime"
],
"sort": [
{
"starttime": {
"order": "asc"
}
}
],
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"range": {
"starttime": {
"lte": 1489602610000
}
}
},
{
"range": {
"endtime": {
"gte": 1489602610000
}
}
}
]
}
},
{
"bool": {
"must": [
{
"range": {
"starttime": {
"gte": 1489602610000
}
}
},
{
"range": {
"endtime": {
"lte": 1489689000000
}
}
}
]
}
},
{
"bool": {
"must": [
{
"range": {
"starttime": {
"lte": 1489689000000
}
}
},
{
"range": {
"endtime": {
"gte": 1489689000000
}
}
}
]
}
}
]
}
}
}

How to query a nested object with combined AND and OR logic?

How should this expression be written as a query:
(attributes.id = 14 OR attributes.id = 15) AND (attributes.id = 4843 OR attributes.id = 4859)
The nested object looks like this:
{
"attributes":[
{
"id":14,
"type":"color",
"name":"Sort",
"version":1
},
{
"id":15,
"type":"color",
"name":"Sølv",
"version":1
},
{
"id":2031,
"type":"brand",
"name":"Jimmy Choo",
"version":1
},
{
"id":4843,
"type":"size",
"name":"36x28",
"version":1
},
{
"id":4859,
"type":"size",
"name":"38x36",
"version":1
},
{
"id":4927,
"type":"size",
"name":"60J",
"version":1
},
{
"id":4958,
"type":"size",
"name":"75F",
"version":1
}
]
}
I've tried using this query - among many - without any luck:
{
"query":{
"nested":{
"path":"attributes",
"query":{
"bool":{
"should":[
{
"terms":{
"attributes.id":[
14,
15
]
}
},
{
"terms":{
"attributes.id":[
4843,
4859
]
}
}
],
"minimum_should_match":2
}
}
}
}
}
The above query returns zero results.
Any help would be appreciated.
{
"query":{
"nested":{
"path":"attributes",
"query":{
"bool":{
"must":[{
"bool": {
"should": [
{
"term": {
"attributes.id": 14
}
},{
"term": {
"attributes.id": 15
}
}
]
}
},{
"bool": {
"should": [
{
"term": {
"attributes.id": 4843
}
},{
"term": {
"attributes.id": 4859
}
}
]
}
}
]
}
}
}
This should work.
I got the answer on Elastic Stack by Daniel_Penning:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "variants.attributes",
"query": {
"terms": {
"variants.attributes.id": [
14, 15
]
}
}
}
},
{
"nested": {
"path": "variants.attributes",
"query": {
"terms": {
"variants.attributes.id": [
4843, 4859
]
}
}
}
}
]
}
}
}

How to do mulitple text search in elastic search

I want to do multiple text search in same field
for example in sub_cat_seo_url field i want to get 'english-news' and 'business-news' filter by language and region
when i tried like below code it is not working
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":[
"english-news",
"business-news"
],
"fields":[
"sub_cat_seo_url"
]
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"lang":"en"
}
},
{
"term":{
"region":"1"
}
}
]
}
}
}
}
}
For single text search it is working fine
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":"english-news",
"fields":[
"sub_cat_seo_url"
]
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"lang":"en"
}
},
{
"term":{
"region":"1"
}
}
]
}
}
}
}
}
Please help what have to change in my code, to do multi text search in same field (or operation)
Thanks
Thanigaivelan
Try out this
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "\"english-news\" AND \"business-news\"",
"fields": [
"sub_cat_seo_url"
]
}
},
"filter": {
"bool": {
"must": [
{
"term": {
"lang": "en"
}
},
{
"term": {
"region": "1"
}
}
]
}
}
}
}
}

Combining Multiple Queries with 'OR' or 'AND' in Elasticsearch

I have two distinct query, and I want to combine them with an 'OR'/'AND' in between. How do I do that?
For example, for the given queries
I just want to run Query1 OR Query2 in the elasticsearch.
Query1:
{
"query": {
"filtered": {
"query": {
"query_string":{
"query":"Batman",
"default_operator":"AND",
"fields"::[
"Movies._all"
]
}
},
"filter": {
"bool": {
"must": [
{
"query":{
"filtered":{
"filter":{
"and":[
{
"term":{
"cast.firstName":"Christian "
}
},
{
"term":{
"cast.lastName":"Bale"
}
}
]
}
}
}
}
]
}
}
}
}
}
Query2:
{
"query": {
"filtered": {
"query": {
"query_string":{
"query":"Dark Knight",
"default_operator":"AND",
"fields"::[
"Movies._all"
]
}
},
"filter": {
"bool": {
"must": [
{
"query":{
"filtered":{
"filter":{
"and":[
{
"term":{
"director.firstName":"Christopher"
}
},
{
"term":{
"director.lastName":"Nolan"
}
}
]
}
}
}
}
]
}
}
}
}
}
You need to use a bool query
Something like below will work fine -
{
"query" : {
"bool" : {
"must" : [
{ // Query1 },
{ // Query2}
]
}
}
}
Use must for AND and should for OR

ElasticSearch nested [AND OR] filter

I have a query that I'm struggling with in ElasticSearch. In fact, I have two queries, the first works, but the second doesn't.
Here's the basics 1st query, which is fine (the query is actually quite a bit bigger than this, I've stripped it down for ease of understanding):
{
"query": {
"filtered": {
"query": {
"match_all":{}
},
"filter": {
"and": [{
"term":{
"relatedID":"214"
}
},
{
"term":{
"relatedType":"deal"
}
}]
}
}
}
}
So basically, grab all items where relatedID == 214 && relatedType == "deal"
However, what I'd also like to do, is this:
{
"query": {
"filtered": {
"query": {
"match_all":{}
},
"filter": {
"and": [{
{
"or":[{
"and":[{
"relatedID":"528"
},
{
"relatedType":"company"
}]
},{
"and":[{
"relatedID":"214"
},
{
"relatedType":"deal"
}
]}
]}
]}
}
}
}
}
}
}
So basically, grab everything where either deal AND 214, or company AND 528.
This is where I seem to be getting stuck.
As I said, there's a lot more in the query (see full query below, which might help understand why I've tried to structure it above):
{
"query":{
"filtered":{
"query":{
"match_all":{}
},
"filter":{
"and":[
{
"or":[
{
"term":{
"timeSensitive":false
}
}
,
{
"range":{
"validTo":{
"gt":"20140513T153030Z"
},
"validFrom":{
"lt":"20140513T153030Z"
}
}
}
]
}
,
{
"term":{
"categoryTree.NAME":"Promotions"
}
}
,
{
"or":[
{
"and":[
{
"relatedID":"528"
}
,
{
"relatedType":"company"
}
]
}
,
{
"and":[
{
"relatedID":"214"
}
,
{
"relatedType":"deal"
}
]
}
]
}
,
{
"terms":{
"permissions": ["member","supplier"]
}
}
,
{
"term":{
"siteID":6
}
}
]
}
}
}
}
}
}
Its syntax error.The problem is You didn't mentions term filter inside (and,or)filter.
try to use boolean filter and query its more faster than (and or) filter.
[AND OR] Filter
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"or": [
{
"and": [
{
"term": {
"relatedID": "528"
}
},
{
"term": {
"relatedType": "company"
}
}
]
},
{
"and": [
{
"term": {
"relatedID": "214"
}
},
{
"term": {
"relatedType": "deal"
}
}
]
}
]
}
]
}
}
}
}
Same Query i converted in to bool filter
curl -XPOST "http://localhost:9200/try/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"relatedID":"528"
}
},
{
"term": {
"relatedType":"company"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"relatedID":"214"
}
},
{
"term": {
"relatedType":"deal"
}
}
]
}
}
]
}
}
}
}
}'

Resources