Combining filters and terms in multiple boolean fields using and - elasticsearch

Suppose we have the following filtering: field > 0 AND field != value
I am a bit mixed up on the many ways that elasticsearch has to express that filter using the bool query.Consider the following queries:
Query one
"bool":{
"must_not":{
"term":{
"field": value
}
},
"must":{
"range":{
"field":{
"gt":0
}
}
}
}
Query 2:
"bool":{
"must_not":{
"term":{
"field": value
}
},
"filter":{
"range":{
"field":{
"gt":0
}
}
}
}
Query 3
"bool":{
"must":{
"bool":{
"must":{
"range":{
"field":{
"gte":value
}
}
},
"must_not":{
"field":value
}
}
}
}
Do query 1 and 2 mean the same thing? Also although I have seen many examples in the form of the query 3 (bool query inside a must|should|must_not clause), it produces an error when validating the query:
org.elasticsearch.index.query.QueryParsingException: [_na] query malformed, no field after start_object'}
What does this error mean? How is the correct form? And why so many ways to filter results?

Related

Elasticsearch DSL query returning result for condition which isn't true

I want to have three conditions in my elasticsearch query and accordingly I have written as below. But I don't know why it is returning a DOCUMENT where AMOUNT is 250 and it EXISTS whereas my condition is ATLEAST one of the two i.e. AMOUNT less than or equal to zero or AMOUNT should not exist.
Below is the DSL Query
{
"from":0,
"size":10,
"track_total_hits":true,
"_source": ["amount", "npa_stageid_loanaccounts"],
"query":{
"bool":{
"must":[
{
"query_string":{
"default_field":"npa_stageid_loanaccounts.keyword",
"query":"Y"
}
},
{
"bool":{
"minimum_should_match":1,
"should":[
{
"range":{
"Amount":{
"lte":0
}
}
},
{
"bool":{
"must_not":[
{
"exists":{
"field":"Amount"
}
}
]
}
}
]
}
}
]
}
}
}
In your documents, you have amount but in your query you have Amount, the casing is not the same.

Can I alter the score of results based on a query within an Elasticsearch Aggregation?

I'm using an Elasticsearch filter aggregation with a nested top_hits aggregation to retrieve top matching documents based on different filters, but I can't seem to change the scores of results in each bucket via boosting or a nested function_score query. Is this just not possible? I haven't found any explicit documentation saying it won't work, and the query executes just fine, however the resulting scores aren't impacted.
Example query (note the huge boost in the first aggregation):
GET _search
{
"size":0,
"query":{
"bool":{
"should":[
{
"multi_match":{
"type":"phrase",
"query":"TV",
"fields":[
"categories^4"
]
}
}
]
}
},
"aggs":{
"1":{
"filter":{
"bool":{
"must":[
{
"multi_match":{
"type":"phrase",
"query":"Music",
"fields":[
"categories^10"
]
}
}
]
}
},
"aggs":{
"1_hits":{
"top_hits":{
"size":10,
"sort":[
{
"_score":{
"order":"desc"
}
}
]
}
}
}
},
"2":{
"filter":{
"bool":{
"must":[
{
"multi_match":{
"type":"phrase",
"query":"Music",
"fields":[
"categories"
]
}
}
]
}
},
"aggs":{
"2_hits":{
"top_hits":{
"size":10,
"sort":[
{
"_score":{
"order":"desc"
}
}
]
}
}
}
}
}
}

Elasticsearch Function Score Query with Should Clause in Bool Query

This is a story of 2 queries. One returns results, while the other does not. Why?
Query that returns results:
{
"index":"my_index",
"type":"places",
"body":{
"from":0,
"size":"12",
"sort":{
"_score":{
"order":"desc"
}
},
"query":{
"function_score":{
"query":{
"bool":{
"must":[
],
"should":[
],
"must_not":[
],
"filter":[
]
}
},
"functions":[
{
"gauss":{
"location":{
"origin":{
"lat":"41.243368",
"lon":"-116.79711"
},
"offset":"0mi",
"scale":"100mi"
}
}
}
],
"score_mode":"sum",
"boost_mode":"sum"
}
}
}
}
Query that does NOT return results:
{
"index":"my_index",
"type":"places",
"body":{
"from":0,
"size":"12",
"sort":{
"_score":{
"order":"desc"
}
},
"query":{
"function_score":{
"query":{
"bool":{
"must":[
],
"should":[
{
"terms":{
"region_id":[
32273
],
"boost":2
}
}
],
"must_not":[
],
"filter":[
]
}
},
"functions":[
{
"gauss":{
"location":{
"origin":{
"lat":"41.243368",
"lon":"-116.79711"
},
"offset":"0mi",
"scale":"100mi"
}
}
}
],
"score_mode":"sum",
"boost_mode":"sum"
}
}
}
}
The Bool Query has only a Should Clause.
Also, I am summing the scores, not multiplying them.
It's true that I do not have any items in the index that have the specified "region_id", but I fail to see how the results of the first query are excluded from the second query. It seems like the 0 score on the Bool Query is acting like a filter.
The first query acts as a match_all query so it returns everything and applies the function score.
In the second query you have a should clause, but since there are no must or filter clause it must match.
From ES docs: "In a boolean query with no must or filter clauses, one or more should clauses must match a document"

How to filter a field using Elastic Search

I'm trying to create a query with elasticsearch to filter the records of the same city and price.
But the city filter is not working.
POST diadeturista/services/_search
{
"query":{
"bool":{
"must":[
],
"filter":{
"bool":{
"must":{
"terms":{
"city":[
"Contagem"
]
},
"range":{
"price_adult":{
"lte":"300",
"gte":"150"
}
}
}
}
}
}
}
}
SHow me this error:
[terms] malformed query, expected [END_OBJECT] but found [FIELD_NAME]
I think what you want todo is
{
"query":{
"bool":{
"must": [
{
"terms":{
"city":[
"Contagem"
]
}
},
{
"range":{
"price_adult":{
"lte":"300",
"gte":"150"
}
}
}
]
}
}
}

Using filter beside query_string in Elastic Search

How to full text search and have filter? I want to search for a text among documents with language_id=10. I've tried it this way:
{
"query": {
"query_string": {
"query": "Declared"
},
{
"filtered": {
"filter": {
"term": {
"language_id": 10
}
}
}
}
}
}
but seems like it's not correct. How to correct it?
In version 5.2, filtered query is replaced by the bool query, and returns error on my Elastic 5.2 instance. See here.
The new syntax is:
{
"query":{
"bool":{
"must":{
"query_string":{
"query":"Declared"
}
},
"filter":{
"term":{
"language_id":10
}
}
}
}
}
Yep, the syntax of the filtered query is a bit cumbersome. AFAIK it should look like that:
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":"Declared"
}
},
"filter":{
"term":{
"language_id":10
}
}
}
}
}
Sorry Ashalynd but the filter is not placed a the right place in your answer.
This is working better:
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":"Declared"
}
},
"filter":{
"term":{
"language_id":10
}
}
}
}
}

Resources