How to achieve the following sql query in elasticsearch? - elasticsearch

Want to know the equivalent elasticsearch query for the below sql query?
SELECT * FROM table1 where val1 in (SELECT val1 FROM table1 WHERE val2 = "123");
How to achieve this in an effiecient way?
One way is to fetch all val1 in 1st Elasticsearch query and with the val1 values fetch all values in the 2nd Elasticsearch query. Is there any other way with which we can get the results in a single Elasticsearch query instead of two Elasticsearch query

You could have your query as such assuming that your heading towards a HTTP POST request.
Request:
http://localhost:9200/yourindex/_search
Request Body:
{
"query": {
"query_string": {
"query": "val1:(val2:\"123\")"
}
}
}
Instead of using the IN keyword, you could go with the : symbol in ES OR you could still use the Terms Query . This SO & this thread could be helpful.
EDIT
Using the terms query:
{
"query" : {
"bool" : {
"filter" : {
"terms" : {
"val1" : [ "val2" : ["123"]]
}
}
}
}
}

Related

Elasticsearch : get compliment of output

I want to get set of result which is outside the provided filters.
Example :
Query : document_id and filter
{
Document_id : 1,2,3,4,5,6,7
},
{
Query Result : 1,2,3,4,5,6
}
Output : 7
I can perform the above manipulation in java code as well but I want to get this result from Query. Any help would be appreciated.
You should put your filter in a must_not clause inside a bool query, like this:
POST _search
{
"query": {
"bool" : {
"must_not" : [
<YOUR FILTER QUERIES HERE>
]
}
}
}

ElasticSearch - Use results of one query as a filter for another

I'm trying to convert to t-sql query to elastic dsl.
I have 2 tables and I'm trying to fetch the data from second table by filtering with the selected guids from first query.
SELECT distinct guid INTO #temp
FROM table1
WHERE code = 13
AND DateDiff(day, log_time, '2019-08-24') = 0
SELECT details,CreatedDate,guid
FROM table2
WHERE guid IN (SELECT guid FROM #temp)
ORDER BY CreatedDate desc,guid
I have tried to convert the first query on index1 in elastic dsl:
{
"query": {
"bool": {
"filter": [
{ "term": { "code": 13 }},
{ "range": { "log_time": { "gt" : "2019-08-23 23:59:59.99",
"lte" : "2019-08-24 23:59:59.99"}}}
]
}
},
"_source": {
"includes": ["_id", "guid"],
"excludes": []
}
}
Now I want to use the guids return by this query as a filter in second query on index2.
Is this possible in elasticsearch?
I know a join is not supported in elasticsearch. But is there any way to perform a subquery in elasticsearch?
I have looked into terms lookup but it seems this only works on _id of the document.
But in my case the guid which is the unique identifier in sql-server would just be a document property in elasticasearch.
Is there a way to tweak terms lookup to work on a field other than _id?

Convert Sql to ES query

I want to get All the records that has latest "created_on" time from elastic search documents.
In sql what i need is
select * from table1
where created_on = (select max(created_on) from table1)
But i'm new to ES and don't know how to do it.
I Can first get the Max(created_on) date from ES and query again to get all the records that has Max(created_on).
Is there a way to get this with single query?
{
"filter" : {
"match_all" : { }
},
"sort": [
{
"created_on": {
"order": "desc"
}
}
],
"size": 1
}
You can try this query and let me know if this works.
You can try
// descending order
var entity= ctx.Table1.OrderByDescending(s => s.Created_on).FirstOrDefault();

How to query an index for documents that match an or condition?

For example lets say I have a resources index with type resource and it has a property named status which can hold a value 1,2 or 3.
How do I make a query that will retrieve all documents with status = 1 OR status = 2?
You can use query string in elasticsearch for this kind of query(https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html).
GET /_search
{
"query": {
"query_string" : {
"default_field" : "status",
"query" : "1 OR 2 OR 3"
}
}
}

How to join 2 match queries into a query for elasticsearch?

I'd like to query for data that user_id is '1' and name is 'John'. It's easy to write a commonly-used SQL:
select * from t where user_id = '1' and name = 'John';
But it's not easy for me to make a query for elasticsearch.
First, I made a query for user_id:
{
"query" : {
"match" : {
"user_id" : "1"
}
}
}
And the results were what I expected.
Then, I made a query for name:
{
"query" : {
"match" : {
"name" : "John"
}
}
}
It worked well, too.
But I couldn't make a query joining 2 conditions with and operation. How can I join those 2 match queries into one using and operation?
What you need is a bool query in which you put all your single queries:
(I could not test the query, it might be wrong, but the bool-query is the answer to your problem)
{
"bool" : {
"must" : [{
"match" : {
"user_id" : "1"
},
"match" : {
"name" : "John"
}
}]
}
}

Resources