gpt4 book ai didi

elasticsearch - 在 Elasticsearch 中使用内置或自定义分析器搜索数字和文本

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

这个问题是我之前的this SO问题的延续。我有一些文本,我想同时在数字和文本上进行搜索。
我的文字:

8080.foobar.getFooLabelFrombar(test.java:91)


我想搜索 getFooLabelFrombarfooBar808091
之前我使用了 simple分析器,该分析器将上面的文本标记为下面的标记。
 "tokens": [
{
"token": "foobar",
"start_offset": 10,
"end_offset": 16,
"type": "word",
"position": 2
},
{
"token": "getfoolabelfrombar",
"start_offset": 17,
"end_offset": 35,
"type": "word",
"position": 3
},
{
"token": "test",
"start_offset": 36,
"end_offset": 40,
"type": "word",
"position": 4
},
{
"token": "java",
"start_offset": 41,
"end_offset": 45,
"type": "word",
"position": 5
}
]
}
Beaucase,对 foobargetFooLabelFrombar的搜索给出了搜索结果,但没有给出 808091的搜索结果,因为 简单分析器不会标记数字
然后按照上一章的建议。这样的帖子,我将分析器更改为 Standard,因为可以搜索哪些数字,但不能搜索其他2个单词的搜索字符串。正如标准分析器将在下面创建 token :-
{
"tokens": [
{
"token": "8080",
"start_offset": 0,
"end_offset": 4,
"type": "<NUM>",
"position": 1
},
{
"token": "foobar.getfoolabelfrombar",
"start_offset": 5,
"end_offset": 35,
"type": "<ALPHANUM>",
"position": 2
},
{
"token": "test.java",
"start_offset": 36,
"end_offset": 45,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "91",
"start_offset": 46,
"end_offset": 48,
"type": "<NUM>",
"position": 4
}
]
}
我使用了ES中所有现有的分析仪,但似乎没有什么可以满足我的要求。我尝试创建下面的自定义分析器,但效果不佳。
{
"analysis" : {
"analyzer" : {
"my_analyzer" : {
"tokenizer" : "letter"
"filter" : ["lowercase", "extract_numbers"]
}
},
"filter" : {
"extract_numbers" : {
"type" : "keep_types",
"types" : [ "<NUM>","<ALPHANUM>","word"]
}
}
}
}
请提出建议,我该如何构建自定义分析仪来满足我的要求。

最佳答案

使用字符过滤器将点替换为空格怎么办?

PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": ["replace_dots"]
}
},
"char_filter": {
"replace_dots": {
"type": "mapping",
"mappings": [
". => \\u0020"
]
}
}
}
}
}

POST /my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "8080.foobar.getFooLabelFrombar(test.java:91)"
}
哪个输出您想要的:
{                                                                               
"tokens" : [
{
"token" : "8080",
"start_offset" : 0,
"end_offset" : 4,
"type" : "<NUM>",
"position" : 0
},
{
"token" : "foobar",
"start_offset" : 10,
"end_offset" : 16,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "getFooLabelFrombar",
"start_offset" : 17,
"end_offset" : 35,
"type" : "<ALPHANUM>",
"position" : 3
},
{
"token" : "test",
"start_offset" : 36,
"end_offset" : 40,
"type" : "<ALPHANUM>",
"position" : 4
},
{
"token" : "java",
"start_offset" : 41,
"end_offset" : 45,
"type" : "<ALPHANUM>",
"position" : 5
},
{
"token" : "91",
"start_offset" : 46,
"end_offset" : 48,
"type" : "<NUM>",
"position" : 6
}
]
}

关于elasticsearch - 在 Elasticsearch 中使用内置或自定义分析器搜索数字和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46873023/

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