【问题标题】:How to add mapping in ElasticSearch using JEST如何使用 JEST 在 ElasticSearch 中添加映射
【发布时间】:2016-08-13 07:55:54
【问题描述】:

我正在尝试使用 JEST 在具有特定分析器和映射的 ES 中创建索引。

我正在使用以下代码:

 CreateIndex createIndex =  new CreateIndex.Builder(indexName)
    .settings(
            ImmutableSettings.builder()
                    .loadFromClasspath(
                            "jestconfiguration.json"
                    ).build().getAsMap()
    ).build();
    
    JestResult result = client.execute(createIndex);

这是 jestconfiguration.java

{
  "settings": {
    "analysis": {
      "analyzer": {
        "second": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonym"
          ]
        }
      },
      "filter": {
        "synonym" : {
            "type" : "synonym",
            "synonyms" : [
                "smart phone => smartphone"
                ]             
                    }
                }
        }
  },
    "mappings": {
    "index_type": {
      "properties": {
        "Name": {
          "type": "string",
          "analyzer": "second"
        }
      }
    }
  }
}

虽然使用指定的“设置”正确创建了索引,但“映射”部分不起作用,我无法为字段“名称”设置映射。有人有想法吗?

是否有一种 putmapping() 开玩笑可以让您添加映射?理想情况下,我希望能够动态设置 field_name 而不是在 .json 文件中。

【问题讨论】:

    标签: java elasticsearch elasticsearch-jest


    【解决方案1】:

    我在尝试查看是否可以一次性创建索引和映射时发现了您的问题。我最终只是创建了第二个创建映射的请求。

        String mappingJson = new String(ByteStreams.toByteArray(mappingFile.getInputStream()));
        boolean indexExists = client.execute(new IndicesExists.Builder(configuration.getIndex()).build()).isSucceeded();
        if (indexExists) {
            logger.info("Updating elasticsearch type mapping.");
            client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build());
        } else {
            logger.info("Creating elasticsearch index and type mapping.");
            client.execute(new CreateIndex.Builder(configuration.getIndex()).build());
            client.execute(new PutMapping.Builder(configuration.getIndex(), configuration.getBaseType(), mappingJson).build());
        }
    

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2017-07-29
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多