【问题标题】:Multilingual queries in ElasticSearchElasticSearch 中的多语言查询
【发布时间】:2011-09-13 18:31:02
【问题描述】:

假设我们在 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"
              }
            }
          }
        }
      }
    }
  }
}

当特定用户查询索引时,我们可以从她的设置中获取她的文化,即我们知道要使用哪个分析器。我们如何制定一个查询,只搜索她自己的语言(比如说德语)的“标题”和“文本”字段,并使用德语分析器对搜索查询进行标记?

【问题讨论】:

  • 这样的映射当然是可能的,但是您没有考虑过为每种语言设置单独的索引吗?它将减少配置中的重复并使添加新语言更容易。
  • 其实我有。但对我来说,这样更有意义。这是一个非常简化的映射。在实际的一个文档中,与语言相关的字段很少,而与语言无关的字段却很多。

标签: full-text-search multilingual elasticsearch


【解决方案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 -> 不返回任何内容,因为 'to' 是停用词
  • locale_container.french.title:to -> 返回文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多