Elasticsearch search array inside elastic object - node.js

I am working on elastic search in node.js. I have 50k+ users in elstic search and now I have an of of phone numbers. Now I want to get those users from elastic search who match these number in array for this purpose I write search query string but this query give me exception I don't know where I am doing mistake in my query string.
Query String
{
"query": {
"bool": {
"must": [
{
"prefix": {
"phone":{"+9665509548","+93565822145",...}
}
}
]
}
}
}

Is this what you're after?
{
"query": {
"bool": {
"should": [
{
"prefix": {
"phone": "+9665509548"
}
},
{
"prefix": {
"phone": "+93565822145"
}
},
//...
],
"minimum_should_match": 1
}
}
}

Related

How to query the elastic search using node js client

I am new in ElasticSearch. I tried some queries, can anyone please help me to get the unique users for that Audi and BMW keyword?
"query": {
"bool": {
"must": [
{
"term": {
"keyword": "Audi"
}
},
{
"term": {
"keyword": "BMW"
}
},
{
"range": {
"date": {
"gte": "2019-11-19",
"lt": "2019-11-29"
}
}
}
]
}
}
}
my Index in the elastic search
enter image description here
Need to implement as below.
enter image description here

Using NOT and OR together in Elastic search

This the query I used in elastic search to filter records that either satisfy one condition or does not satisfy other condition.
{
"query":
{
"query_string":
{
"query": "(NOT col1: \"val1\") OR (col2: val2)",
"analyze_wildcard": true
}}}
The problem is I am not able to write an equivalent syntax in nodejs to extract the information. We cant use must_not here as it is an OR condition
You will make both conditions in should array, as, if anyone matches you will get the record in results, you will have to use must match and must_not match
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": {
"match": {
"col1": {
"query": "val1",
"type": "phrase"
}
}
}
}
},
{
"match": {
"col2": {
"query": "val2",
"type": "phrase"
}
}
}
]
}
}
}

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
}
}
]
}
}
}
}
}

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.

ElasticSearch: Show partial match for multi search even if one field does not match

I am currently trying to do a multi search query on first name, last name, and date of birth. I want the results to show a complete match for first, last, and dob but also show results if the first name and last name match but a different date of birth exists then what was queried on.
As of right now my code only returns a result if all three fields have exact matches
GET /account/data/_search
{
"query": {
"match": {
"first": {
"query": "Chris"
}
}
},
"query": {
"match": {
"last": {
"query": "Johnson"
}
}
},
"query": {
"match": {
"dob": {
"query": "10-10-1990"
}
}
}
}
This can be solved with simple bool query
{
"query": {
"bool": {
"must": [
{
"match": {
"first": "TEXT"
}
},
{
"match": {
"last": "TEXT"
}
}
],
"should": [
{
"match": {
"dob": "TEXT"
}
}
]
}
}
}

Resources