Elasticsearch for multiple language support

情到浓时终转凉″ 提交于 2020-07-11 06:10:28

问题


I am using elasticsearch 5.1.1. I have a requirement where in I want to index data in multiple languages.

I used following mapping:

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"
            }
          }
        }
      }
    }
  }
}

when I try to insert some data as :

POST http://localhost:9200/movies/movie/1

{
"title.en" :"abc123"
}

I am getting following error:

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

Can someone point me what is wrong here?


回答1:


The problem is that the title field is declared as a string and you're trying to access the title.en sub-field like you would do if title was and object field. You need to change your mapping like this instead and then it will work:

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



回答2:


As I can see, you have defined the title both as a type and as a property. The error seems to state this issue.

From your post call, I see that the type is the movie. Do you really want the title as a type? You should define the mapping for the title inside the movie type.



来源:https://stackoverflow.com/questions/43870172/elasticsearch-for-multiple-language-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!