弹性搜索词查询 2 个相互依赖的属性的 AND 条件

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

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

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

这显示了这样的过滤器的结果 获取 typeId 在 ["1","2"] 和 classId 在 ["1","2"]

中的记录

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

有什么办法可以得到这个吗?我正在使用 NEST,这个查询是从那里生成的,如果你能给我 C# 中的代码,Elastic v 5.5

会很棒

您可以使用嵌套的 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 了解更多详情。

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

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

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

尝试将 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"
              }
            }
          ]
        }
      }
    }

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

相同的 Elastic Search 文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_executing_searches.html