ElasticSearch Java Api:更新现有文档

时间:2016-03-03 18:06:09

标签: java elasticsearch groovy elasticsearch-api

我目前正在尝试通过Java API更新ElasticSearch文档。我有一个groovy脚本,代码如下:

static updateRequestById(String agencyIndex, String type, String id, def policy) {        
        UpdateRequest updateRequest = new UpdateRequest()
        updateRequest.docAsUpsert(true);
        updateRequest.parent("agentNumber");
        updateRequest.index(agencyIndex)
        updateRequest.type(type)
        updateRequest.id(id)
        updateRequest.doc("policies", policy)
        elasticsearchClient.update(updateRequest).get()
    }

我遇到的问题是我想在以下文档中更新数组:

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 10,
      "successful": 10,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "int-b-agency",
            "_type": "jacket",
            "_id": "99808.1.27.09_4644",
            "_score": 1,
            "_source": {
               "agentNumber": "99808.1.27.09",
               "fileNumber": "4644",
               "policies": [
                  {
                     "agentNumber": "99808.1.27.09",
                     "fileNumber": "4644",
                     "policyNumber": "2730609-91029084",
                     "checkNumber": "0",
                     "checkAmount": 0,
                     "createdOn": null,
                     "createdBy": "traxuser621",
                     "propertyTypeCode": "",
                     "propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
                     "ppaddress": "110 Allan Ct ",
                     "ppcity": "Jacksonville",
                     "ppstate": "FL",
                     "ppzip": "32226",
                     "ppcounty": "Duval",
                     "policytype": "",
                     "status": "Active",
                     "effectiveDate": "2015-04-01T00:00:00-05:00",
                     "formType": "BASIC OWNERS - ALTA Owners Policy 06_306_FL - FL Original Rate",
                     "rateCode": "FLOR",
                     "rateCodeDesc": "FL Original Rate",
                     "policyTypeCode": "1",
                     "policyTypeCodeDesc": "BASIC OWNERS",
                     "amount": 200000,
                     "hoiAgentNumber": "",
                     "proForma": false,
                     "pdfLocation": "\\\\10.212.61.206\\FNFCenter\\legacy_jacket_pdfs\\2015_4_FL6465\\Policy_2730609-91029084.pdf",
                     "legacyPolicy": "true",
                     "associatedPolNbr": null
                  }
               ]
            }
         }
      ]
   }
}

在上面的文档中,我有一个文档,其中包含一个名为“policies”的数组,其中包含一个对象。我希望能够使用其他对象更新“policies”数组。最终结果应如下所示:

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 10,
      "successful": 10,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "int-b-agency",
            "_type": "jacket",
            "_id": "41341.1.81.38_41340103",
            "_score": 1,
            "_source": {
               "agentNumber": "41341.1.81.38",
               "fileNumber": "41340103",
               "policies": [
                  {
                     "agentNumber": "41341.1.81.38",
                     "fileNumber": "41340103",
                     "policyNumber": "8122638-91036874",
                     "checkNumber": "0",
                     "checkAmount": 0,
                     "createdOn": null,
                     "createdBy": "traxuser621",
                     "propertyTypeCode": "",
                     "propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
                     "ppaddress": "1800 Smith St ",
                     "ppcity": "sicklerville",
                     "ppstate": "PA",
                     "ppzip": "08105",
                     "ppcounty": "Dauphin",
                     "policytype": "",
                     "status": "Active",
                     "effectiveDate": "2016-02-01T00:00:00-06:00",
                     "formType": "TestData",
                     "rateCode": "PASALERATE",
                     "rateCodeDesc": "Sale Rate - Agent",
                     "policyTypeCode": "26",
                     "policyTypeCodeDesc": "SALE OWNERS",
                     "amount": 180000,
                     "hoiAgentNumber": "",
                     "proForma": false,
                     "pdfLocation": "SomeLocation1",
                     "legacyPolicy": "true",
                     "associatedPolNbr": null
                  },
                  {
                     "agentNumber": "41341.1.81.38",
                     "fileNumber": "41340103",
                     "policyNumber": "8122638-91036875",
                     "checkNumber": "0",
                     "checkAmount": 0,
                     "createdOn": null,
                     "createdBy": "traxuser621",
                     "propertyTypeCode": "",
                     "propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
                     "ppaddress": "1800 Smith St ",
                     "ppcity": "sicklerville",
                     "ppstate": "PA",
                     "ppzip": "08105",
                     "ppcounty": "Dauphin",
                     "policytype": "",
                     "status": "Active",
                     "effectiveDate": "2016-02-01T00:00:00-06:00",
                     "formType": "Test Data",
                     "rateCode": "PASALERATE",
                     "rateCodeDesc": "Sale Rate - Agent",
                     "policyTypeCode": "26",
                     "policyTypeCodeDesc": "SALE OWNERS",
                     "amount": 180000,
                     "hoiAgentNumber": "",
                     "proForma": false,
                     "pdfLocation": "SomeLocation2",
                     "legacyPolicy": "true",
                     "associatedPolNbr": null
                  }
               ]
            }
         }
      ]
   }
}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以使用脚本更新:

  1. 将新政策放入参数中,例如policy
  2. 使用如下脚本:

    if (!ctxt._source.policies) { ctxt._source.policies = [] }
    ctxt._source.policies += policy
    
  3. 请参阅此文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

答案 1 :(得分:1)

倒排索引中的更新是文档的删除和替换。没有像在db中找到的就地更新。 ES在引擎盖下使用了Lucene,后者又实现了反向索引。