我有一份有汽车文件的索引。汽车可以有选择。选项是可以包含可用性对象的嵌套对象。我的地图如下所示:
{
"car": {
"properties": {
"carId": {
"type": "long"
},
"carName": {
"type": "string",
"index": "not_analyzed"
},
"option": {
"type": "nested",
"properties": {
"optionId": {
"type": "long"
},
"optionName": {
"type": "string",
"index": "not_analyzed"
},
"availability": {
"type": "nested",
"properties": {
"from": {
"type": "long"
},
"to": {
"type": "long"
}
}
}
}
}
}
}
}我索引了以下两个汽车文件。一辆(酷车)有空调选项:
{
"carId": 100,
"carName": "cool car",
"option": {
"optionId": 10,
"optionName": "AC",
"availability": {
"from": 12345,
"to": 67890
}
}
}另一辆(不太酷的汽车)没有选择:
{
"carId": 200,
"carName": "uncool car"
}当我搜索一辆带有AC选项的汽车时,我会像预期的那样得到“酷车”:
{
"query": {
"nested": {
"filter": {
"term": {
"option.optionId": 10
}
},
"path": "option"
}
}
}现在最大的问题是:我怎么能得到一辆没有空调选项的汽车?我在查询中添加了一个非筛选器,但没有返回任何内容:
{
"query": {
"nested": {
"filter": {
"not": {
"filter": {
"term": {
"option.optionId": 10
}
}
}
},
"path": "option"
}
}
}事先非常感谢!
发布于 2015-08-27 17:48:42
我觉得你很接近,但嵌套过滤器的位置搞砸了。我们想要否定嵌套过滤器的结果,而不是从一个被否定的过滤器中找到嵌套文档。
这是我的解决方案:
{
"filter": {
"not": {
"filter": {
"nested": {
"path": "option",
"query": {
"filtered": {
"filter": {
"term": {
"option.optionId": 10
}
}
}
}
}
}
}
}
}来自文档:The query path points to the nested object path, and the query (or filter) includes the query that will run on the nested docs matching the direct path
https://stackoverflow.com/questions/32255274
复制相似问题