gpt4 book ai didi

elasticsearch - 为什么在此示例中同义词不起作用

转载 作者:行者123 更新时间:2023-12-02 23:31:50 28 4
gpt4 key购买 nike

这是我的代码

curl -XPUT "http://localhost:9200/my_index" -d '
{
"settings" : {
"analysis" : {
"filter" : {
"my_synonym_filter" : {
"type" : "synonym",
"synonyms" : [
"luck,love"
]
}
},
"analyzer" : {
" my_synonym_filter " : {
"tokenizer" : "standard",
"filter" : [
"lowercase",
"my_synonym_filter"
]
}}}}}'


curl -XPUT "http://localhost:9200/my_index/_mapping/doc?pretty" -d '
{
"properties" : {
"description" : {
"type" : "string",
"fields" : {
"ss" : {
"type" : "string",
"analyzer" : " my_synonym_filter "
}}}}}'


curl -XPUT "http://localhost:9200/my_index/doc/1" -d '
{
"description" : "luck is the best in the world"
}
'


curl -XPUT "http://localhost:9200/my_index/doc/2" -d '
{
"description" : "luck is just wonderful"
}
'

如您所见,我使用自定义分析器创建了两个同义词单词 lucklove
但是当我执行此查询时
curl -XGET "http://localhost:9200/my_index/_search?pretty" -d '
{
"query" : {
"match" : {
"description" : "love"
}
}
}
'

我没有结果,尽管爱情是运气的同义词

告诉我为什么?我的代码有什么问题?

最佳答案

我同意@BrookeB,但还要添加两点:

  1. Your filter and analyzer have same name which could be confusing. I renamed analyzer to "my_analyzer"
  2. If you are defining multi_field declare it as such.


这是对我有用的完整示例:
# combined settings and mappings in one call
curl -XPUT "http://localhost:9200/my_index3" -d '
{
"settings" :
{
"analysis" :
{
"filter" :
{
"my_synonym_filter" :
{
"type" : "synonym",
"synonyms" : [ "luck,love" ]
}
},
"analyzer" :
{
"my_analyzer" :
{
"tokenizer" : "standard",
"filter" : [
"lowercase",
"my_synonym_filter"
]
}
}
}
},
"mappings": {
"doc": {
"properties" : {
"description" : {
"type" : "multi_field",
"fields" : {
"ss" : {
"type" : "string",
"analyzer": "my_analyzer"
}
}
}
}
}
}
}'

# check the analyzer
curl -XGET "http://localhost:9200/my_index3/_analyze?analyzer=my_analyzer&pretty" -d 'luck is the best in the world'

# doc 1
curl -XPUT "http://localhost:9200/my_index3/doc/1" -d '
{
"description.ss" : "luck is the best in the world"
}
'

# doc 2, you can put to the property field
curl -XPUT "http://localhost:9200/my_index3/doc/2" -d '
{
"description.ss" : "luck is just wonderful"
}
'

# doc 3, you can put directly to the property, but...
curl -XPUT "http://localhost:9200/my_index3/doc/3" -d '
{
"description" : "love conquors all"
}
'

# gets no documents
curl -XPOST "http://localhost:9200/my_index3/_search?pretty" -d '
{
"query" : {
"match" : {
"description" : "love"
}
}
}
'

# gets all 3 documents
curl -XPOST "http://localhost:9200/my_index3/_search?pretty" -d '
{
"query" : {
"match" : {
"description.ss" : "love"
}
}
}
'

关于elasticsearch - 为什么在此示例中同义词不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35566365/

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