[英]How to search by array field in Elasticsearch?
我已經索引了這樣的文檔:
[PUT] /my_index/cars/1
{
"color" : "red",
"passengers" : ["John", "Annie", "William"]
}
我正在嘗試搜索“約翰”是乘客的所有汽車。
您可以像搜索其他任何字段一樣進行搜索。
-bash-4.2$ curl 'localhost:9200/my_index/_search?pretty=true&q=passengers:John'
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "my_index",
"_type" : "cars",
"_id" : "1",
"_score" : 0.15342641,
"_source" : {
"color" : "red",
"passengers" : [ "John", "Annie", "William" ]
}
} ]
}
}
盡管codegrep_admin的答案有效,但我一直在尋找可以更輕松地操作的東西。
我最終使用了bool
查詢,如下所示:
[GET] /my_index/cars/_search
{
"query": {
"bool": {
"must": [
{ "match": { "passengers": "John" }},
{ "match": { "passengers": "Annie" }}
]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.