So I have a query to get records and the filter condition is something like this
GET tenantforsneha55/permits/_search/
{
"from":0,
"size":10,
"sort":[
{
"permitNumber.keyword":{
"order":"asc"
}
}
],
"query":{
"bool":{
"must":[
{
"terms":{
"workClassId":[
"1",
"2"
]
}
},
{
"terms":{
"typeId":[
"1",
"2"
]
}
}
]
}
}
}
This shows results for a filter like this Get records where typeId in ["1","2"] and classId in ["1","2"]
But I want the filter condition to be like this typeId = 1 and classId = 1 OR typeId = 2 and classId = 2.
Is there any way to have this ? I am using NEST,, this query is generated from that,will be great if you can give me the code in C#, Elastic v 5.5
To better search text fields, the match query also analyzes your provided search term before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. The term query does not analyze the search term. The term query only searches for the exact term you provide.
Term queryedit. Returns documents that contain an exact term in a provided field. You can use the term query to find documents based on a precise value such as a price, a product ID, or a username. Avoid using the term query for text fields.
However, when it comes to joining two indexes, there are some challenges that need to be considered. First, each index must have the same mapping type. Second, the data in each index must be compatible with the other index. Lastly, the order of the data in each index must be the same.
You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses. You may want to schearch for both the field you want, and then aggregate by the most important field.
You can use nested should
and must
queries like below,
{
"from":0,
"size":10,
"sort":[
{
"permitNumber.keyword":{
"order":"asc"
}
}
],
"query":{
"bool":{
"should":[
{
"bool": {
"must": [
{
"term":{ "workClassId":"1" }
},
{
"term":{ "typeId":"1" }
}
]
}
},
{
"bool": {
"must": [
{
"term":{ "workClassId":"2" }
},
{
"term":{ "typeId":"2" }
}
]
}
}
]
}
}
}
This isn't simplest way but should do. Read more on this at https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query
You can also use filters in similar way. Check https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With