Use query result as parameter for another query in Elasticsearch - elasticsearch

How can I use query result of first query as a parameter to another query in elastic search
lets consider the dummy data
`PUT /_bulk`
`{"index":{"_index":"movies","_id":"2"}}`
`{"name":"abc","old_data":20,"data":0,"old_data_id":"xy"}`
`{"index":{"_index":"movies","_id":"3"}}`
`{"name":"def","old_data":20,"data":2,"old_data_id":"xy"}`
`{"index":{"_index":"movies","_id":"4"}}`
`{"name":"ghi","old_data":20,"data":0,"old_data_id":"yz"}`
`{"index":{"_index":"movies","_id":"4"}}`
`{"name":"jkl","old_data":18,"data":2,"old_data_id":"xy"}`
`{"index":{"_index":"movies","_id":"4"}}`
`{"name":"mno","old_data":18,"data":18,"old_data_id":"rt"}`
I'm using Elasticsearch , I'm trying to use a query result as a parameter for another query just like the result of this SQL query
SELECT name FROM movies
WHERE old_data_id IN (SELECT old_data_id FROM movies WHERE old_data \> 0 AND data = 0);
I am running all this query on my local host of kibana
I had tried to write sql query like this:
POST /_sql?format=txt
{
"query": "SELECT name FROM test_2 WHERE old_data_id IN (SELECT old_data_id FROM test_2 where name='abc')"
}
but i was showing error
{
"error" : {
"root_cause" : [
{
"type" : "parsing_exception",
"reason" : "line 1:32: IN query not supported yet"
}
],
"type" : "parsing_exception",
"reason" : "line 1:32: IN query not supported yet"
},
"status" : 400
}
after research I found this post Can we use result from one query as an input to another query in elasticsearch? but how can I implement it
I wanna write two separate query in elastic search in which first query will give all the old_data_id and in second query we will use this result of first query to find all the name corresponding to that id. How can we do so?
How can we connect two queries ? (Nested queries)
Do we have to store the result of first query somewhere and then use this ?
Do we have to use java or python if yes then how can I do explain if possible plz give code snippet?
Is there any ways to do it or it is not feasible?

Related

Elastic Search - Conditional field query if no match found for another field

Is it possible to do conditional field query if match was not found for another field ?
for eg: if I have a 3 fields in the index local_rating , global_rating and default_rating , I need to first check in local_rating and if there is no match then try for global_rating and finally for default_rating .
is this possible to do with one query ? or any other ways to achieve this
thanks in advance
Not sure about any existing features of Elasticsearh to fulfill your current requirements but you can try with fields and per-fields boosting, Individual fields can be boosted with the caret (^)notation. Also I don't know boosting is possible with numeric value or not?
GET /_search
{
"query": {
"multi_match" : {
"query" : 10,
"fields" : [ "local_rating^6", "global_rating^3","default_rating"]
}
}
}
See: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#field-boost

elastic search fetch the exact match first followed by others

I am newbie to elastic search
I have an education index in es
index creation
when i search 'btech' with match query as
"match" : { "name" : "btech" }
the result is like
result json object
but i need btech(exact match word) as the first document and remaining documents followed by it.
so for that what i have to change in my index creation
can anybody please help me
You can use term query
"term" : { "name" : "btech" }
Or regexp query
"regexp" : { "name" : "btech" }
You are using text type, make sure to check keyword type too
from documentation
If you need to index structured content such as email addresses,
hostnames, status codes, or tags, it is likely that you should rather
use a keyword field.

Merge Documents based on field value?

I have multiple Documents within an Index, each have the following fields:
id serviceName Type
Now, stupidly, id is not unique and I want to change that. I want to use Kibana/Elasticsearch to query the data so that I have id unique and the behaviour I want is that if I have the following Docs:
id serviceName Type
1 A T1
1 B T2
1 D T2
I use a query so that I get this result
1 A,B,C T1,T2,T3
Is there a way for this?
You cannot do this with just Elasticsearch/Kibana, you have to write some code. You can use the scroll api to iterate through all the documents in the index, and then use an upsert query to index them into a new index. I think your upsert request will look something like this:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.serviceName.add(params.serviceName); ctx._source.Type.add(params.Type)",
"lang": "painless",
"params" : {
"serviceName" : "A",
"Type": "T1"
}
},
"upsert" : {
"serviceName": ["A"],
"Type": ["T1"]
}
}
This means in case id 1 doesn't exist yet, add it with the "upsert" value for the document, otherwise do the script (which appends the serviceName and Type values to the existing doc).
This would be pretty straightforward to do with very little code using elasticsearch-py, check out the scan helper and bulk helper

can terms lookup mechanism query by other field but id?

here is elasticsearch official website about terms:
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/query-dsl-terms-query.html
As we can see, if we want to do terms lookup mechanism query, we should use command like this:
curl -XGET localhost:9200/tweets/_search -d '{
"query" : {
"terms" : {
"user" : {
"index" : "users",
"type" : "user",
"id" : "2",
"path" : "followers"
}
}
}
}'
But what if i want to do query by other field of users.
Assume that users has some other fields such as name and can i use terms lookup mechanism finding the tweets by giving users name but not id.
I have tried to use command like this:
curl -XGET localhost:9200/tweets/_search -d '{
"query" : {
"terms" : {
"user" : {
"index" : "users",
"type" : "user",
"name" : "Jane",
"path" : "followers"
}
}
}
}'
but it occurs error.
Looking forward to your help. Thank you!
The terms lookup mechanism is basically a built-in optimization to not have to make two queries to JOIN two indices, i.e. one in index A to get the ids to lookup and a second to fetch the documents with those ids in index B.
In contrary to SQL, such a JOIN can only work on the id field since this is the only way to uniquely retrieve a document from Elasticsearch via a GET call, which is exactly what Elasticsearch will do in the terms lookup.
So to answer your question, the terms lookup mechanism will not work on any other field than the id field since the first document to be retrieved must be unique. In your case, ES would not know how to fetch the document for the user with name Jane since name is just a field present in the user document, but in no way a unique identifier for user Jane.
I think you did not understand exactly how this works. Terms lookup query works by reading values from a field of a document with the given id. In this case, you are trying to match the value of field user in tweets index with values of field followers in document with id "2" present in users index and user type.
If you want to read from any other field then simply mention that in "path".
What you mainly need to understand is that the lookup values are all fetched from a field of a single document and not multiple documents.

Elasticsearch error when same search query

Now i am working with Elastic search.But when i run same query for search,i can not get same results.
First query:"GET /mega/employee/_search?q=last_name:Smith"
Result:I only get 2 results that "last_name==Smith"
Second query:
"GET /mega/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}"
I get 3 results,with last result:last_name==Fer
Someones can explain for me?
You probably need to POST to _search. GET with a body is ill-supported (and, I'd argue, meaningless according to the spec), and is rather abused by Elasticsearch. Your request is almost certainly being interpreted at a GET to your _search endpoint with no query attached.

Resources