【问题标题】:How put index with mapping template to elastic search with elastic4s?如何将带有映射模板的索引放入带有elastic4s的弹性搜索中?
【发布时间】:2016-06-25 01:35:10
【问题描述】:

我想用动态模板创建索引并关闭对字符串字段的分析。我已经为弹性搜索创建了查询,但是如何将其转换为 elastic4s 语句? (首选 elastic4s 1.3.x 版本)

声明是:

PUT /myIndex
{
    "mappings": {
        "myType": {
            "dynamic_templates": [
                {
                    "templateName": {
                        "match":              "*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "type":           "string",
                            "index" : "not_analyzed",
                            "omit_norms" : true
                    }
                  }
                }
            ]
}}}

附言

也许可以通过执行这个“原始”请求来创建这个索引,但是我没有找到如何使用 elastic4s 1.3.4 来创建这个索引:(

【问题讨论】:

    标签: scala elasticsearch elastic4s


    【解决方案1】:

    Elastic4s(从 1.5.4 开始)在创建索引时支持动态模板。所以你可以这样做:

     val req = create.index("my_index").mappings(
        "my_type" templates (
          template name "es" matching "*_es" matchMappingType "string" mapping {
            field withType StringType analyzer SpanishLanguageAnalyzer
          },
          template name "en" matching "*" matchMappingType "string" mapping {
            field withType StringType analyzer EnglishLanguageAnalyzer
          }
        )
      )
    

    因此,您发布的示例相当于:

      create.index("my_index").mappings(
        "my_type" templates (
          template name "templateName" matching "*" matchMappingType "string" mapping {
            field typed StringType index NotAnalyzed omitNorms true
          }
      )
    

    【讨论】:

      【解决方案2】:

      有时在原始 JSON 中管理映射会更容易。您可以将原始 JSON 放在一个文件中,以便在无需重新构建应用程序的情况下进行更新。如果你想使用这个原始 JSON 来创建索引,你可以这样做:

      client.execute {
          create index "myIndex" source rawMapping
      }
      

      rawMapping 是包含原始 JSON 内容的字符串。

      【讨论】:

        猜你喜欢
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 2023-03-02
        • 1970-01-01
        • 2015-12-28
        相关资源
        最近更新 更多