![](/img/trans.png)
[英]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.