custom filters score query for elasticsearch giving parsing error - elasticsearch

it's telling me there's No query registered for [custom_filters_score]];
here's my code in basic form:
"query": {
"custom_filters_score": {
"query": {
"match_all": {}
},
"filters": [
{
"filter": {
"exists": {
"field": "salesrank"
}
}
,"script": "1 / doc['salesrank'].value"
}
]
}
}
}
EDIT:
the only thing i've noticed that works is to put another query after the first 'query' and before 'custom_filters_score'. this then leaves me with two queries - not sure what to do with them both however, and the documentation does not indicate this. :(
i also have to get rid of the 'filters' array and only use a 'filter' object. so, the only thing i've found to work is something like this:
{
"query": {
"match_all": {},
"custom_filters_score": {
"query": {
"match_all": {}
},
"filter": {
"exists": {
"field": "salesrank"
}
,"script": "1 / doc['salesrank'].value"
}
}
}
}

According to the elasticsearch documentation: http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-custom-filters-score-query.html, the "custom_filters_score" was deprecated. The correct way to do it is to replace it with "function_score"
{
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": "1 / doc['salesrank'].value"
}
}
],
"query": {
"match_all": {}
},
"filter": {
"exists": {
"field": "salesrank"
}
}
}
}
}

Related

How do i combine different search parameters in an elasticscearch dsl query?

Good day together,
I have a little problem in Elastic/Kibana. In the Kibana Query Language "KQL" it is possible for me to execute a certain query:
car:* AND coun: * AND doc: (bes* OR *rvr*) AND NOT coun: (SIP OR LUK)
I would like to use this as a filter query using Elasticscearch query DSL. Only I don't get the same result. For this I use the boolean operator. My query looks like this:
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "car"
}
},
{
"exists": {
"field": "coun"
}
}
],
"should": [
{
"wildcard": {
"doc.keyword": {
"value": "bes*"
}
}
},
{
"wildcard": {
"doc.keyword": {
"value": "*rvr*"
}
}
}
],
"must_not": [
{
"term": {
"coun.keyword": "SIP"
}
},
{
"term": {
"coun.keyword": "LUK"
}
}
],
"minimum_should_match": 1
}
}
}
Unfortunately, I do not get the same result. My guess is the "should" operator. But I don't know exactly how to adjust the code.
I would be very grateful for any answer! Thanks a lot!
Problem here, that you putting OR outside AND. Just move should clause inside must. Like this
GET _search
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "car"
}
},
{
"exists": {
"field": "con"
}
},
{
"bool": {
"should": [
{
"wildcard": {
"doc.keyword": {
"value": "bes*"
}
}
},
{
"wildcard": {
"doc.keyword": {
"value": "*rvr*"
}
}
}
]
}
}
],
"must_not": [
{
"term": {
"coun.keyword": "SIP"
}
},
{
"term": {
"coun.keyword": "LUK"
}
}
],
"minimum_should_match": 1
}
}
}

Error in my elastic average query - malformed

I am getting a reason": "[query] query malformed, no start_object after query name" error, not sure why.
The query is meant to grab the difference between two date fields and calculate average of all the results, I believe this should work but it may not work.
Any help would be greatly appreciated.
I am on elastic version 5.6.12
query below:
POST index_my.test/_search
{
"size":10,
"query": {
"bool": {
"must": [
{
"query":
"match_all": {}
}
}
]
}
"filter": {
"and": [
{
"exists": {
"field": "activity.timeline.found"
}
}
{
"exists": {
"field": "activity.timeline.sent"
}
}
]
},
"aggs": {
"avg_timedifference": {
"avg": {
"script" : "Math.ceil(doc['activity.timeline.found'].value - doc['activity.timeline.sent'].value)"
}
}
}
}
You forgot a comma before "filter". Try this:
POST index_my.test/_search
{
"size":10,
"query": {
"bool": {
"must": [
{
"query":
"match_all": {}
}
}
]
},
"filter": {
"and": [
{
"exists": {
"field": "activity.timeline.found"
}
}
{
"exists": {
"field": "activity.timeline.sent"
}
}
]
},
"aggs": {
"avg_timedifference": {
"avg": {
"script" : "Math.ceil(doc['activity.timeline.found'].value - doc['activity.timeline.sent'].value)"
}
}
}
}

How to do nested AND and OR filters in ElasticSearch?

My filters are grouped together into categories.
I would like to retrieve documents where a document can match any filter in a category, but if two (or more) categories are set, then the document must match any of the filters in ALL categories.
If written in pseudo-SQL it would be:
SELECT * FROM Documents WHERE (CategoryA = 'A') AND (CategoryB = 'B' OR CategoryB = 'C')
I've tried Nested filters like so:
{
"sort": [{
"orderDate": "desc"
}],
"size": 25,
"query": {
"match_all": {}
},
"filter": {
"and": [{
"nested": {
"path":"hits._source",
"filter": {
"or": [{
"term": {
"progress": "incomplete"
}
}, {
"term": {
"progress": "completed"
}
}]
}
}
}, {
"nested": {
"path":"hits._source",
"filter": {
"or": [{
"term": {
"paid": "yes"
}
}, {
"term": {
"paid": "no"
}
}]
}
}
}]
}
}
But evidently I don't quite understand the ES syntax. Is this on the right track or do I need to use another filter?
This should be it (translated from given pseudo-SQL)
{
"sort": [
{
"orderDate": "desc"
}
],
"size": 25,
"query":
{
"filtered":
{
"filter":
{
"and":
[
{ "term": { "CategoryA":"A" } },
{
"or":
[
{ "term": { "CategoryB":"B" } },
{ "term": { "CategoryB":"C" } }
]
}
]
}
}
}
}
I realize you're not mentioning facets but just for the sake of completeness:
You could also use a filter as the basis (like you did) instead of a filtered query (like I did). The resulting json is almost identical with the difference being:
a filtered query will filter both the main results as well as facets
a filter will only filter the main results NOT the facets.
Lastly, Nested filters (which you tried using) don't relate to 'nesting filters' like you seemed to believe, but related to filtering on nested-documents (parent-child)
Although I have not understand completely your structure this might be what you need.
You have to think tree-wise. You create a bool where you must (=and) fulfill the embedded bools. Each embedded checks if the field does not exist or else (using should here instead of must) the field must (terms here) be one of the values in the list.
Not sure if there is a better way, and do not know the performance.
{
"sort": [
{
"orderDate": "desc"
}
],
"size": 25,
"query": {
"query": { #
"match_all": {} # These three lines are not necessary
}, #
"filtered": {
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"not": {
"exists": {
"field": "progress"
}
}
},
{
"terms": {
"progress": [
"incomplete",
"complete"
]
}
}
]
}
},
{
"bool": {
"should": [
{
"not": {
"exists": {
"field": "paid"
}
}
},
{
"terms": {
"paid": [
"yes",
"no"
]
}
}
]
}
}
]
}
}
}
}
}

How to apply a size for ElasticSearch filtered query?

For regular query size works ok:
{
"query": {
"match_all": {}
},
"size": 2
}
returns 2 results. But when I try to add a filter by geo_polygon:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_polygon": {
"coordinate": {
"points": {
"points": [
[
-84.293222919922,
33.865223592668
],
[
-84.293222919922,
33.632776407332
],
[
-84.482737080078,
33.632776407332
],
[
-84.482737080078,
33.865223592668
],
[
-84.293222919922,
33.865223592668
]
]
}
}
}
}
}
},
"size": 2
}
it always returns 10 results and looks like it ignores "size" parameter completely. Are there are any specific approaches to make "size" work for filtered query?
MacOS,
elasticsearch
version: {
number: 1.0.1
lucene_version: 4.6
}
This works fine in version 1.2.1 running on Ubuntu:
{
"from": 0,
"size": 100,
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
...blah...
}
}
}
}
It seems this also works if you put the size parameter inside the query parameter.
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
...blah...
}
},
"size": 100
}
}

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