【发布时间】:2016-04-20 19:31:16
【问题描述】:
在弹性搜索 2.3 上运行
我有一个 foos 的索引。每个 foo 都有 0 个或更多条存储在嵌套对象中。每个条形图都有 0 个或多个带有名称和值的属性。
使用https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html的文档
编写查询非常简单:
- 返回所有至少有 1 个
bar.name = "sandwich"或的 foos - 返回所有没有
bar.name = "pizza"的foo
我似乎无法弄清楚的是如何编写一个返回所有 foo 的查询:
- 至少有 1 个
bar.name = "sandwich"AND - 没有
bar.name = "pizza"
我用头撞这个的时间比我愿意承认的要长。任何帮助表示赞赏。
详情:
一个 foo 有 2 个属性:
-
foo_id:一个数字 -
bar: 0 个或更多条的列表,存储为嵌套文档
一个栏有 2 个属性:
-
bar_id:一个数字 -
properties:0 个或更多 kv 对的列表。
精简映射。真正的节目与食物无关。
{
"some_index" : {
"mappings" : {
"foo" : {
"properties" : {
"bar" : {
"type" : "nested",
"properties" : {
"bar_id" : {
"type" : "long"
},
"details" : {
"properties" : {
"name" : {
"type" : "string"
},
"value" : {
"type" : "long"
}
}
}
}
},
"foo_id" : {
"type" : "long",
"index" : "not_analyzed",
"store" : true
}
}
}
}
}
}
没有披萨的典型 foo
# this one does have a bar.name = sandwich
# this one doesn't have pizza for ANY bars
# this would match the query
{
"foo_id": 186456,
"bar": [
{
"bar_id": 1056791,
"details": [
{
"name": "taco",
"value": 2
},
{
"name": "sandwich",
"value": 1
}
]
},
{
"bar_id": 1056800,
"details": [
{
"name": "sandwich",
"value": 0
}
]
}
]
}
典型的 foo 和一些比萨饼
# this one has a bar.name = sandwich
# this one has pizza for some bars, but not all of them
# this would NOT match the query
{
"foo_id": 187390,
"bar": [
{
"bar_id": 1057455,
"details": [
{
"name": "taco",
"value": 1
}
]
},
{
"bar_id": 1057457,
"details": [
{
"name": "taco",
"value": 0
},
{
"name": "sandwich",
"value": 1
}
]
},
{
"bar_id": 1057458,
"details": [
{
"name": "sandwich",
"value": 1
},
{
"name": "pizza",
"value": 0
}
]
}
]
}
【问题讨论】:
标签: elasticsearch