Combine Elastic Search Queries - elasticsearch

I have the following 2 queries:
GET places/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"term": {
"approved": false
}
}
]
}
}
}
}
}
GET places/_search
{
"query": {
"filtered": {
"filter": {
"geo_bounding_box": {
"loc": {
"top_left": "54.6152065515344, -6.09334913041994",
"bottom_right": "54.5754258987271, -5.76633420732423"
}
}
}
}
}
}
Both work fine, however I am having issues combining the queries and was wondering if anyone could help. Basically I want to retuen all items within the bounding box specified, where the "approved" property is false.

You can keep your filtered query and merge both conditions simply like this inside in a bool/mustfilter
curl -XPOST localhost:9200/places/_search -d '{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"approved": false
}
},
{
"geo_bounding_box": {
"loc": {
"top_left": "54.6152065515344, -6.09334913041994",
"bottom_right": "54.5754258987271, -5.76633420732423"
}
}
}
]
}
}
}
}
}'

Related

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"
}
}
]
}
}
}

How to combine term filters with a missing filter in Elasticsearch?

We are using Elasticsearch 1.6 and I have a working three term query that I need to modify with a stand alone working missing filter. Here is the current code:
The original term query with three entries
GET ...
{
"query": {
"nested": {
"path": "MAIN_FIELD",
"query": {
"bool": {
"must": [
{
"term": {
"MAIN_FIELD.ID": 1234
}
},
{
"term": {
"MAIN_FIELD.OTHER_IND": "false"
}
},
{
"term": {
"MAIN_FIELD.INDICATOR": "Y"
}
}
]
}
}
}
}
}
The stand alone missing query:
GET ...
{
"query" : {
"filtered" : {
"filter" : {
"missing" : { "field" : "MAIN_FIELD.OTHER_IND" }
}
}
}
}
How do I change the term query from the first query:
"term": {
"MAIN_FIELD.OTHER_IND": "false"
}
to use a missing filter?
I think what you want is below:
{
"query": {
"nested": {
"path": "MAIN_FIELD",
"query": {
"bool": {
"must": [
{
"term": {
"MAIN_FIELD.ID": 1234
}
},
{
"filtered": {
"filter": {
"missing": {
"field": "MAIN_FIELD.OTHER_IND"
}
}
}
},
{
"term": {
"MAIN_FIELD.INDICATOR": "Y"
}
}
]
}
}
}
}
}

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"
}
}
]
}
}
]
}
}
}
}
}'

ElasticSearch ignoring sort when filtered

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

What's wrong with this Elastic query

Why this query works fine (returns the proper result):
{
"filter": {
"term": { "id": "123456" }
}
}
and this one does not (returns HTTP 500):
{
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": { "id": "123456" }
}
}
}
?
Elasticsearch expects query element on the root level similar to the "filter" element. Try this:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": { "id": "123456" }
}
}
}
}

Resources