【问题标题】:Elastic search terms query for an AND condition for 2 properties dependent on each other弹性搜索词查询 2 个相互依赖的属性的 AND 条件
【发布时间】:2017-08-30 08:19:21
【问题描述】:

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

 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 会很棒

【问题讨论】:

    标签: elasticsearch-5


    【解决方案1】:

    您可以使用嵌套的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

    【讨论】:

    • 我要查询 SQL:从 (1, 2, 3) 中的用户 id 或 (12, 11, 14) 中的密码中选择名称
    【解决方案2】:

    尝试将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

    【讨论】:

      【解决方案3】:

      阅读此内容以进行嵌套搜索 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        • 2016-07-20
        • 2018-04-14
        • 1970-01-01
        • 2013-07-19
        • 2016-04-13
        相关资源
        最近更新 更多