0

我正在使用 Nest Elastic 并使用 Head 插件为布尔搜索构建查询,我正在组合多个查询

数据库结构和弹性映射注意事项

  1. 数据库中的每个文档都链接到特定的 profileId,而 profileId 又具有多个属性
  2. 每个文档都有多个与之关联的属性值

在这个查询中,我试图获取所有具有特定配置文件和属性值 > 30 的文档,记住这个属性应该只有属性 ID 2。

SQL 查询:

选择 av.*, d.name from document d inner join attributeValue av on d.DocumentId = av.DocumentId where d.profileid = 1 and av.AttributeId = 2 and av.Intvalue >30

弹性查询

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

问题

结果还包含一个具有以下属性值的文档

  1. 属性值 = 3 和 attributeId = 2(值 < 30)
  2. 属性值 = 34,但 attributeId 不同于 2 (不正确)

不得包含此文件,因为它不能满足我的需要。

我怎样才能建立这个查询?

4

1 回答 1

2

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

{
  "query": {
    "nested": {
      "path": "lstChildren",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "lstChildren.AttributeID":"2"
              }
            },
            {
              "match": {
                "lstChildren.IntValue": "31"
              }
            }
          ]
        }
      }
    }
  }
}
于 2015-12-24T18:17:57.410 回答