Elasticsearch:如何允许多语言搜索?

cur*_*us1 0 elasticsearch

我是 Elasticsearch 的新手。我发布了一个名为

Elasticsearch:对同一数据记录的每种语言使用单独的索引

这是链接:Elasticsearch:为同一数据记录的每种语言使用单独的索引

发布的答案提到“允许多语言搜索”。我对此感到困惑。我读了“Elasticsearch 服务器(第二版)”一书,但没有看到这个主题。我刚刚进行了谷歌搜索,无法在网上看到任何相关内容。

有没有人碰巧有任何关于“允许多语言搜索”的链接?这是配置问题吗?如何?

非常感谢您的任何意见!

问候。

Dan*_*ery 5

您不需要为每种语言设置不同的索引。假设您有一个“产品”类型,其标题字段可能是德语或法语或两者兼而有之,您需要使用不同的分析器对法语标题与德语标题进行不同的索引。在您的映射中指定您要使用的分析器:

{
    "product": {
        "properties": {
            "title": {
                "properties": {
                    "de": {
                      "type": "string",
                      "analyzer": "de_analyzer"
                    },
                    "fr": {
                      "type": "string",
                      "analyzer": "fr_analyzer"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

索引您的文档

curl -XPOST 'http://localhost:9200/yourindex/product/1' -d
'{
    "title": {
        "fr": "Bonjour"
    }
}'

curl -XPOST 'http://localhost:9200/yourindex/product/2' -d
'{
    "title" : {
        "de": "Hallo"
    }

}'
Run Code Online (Sandbox Code Playgroud)

当您想对法语标题进行搜索时,您可以在查询中引用它

http://localhost:9200/yourindex/_search?q=title.fr:bonjour
Run Code Online (Sandbox Code Playgroud)

当您想搜索德语标题时:

http://localhost:9200/yourindex/_search?q=title.de=hallo
Run Code Online (Sandbox Code Playgroud)

如果要搜索这两个字段,可以使用 multi_field 搜索:

{
    "query":{
        "multi_match" : {
            "query": "bonjour hallo", 
            "fields": [ "title.fr", "title.de" ] 
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

这篇博客将让您很好地了解如何使用要索引的每种语言的分析器:

http://gibrown.wordpress.com/2013/05/01/three-principles-for-multilingal-indexing-in-elasticsearch/