【发布时间】:2015-07-18 08:39:10
【问题描述】:
我正在尝试查询具有多个值的嵌套属性。
这是一个更清楚的例子。
使用嵌套字段创建索引
curl -X DELETE "http://localhost:9200/testing_nested_query/"
curl -X POST "http://localhost:9200/testing_nested_query/" -d '{
"mappings": {
"class": {
properties: {
title: {"type": "string"},
"students": {
"type": "nested",
"properties": {
"name": {"type": "string"}
}
}
}
}
}
}'
添加一些值
curl -XPUT 'http://localhost:9200/testing_nested_query/class/1' -d '{
"title": "class1",
"students": [{"name": "john"},{"name": "jack"},{"name": "jim"}]
}'
curl -XPUT 'http://localhost:9200/testing_nested_query/class/2' -d '{
"title": "class2",
"students": [{"name": "john"},{"name": "chris"},{"name": "alex"}]
}'
查询 john 所在的所有类(按预期命中 2 次)
curl -XGET 'http://localhost:9200/testing_nested_query/class/_search' -d '{
"query": {
"nested": {
"path":"students",
"query": {
"bool": {
"must": [
{"match": {"students.name": "john"}}
]
}
}
}
}
}'
查询 john 和 jack 都参加的课程(0 个结果而不是 1 个)
curl -XGET 'http://localhost:9200/testing_nested_query/class/_search' -d '{
"query": {
"nested": {
"path":"students",
"query": {
"bool": {
"must": [
{"match": {"students.name": "john"}},
{"match": {"students.name": "jack"}}
]
}
}
}
}
}'
我已经尝试过匹配和过滤,但我永远无法让查询返回预期值。
【问题讨论】:
-
查询只需使用“should”而不是“must”即可。
-
不,“应该”返回 2 个点击而不是 1 个。
-
你说得对,我看错了问题。
标签: elasticsearch