gpt4 book ai didi

elasticsearch - ElasticSearch从查询自动完成功能开始

转载 作者:行者123 更新时间:2023-12-02 22:47:39 24 4
gpt4 key购买 nike

我想使用ElasticSearch和C#构建自动完成功能。但是我没有得到理想的结果。出于演示目的,这是我所做的。

1)创建索引称为“名称”:

PUT names?pretty

2)使用POST命令添加了20个条目:
POST names/_doc/1
{
"name" : "John Smith"
}

3)名单:
[ "John Smith", "John Smitha", "John Smithb", "John Smithc", "John Smithd", "John Smithe", "John Smithf",
"John Smithg", "John Smithh", "John Smithi", "Smith John", "Smitha John", "Smithb John", "Smithc John",
"Smithd John", "Smithe John", "Smithf John", "Smithg John", "Smithh John", "Smithi John",]

4)当我运行前缀查询时:
GET names/_search
{
"query": {
"prefix": {
"name": {
"value": "Smith"
}
}
}
}

我希望取回 "Smith John", "Smitha John" ...但是我要取回 "John Smith", "John Smitha" ...

我究竟做错了什么?我需要更改什么,在哪里更改?

最佳答案

您正在将name字段定义为text字段,默认情况下使用standard分析器并将 token 转换为小写。您可以使用ES的analyze API对此进行测试。

关键字分析器的 token 示例

网址:-http:// {{hostname}}:{{port}} / {{index}} / _analyze

{
"text": "John Smith",
"analyzer" : "keyword"
}

以上API的输出
{
"tokens": [
{
"token": "John Smith",
"start_offset": 0,
"end_offset": 10,
"type": "word",
"position": 0
}
]
}

注意,它并没有破坏 text并按照 official ES doc中的说明存储它。

使用标准分析仪的代币
{
"text": "Smith John",
"analyzer" : "standard"
}

以上API的输出:
{
"tokens": [
{
"token": "john",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "smith",
"start_offset": 5,
"end_offset": 10,
"type": "<ALPHANUM>",
"position": 1
}
]
}

现在,当不分析前缀查询并将其按原样发送给ES时,因此具有大写 SmithS通知将被发送到ES以进行 token 匹配,现在具有更新的映射,仅以 Smith开头的文档将具有该前缀,并且只有这些会进入搜索结果。

制图
{
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "keyword"
}
}
}
}

搜索查询
{
"query": {
"prefix": {
"name": {
"value": "Smith"
}
}
}
}

编辑::-**根据OP注释并根据上述设置和搜索查询更新了设置,如下图所示,仅获取以 Smith开头的结果
{
"took": 811,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "59977669",
"_type": "_doc",
"_id": "6",
"_score": 1.0,
"_source": {
"name": "Smith John"
}
},
{
"_index": "59977669",
"_type": "_doc",
"_id": "7",
"_score": 1.0,
"_source": {
"name": "Smithb John"
}
},
{
"_index": "59977669",
"_type": "_doc",
"_id": "8",
"_score": 1.0,
"_source": {
"name": "Smithc John"
}
},
{
"_index": "59977669",
"_type": "_doc",
"_id": "9",
"_score": 1.0,
"_source": {
"name": "Smithd John"
}
},
{
"_index": "59977669",
"_type": "_doc",
"_id": "10",
"_score": 1.0,
"_source": {
"name": "Smithe John"
}
}
]
}
}

注意:This博客介绍了各种实现自动完成功能的折衷方法。

关于elasticsearch - ElasticSearch从查询自动完成功能开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977669/

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