gpt4 book ai didi

indexing - elasticsearch:如何索引仅是停用词的术语?

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

我在后台使用 elasticsearch 构建自己的小搜索取得了很大的成功。但是我在文档中找不到一件事。

我正在为音乐家和乐队的名字编制索引。有一支名为“The The”的乐队,由于停用词列表,该乐队从未被编入索引。

我知道我可以完全忽略停用词列表,但这不是我想要的,因为搜索其他乐队(如“the who”)的结果会爆炸。

那么,是否可以在索引中保存“The The”但根本不禁用停用词?

最佳答案

您可以使用 synonym filterThe The 转换为单个标记,例如 thethe,它不会被停用词过滤器删除。

首先,配置分析器:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '
{
"settings" : {
"analysis" : {
"filter" : {
"syn" : {
"synonyms" : [
"the the => thethe"
],
"type" : "synonym"
}
},
"analyzer" : {
"syn" : {
"filter" : [
"lowercase",
"syn",
"stop"
],
"type" : "custom",
"tokenizer" : "standard"
}
}
}
}
}
'

然后用字符串 "The The The Who" 测试它。

curl -XGET 'http://127.0.0.1:9200/test/_analyze?pretty=1&text=The+The+The+Who&analyzer=syn' 

{
"tokens" : [
{
"end_offset" : 7,
"position" : 1,
"start_offset" : 0,
"type" : "SYNONYM",
"token" : "thethe"
},
{
"end_offset" : 15,
"position" : 3,
"start_offset" : 12,
"type" : "<ALPHANUM>",
"token" : "who"
}
]
}

“The The” 已标记为 “the the”“The Who” 标记为 “who” 因为前面的 “the” 已被停用词过滤器删除。

停止还是不停止

这让我们回到是否应该包含停用词的问题?你说:

I know I can ignore the stop words list completely 
but this is not what I want since the results searching
for other bands like "the who" would explode.

你这是什么意思?怎么爆?索引大小?性能?

停用词最初是为了通过删除可能对查询的相关性影响不大的常用词来提高搜索引擎性能而引入的。然而,从那时起我们已经走了很长一段路。与 80 年代相比,我们的服务器的功能要强大得多。

索引停用词不会对索引大小产生巨大影响。例如,索引单词 the 意味着向索引中添加一个术语。您已经有数千个术语 - 索引停用词也不会对大小或性能产生太大影响。

实际上,更大的问题是 the 很常见,因此对相关性的影响很小,因此搜索 “The The concert Madrid” 会更受欢迎Madrid 优于其他条款。这可以通过使用 shingle 来缓解过滤器,这将导致这些标记:

['the the','the concert','concert madrid']

虽然 the 可能很常见,但 the 不常见,因此排名更高。

您不会单独查询 shingled 字段,但您可以将针对由标准分析器(没有停用词)标记化的字段的查询与针对 shingled 字段的查询结合起来。

我们可以使用多字段以两种不同的方式分析text字段:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '
{
"mappings" : {
"test" : {
"properties" : {
"text" : {
"fields" : {
"shingle" : {
"type" : "string",
"analyzer" : "shingle"
},
"text" : {
"type" : "string",
"analyzer" : "no_stop"
}
},
"type" : "multi_field"
}
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"no_stop" : {
"stopwords" : "",
"type" : "standard"
},
"shingle" : {
"filter" : [
"standard",
"lowercase",
"shingle"
],
"type" : "custom",
"tokenizer" : "standard"
}
}
}
}
}
'

然后使用 multi_match 查询来查询字段的两个版本,为叠瓦版本提供更多“提升”/相关性。在此示例中,text.shingle^2 表示我们希望将该字段增加 2:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
"query" : {
"multi_match" : {
"fields" : [
"text",
"text.shingle^2"
],
"query" : "the the concert madrid"
}
}
}
'

关于indexing - elasticsearch:如何索引仅是停用词的术语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14640732/

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