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
]
}
}
}
}
]
}
}
}
Related
I am having nested array fields, I need to query and filter the records for that.
Sample
"test":{
"name":[
{
"name": "vanaraj",
"Age" : 26
},
{
"name": "vanaraj",
"Age" : 10
},
{
"name": "ranjit",
"Age" : 26
},
]
}
Here how I need the query for below conditions,
1. Where Name is equal to both ["vanaraj","ranjit"] to fetch
2. Add condition where Age > 25 for only "vanaraj"
I need a query like below, but it is not working.
{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "data.test.name",
"query": {
"bool": {
"filter": [
{
"terms": {
"data.test.name.name": ["vanaraj","ranjit"]
}
}
]
}
}
}
},
{
"nested": {
"path": "data.test.name",
"query": {
"bool": {
"filter": [
{
"term": {
"data.test.name.name": "vanaraj"
}
},
{
"range": {
"data.test.name.Age": {
"gt": 25
}
}
}
]
}
}
}
}
]
}
}
}
Mapping :
{
"mappings":{
"properties":{
"test":{
"properties":{
"name":{
"type":"nested",
"properties":{
"Age":{
"type":"long"
},
"name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
}
}
}
}
}
}
}
}
Based on your further clarification, the below code should help you:
Solution:
POST <your_index_name>/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "test.name",
"query": {
"bool": {
"must": [
{
"match": {
"test.name.name": "vanaraj"
}
},
{
"range": {
"test.name.Age": {
"gte": 26
}
}
}
]
}
}
}
},
{
"nested": {
"path": "test.name",
"query": {
"bool": {
"must": [
{
"match": {
"test.name.name": "ranjith"
}
}
]
}
}
}
}
]
}
}
}
The above solution would return you all the documents having name as ranjith OR if name is vanaraj and age > 25
Summary of query:
Bool
- Should
- Must clause for name=vanaraj and age >= 26
- Must clause for name=ranjith
Updated Solution:
POST <your_index_name>/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "test.name",
"query": {
"bool": {
"must": [
{
"match": {
"test.name.name": "vanaraj"
}
},
{
"range": {
"test.name.Age": {
"gte": 26
}
}
}
]
}
}
}
},
{
"nested": {
"path": "test.name",
"query": {
"bool": {
"must": [
{
"terms": {
"test.name.name": [
"abc",
"ranjit"
]
}
}
]
}
}
}
}
],
"must_not": [
{
"nested": {
"path": "test.name",
"query": {
"bool": {
"must": [
{
"terms": {
"test.name.name": [
"vanaraj"
]
}
},{
"range": {
"test.name.Age": {
"lte": 25
}
}
}
]
}
}
}
}
]
}
}
}
Please run the above and let me know if this helps!
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
}
I'm trying to exclude duplicated documents which have the same slug parameters to do it I use aggs in ElasticSearch (version 2.4). I use - this query:
{
"fields":[
"id",
"score"],
"size":0,
"query":{
"function_score":{
"query":{
"bool":{
"should":[
{
"match":{
"main_headline.en":{
"query":"headline_for_search"
}
}
},
{
"match":{
"body.en":"body for search"
}
}],
"must_not":{
"term":{
"id":75333
}
},
"filter":[
{
"term":{
"status":3
}
},
[
{
"term":{
"sites":6
}
}]]
}
},
"functions":[
{
"gauss":{
"published_at":{
"scale":"140w",
"decay":0.3
}
}
}
]
},
"aggs":{
"postslug":{
"terms":{
"field":"slug",
"order":{
"top_score":"desc"
}
},
"aggs":{
"grouppost":{
"top_hits": {
"_source": {
"include": [
"id",
"slug",
]
},
"size" : 10
}
}
}
}
}
}
}
When I run it I get error
failed to parse search source. expected field name but got [START_OBJECT]
I can`t figure out where is a mistake.
Without section aggs all works fine (except present duplicates)
I see one issue which relates to the fact that in the source filtering section include should read includes. Also, the aggs section is not at the right location, you have it in the query section, and it should be at the top-level:
{
"fields": [
"id",
"score"
],
"size": 0,
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"main_headline.en": {
"query": "headline_for_search"
}
}
},
{
"match": {
"body.en": "body for search"
}
}
],
"must_not": {
"term": {
"id": 75333
}
},
"filter": [
{
"term": {
"status": 3
}
},
[
{
"term": {
"sites": 6
}
}
]
]
}
},
"functions": [
{
"gauss": {
"published_at": {
"scale": "140w",
"decay": 0.3
}
}
}
]
}
},
"aggs": {
"postslug": {
"terms": {
"field": "slug",
"order": {
"top_score": "desc"
}
},
"aggs": {
"grouppost": {
"top_hits": {
"_source": {
"includes": [
"id",
"slug"
]
},
"size": 10
}
}
}
}
}
}
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"
}
}
]
}
}
]
}
}
}
}
}'
ElasticSearch Version: 0.90.1, JVM: 1.6.0_51(20.51-b01-457)
I'm trying to do two things with my ElasticSearch query: 1) filter the results based on a boolean (searchable) and "open_date < tomorrow" and 2) two sort by the field "open_date" DESC
This produces the following query:
{
"query": {
"bool": {
"should": [
{
"prefix": {
"name": "foobar"
}
},
{
"query_string": {
"query": "foobar"
}
},
{
"match": {
"name": {
"query": "foobar"
}
}
}
],
"minimum_number_should_match": 1
},
"filtered": {
"filter": {
"and": [
{
"term": {
"searchable": true
}
},
{
"range": {
"open_date": {
"lt": "2013-07-16"
}
}
}
]
}
}
},
"sort": [
{
"open_date": "desc"
}
]
}
However, the results that come back are not being sorted by "open_date". If I remove the filter:
{
"query": {
"bool": {
"should": [
{
"prefix": {
"name": "foobar"
}
},
{
"query_string": {
"query": "foobar"
}
},
{
"match": {
"name": {
"query": "foobar"
}
}
}
],
"minimum_number_should_match": 1
}
},
"sort": [
{
"open_date": "desc"
}
]
}
... the results come back as expected.
Any ideas?
I'm not sure about the Tire code, but the JSON does not correctly construct a filtered query. My guess is that this overflows and causes the sort element to also not be correctly parsed.
A filtered query should be constructed like this (see http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/ ):
{
"query": {
"filtered": { // Note: this contains both query and filter
"query": {
"bool": {
"should": [
{
"prefix": {
"name": "foobar"
}
},
{
"query_string": {
"query": "foobar"
}
},
{
"match": {
"name": {
"query": "foobar"
}
}
}
],
"minimum_number_should_match": 1
}
},
"filter": {
"and": [
{
"term": {
"searchable": true
}
},
{
"range": {
"open_date": {
"lt": "2013-07-16"
}
}
}
]
}
}
},
"sort": [
{
"open_date": "desc"
}
]
}
Cheers,
Boaz