gpt4 book ai didi

java - 按名字或姓氏以及特定的相关 ID 搜索?

转载 作者:行者123 更新时间:2023-12-02 06:21:23 24 4
gpt4 key购买 nike

我正在尝试按名字或姓氏查找具有相同building_id 的人。
例如,有人会在文本字段中输入一个名字,该名字可能是名字或姓氏。它们都有相同的建筑物 ID。我似乎无法在 Elasticsearch 的文档中找到这一点。我的结果数据如下所示:

{
took: 9,
timed_out: false,
_shards: {
total: 5,
successful: 5,
failed: 0
},
hits: {
total: 56,
max_score: 1,
hits: [
{
_index: "jdbc",
_type: "jdbc",
_id: "5",
_score: 1,
_source: {
first_name: "some first name",
last_name: "some last name",
building_id: 1
}
},
...
...

最佳答案

一种方法是将 term 过滤器与 multi_match 查询结合使用。为了说明这一点,首先创建一些文档:

curl -XPUT "http://localhost:9200/test_index"

curl -XPUT "http://localhost:9200/test_index/person/1" -d'
{
"first_name": "Bob",
"last_name": "Jones",
"building_id": 1
}'

curl -XPUT "http://localhost:9200/test_index/person/2" -d'
{
"first_name": "Bill",
"last_name": "Smith",
"building_id": 1
}'

curl -XPUT "http://localhost:9200/test_index/person/3" -d'
{
"first_name": "Joe",
"last_name": "Williams",
"building_id": 2
}'

curl -XPUT "http://localhost:9200/test_index/person/4" -d'
{
"first_name": "John",
"last_name": "Taylor",
"building_id": 2
}'

curl -XPUT "http://localhost:9200/test_index/person/5" -d'
{
"first_name": "Taylor",
"last_name": "Johnson",
"building_id": 2
}'

然后,如果您想搜索 2 号楼中的所有“Taylor”,请执行以下命令:

curl -XPOST "http://localhost:9200/test_index/person/_search" -d'
{
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "taylor",
"fields": [
"first_name",
"last_name"
]
}
},
"filter": {
"term": {
"building_id": 2
}
}
}
}
}'

你会得到结果:

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.94125634,
"hits": [
{
"_index": "test_index",
"_type": "person",
"_id": "5",
"_score": 0.94125634,
"_source": {
"first_name": "Taylor",
"last_name": "Johnson",
"building_id": 2
}
},
{
"_index": "test_index",
"_type": "person",
"_id": "4",
"_score": 0.5906161,
"_source": {
"first_name": "John",
"last_name": "Taylor",
"building_id": 2
}
}
]
}
}

这是一个可运行的示例(您需要在计算机上安装 Elasticsearch 并在 localhost:9200 上运行):

http://be6c2e3260c3e2af000.qbox.io/gist/cc47b232ec60f09b93906a5c80028c1317dbdf28

关于java - 按名字或姓氏以及特定的相关 ID 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20977891/

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