elasticsearch - Nest ElasticSearch:使用嵌套查询和嵌套对象的 bool 搜索

标签 elasticsearch nest elasticsearch-plugin booleanquery

我正在使用Nest Elastic并使用Head插件为 bool(boolean) 搜索构建查询,我正在组合多个查询

Notes about DB Structure and Elastic Mapping


  • 数据库中的每个文档都链接到特定的profileId,
    依次具有多个属性
  • 每个文档都有与其关联的多个属性值

  • 在此查询中,我试图获取所有具有特定配置文件和属性值> 30的文档,同时请记住,此属性应仅具有ID 2。

    SQL查询:

    从文档d内部联接属性中选择av。*,d.name属性值av

    d.DocumentId = av.DocumentId
    其中d.profileid = 1和av.AttributeId = 2且av.Intvalue> 30

    Elastic Query


       { "query": {
        "bool": {
        "must": [
        {
           "term": { "Document.profileid": "1"  }
        }
        ,
        {
          "term": {"Document.lstChildren.AttributeID": "2" }
        }
        ,
        { 
          "range": { "Document.lstChildren.IntValue": { "gt": "30"} }
        }
        ,
        {
        "match_all": { }
        }
        ],
        "must_not": [ ],
        "should": [ ]
        }
        },   "from": 0, "size": 10, "sort": [ ], "facets": { }
        }
    

    Problem



    结果还包含一个具有以下属性值的文档
  • 属性值= 3,attributeId = 2(值<30)
  • 属性值= 34,但attributeId不同于2 (不正确)

  • 不能包含此文档,因为它不能满足我的需求。

    如何建立此查询?

    最佳答案

    解决方案是通过使lstChildren成为嵌套对象来首先更改映射。然后,使用嵌套查询将确保符合指定的所有条件。下面的嵌套查询指定了两个仅返回预期结果的条件,但为了简单起见,我对“IntValue”使用“等于”而不是“大于”:

    {
      "query": {
        "nested": {
          "path": "lstChildren",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "lstChildren.AttributeID":"2"
                  }
                },
                {
                  "match": {
                    "lstChildren.IntValue": "31"
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    关于elasticsearch - Nest ElasticSearch:使用嵌套查询和嵌套对象的 bool 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400601/

    相关文章:

    search - ElasticSearch More_Like_This API 和嵌套对象属性

    elasticsearch - 创建索引 Nest

    elasticsearch - 无法将Marvel安装到Kibana中

    elasticsearch - kibana 4.0 中比率的趋势

    Elasticsearch 聚合返回多个字段

    c# - Elasticsearch 使用不同排序顺序获取距离

    Elasticsearch 管理工具,例如 phpMyAdmin for mysql

    elasticsearch - 如何使用Slack配置Elasticsearch Watcher

    java - Kibana 查询语言到 java elasticsearch 查询

    elasticsearch - Elasticsearch 更像是 5.x 中的查询分数问题