How to sum two fields inside a bool Query in Elastic Search? - node.js

I am using Elasticsearch to find my result. How I sum two fields inside my query?
{
"query": {
"bool": {
"must": [
{ "term": { "x.flag1": "false" } },
{ "term": { "x.flag2": "false" } }
]
}
}
}
Now I want to find sum of two fields x.a and x.b and compare it with an other field, say x.c
(x.a + x.b === x.c)
I have to include this inside the bool query. So that only those records that satisfy both these conditions will be in my result. Is this possible?

You can do it via a script query like this:
The query for ES 2.x onwards:
{
"query": {
"bool": {
"must": [
{
"term": {
"x.flag1": "false"
}
},
{
"term": {
"x.flag2": "false"
}
}
],
"filter": {
"script": {
"script": "doc['x.a'].value + doc['x.b'].value === doc['x.c'].value"
}
}
}
}
}
The same query for a pre-2.0 ES version:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"x.flag1": "false"
}
},
{
"term": {
"x.flag2": "false"
}
},
{
"script": {
"script": "doc['x.a'].value + doc['x.b'].value === doc['x.c'].value"
}
}
]
}
}
}
}
}
Note that you need to enable dynamic scripting for this to work (i.e. script.inline: true).
However, if you have many documents, this can quickly slow down your ES cluster. If possible this should be done at indexing time, where you'd create another boolean field exactly for this purpose and then you can simply filter documents based on that boolean field which contains the true/false condition you need.

Related

How do I create an “or” Condition filter using elasticsearch-dsl-py?

The query below is what I would like to construct using elasticsearch-dsl-py, but I do not know how to do it.
GET /my_index/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"or": {
"filters": [
{
"term": {
"status": "a"
},
"term": {
"x_status": "a"
},
}
]
}
}
}
}
}
}
I just want to execute a query like below in SQL format
select * from my_index where status = "a" or x_status="a"
I'm not sure which version of ES you're running, but just know that filtered has been replaced by bool a long time ago in version 5. So your query can be rewritten like this:
GET /my_index/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"status": "a"
}
},
{
"term": {
"x_status": "a"
}
}
]
}
}
}
Using elasticsearch-dsl-py, this translates to:
s = Search()
s = s.query('bool', should=[Q('term', status='a'), Q('term', x_status='a')])

How to write conditions in Elastic Search

I want to write a conditional query in elastic search. This is my complete query but its not working as expected.
like:
{"query":{"bool":{"must":[{"nested":{"path":"ticket_group","score_mode":"max","inner_hits":{"from":0,"size":10000},"query":{"bool":{"must":[{"range":{"ticket_group.available":{"gte":1}}},{"bool":{"minimum_should_match":1,"should":[{"bool":{"must":[{"match":{"ticket_group.customer_id":1}},{"match":{"ticket_group.ticket_type":1}},{"match":{"ticket_group.individual_purchase":1}}]}},{"bool":{"must_not":[{"match":{"ticket_group.ticket_type":1}}]}}]}},{"terms":{"ticket_group.ticket_type":[1,3,4]}}]}}}}]}},"size":10,"from":0}
I want to perform search individual_purchase=1 only if ticket_type=1.
How can i write this query?
Then you can do something like this:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"match": {
"ticket_group.ticket_type": 1
}
},
{
"match": {
"ticket_group.individual_purchase": 1
}
}
]
}
},
{
"bool": {
"must_not": [
{
"match": {
"ticket_group.ticket_type": 1
}
}
]
}
}
]
}
}
}
The above query translates to:
either ticket_type = 1 AND individual_purchase = 1
or ticket_type != 1

Elasticsearch bool query with filter and should

My main aim is to write a query that must search for the searchtext and if any filter of loading port or unloading port is applied then it must filter that from the result of search text.
And if no filter is applied then it should proceed further.
For eg: I search for jeans and if I apply filter then the loading port must be India and its unloading port must be US.
I tried to write this report but it's not giving appropriate result.
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"query_string": {
"query": searchText
}
}
]
}
},
{
"terms": {
"loadingPort":loadingPortArray
}
},
{
"terms": {
"unloadingPort":unloadingPortArray
}
}
]
}
}
}
Your problem is that you are wrapping all the quires\filters inside should, which means that all the statements with be combined by OR logic. If you need AND you need to wrap statements inside must.
By the way, you are not using filter, you are using term query. There is huge difference between query and filter in elasticsearch. To use filter you need to use bool query with filter.
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"query_string": {
"query": searchText
}
}
]
}
}
],
"filter": {
"bool": {
"must": [
{
"terms": {
"loadingPort":loadingPortArray
}
},
{
"terms": {
"unloadingPort":loadingPortArray
}
}
]
}
}
}
}
}

ElasticSearch : How to combine nested 'AND' Not Equal

I want build query for search matching with nested and not equal.
This is my elasticSearch query:
{
"from":0,"size":1000,
"query":{
"nested" : {
"path" : "data",
"query" : {
"match" : {
"data.city" : "california"
}
}
},
"filter":{
"not":{
"filter":{
"term":{
"_id":"01921asda01201"
}
}
}
}
}
}
But I got error, am I write something wrong ? thanks
You can use bool Filter too with must and must_not clause.
{
"from": 0,
"size": 1000,
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "data",
"query": {
"match": {
"data.city": "california"
}
}
}
}
],
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
You need to use filtered query
GET _search
{
"query": {
"filtered": {
"query": {
"nested": {
"path" : "data",
"query" : {
"match" : {
"data.city" : "california"
}
}
}
},
"filter": {
"bool": {
"must_not": [
{
"term": {
"_id": "01921asda01201"
}
}
]
}
}
}
}
}
You should use a bool query for this, and put your two clauses in the must and must_not sections respectively.
If you don't care about scoring on the data.city field (from your example it's not clear), you might want to use the filter portion instead of the must portion.
{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "data",
            "query": {
              "match": {
                "data.city": "california"
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "_id": "01921asda01201"
          }
        }
      ]
    }
  }
}

Elasticsearch lowercase filter search

I'm trying to search my database and be able to use upper/lower case filter terms but I've noticed while query's apply analyzers, I can't figure out how to apply a lowercase analyzer on a filtered search. Here's the query:
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"term": {
"language": "mandarin" // Returns a doc
}
},
{
"term": {
"language": "Italian" // Does NOT return a doc, but will if lowercased
}
}
]
}
}
}
}
}
I have a type languages that I have lowercased using:
"analyzer": {
"lower_keyword": {
"type": "custom",
"tokenizer": "keyword",
"filter": "lowercase"
}
}
and a corresponding mapping:
"mappings": {
"languages": {
"_id": {
"path": "languageID"
},
"properties": {
"languageID": {
"type": "integer"
},
"language": {
"type": "string",
"analyzer": "lower_keyword"
},
"native": {
"type": "string",
"analyzer": "keyword"
},
"meta": {
"type": "nested"
},
"language_suggest": {
"type": "completion"
}
}
}
}
The problem is that you have a field that you have analyzed during index to lowercase it, but you are using a term filter for the query which is not analyzed:
Term Filter
Filters documents that have fields that contain a term (not analyzed).
Similar to term query, except that it acts as a filter.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html
I'd try using a query filter instead:
Query Filter
Wraps any query to be used as a filter. Can be placed within queries
that accept a filter.
Example:
{
"constantScore" : {
"filter" : {
"query" : {
"query_string" : {
"query" : "this AND that OR thus"
}
}
}
} }
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-filter.html#query-dsl-query-filter
This may be achieved by appending .keyword to your field to query against the keyword version of the field. Assuming language was defined in the mapping with type keyword.
Note that now only the exact text would match: mandarin won't match and Italian would.
Your query would end up like this:
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"term": {
"language.keyword": "mandarin" // Returns Empty
}
},
{
"term": {
"language.keyword": "Italian" // Returns Italian.
}
}
]
}
}
}
}
}
Combining the term values is also allowed:
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"term": {
"language.keyword":
["mandarin", "Italian"]
}
}
]
}
}
}
}
}

Resources