如何在弹性搜索中实现同义词?

时间:2017-01-30 13:33:52

标签: elasticsearch

我想在我的映射中实现同义词。我创建了父子映射。这是我的映射。

{  
   "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",

               }
            }
         }
      }
   }
}

提前致谢。

1 个答案:

答案 0 :(得分: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"
                }
            }
        }
    }
}

在上面的设置中,我为两个不同的字段定义了两个分析器。这些分析仪支持同义词,并在每个分析器的过滤器中定义同义词。

您还可以在单​​独的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

希望这有帮助