来自json文件错误的elasticsearch java api putmapping [英] elasticsearch java api putmapping from json file error

查看:73
本文介绍了来自json文件错误的elasticsearch java api putmapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the Elasticsearch java api to dynamically create mappings. This is important because I don't want to have to change compiled code to change the mapping.

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

Almost all of the examples out there are using XContentBuilder to do this, but I want to use a json string from a file.

代码:

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 抛出的错误:

Error thrown from Elasticsearch PutMappingRequest.class:

failed to generate simplified mapping definition

使用 XContentbuilder 定义的相同 json 完美运行.

The same json defined using XContentbuilder works perfectly.

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

推荐答案

我无法使用 XContentBuilder 以外的任何其他工具使其工作.我决定使用 Jackson 将 json 转换为映射,然后使用 XContentFactory.jsonBuilder() 映射对象.然后我将 XContentBuilder 直接传递给 putMapping 调用.

I couldn't ever get it to work using anything other than an XContentBuilder. I decided to convert the json to a map using Jackson, then map the object using the XContentFactory.jsonBuilder(). I then pass the XContentBuilder directly to the putMapping call.

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

这篇关于来自json文件错误的elasticsearch java api putmapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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