[英]How to use AND operator in elasticsearch to query nested fields?
[英]Elasticsearch - How to query nested fields?
我使用以下命令创建了一个 ES 索引:
curl -XPUT -H "Content-Type: application/json" http://localhost:9200/nested_test?pretty=true -d '{"mappings": {"properties": {"name": {"type": "keyword"}, "related": {"type": "nested", "properties": {"name": {"type": "keyword"}, "count": {"type": "long"}}}}}}'
这给了我回应:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "nested_test"
}
然后我在里面放了一些样本数据:
curl -XPUT -H "Content-Type: application/json" http://localhost:9200/nested_test/_doc/1?pretty=true -d '{"name": "php", "related": [{"name": "c#", "count": 6806}, {"name": "c", "count": 4080}, {"name": "java", "count": 9745}, {"name": "javascript", "count": 9141}]}'
这给了我回应:
{
"_index" : "nested_test",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
然后我终于尝试使用以下命令查询它:
curl -X GET http://localhost:9200/nested_test/_search?pretty=true -H 'Content-Type: application/json' -d '{"query": {"nested": {"path": "related", "query": {"match": {"related.name": "javascript"}}}}}'
它给了我以下回应:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.2039728,
"hits" : [
{
"_index" : "nested_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.2039728,
"_source" : {
"name" : "php",
"related" : [
{
"name" : "c#",
"count" : 6806
},
{
"name" : "c",
"count" : 4080
},
{
"name" : "java",
"count" : 9745
},
{
"name" : "javascript",
"count" : 9141
}
]
}
}
]
}
}
当我期待以下 output 时:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.2039728,
"hits" : [
{
"_index" : "nested_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.2039728,
"_source" : {
"name" : "php",
"related" : [
{
"name" : "javascript",
"count" : 9141
}
]
}
}
]
}
}
谁能指出我做错了什么?
伟大的开始! 您只需要请求嵌套的内部点击:
curl -X GET http://localhost:9200/nested_test/_search?pretty=true -H 'Content-Type: application/json' -d '{
"_source": {
"_excludes": ["related"]
},
"query": {
"nested": {
"path": "related",
"query": {
"match": {
"related.name": "javascript"
}
},
"inner_hits": {} <--- add this
}
}
}'
另请注意,我将“相关”字段排除在_source
文档中的返回之外,因为您将在fields
部分(与_source
同级)中获得您所追求的内部点击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.