【问题标题】:Elasticsearch Java API Create Index with Mapping failsElasticsearch Java API 创建索引与映射失败
【发布时间】:2019-06-18 15:30:26
【问题描述】:

我正在尝试利用 java api 在 Elasticsearch 7 中创建新索引。我能够很好地创建一个新索引,期望当我尝试使用映射创建它,或者尝试在每个文档之后添加映射:

add mapping

create index with mapping

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

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 完成的。虽然我找不到任何迹象表明这是罪魁祸首?当我只创建一个没有附加映射的索引时,它工作正常。

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

【问题讨论】:

  • 您是否尝试创建映射类型?如果是,则映射类型在 es 7 中已弃用/删除。
  • 不,不是类型,我相信你是正确的类型在版本 6 中已被弃用。而是索引上的映射,请参阅问题中文档的链接。
  • 好的。谢谢让我在本地设置。

标签: java elasticsearch elasticsearch-java-api elasticsearch-mapping


【解决方案1】:

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

【讨论】:

  • 嗨,我在使用 RestHighLevelClient 和 Spring 时也遇到了这些异常。你能分享一下你是如何修复它的吗?
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2018-11-22
相关资源
最近更新 更多