2016-02-01 72 views

回答

1

是的,您可以通過使用synonym token filter來實現此目的。

首先,您需要在索引中定義一個新的自定義分析器,並在映射中使用該分析器。

curl -XPUT localhost:9200/cars -d '{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "my_analyzer": { 
      "type": "custom", 
      "tokenizer": "standard", 
      "filter": [ 
      "synonyms" 
      ] 
     } 
     }, 
     "filter": { 
     "synonyms": { 
      "type": "synonym", 
      "synonyms": [ 
      "good, awesome, fantastic" 
      ] 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "car": { 
     "properties": { 
     "carName": { 
      "type": "string" 
     }, 
     "review": { 
      "type": "string", 
      "analyzer": "my_analyzer" 
     } 
     } 
    } 
    } 
}' 

,只要你想你可以添加儘可能多的同義詞,無論是在直接設置或者,你可以在設置中使用synonyms_path屬性引用一個單獨的文件。

然後我們可以索引示例文檔上面:

curl -XPUT localhost:9200/cars/car/1 -d '{ 
    "carName": "Porche", 
    "review": " this car is so awesome" 
}' 

這是怎麼發生的是,當synonyms令牌過濾踢,它也將指數令牌goodawesome沿fantastic,讓你也可以通過這些令牌來搜索和查找該文檔。具體而言,分析句子this car is so awesome ...

curl -XGET 'localhost:9200/cars/_analyze?analyzer=my_analyzer&pretty' -d 'this car is so awesome' 

...會產生下列標記(見最後三個令牌)

{ 
    "tokens" : [ { 
    "token" : "this", 
    "start_offset" : 0, 
    "end_offset" : 4, 
    "type" : "<ALPHANUM>", 
    "position" : 1 
    }, { 
    "token" : "car", 
    "start_offset" : 5, 
    "end_offset" : 8, 
    "type" : "<ALPHANUM>", 
    "position" : 2 
    }, { 
    "token" : "is", 
    "start_offset" : 9, 
    "end_offset" : 11, 
    "type" : "<ALPHANUM>", 
    "position" : 3 
    }, { 
    "token" : "so", 
    "start_offset" : 12, 
    "end_offset" : 14, 
    "type" : "<ALPHANUM>", 
    "position" : 4 
    }, { 
    "token" : "good", 
    "start_offset" : 15, 
    "end_offset" : 22, 
    "type" : "SYNONYM", 
    "position" : 5 
    }, { 
    "token" : "awesome", 
    "start_offset" : 15, 
    "end_offset" : 22, 
    "type" : "SYNONYM", 
    "position" : 5 
    }, { 
    "token" : "fantastic", 
    "start_offset" : 15, 
    "end_offset" : 22, 
    "type" : "SYNONYM", 
    "position" : 5 
    } ] 
} 

最後,你可以搜索這樣和文檔會檢索:

curl -XGET localhost:9200/cars/car/_search?q=review:good 
相關問題