gpt4 book ai didi

python - Elasticsearch - 不需要完全匹配

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

目前,我的 elasticsearch 索引中有一个项目,标题为:testing123

当我搜索它时,只有精确搜索 testing123 才能返回它。但是,我希望能够搜索 testing 并返回它。

我怎样才能拥有它,以便搜索必须以该术语开头,但又不能完全匹配?

最佳答案

使用Simple Analyzer在你的映射中。

创建一个索引,其中使用标准(默认)和简单分析器对 title 字段进行索引:

POST /demo
{
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string",
"fields": {
"simple": {
"type": "string",
"analyzer": "simple"
}
}
}
}
}
}
}

索引文档

POST /demo/doc/1
{
"title": "testing123"
}

最后,使用 multi_match 查询进行搜索:

POST /demo/doc/_search
{
"query": {
"multi_match": {
"fields": [
"title",
"title.simple"
],
"query": "testing"
}
}
}

此查询返回文档。如果您要将查询词更改为 testing123,也会有匹配项。

另一种可能的解决方案是使用 Prefix Query

关于python - Elasticsearch - 不需要完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29144517/

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