2017-05-09 87 views
0

我正在使用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 
} 

有人能指出我這裏有什麼問題?

回答

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

感謝Val和@ christinabo,您的兩個解決方案都能正常工作。 基本上我想用「電影」作爲類型。現在我可以發佈數據。 \t \t 現在我想測試,如果我的分析儀確實對不同的語言,我怎麼能做到這一點。 現在它接受我發佈並返回搜索的白名單。 – SSG

+0

在字段中設置英文分析器不會阻止您將西班牙文內容編入索引,因此ES會接受您在所有語言特定字段中發佈的任何字符串。最好的測試方法是打開[分析API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html),看看你的文本是如何分析的。 – Val

+0

用'spanish'分析器分析'summer'沒有什麼不對。 ES不會翻譯你的內容。 – Val

0

正如我所看到的,您已將title定義爲type,並將其定義爲property。 錯誤似乎說明了這個問題。

從您的電話後,我看到type電影。 你真的想要標題作爲一種類型嗎? 您應該爲電影類型中的標題定義映射。