gpt4 book ai didi

elasticsearch - 尝试构建同义词过滤器时获取 “Failed to build synonyms”消息

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

我正在将Elasticsearch 6.8与python 3.7一起使用

我正在尝试创建自己的同义词,将表情符号称为文本。
例如:“:-)”将被称为“快乐笑脸”。

我正在尝试使用以下代码构建和创建同义词和索引:

def create_analyzer(es_api, index_name, doc_type):
body = {
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
":-), happy-smiley",
":-(, sad-smiley"
]
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "synonym_filter"]
}
}
}
}
},
"mappings": {
doc_type: {
"properties": {
"tweet": {"type": "text", "fielddata": "true"},
"existence": {"type": "text"},
"confidence": {"type": "float"}
}
}}
}
res = es_api.indices.create(index=index_name, body=body)

但是我遇到了错误:
lasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', 'failed to build synonyms')

有什么问题,我该如何解决?

最佳答案

我可以说你怎么了,(更新)如何解决这个问题。

因此,如果您将在开发工具或bu URL中运行此查询,您将看到错误原因-认为Python剪切了错误详细信息,因此您看不到原因。

PUT st_t3
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
":-), happy-smiley",
":-(, sad-smiley"
]
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter"
]
}
}
}
}
},
"mappings": {
"properties": {
"tweet": {
"type": "text",
"fielddata": "true"
},
"existence": {
"type": "text"
},
"confidence": {
"type": "float"
}
}
}
}

响应:
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[127.0.0.1:9301][indices:admin/create]"
}
],
"type": "illegal_argument_exception",
"reason": "failed to build synonyms",
"caused_by": {
"type": "parse_exception",
"reason": "parse_exception: Invalid synonym rule at line 1",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "term: :-) was completely eliminated by analyzer"
}
}
},
"status": 400
}

因此 "reason": "term: :-) was completely eliminated by analyzer"的原因-意味着Elastic在同义词过滤器中不支持此字符。

更新

可以通过 char_filter过滤器完成。

例:
PUT st_t3
{
"settings": {
"index": {
"analysis": {
"char_filter": {
"happy_filter": {
"type": "mapping",
"mappings": [
":-) => happy-smiley",
":-( => sad-smiley"
]
}
},
"analyzer": {
"smile_analyzer": {
"type": "custom",
"char_filter": [
"happy_filter"
],
"tokenizer": "standard",
"filter": [
"lowercase"
]
}
}
}
}
},
"mappings": {
"properties": {
"tweet": {
"type": "text",
"fielddata": "true"
},
"existence": {
"type": "text"
},
"confidence": {
"type": "float"
}
}
}
}

测试
POST st_t3/_analyze
{
"text": ":-) test",
"analyzer": "smile_analyzer"
}

回答
{
"tokens" : [
{
"token" : "happy",
"start_offset" : 0,
"end_offset" : 2,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "smiley",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "test",
"start_offset" : 4,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}

关于elasticsearch - 尝试构建同义词过滤器时获取 “Failed to build synonyms”消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59551894/

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