2016-02-23 19 views
1

這是我的代碼爲什麼同義詞本例中沒有工作

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

我沒有結果,雖然愛情是運氣的同義詞

爲什麼要這樣?我的代碼有什麼問題?

+0

你解決到底問題了嗎? – CrnaStena

+0

@CrnaStena是的比你,你的解決方案工作 –

+0

好極了。你介意加註和/或將其標記爲答案? – CrnaStena

回答

0

我同意@BrookeB但想補充兩兩件事:

  1. 您的篩選和分析有可能是混亂的相同的名稱。我將分析儀更名爲「my_analyzer
  2. 如果您正在定義multi_field聲明爲如此。

這裏是爲我的作品完整的例子:

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

看起來您已將「description」創建爲映射中的多字段,並且只將自定義分析器應用於「ss」部分。

嘗試對「description.ss」字段運行相同的查詢,以查看您是否得到期望的結果。

+1

也可以刪除分析器名稱中的空格(例如,「my_synonym_filter」而不是「my_synonym_filter」),因爲這可能會產生不需要的錯誤。 – Val

相關問題