elasticsearch Java API putmapping from JSON file错误

mi7gmzs6  于 4个月前  发布在  ElasticSearch
关注(0)|答案(3)|浏览(49)

我尝试使用Elasticsearch Java API来动态创建Map。这很重要,因为我不想更改编译代码来更改Map。
几乎所有的例子都是使用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抛出的错误:
无法生成简化的Map定义
使用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();

icomxhvb

icomxhvb1#

请尝试以下操作:
在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;
    .
    .


要在索引创建期间应用Map,请尝试:

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


如果你想在之后应用Map,请尝试:

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);
    }

oxiaedzo

oxiaedzo2#

我决定使用Jackson将json转换为map,然后使用XContentFactory.jsonBuilder()Map对象。然后将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);
}

字符串

c9qzyr3d

c9qzyr3d3#

你可以使用Jackson库。这个例子使用了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>LATEST</version>
    </dependency>
    ...

相关问题