支持多种语言的Elasticsearch
问题描述:
我正在使用elasticsearch 5.1.1。 我有一个要求,我想在多种语言索引数据。支持多种语言的Elasticsearch
我用下面的映射:
PUT http://localhost:9200/movies
{
"mappings": {
"title": {
"properties": {
"title": {
"type": "string",
"fields": {
"de": {
"type": "string",
"analyzer": "german"
},
"en": {
"type": "string",
"analyzer": "english"
},
"fr": {
"type": "string",
"analyzer": "french"
},
"es": {
"type": "string",
"analyzer": "spanish"
}
}
}
}
}
}
}
当我尝试插入一些数据:
POST http://localhost:9200/movies/movie/1
{
"title.en" :"abc123"
}
我收到以下错误:
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[IQ7CUTp][127.0.0.1:9300][indices:data/write/index[p]]"
}
],
"type": "illegal_argument_exception",
"reason": "[title] is defined as an object in mapping [movie] but this name is already used for a field in other types"
},
"status": 400
}
有人能指出我这里有什么问题?
答
的问题是,title
场被声明为string
和你试图访问title.en
子场一样,你会怎么做,如果title
是和object
领域。您需要像这样改变您的映射,然后它会起作用:
{
"mappings": {
"title": {
"properties": {
"title": {
"type": "object", <--- change this
"properties": { <--- and this
"de": {
"type": "string",
"analyzer": "german"
},
"en": {
"type": "string",
"analyzer": "english"
},
"fr": {
"type": "string",
"analyzer": "french"
},
"es": {
"type": "string",
"analyzer": "spanish"
}
}
}
}
}
}
}
答
正如我所看到的,您已将title
定义为type
,并将其定义为property
。 错误似乎说明了这个问题。
从您的电话后,我看到type
是电影。 你真的想要标题作为一种类型吗? 您应该为电影类型中的标题定义映射。
感谢Val和@ christinabo,您的两个解决方案都能正常工作。 基本上我想用“电影”作为类型。现在我可以发布数据。 \t \t 现在我想测试,如果我的分析仪确实对不同的语言,我怎么能做到这一点。 现在它接受我发布并返回搜索的白名单。 – SSG
在字段中设置英文分析器不会阻止您将西班牙文内容编入索引,因此ES会接受您在所有语言特定字段中发布的任何字符串。最好的测试方法是打开[分析API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html),看看你的文本是如何分析的。 – Val
用'spanish'分析器分析'summer'没有什么不对。 ES不会翻译你的内容。 – Val