2016-09-30 21 views
0

我正試圖學習如何正確添加「同義詞功能」到我現有的ElasticSearch設置。以下是我對此過程的理解。如果您能指出我的任何誤解,我會很感激的 - 我對elasticsearch很陌生。基本ElasticSearch + WordNet同義詞設置與預先存在的索引

this page我已經學會了,我需要一個路徑我的同義詞文件中添加的代名詞分析儀的代名詞過濾器,以我的索引配置,使它看起來像這樣:

{ 
    "index" : { 
     "analysis" : { 
      "analyzer" : { 
       "synonym" : { 
        "tokenizer" : "whitespace", 
        "filter" : ["synonym"] 
       } 
      }, 
      "filter" : { 
       "synonym" : { 
        "type" : "synonym", 
        "format" : "wordnet", 
        "synonyms_path" : "analysis/wordnet_synonyms.txt" 
       } 
      } 
     } 
    } 
} 

this page我已經學會了如何添加分析儀:

curl -XPOST 'localhost:9200/myindex/_close' 

curl -XPUT 'localhost:9200/myindex/_settings' -d '{ 
    "analysis" : { 
    "analyzer":{ 
     "synonym":{ 
     "tokenizer":"whitespace", 
     "filter" : ["synonym"] 
     } 
    } 
    } 
}' 

curl -XPOST 'localhost:9200/myindex/_open' 

但我不知道如何添加過濾器。難道是像這樣簡單?:

curl -XPOST 'localhost:9200/myindex/_close' 

curl -XPUT 'localhost:9200/myindex/_settings' -d '{ 
    "analysis" : { 
    "filter":{ 
     "synonym":{ 
     "type" : "synonym", 
     "format" : "wordnet", 
     "synonyms_path" : "analysis/wordnet_synonyms.txt", 
     "ignore_case" : true 
     } 
    } 
    } 
}' 

curl -XPOST 'localhost:9200/myindex/_open' 

我也不知道哪裏analysis/wordnet_synonyms.txt是相對。在this page它說「相對於config位置」。 config位置在哪裏?在etc/elasticsearch某處(在Ubuntu上)?謝謝!

編輯:This answer給出了這樣的解決方案:

curl -XPOST 'localhost:9200/myindex/_close' 
curl -XPUT 'localhost:9200/myindex/_settings' -d '{ 
"analysis" : { 
    "analyzer" : { 
     "synonym" : { 
      "tokenizer" : "whitespace", 
      "filter" : ["synonym"] 
     } 
    }, 
    "filter":{ 
     "synonym":{ 
     "type" : "synonym", 
     "format" : "wordnet", 
     "synonyms_path" : "analysis/wordnet_synonyms.txt", 
     "ignore_case" : true 
     } 
}' 
curl -XPOST 'localhost:9200/myindex/_open' 

這可能嗎?一位評論者說,當更改分析儀設置時,需要重新創建索引 - 這是真的嗎?而且我還不確定在哪裏放置「wordnet_synonyms.txt」。

回答

2

最簡單的方法是首先刪除索引,然後使用分析器和同義詞標記過濾器創建它,如下所示(我還添加了一個映射類型和一個虛擬字段來向您展示如何使用分析器):

curl -XDELETE localhost:9200/myindex 

curl -XPUT localhost:9200/myindex -d '{ 
    "settings": { 
    "index" : { 
     "analysis" : { 
      "analyzer" : { 
       "synonym" : { 
        "tokenizer" : "whitespace", 
        "filter" : ["synonym"] 
       } 
      }, 
      "filter" : { 
       "synonym" : { 
        "type" : "synonym", 
        "format" : "wordnet", 
        "synonyms_path" : "analysis/wordnet_synonyms.txt" 
       } 
      } 
     } 
    } 
    }, 
    "mappings": { 
    "typename": { 
     "fieldname": { 
      "type": "string", 
      "analyzer": "synonym" 
     } 
    } 
    } 
}' 

你需要把analysis/wordnet_synonyms.txt文件在同一文件夾作爲elasticsearch.yml配置文件。在Ubuntu上,它將在

/etc/elasticsearch/analysis/wordnet_synonyms.txt 
+0

謝謝,這工作,但我正在尋找一種方法來做到預先存在的索引,但如果沒有辦法,我會接受你的答案爲是。 – JoeRocc

+1

唯一的問題是您的字段已存在。您肯定可以修改設置併爲您的新分析儀添加一個新的區域,但不能將分析儀添加到已經存在的區域。 – Val

+0

感謝您的澄清! – JoeRocc