0

我正在尝试利用 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 上下文中,并且索引/文档的创建/插入是从单例 bean 完成的。虽然我找不到任何迹象表明这是罪魁祸首?当我只创建一个没有附加映射的索引时,它工作正常。

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

4

1 回答 1

0

Apologies for answering my own but in case someone else runs into this: All of the above is fine, it was the way my RestHighLevelClient was being returned from Spring bean that was the issue.

于 2019-06-20T12:49:13.107 回答