gpt4 book ai didi

java - Elasticsearch 查询字符串查询无法与同义词分析器一起使用

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

我正在尝试使用同义词配置 Elasticsearch 。

这些是我的设置:

                "analysis": {
"analyzer": {
"category_synonym": {
"tokenizer": "whitespace",
"filter": [
"synonym_filter"
]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
}
}
}

映射配置:

        "category": {
"properties": {
"name": {
"type":"string",
"search_analyzer" : "category_synonym",
"index_analyzer" : "standard",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

以及我的同义词列表

film => video,
ooh => panels , poster,
commercial => advertisement,
print => magazine

我必须说我正在使用 Elasticsearch Java API。我正在使用 QueryBuilders.queryStringQuery ,因为这是我根据请求设置分析器的唯一方法。所以,当我制作时:

QueryBuilders.queryStringQuery("name:film").analyzer(analyzer)

它返回我

[
{
"id": 71,
"name": "Pitch video",
"description": "... ",
"parent": null
},
{
"id": 25,
"name": "Video",
"description": "... ",
"parent": null
}
]

这对我来说是完美的,但是当我调用这样的东西时

QueryBuilders.queryStringQuery("name:vid").analyzer(analyzer)

我希望它应该返回相同的对象,但什么也没有:[]

因此,我向 queryStringQuery 添加了星号:

QueryBuilders.queryStringQuery("name:vid*").analyzer(analyzer)

效果很好,但现在

QueryBuilders.queryStringQuery("name:film*").analyzer(analyzer)

返回我[]

那么,如何配置我的 Elasticsearch ,使其在搜索 videovidfilmfil

提前致谢!

最佳答案

嗯,我认为 Elasticsearch 不会知道将 fil“翻译”为 vid :-)。因此,我认为您在索引和搜索时都需要 edgeNGram

PUT test
{
"settings": {
"analysis": {
"analyzer": {
"category_synonym": {
"tokenizer": "whitespace",
"filter": [
"synonym_filter",
"my_edgeNGram_filter"
]
},
"standard_edgeNGram": {
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter",
"my_edgeNGram_filter"
]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
},
"my_edgeNGram_filter": {
"type": "edgeNGram",
"min_gram": 2,
"max_gram": 8
}
}
}
},
"mappings": {
"test": {
"properties": {
"name": {
"type": "string",
"analyzer": "category_synonym",
"index_analyzer": "standard_edgeNGram",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}

POST test/test/1
{"name": "Pitch video"}
POST test/test/2
{"name": "Video"}

GET /test/test/_search
{
"query": {
"query_string": {
"query": "name:fil"
}
}
}

关于java - Elasticsearch 查询字符串查询无法与同义词分析器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502362/

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