使用 java api 配置 elasticsearch 映射 [英] Configure elasticsearch mapping with java api

查看:65
本文介绍了使用 java api 配置 elasticsearch 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些在索引之前不想分析的 elasticsearch 字段.我已经读到正确的方法是改变索引映射.现在我的映射如下所示:

I have a few elasticsearch fields that I don't want to analyze before indexing. I have read that the right way to do this is by altering the index mapping. Right now my mapping looks like this:

{
  "test" : {
   "general" : {
      "properties" : {
        "message" : {
          "type" : "string"
        },
        "source" : {
          "type" : "string"
        }
      }
    }
  }
}

我希望它看起来像这样:

And I would like it to look like this:

{
  "test" : {
   "general" : {
      "properties" : {
        "message" : {
          "type" : "string",
          "index" : "not_analyzed"
        },
        "source" : {
          "type" : "string"
        }
      }
    }
  }
}

我一直在尝试通过

client.admin().indices().prepareCreate("test")
        .setSettings(getGrantSettings());

getGrantSettings() 如下所示:

Where getGrantSettings() looks like:

static Settings getGrantSettings(){
    JSONObject settingSource = new JSONObject();
    try{
        settingSource.put("mapping", new JSONObject()
        .put("message", new JSONObject()
            .put("type", "string")
            .put("index", "not_analyzed")
        ));
    } catch (JSONException e){
        e.printStackTrace();
    }


    Settings set = ImmutableSettings.settingsBuilder()
            .loadFromSource(settingSource.toString()).build();
    return set;
}

推荐答案

我已经成功地使用 Java API 将映射应用到 Elasticsearch 索引,如下所示:

I have successfully applied mappings to an Elasticsearch index using the Java API like the following:

 XContentBuilder mapping = jsonBuilder()
                              .startObject()
                                   .startObject("general")
                                        .startObject("properties")
                                            .startObject("message")
                                                .field("type", "string")
                                                .field("index", "not_analyzed")
                                             .endObject()
                                             .startObject("source")
                                                .field("type","string")
                                             .endObject()
                                        .endObject()
                                    .endObject()
                                 .endObject();

  PutMappingResponse putMappingResponse = client.admin().indices()
                .preparePutMapping("test")
                .setType("general")
                .setSource(mapping)
                .execute().actionGet();

希望这会有所帮助.

这篇关于使用 java api 配置 elasticsearch 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆