query malformed, no start_object after query name - elasticsearch

I am running this query against AWS Elasticsearch 5.1 and getting a malformed query error. Here is the body of the request. I am basically just checking if the field exists during the time range.
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"range": {
"#timestamp": {
"gt": "2017-03-21T15:37:08.595919Z",
"lte": "2017-04-21T15:52:08.595919Z"
}
}
},
{
"query": [
{
"query_string": {
"query": "_exists_: $event.supplier"
}
}
]
}
]
}
}
}
},
"sort": [
{
"#timestamp": {
"order": "asc"
}
}
]
}

The second must statement was incorrect:
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"range": {
"#timestamp": {
"gt": "2017-03-21T15:37:08.595919Z",
"lte": "2017-04-21T15:52:08.595919Z"
}
}
},
{
"query_string": {
"query": "_exists_: $event.supplier"
}
}
]
}
}
}
},
"sort": [
{
"#timestamp": {
"order": "asc"
}
}
]
}

Related

How to write complex ElasticSearch query

I want to return the data from ElasticSearch using the range query.
My Condition is something like this.
((Range(Price and Discount) OR Range(Price) AND Filter(Must1) AND Filter(Must2))
The issue I am facing is that some document contains both price and discount but some only contains Price. I need a query to get data according to the specified range. So, it returns the discount field but not the specified range which I want.
right now I am using this query.
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"range": {
"discount": {
"gte": 10,
"lte": 12
}
}
},
{
"range": {
"price": {
"gte": 10,
"lte": 12
}
}
}
]
}
},
{
"bool": {
"should": [
{
"range": {
"discount": {
"gte": 10,
"lte": 12
}
}
},
{
"range": {
"price": {
"gte": 10,
"lte": 12
}
}
}
]
}
}
]
}
},
{
"terms": {
"Category": [
"123"
]
}
},
{
"nested": {
"path": "the_path",
"query": {
"bool": {
"must": {
"match": {
}
},
"filter": [
]
}
}
}
}
]
}
}
Please help me with this I am stuck with it from past few days.
Based on the condition you have given, following DSL Query will be created
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
},
{
"range": {
"deiscount": {
"gte": 10,
"lte": 20
}
}
}
]
}
},
{
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
}
]
}
},
{
"bool": {
"filter": {
"term": {
"user.id": "kimchy"
}
}
}
},
{
"bool": {
"filter": {
"term": {
"user.id": "kimchy"
}
}
}
}
]
}
}
}

ElasticSearch - combining search queries not working

I would like to have an intersection of 2 queries
I got 3 documents in the index:
"_id": "68c220aa-ea51-4f84-b880-29af3302cae9",
"_id": "b6c1c3c5-e959-480f-a145-f5598fafea66",
"_id": "2d30de72-0a2b-465c-8770-970ad9760d47",
Query1:
{
"from": 0,
"query": {
"nested": {
"path": "attributes",
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"attributes.asReference": {
"query": "8670ff39-6a0d-4ae8-e217-08d88efd4771"
}
}
},
{
"match_phrase": {
"attributes.attributeId": {
"query": "f51ca670-4223-4ea2-8007-d111dd38a14f"
}
}
}
]
}
}
]
}
}
}
},
"size": 10,
"sort": [
{
"modified": {
"order": "asc"
}
},
{
"created": {
"order": "asc"
}
}
]
}
returns all 3 documents as it should
"_id": "68c220aa-ea51-4f84-b880-29af3302cae9",
"_id": "b6c1c3c5-e959-480f-a145-f5598fafea66",
"_id": "2d30de72-0a2b-465c-8770-970ad9760d47",
Then I do query2:
{
"from": 0,
"query": {
"nested": {
"path": "attributes",
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"attributes.asShortString": {
"query": "RA-005"
}
}
},
{
"match_phrase": {
"attributes.attributeId": {
"query": "7ff3dbc1-3586-4475-9162-5430bb06c6d0"
}
}
}
]
}
}
]
}
}
}
},
"size": 10,
"sort": [
{
"modified": {
"order": "asc"
}
},
{
"created": {
"order": "asc"
}
}
]
}
returns 1 document:
"_id": "b6c1c3c5-e959-480f-a145-f5598fafea66"
But when I combine the queries to:
{
"from": 0,
"query": {
"nested": {
"path": "attributes",
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"attributes.asReference": {
"query": "8670ff39-6a0d-4ae8-e217-08d88efd4771"
}
}
},
{
"match_phrase": {
"attributes.attributeId": {
"query": "f51ca670-4223-4ea2-8007-d111dd38a14f"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"match_phrase": {
"attributes.asShortString": {
"query": "RA-005"
}
}
},
{
"match_phrase": {
"attributes.attributeId": {
"query": "7ff3dbc1-3586-4475-9162-5430bb06c6d0"
}
}
}
]
}
}
]
}
}
}
},
"size": 10,
"sort": [
{
"modified": {
"order": "asc"
}
},
{
"created": {
"order": "asc"
}
}
]
}
Here I do not get any documents
So the subqueries are working but combined it does not work (it produces 0 results)
What am I missing here?
Due to the way nested documents and queries work, you need to have two separate nested queries in your bool/must query, because each will/might match a different nested document of the same parent document:
{
"from": 0,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "attributes",
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"attributes.asReference": {
"query": "8670ff39-6a0d-4ae8-e217-08d88efd4771"
}
}
},
{
"match_phrase": {
"attributes.attributeId": {
"query": "f51ca670-4223-4ea2-8007-d111dd38a14f"
}
}
}
]
}
}
]
}
}
}
},
{
"nested": {
"path": "attributes",
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase": {
"attributes.asShortString": {
"query": "RA-005"
}
}
},
{
"match_phrase": {
"attributes.attributeId": {
"query": "7ff3dbc1-3586-4475-9162-5430bb06c6d0"
}
}
}
]
}
}
]
}
}
}
}
]
}
},
"size": 10,
"sort": [
{
"modified": {
"order": "asc"
}
},
{
"created": {
"order": "asc"
}
}
]
}

elasticsearch nested range query

Suppose i want this structure for a document:
{
"hours": {
"open": [
{
"start": 10,
"end": 19
},
{
"start": 21,
"end": 29
}
...
],
"closed": [
{
"start": 100,
"end": 199
},
{
"start": 201,
"end": 299
}
...
]
}
}
whose index has this mapping:
{
"mappings": {
"_doc": {
"properties": {
"hours": {
"properties": {
"open": {
"type": "nested",
"properties": {
"start": { "type": "integer" },
"end": { "type": "integer" }
}
},
"closed": {
"type": "nested",
"properties": {
"start": { "type": "integer" },
"end": { "type": "integer" }
}
}
}
}
}
}
}
}
In the Elasticsearch Query DSL, how do i find all documents where 20 lies inside an open segment and not inside a closed segment. The query I tried was incorrect.
failed query
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"path": "hours.open",
"query": {
"range": {
"hours.open.start": { "lte": 20 }
}
}
}
},
{
"nested": {
"path": "hours.open",
"query": {
"range": {
"hours.open.end": { "gte": 20 }
}
}
}
}
]
}
},
{
"bool": {
"must_not": [
{
"bool": {
"must": [
{
"nested": {
"path": "hours.closed",
"query": {
"range": {
"hours.closed.start": { "lte": 20 }
}
}
}
},
{
"nested": {
"path": "hours.closed",
"query": {
"range": {
"hours.closed.end": { "gte": 20 }
}
}
}
}
]
}
}
]
}
}
]
}
}
}
whats wrong with my query? it is returning this document which is not what i intended. 20 does not lie inside an open segment.
I finally got it working. The following is the correct query:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "hours.open",
"query": {
"bool": {
"must": [
{ "range": { "hours.open.start": { "lte": 20 } } },
{ "range": { "hours.open.end": { "gte": 20 } } }
]
}
}
}
}
],
"must_not": [
{
"nested": {
"path": "hours.closed",
"query": {
"bool": {
"must": [
{ "range": { "hours.closed.start": { "lte": 20 } } },
{ "range": { "hours.closed.end": { "gte": 20 } } }
]
}
}
}
}
]
}
}
}
With that said, it looks like my original attempt was wrong because there were two different hours.open nested path queries and likewise two different hours.closed nested path queries. The parser must only take one of them for a single path.
Seems like you need to swap lte and gte:
"hours.open.start": { "gte": 20 }
"hours.open.end": { "lte": 20 }
and same for the closing times:
"hours.closed.start": { "gte": 20 }
"hours.closed.end": { "lte": 20 }
Edit:
The must and must_not need to be part of the same bool query:
{
"query": {
"bool": {
"must": [{
"nested": {
"path": "hours.open",
"query": {
"range": {
"hours.open.start": {
"gte": 20
}
}
}
}
},
{
"nested": {
"path": "hours.open",
"query": {
"range": {
"hours.open.end": {
"lte": 20
}
}
}
}
}
],
"must_not": [{
"bool": {
"must": [{
"nested": {
"path": "hours.closed",
"query": {
"range": {
"hours.closed.start": {
"gte": 20
}
}
}
}
},
{
"nested": {
"path": "hours.closed",
"query": {
"range": {
"hours.closed.end": {
"lte": 20
}
}
}
}
}
]
}
}]
}
}
}

How to use range post_filter in elasticsearch

I cannot find examples of the syntax anywhere and the following does not work:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{ "term": { "category": "catname" }}
]
}
}
}
},
"post_filter": {
"terms": {"type": ["foo1", "foo2"] },
"range": { "price": { "gte": 300, "lte": 600 } }
}
}
You just need to wrap all your terms filters in an additional bool/must filter:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"category": "catname"
}
}
]
}
}
}
},
"post_filter": {
"bool": {
"must": [
{
"terms": { "type": [ "foo1", "foo2" ] }
},
{
"range": { "price": { "gte": 300, "lte": 600 } }
}
]
}
}
}

ElasticSearch order by _score

How can I order the results by _score?
I can't figure out how to calculate the score for each result, also :)
I managed to write this:
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"term": {
"type_licitatie": "3"
}
},
{
"term": {
"tip_sursa": "5"
}
}
]
}
}
}
},
"sort": [
{
"_score": {
"order": "desc"
}
}
]
}
and this:
{
"query": {
"function_score": {
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"term": {
"country_id": "1"
}
},
{
"term": {
"industry_id": "3"
}
}
]
}
}
}
},
"script_score" : {
"script": "(doc['country_id'].values=1) + (doc['industry_id'].values=3)"
},
"boost_mode": "replace"
}
}
}

Resources