弹性搜索词查询2个依赖于彼此的属性的AND条件

时间:2017-08-30 08:19:21

标签: elasticsearch-5

所以我有一个查询来获取记录,过滤条件是这样的

 GET  tenantforsneha55/permits/_search/ 
    {
      "from":0,
      "size":10,
      "sort":[
        {
          "permitNumber.keyword":{
            "order":"asc"
          }
        }
      ],
      "query":{
        "bool":{
          "must":[
            {
              "terms":{
                "workClassId":[
                  "1",
                  "2"
                ]
              }
            },
            {
              "terms":{
                "typeId":[
                  "1",
                  "2"
                ]
              }
            }
          ]
        }
      }
    }

这显示了像这样的过滤器的结果 获取[“1”,“2”]中typeId和[“1”,“2”]中的classId的记录

但我希望过滤条件是这样的 typeId = 1且classId = 1 OR typeId = 2且classId = 2.

有没有办法有这个?我正在使用NEST,这个查询是从那里生成的,如果你能用C#,Elastic v 5.5给我代码那将是很好的

3 个答案:

答案 0 :(得分:4)

您可以使用嵌套的shouldmust查询,如下所示

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

这不是最简单的方法,但应该这样做。请在https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query

了解详情

您也可以以类似的方式使用过滤器。查看https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html了解详情。

答案 1 :(得分:0)

阅读此内容进行嵌套搜索 https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

您必须在此处使用嵌套属性。 和Nest的查询就像  mustFilters.Add(j => j.Nested(k => k.Path(“Terms”)。Query(qj => qj.Bool(p => p.Must(queries)))));

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

答案 2 :(得分:0)

should运算符与must

结合使用
GET  tenantforsneha55/permits/_search/ 
    {
      "from":0,
      "size":10,
      "sort":[
        {
          "permitNumber.keyword":{
            "order":"asc"
          }
        }
      ],
      "query":{
        "bool":{
          "should":[
            {
              "match":{
                "workClassId": "1",
                "typeId": "1"
              }
            },
            {
              "match":{
                "workClassId": "2",
                "typeId": "2"
              }
            }
          ]
        }
      }
    }

我是在记事本中写的,所以请原谅任何语法问题。

相同的弹性搜索文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_searches.html