Elasticsearch支持多语言

时间:2017-05-09 12:43:52

标签: elasticsearch spring-data-elasticsearch

我正在使用elasticsearch 5.1.1。 我有一个要求,我想用多种语言索引数据。

我使用了以下映射:

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
}

有人能指出我这里有什么问题吗?

2 个答案:

答案 0 :(得分:0)

正如我所看到的,您已将title定义为typeproperty。 该错误似乎表明了这个问题。

在通话后,我看到type电影。 你真的想要标题作为一种类型吗? 您应该在影片类型中定义标题的映射。

答案 1 :(得分:0)

问题在于,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"
            }
          }
        }
      }
    }
  }
}