繁体   English   中英

Elasticsearch Java API使用映射创建索引失败

[英]Elasticsearch Java API Create Index with Mapping fails

我正在尝试利用java api在Elasticsearch 7中创建新索引。 我可以创建一个新的索引,当我尝试使用映射创建它时,或者尝试按照文档后的事实添加映射:

添加映射

使用映射创建索引

当我只是创建一个索引时,这很好用

public boolean createIndex(RestHighLevelClient client, String indexName) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);

    //no options just straight forward
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    return response.isAcknowledged();
}

但是,添加request.mapping(这个例子来自网页)打破了它?

request.mapping(
        "{\n" +
        "  \"properties\": {\n" +
        "    \"firstName\": {\n" +
        "      \"type\": \"text\"\n" +
        "    }\n" +
        "  }\n" +
        "}", 
        XContentType.JSON);

即使我尝试使用putMapping应用映射之后它也会破坏它

public boolean createMappingOnIndex(RestHighLevelClient client, String indexName, String mapping) throws IOException {
    PutMappingRequest request = new PutMappingRequest(indexName);

    //instead of using my own, using the example from docs to simplify, still not working
    request.source(
       "{\n" +
       "  \"properties\": {\n" +
       "    \"firstName\": {\n" +
       "      \"type\": \"text\"\n" +
       "    }\n" +
       "  }\n" +
       "}", 
     XContentType.JSON);

    AcknowledgedResponse response = client.indices(). putMapping(request, RequestOptions.DEFAULT);
    return response.isAcknowledged();
}

我得到的错误

java.lang.IllegalStateException: Failed to close the XContentBuilder
     at org.elasticsearch.common.xcontent.XContentBuilder.close
caused by: java.io.IOException: Unclosed Object or array found
     at org.elasticsearch.common.xcontent.json.JsonXContentGenerator.close(JsonXContentGenerator.java ###)

我已经尝试使用Hashmap实现而不是字符串版本,虽然一旦它进入es字节,它似乎是相同的事情。 这很奇怪,因为无论我使用像Gson这样的东西,还是只写一个转义的字符串示例,请求对象都会在内部进行转换(我认为),然后弹性是否会产生它所创建的格式的问题?

我应该提到这都在Spring Maven上下文中,索引/文档的创建/插入是从singleton bean完成的。 虽然我找不到任何迹象表明这是罪魁祸首? 当我创建一个没有附加映射的索引时,它工作正常。

一如既往地非常感谢任何帮助。

回答我自己的道歉,但万一其他人遇到这个问题:以上所有都很好,这就是我的RestHighLevelClient从Spring bean返回的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM