2011-09-13 58 views
2

比方說,我們在ElasticSearch中有以下映射。ElasticSearch中的多語言查詢

{ 
    "content": { 
    "properties": { 
     "id": { 
     "type": "string", 
     "index": "not_analyzed", 
     "store": "yes" 
     }, 
     "locale_container": { 
     "type": "object", 
     "properties": { 
      "english": { 
      "type": "object", 
      "properties": { 
       "title": { 
       "type": "string", 
       "index_analyzer": "english", 
       "search_analyzer": "english", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       }, 
       "text": { 
       "type": "string", 
       "index_analyzer": "english", 
       "search_analyzer": "english", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       } 
      } 
      }, 
      "german": { 
      "type": "object", 
      "properties": { 
       "title": { 
       "type": "string", 
       "index_analyzer": "german", 
       "search_analyzer": "german", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       }, 
       "text": { 
       "type": "string", 
       "index_analyzer": "german", 
       "search_analyzer": "german", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       } 
      } 
      }, 
      "russian": { 
      "type": "object", 
      "properties": { 
       "title": { 
       "type": "string", 
       "index_analyzer": "russian", 
       "search_analyzer": "russian", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       }, 
       "text": { 
       "type": "string", 
       "index_analyzer": "russian", 
       "search_analyzer": "russian", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       } 
      } 
      }, 
      "italian": { 
      "type": "object", 
      "properties": { 
       "title": { 
       "type": "string", 
       "index_analyzer": "italian", 
       "search_analyzer": "italian", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       }, 
       "text": { 
       "type": "string", 
       "index_analyzer": "italian", 
       "search_analyzer": "italian", 
       "index": "analyzed", 
       "term_vector": "with_positions_offsets", 
       "store": "yes" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

當特定用戶查詢索引時,我們可以從她的設置中獲取她的文化,即我們知道使用哪個分析器。我們怎樣才能制定一個查詢,它將以她自己的語言(比如德語)只搜索「標題」和「文本」字段,並使用德語分析器來標記搜索查詢?

+0

這樣的映射肯定有可能,但是您是否考慮過爲每種語言分別編制索引?這將減少配置中的重複並使添加新語言變得更容易。 –

+0

其實我有。但對我來說,這樣做更有意義。這是一個非常簡化的映射。實際上,一個文檔中有很少依賴於語言的字段,還有很多與語言無關的字段。 –

回答

1

我簡化了示例,使用'英語'的standard分析器和'法語'的simple(不停止)。對於這樣的文件:

{ 
    id: "abc", 
    locale_container: { 
    english: { 
     title: "abc to ABC", 
     text: "" 
    }, 
    french: { 
     title: "def to DEF", 
     text: "" 
    } 
    } 
} 

下面的查詢做的伎倆:

  • locale_container.english.title:abc - >返回文檔
  • locale_container.french.title:def - >返回文檔以及
  • locale_container.english.title:to - >沒有按」 t返回任何內容,因爲'to'是一個停用詞
  • locale_container.french.title:to - >返回文檔