elasticsearch java api putmapping from json file error

时间:2014-12-11 16:22:05

标签: java json api elasticsearch

我正在尝试使用Elasticsearch java api动态创建映射。这很重要,因为我不想更改已编译的代码来更改映射。

几乎所有的示例都使用XContentBuilder来执行此操作,但我想使用文件中的json字符串。

代码:

client.admin().indices().preparePutMapping(indexName)
    .setType("test")
    .setSource(indexMapping)
    .execute().actionGet();

文件字符串:

{
"test": {
    "dynamic": "strict",
    "_id": {
        "path": "id"
    },
    "properties": {
        "address": {
            "index_analyzer": "ip4-pattern-analyzer",
            "store": true,
            "type": "string",
            "fields": {
                "raw": {
                    "index": "not_analyzed",
                    "type": "string"
                }
            }
        }
    }
}

}

从Elasticsearch PutMappingRequest.class抛出错误:

failed to generate simplified mapping definition

使用XContentbuilder定义的相同json工作正常。

String type = "test";
XContentBuilder jb = XContentFactory.jsonBuilder().
      startObject().
         startObject(type).
            field("dynamic", "strict").
            startObject("_id").
                 field("path", "id").
            endObject().
            startObject("_all").
                 field("enabled", "true").
            endObject().
            startObject("properties").
                 startObject("address").
                    field("type", "string").
                    field("store", "yes"). 
                    field("index_analyzer", "ip4-pattern-analyzer").
                    startObject("fields").
                        startObject("raw").
                            field("type","string").
                            field("index","not_analyzed").
                        endObject().
                    endObject().
                 endObject().
            endObject().
        endObject().
    endObject();

3 个答案:

答案 0 :(得分:2)

尝试以下内容:

applicationContext.xml中的

类似于:

<bean id="indexMapping" class="org.apache.commons.io.IOUtils" factory-method="toString">
        <constructor-arg value="classpath:test.json" type="java.io.InputStream" />
</bean>

然后你可以做

   @Autowired
    private String indexMapping;
    .
    .

在索引创建期间应用映射尝试:

CreateIndexResponse indexResponse = admin.prepareCreate(indexName).setSource(indexMapping).execute().actionGet();

如果您想在之后应用映射,请尝试:

PutMappingRequest putRequest = new PutMappingRequest(indexName); 
    putRequest.source(indexMapping); 
    putRequest.type("test"); 
    try {
        PutMappingResponse response = admin.putMapping(putRequest).actionGet(); 

    } catch (Exception e) {
        log.warn("Failed to add mapping", e);
        throw new RuntimeException(e);
    }

答案 1 :(得分:0)

你可以使用杰克逊图书馆。此示例使用elasticsearchTemplate。例如:

ObjectMapper mapper = new ObjectMapper();
    URL url = this.getClass().getResource("/yourmapping.json");
    JsonNode tree = mapper.readTree(new File(url.getFile()));
    elasticsearchTemplate.putMapping("index_name", "index_type", tree.toString());

和maven依赖:

    ...
     <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.2</version>
    </dependency>
    ...

答案 2 :(得分:0)

除了XContentBuilder之外,我无法使用其他任何东西。 我决定使用Jackson将json转换为地图,然后使用XContentFactory.jsonBuilder()映射对象。然后我将XContentBuilder直接传递给putMapping调用。

public static XContentBuilder builderFromJson(String json) throws JsonParseException, JsonMappingException, IOException{
    Map<String, Object> map = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>(){});
    return XContentFactory.jsonBuilder().map(map);
}