gpt4 book ai didi

elasticsearch - Elastic Search Rails查找ID为部分的记录

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

我正在尝试使用elasticsearch-rails gem使用Rails和Elastic Search实现自动完成。

说我有以下记录:

[{id: 1, name: "John White"}, 
{id:2, name: "Betty Johnson"}]

在搜索“John”时,我可以使用哪种 Elasticsearch 方法返回两个记录。

自动完成功能只会返回“John White”,而不会返回id:1。

最佳答案

一种方法是使用edgeNgram filter:

PUT office
{
"settings": {
"analysis": {
"analyzer": {
"default_index":{
"type":"custom",
"tokenizer":"standard",
"filter":["lowercase","edgeNgram_"]
}
},
"filter": {
"edgeNgram_":{
"type":"edgeNgram",
"min_gram":"2",
"max_gram":"10"
}
}
}
},
"mappings": {
"employee":{
"properties": {
"name":{
"type": "string"
}
}
}
}
}

PUT office/employee/1
{
"name": "John White"
}
PUT office/employee/2
{
"name": "Betty Johnson"
}
GET office/employee/_search
{
"query": {
"match": {
"name": "John"
}
}
}

结果将是:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.19178301,
"hits": [
{
"_index": "office",
"_type": "employee",
"_id": "1",
"_score": 0.19178301,
"_source": {
"name": "John White"
}
},
{
"_index": "office",
"_type": "employee",
"_id": "2",
"_score": 0.19178301,
"_source": {
"name": "Betty Johnson"
}
}
]
}
}

关于elasticsearch - Elastic Search Rails查找ID为部分的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124771/

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