gpt4 book ai didi

elasticsearch - elasticsearch中的多字段操作

转载 作者:行者123 更新时间:2023-12-02 22:16:21 25 4
gpt4 key购买 nike

我开始查看 elasticsearch,我想知道是否可以用它完成此操作:(我进行了一些搜索,但我承认我不知道要查找什么)。

我有这两个联系人数据:

{
"id" : "id1",
"name" : "Roger",
"phone1" : "123",
"phone2" : "",
"phone3" : "980"
}

{
"id" : "id2",
"name" : "Lucas",
"phone1" : "789",
"phone2" : "123",
"phone3" : ""
}

我很想知道 elasticsearch 是否可以帮助我找到重复的电话号码,即使它们位于不同的电话字段中(此处的“123”出现在两个记录中)。我已经看到我可以在多个字段中搜索一个字符串,所以如果我搜索 123,我可以得到这两条记录。但是,我希望能够发出可以返回如下内容的请求:

{
"phones" : {
"123" : ["id1", "id2"],
"980" : ["id1"],
"789" : ["id2"]
}
}

或者这甚至会有用(联系人数量与号码):

{
"phones" : {
"123" : 2,
"980" : 1,
"789" : 1
}
}

知道这是否可能吗?如果它能做到,那就太棒了。

最佳答案

我同意 DrTech 更改数据结构的建议。但是,如果出于某种原因,您更愿意保持原样,则可以使用多字段术语 facet 获得相同的结果:

curl "localhost:9200/phonefacet/_search?pretty=true&search_type=count" -d '{
"query" : {
"match_all" : { }
},
"facets" : {
"tag" : {
"terms" : {
"fields" : ["phone1", "phone2", "phone3"],
"size" : 10
}
}
}
}'

结果是这样的:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.0,
"hits" : [ ]
},
"facets" : {
"tag" : {
"_type" : "terms",
"missing" : 2,
"total" : 4,
"other" : 0,
"terms" : [ {
"term" : "123",
"count" : 2
}, {
"term" : "980",
"count" : 1
}, {
"term" : "789",
"count" : 1
} ]
}
}
}

关于elasticsearch - elasticsearch中的多字段操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11556718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com