如何使用JEST

时间:2016-04-20 14:35:20

标签: java elasticsearch jest

我正在尝试使用特定的分析器和映射在ES中创建索引,使用JEST。 我使用以下代码:

 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"
        }
      }
    }
  }
}

虽然索引使用指定的“设置”正确装箱,但“映射”部分不起作用,我无法设置字段“名称”的映射。有人有想法吗? JESt中是否有一种putmapping()允许您添加映射?理想情况下,我希望能够动态设置field_name而不是.json文件。

Thnks

1 个答案:

答案 0 :(得分:0)

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

    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());
    }
相关问题