您可以使用terms facet到达那里,但您必须更改数据结构以将所有电话号码包含在单个字段中:
创建索引:
curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'
索引您的数据:
curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1' -d '
{
"name" : "Roger",
"id" : "id1",
"phone" : [
"123",
"980"
]
}
'
curl -XPOST 'http://127.0.0.1:9200/test/test?pretty=1' -d '
{
"name" : "Lucas",
"id" : "id2",
"phone" : [
"789",
"123"
]
}
'
搜索所有字段,返回 中的项数phone
:
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"facets" : {
"phone" : {
"terms" : {
"field" : "phone"
}
}
}
}
'
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "name" : "Roger",
# "id" : "id1",
# "phone" : [
# "123",
# "980"
# ]
# },
# "_score" : 1,
# "_index" : "test",
# "_id" : "StaJK9A5Tc6AR7zXsEKmGA",
# "_type" : "test"
# },
# {
# "_source" : {
# "name" : "Lucas",
# "id" : "id2",
# "phone" : [
# "789",
# "123"
# ]
# },
# "_score" : 1,
# "_index" : "test",
# "_id" : "x8w39F-DR9SZOQoHpJw2FQ",
# "_type" : "test"
# }
# ],
# "max_score" : 1,
# "total" : 2
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "facets" : {
# "phone" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 2,
# "term" : "123"
# },
# {
# "count" : 1,
# "term" : "980"
# },
# {
# "count" : 1,
# "term" : "789"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 4
# }
# },
# "took" : 5
# }