【发布时间】:2016-03-27 19:37:53
【问题描述】:
我正在使用 Nest Elastic 并使用 Head 插件构建布尔搜索查询,我正在组合多个查询
关于数据库结构和弹性映射的注意事项
- 数据库中的每个文档都链接到特定的 profileId 又具有多个属性
- 每个文档都有多个与之关联的属性值
在这个查询中,我试图获取所有具有特定配置文件和属性值 > 30 的文档,记住这个属性应该只有属性 ID 2。
SQL 查询:
Select av.*, d.name from document d inner join attributeValue av 在 d.DocumentId = av.DocumentId 其中 d.profileid = 1 且 av.AttributeId = 2 且 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": { }
}
问题
结果还包含一个具有以下属性值的文档
- 属性值=3,attributeId=2(值
- 属性值 = 34,但 attributeId 不同于 2 (不正确)
这个文件不能满足我的需要,所以不能包括在内。
如何构建这个查询?
【问题讨论】:
-
可以分享示例文档和索引映射吗?
-
您可以尝试从您的
must列表中删除"match_all": {}查询吗?这是完全没有必要的,如果因此出现一些错误,我不会感到惊讶。 -
@Sam
match_all": {}没有解决问题 -
@Rob 我已经把文档和索引的映射放在了这个链接里[jsfiddle.net/hsalman/rknhsc58/#]css窗口的索引映射和JS部分的文档映射
-
DSL 查询中的问题:它正在检查应该在 任何子数组 中具有 attributeId 和 IntValue 的文档。但是我们必须构建查询,以便它在其 lstChildren 数组的至少一个上满足 两个条件。
标签: elasticsearch nest elasticsearch-plugin booleanquery