2017-01-30 52 views
0

我想在我的映射中實現同義詞。我已經創建了父子映射。這是我的映射。如何在彈性搜索中實現同義詞?

{ 
    "mapping":{ 
     "mappings":{ 
     "question_data":{ 
      "properties":{ 
       "question_id":{ 
        "type":"integer" 
       }, 
       "question":{ 
        "type":"string" 
       } 
      } 
     }, 
     "answer_data":{ 
      "_parent":{ 
       "type":"question_data" 
      }, 
      "_routing":{ 
       "required":true 
      }, 
      "properties":{ 
       "answer_id":{ 
        "type":"integer" 
       }, 
       "answer":{ 
        "type":"string", 

       } 
      } 
     } 
     } 
    } 
} 

在此先感謝。

+0

可以wherte你想有同義詞也分享你的映射 – user3775217

+0

? – Mysterion

回答

2

要在elasticsearch中使用同義詞,您必須首先在設置中創建同義詞分析器,以便爲特定字段添加同義詞支持。在設置中也可以定義同義詞。

PUT testindex_510 
{ 
    "settings": { 
     "analysis": { 
      "analyzer": { 
       "synonymanalyzer": { 
        "tokenizer": "standard", 
        "filter": ["lowercase", "locationsynfilter"] 
       }, 
       "synonymanalyzer1": { 
        "tokenizer": "standard", 
        "filter": ["lowercase", "titlesynfilter"] 
       } 
      }, 
      "filter": { 
       "locationsynfilter": { 
        "type": "synonym", 
        "synonyms": [ 
         "lokhandwala,andheri west", 
         "versova,andheri west", 
         "mazgaon,byculla" 
        ] 
       }, 
       "titlesynfilter": { 
        "type": "synonym", 
        "synonyms": [ 
         "golds , gold", 
         "talwalkars, talwalkar" 
        ] 
       } 
      } 

     } 

    }, 
    "mappings": { 
     "testtype": { 
      "properties": { 
       "title": { 
        "type": "string", 
        "analyzer": "synonymanalyzer1" 
       }, 
       "location": { 
        "type": "string", 
        "analyzer": "synonymanalyzer" 
       } 
      } 
     } 
    } 
} 

在上述設置中,我爲兩個不同的字段定義了兩個分析器。這些分析儀支持synonms,並在每個分析儀的過濾器中定義同義詞。

您還可以在單​​獨的txt文件中定義同義詞,而不是像下面這樣映射中定義同義詞。

{ 
    "settings": { 
     "analysis": { 
      "analyzer": { 
       "synonymanalyzer": { 
        "tokenizer": "standard", 
        "filter": ["lowercase", "locationsynfilter"] 
       }, 
       "synonymanalyzer1": { 
        "tokenizer": "standard", 
        "filter": ["lowercase", "titlesynfilter"] 
       } 
      }, 
      "filter": { 
       "titlesynfilter": { 
        "type": "synonym", 
        "synonyms_path": "analysis/titlesynonym.txt" 
       }, 
       "locationsynfilter": { 
        "type": "synonym", 
        "synonyms_path": "analysis/locationsynonym.txt" 
       } 
      } 

     } 

    }, 
    "mappings": { 
     "testtype": { 
      "properties": { 
       "title": { 
        "type": "string", 
        "analyzer": "synonymanalyzer1" 
       }, 
       "location": { 
        "type": "string", 
        "analyzer": "synonymanalyzer" 
       } 
      } 
     } 
    } 
} 

你的txt文件應該看起來像什麼樣子。有關更多配置,請參閱documentation

ipod, i-pod, i pod 
foozball , foosball 
universe , cosmos 

希望這有助於

+0

謝謝!它幫助我很多.. –

+0

它按預期工作..... –

+0

謝謝,請你接受答案。 – user3775217