【问题标题】:elasticsearch java api putmapping from json file errorelasticsearch java api putmapping来自json文件错误
【发布时间】:2015-02-10 05:13:54
【问题描述】:

我正在尝试使用 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();

【问题讨论】:

    标签: java json api elasticsearch


    【解决方案1】:

    尝试以下方法:

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

    【讨论】:

    • 您是否建议我必须在创建索引时创建映射?问题是 json 字符串/格式抛出错误。
    • 是的,上面就是这样做的
    • 这不是问题。如果您使用 XContentBuilder,则在创建索引后添加映射是可行的。
    • 感谢 cmets,但我认为您不理解这个问题。使用字符串映射失败,并出现错误“无法生成简化的映射定义”。使用 XContentBuilder 创建的相同映射成功。
    【解决方案2】:

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

    【讨论】:

    • 除了上述解决方案之外,您是否找到任何具体的解决方法?
    【解决方案3】:

    您可以为此使用杰克逊图书馆。此示例使用 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>
        ...
    

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 2017-03-25
      • 1970-01-01
      • 2015-11-09
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 2017-01-01
      相关资源
      最近更新 更多