Elastic Search: No query registered for [or] - ruby

Can anyone help me with this query? I am getting an error returned: "No query registered for [or]" Did I structure this wrong? It's supposed to filter all results where area is 530 and start is blank OR area is 530 and start is "06192013", then based on that boost the document with the other filters.
{
"query": {
"custom_filters_score": {
"query": {
"bool": {
"must": [
{"field":{"sector":"sector1"}},
{"term":{"user_type":"ghost"}},
{"term":{"area":"530"}}
]
},
"filter":{
"or": [
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"06192013"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"blank"}}
]
}
]
}
},
"filters": [
{"filter":{"term":{"relevance" :5726}},"boost":"1000"},
{"filter":{"term":{"relevance2":5726}},"boost":"100"}
],
"score_mode":"total"
}
}
}

The filter object that contains the or filter is misplaced. I guess you wanted to use a filtered query containing the bool query and the or filter like this:
{
"filtered" : {
"bool": {
"must": [
{"field":{"sector":"sector1"}},
{"term":{"user_type":"ghost"}},
{"term":{"area":"530"}}
]
},
"filter" : {
"or": [
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"06192013"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"blank"}}
]
}
]
}
}
}

Related

Elasticsearch query using more_like_this field renders a failed to parse search source. expected field name but got [START_OBJECT] error

We're using Elasticsearch 2.4.5. Have an application that can generate fairly complicated queries. I'm trying to add a more_like_this field to the query like so:
{
"query": {
"more_like_this": {
"fields": [
"title"
],
"ids": [
1234
],
"min_term_freq": 1,
"max_query_terms": 25
},
"function_score": {
"query": {
"bool": {
"must": [
{
"query_string": {
"default_operator": "AND",
"fields": [
"title",
"author"
],
"query": "((title:(\"Tale of Two Cities\"^2)))",
"lenient": true
}
}
],
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_distance": {
"distance": "50mi",
"location": {
"lat": 49.32,
"lon": -45.67
},
"distance_type": "plane",
"_cache": true
}
}
]
}
},
{
"term": {
"merged": 0
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "title_type"
}
}
}
}
]
}
}
}
},
"functions": [
{
"field_value_factor": {
"field": "quality_score",
"factor": 1,
"missing": 0
}
}
]
}
},
"filter": {
"bool": {
"must": []
}
},
"sort": "_score",
"size": 20,
"from": 0
}
I'm getting a failed to parse search source. expected field name but got [START_OBJECT] error when I try to run the above code. When I remove that piece of code the query executes correctly. I've looked at documentation and other examples of more_like_this usage and I can't determine what's wrong with my query. I'm assuming it has something to do with the way the rest of the query is formed.

How to search by an specific value or where field does not exists in Elasticsearch

Im trying to do a query where the field does not exists or is in the array of values, in mongo it would be something like this.
$or: [
{
field: {
$exists: false
}
},
{
field: [val1, val2]
}
]
I've tried with a couple of variations but cant seem to find the solution for this.
Edit1:
Basically on my mind, the query should look like.
query: {
"bool": {
"should": [
"must": {...logic}
"must_not": {...logic}
]
}
}
this returns
"must_not" malformed
You need to do it like this:
{
"query": {
"bool": {
"should": [
{
"terms": {
"field": [
"val1",
"val2"
]
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "field"
}
}
}
}
]
}
}
}

Bool query not working as expected

POST /test/topic/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "Predisposition",
"fields": [
"_all"
]
}
},
{
"multi_match": {
"query": "thrombosis",
"fields": [
"_all"
]
}
}
],
"should": [
{
"multi_match": {
"query": "cancer",
"fields": [
"_all"
]
}
}
]
}
}
}
My understanding of the above query is that it must match on predisposition AND thrombosis OR cancer, however I'm only getting a handful of documents that match on predisposition AND thrombosis, I was expecting lots of cancer documents but have zero. What am I missing?
The must needs to always match. should will only give a boost to the score if it matches.
Also, there is another case when there are no must statements and in this case at least one should must match.
I think you are looking for the following, instead:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"multi_match": {
"query": "Predisposition",
"fields": [
"_all"
]
}
},
{
"multi_match": {
"query": "thrombosis",
"fields": [
"_all"
]
}
}
]
}
},
{
"bool": {
"must": [
{
"multi_match": {
"query": "cancer",
"fields": [
"_all"
]
}
}
]
}
}
]
}
}
}
The way you are searching is, documents must have predisposition AND thrombosis regardless of cancer because they are inside must filter.
You basically need to wrap your must clause inside should clause like this
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"multi_match": {
"query": "predisposition",
"fields": "_all"
}
},
{
"multi_match": {
"query": "thrombosis",
"fields": "_all"
}
}
]
}
},
{
"multi_match": {
"query": "cancer",
"fields": "_all"
}
}
]
}
}
}
This will give you the desired results.

Use partial_fields in elasticsearch kibana query

I am trying to add the partial_fields directive to an elasticsearch query (generated from kibana's table widget).
Where exactly would I have to place this statement in the below ES query?
Already tried to add it right after the first "query" node which produces valid json but still doesn't exclude xmz_Data
"partial_fields": {
"partial1": {
"exclude": "xmz_Data"
}
},
ES Query
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "*"
}
}
]
}
},
"filter": {
"bool": {
"must": [
{
"match_all": {}
},
{
"bool": {
"must": [
{
"match_all": {}
}
]
}
}
]
}
}
}
},
"highlight": {
"fields": {},
"fragment_size": 2147483647,
"pre_tags": [
"#start-highlight#"
],
"post_tags": [
"#end-highlight#"
]
},
"size": 250,
"sort": [
{
"timestamp": {
"order": "desc"
}
}
]
}
You can place the partial_fields directive anywhere in your query, I tested successfully with it both before and after the query node. However, your formatting for the excluded fields value is incorrect. Your exclude fields value needs to be an array. Try this instead...
"partial_fields": {
"partial1": {
"exclude": ["xmz_Data"]
}
},

Creating an Elastic Search AND OR Query

I am trying to write a query that requires "area" to be 530 and "starts" to be 06192013 OR "area" to be "530" and "starts" to be "blank". Additionally, in both of those scenarios "space" to be "top" OR "space2" to be "bottom". This query seems to be grabbing anything that matches any of these scenarios, how do I change it to make it work like I would like?
{
"size":25,
"from":0,
"query": {
"custom_filters_score": {
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{"term":{"type":"ghost"}},
{"term":{"area":"530"}}
]
}
},
"filter" : {
"or": [
{"terms":{"space": ["top"]}},
{"terms":{"space2":["bottom"]}},
{
"and": [
{"term":{"area":"530"}},
{"term":{"start":"06192013"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{"term":{"starts":"blank"}}
]
}
]
}
}
},
"filters": [
{"filter":{"term":{"filter1":5743}}, "boost":"1000"},
{"filter":{"term":{"filter2":4451}}, "boost":"64"},
{"filter":{"term":{"filter3":["tech"]}}, "boost":"16"},
{"filter":{"terms":{"filter4":[]}}, "boost":"8"},
{"filter":{"terms":{"filter5":[]}}, "boost":"5"},
{"filter":{"term":{"access":"1"}}, "boost":"2"}
],
"score_mode":"total"
}
}
}
You are almost there! Try this:
...
"query" : { "match_all":{} },
"filter" : {
"and": [
{
"or": [
{"term":{"space": "top"}},
{"term":{"space2":"bottom"}}
]
},
{
"and": [
{"term":{"area":"530"}},
{
"or": [
{"term":{"start":"06192013"}},
{"term":{"starts":"blank"}}
]
}
]
}
]
}
...
Unless you need scoring (which it doesnt look like) you should do all of this with filters, they don't calculate scores and are faster because of this.
The "query" : { "match_all":{} } will end up giving all docs witch match the filter the same score.
Note: I also turned your terms query with an array of one element to a term query...

Resources