How to write conditions in Elastic Search - node.js

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

Related

elasticsearch must OR must_not

I have this query for my elasticsearch request:
{
"query": {
"bool": {
"filter": {
"bool": {
"should" : [
{
"bool" : {
"must_not": {
"exists": {
"field": "visibility_id"
}
}
}
},
{
"bool" : {
"must": {
"terms": {
"visibility.visibility": ["visible"]
}
}
}
}
]
}
}
}
}
}
The goal is to check if the row visibility_id is in the table. If not it will return true has it reach the "must_not". But if the visibility_id column is present it needs to check that this is set to "visible".
At the moment it works if the visibility_id is null but it does not check the terms. terms can be anything else but visible and it will works.
Can someone help me please, I am new to elasticsearch. (I have tried without the filter, bool, only with the should but it does not work neither.)
Try this query, you're missing minimum_should_match: 1
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must_not": {
"exists": {
"field": "visibility_id"
}
}
}
},
{
"terms": {
"visibility.visibility": [
"visible"
]
}
}
]
}
}
}
If visibility is nested in your mapping, your query needs to be like this instead:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must_not": {
"exists": {
"field": "visibility_id"
}
}
}
},
{
"nested": {
"path": "visibility",
"query": {
"terms": {
"visibility.visibility": [
"visible"
]
}
}
}
}
]
}
}
}

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')])

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 search in any child

this is my elastic search query,
I want to search in all languages under data.title and not particularly eng.
if i put data.title there are 0 results.
{
"bool": {
"must": [
{
"span_near": {
"clauses": {
"0": {
"span_multi": {
"match": {
"wildcard": {
"data.Title.eng": "method"
}
}
}
},
"1": {
"span_multi": {
"match": {
"wildcard": {
"data.Title.eng": "system"
}
}
}
}
},
"slop": 1,
"in_order": false
}
}
]
}
}
Instead of wildcard query , try to use query_string :-
Example :-
{
"query": {
"query_string": {
"default_field": "data.Title.*",
"query": "*zz* *ell*"
}
}
}
query_string is more effective than wildcard in search .
To get more difference between them , check this link

How to sum two fields inside a bool Query in Elastic Search?

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.

Resources