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"
}
}
}
}
}
}
}
}
當特定用戶查詢索引時,我們可以從她的設置中獲取她的文化,即我們知道使用哪個分析器。我們怎樣才能制定一個查詢,它將以她自己的語言(比如德語)只搜索「標題」和「文本」字段,並使用德語分析器來標記搜索查詢?
這樣的映射肯定有可能,但是您是否考慮過爲每種語言分別編制索引?這將減少配置中的重複並使添加新語言變得更容易。 –
其實我有。但對我來說,這樣做更有意義。這是一個非常簡化的映射。實際上,一個文檔中有很少依賴於語言的字段,還有很多與語言無關的字段。 –