我正在使用 Nest Elastic 并使用 Head 插件为布尔搜索构建查询,我正在组合多个查询
数据库结构和弹性映射注意事项
- 数据库中的每个文档都链接到特定的 profileId,而 profileId 又具有多个属性
- 每个文档都有多个与之关联的属性值
在这个查询中,我试图获取所有具有特定配置文件和属性值 > 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": { }
}
问题
结果还包含一个具有以下属性值的文档
- 属性值 = 3 和 attributeId = 2(值 < 30)
- 属性值 = 34,但 attributeId 不同于 2 (不正确)
不得包含此文件,因为它不能满足我的需要。
我怎样才能建立这个查询?