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

时间:2016-02-23 00:02:56

标签: elasticsearch

这是我的代码

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" 
}
}
}
'

我没有结果,虽然爱是运气的同义词

为什么好吗?我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

我同意@BrookeB,但又想添加两件事:

  
      
  1. 您的过滤器和分析仪具有相同的名称,可能会造成混淆。我将分析器重命名为" my_analyzer "
  2.   
  3. 如果您要定义 multi_field ,请将其声明为此。
  4.   

以下是适合我的完整示例:

# 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" 
}
}
}
'

答案 1 :(得分:0)

看起来你已经创建了" description"作为映射中的多字段,仅将自定义分析器应用于" ss"一部分。

尝试针对" description.ss"运行相同的查询。字段,看看你是否得到了你期望的结果。