当前位置: 首页 > 知识库问答 >
问题:

ElasticSearch-Spring Boot-Upert正在抛出DocumentMissingException

贺俊杰
2023-03-14

我正在使用Spring Boot和ElasticSearch。当我试图使用Spring升级时,当ElasticSearch中没有文档时,它会抛出DocumentMissingException。当ElasticSearch中存在文档时,同样的代码也可以正常工作。

异常堆栈跟踪:

组织。springframework。数据弹性搜索。ElasticsearchException:批量索引失败。使用ElasticsearchException。getFailedDocuments()获取详细消息[{U65929AR1978SGC001748=[company/vteSxfKoRF-k4g982vissw][[company][2]]DocumentMissingException[[U doc][U65929AR1978SGC001748]:文档缺失],U45309AR2000PTC006288=[company/vteSxfKoRF-k4g982vissw][[company][3]]文档缺失[DocumentMissingException][doc][U45309AR2000PTC006288],。。。

代码:

public <S extends Company> void saveAllCustom(Iterable<S> companies) {

    List<UpdateQuery> updateQueries = new ArrayList<UpdateQuery>();

    ObjectMapper oMapper = new ObjectMapper();

    for (S company : companies) {

        Map<String, Object> companyJsonMap = (Map<String, Object>) oMapper.convertValue(company, Map.class);
    
        Map<String, Object> paramsDocument = new HashMap<String, Object>();
        paramsDocument.put("doc", companyJsonMap);
    
        Script script = new Script(
            ScriptType.INLINE
            , "painless"
            , "if (ctx._source.dataAsOf < params.doc.dataAsOf) {ctx._source = params.doc}"
            , paramsDocument);
    
        UpdateRequest updateRequest = new UpdateRequest()
            .index("company")
            .type("_doc")
            .id(company.cin)
            .script(script) // Conditional Update
            .upsert(companyJsonMap);

        UpdateQuery updateQuery = new UpdateQueryBuilder()
            .withIndexName("company")
            .withType("_doc")
            .withId(company.cin)
            .withDoUpsert(true)
            .withClass(Company.class)
            .withUpdateRequest(updateRequest)
            .build();
    
        updateQueries.add(updateQuery);
        
    }

    elasticsearchTemplate.bulkUpdate(updateQueries);
}

但类似的upsert命令也在使用CURL:

curl -X POST "localhost:9200/company/_doc/10001/_update" -H 'Content-Type: application/json' -d'
{
    "script": {
        "lang": "painless",
        "source": "ctx._source = params.doc",
        "params": {
            "doc": {
                "name": "XYZ LIMITED - updated",
                "cin": "10001"
            }
        }
    },
    "upsert" : {
        "name": "XYZ LIMITED - newly created",
        "cin": "10001"
    }
}'

根据我的理解,当ElasticSearch中没有文档时,它不应该抛出DocumentMissingException,因为我在查询中也添加了upert(...)

Elasticsearch版本:版本:6.4.3,内部版本:default/tar/fe40335/2018-10-30T23:17:19.084789Z,JVM:1.8.0_191

安装插件:无

JVM版本:java版本"1.8.0_191"Java(TM)SE运行时环境(build 1.8.0_191-b12)JavaHotSpot(TM)64位服务器VM(build 25.191-b12,混合模式

操作系统版本:Darwin Kernel Version 18.5.0: Mon Mar11 20:40:32PDT 2019; root: xnu-4903.251.3~3/RELEASE_X86_64x86_64

Spring Boot版本:2.1.7。释放

Spring Data ElasticSearch版本:3.2.0。RC2

共有1个答案

全誉
2023-03-14

Spring Data Elasticsearch目前不支持此功能

我们可以在ElasticsearchTemplate的源代码中看到,在使用脚本时不考虑upsert。

请注意,此问题已报告,但仍未解决。

 类似资料:
  • 我正在制作一个小应用程序,我可以使用spring-boot保存用户详细信息。我创建了实体及其相应的存储库。当我发出帖子请求以添加用户的用户对象的id时,该id在保存到数据时为空base.Thisid在MySQL中自动生成(自动增量)。从POST请求中,我得到3个字段,即用户名、电子邮件、密码。用户类包含字段id、用户名、电子邮件、密码。我添加了注释 作为 id 字段。构造函数是 我的用户服务类 我

  • 我使用的是SpringBoot2.2。2.发布,version 在 超时失败统计(总计=30,活动=1,空闲=29,等待=163) 有29个空闲连接,仍然发生超时故障。

  • 我试图升级骆驼cxf从2.15.0到2.18.2和得到以下异常。 JDK版本是1.8 使用SOAP服务的CXFendpoint如下 endpoint id=“OTAHotelAvailability\u OTA\u 2012B\u endpoint”uri=“cxf:/HotelAvailabilityPort?wsdlURL=${wsdlpath}/test.wsdl 下一条从CXFendpoi

  • 我有一个非常简单/基本的Web应用程序。当我跑的时候 mvn部署-e 我在控制台中看到以下错误。 [错误]无法执行目标组织。阿帕奇。专家plugins:maven deploy plugin:2.7:project TestWebApp上的部署(默认部署):部署失败:未在POM inside distributionManagement元素或-DaltDeploymentRepository=id

  • 这是我连接HTTP的代码。 这就是android。操作系统。NetworkOnMainThreadException 请帮忙。

  • 有人能告诉我为什么这个代码不起作用吗?它抛出。 错误: