Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search terms query for an AND condition for 2 properties dependent on each other

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

like image 862
user1393503 Avatar asked Aug 30 '17 08:08

user1393503


People also ask

What is the difference between match and term query in Elasticsearch?

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.

What is Terms query in Elasticsearch?

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.

How do I combine two indexes in Elasticsearch?

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.

How do I merge two queries in Elasticsearch?

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.


1 Answers

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.

like image 163
maximus ツ Avatar answered Dec 14 '22 06:12

maximus ツ