简体   繁体   English

弹性搜索:在数组上添加一个元素

[英]Elastic Search: Adding an element on an array

I am trying to batch update documents on the elastic search index.我正在尝试批量更新弹性搜索索引上的文档。 I want to know how can I achieve this scenario.我想知道如何实现这种情况。

  1. I have to create document if no document of that primaryKey exist.如果不存在该主键的文档,我必须创建文档。
  2. I have to add the data in the array of the document if the primary key exist.如果主键存在,我必须将数据添加到文档的数组中。

For example -例如 -

For initial write / if primary key not present.对于初始写入/如果主键不存在。

Document written =文档编写 =

{
  PrimaryKey,
  DataList: [
    {
      DataField1: fieldValue1,
      DataField2: fieldValue2,
    }
  ]
}

if the document was present, the entry would have been appended to the list如果文件存在,该条目将被附加到列表中

{
  PrimaryKey,
  DataList: [
    {
      DataField1: fieldValue1,
      DataField2: fieldValue2,
    },
    {
      DataField1: fieldValue3,
      DataField2: fieldValue4
    }
    ....
  ]
}

In a batch update both types of primaryKeys may be present one which have document already present in the index, some document which was never added to the index.在批量更新中,可能会出现两种类型的主键,一种是索引中已经存在文档,另一种是从未添加到索引中的文档。

I think this example can serve as a basis for your bulk.我觉得这个例子可以作为你散装的基础。 What I did was to consider that the _id and PrimaryKey are the same because the way to know if the docmentos exists is through the _id, if it doesn't exist a new document is created.我所做的是考虑 _id 和 PrimaryKey 是相同的,因为知道 docmentos 是否存在的方法是通过 _id,如果它不存在,则会创建一个新文档。 I used the script to add items to the list if it already exists.我使用脚本将项目添加到列表中(如果它已经存在)。

Read more about Update API upsert parameter.阅读有关更新 API upsert 参数的更多信息。

Mapping测绘

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "PrimaryKey": {
        "type": "keyword"
      },
      "DataField1": {
        "type": "nested"
      }
    }
  }
}

POST my-index-000001/_doc/1
{
  "PrimaryKeyame": 1,
  "DataList": [
    {
      "DataField1": "fieldValue1",
      "DataField2": "fieldValue2"
    }
  ]
}

Bulk will add items to doc 1 and create the new document 2 (this does not exist in the index). Bulk 会将项目添加到 doc 1 并创建新文档 2(索引中不存在)。

POST _bulk
{ "update" : { "_id" : "1", "_index" : "my-index-000001", "retry_on_conflict" : 3} }
{ "script" : { "source": "if (ctx._source.PrimaryKeyame != null) { ctx._source.DataList.addAll(params.DataList); }", "lang" : "painless", "params": { "PrimaryKeyame": "1", "DataList": [{"DataField1": "fieldValue3","DataField2": "fieldValue4"}]}}, "upsert" : {"PrimaryKeyame": "1", "DataList": [{"DataField1": "fieldValue3","DataField2": "fieldValue4"}]}}
{ "update" : { "_id" : "2", "_index" : "my-index-000001", "retry_on_conflict" : 3} }
{ "script" : { "source": "if (ctx._source.PrimaryKeyame != null) { ctx._source.DataList.addAll(params.DataList); }", "lang" : "painless", "params": { "PrimaryKeyame": "2", "DataList": [{"DataField1": "fieldValue3","DataField2": "fieldValue4"}]}}, "upsert" : {"PrimaryKeyame": "2", "DataList": [{"DataField1": "fieldValue3","DataField2": "fieldValue4"}]}}

Get Documents:获取文件:

"hits": [
      {
        "_index": "my-index-000001",
        "_id": "1",
        "_score": 1,
        "_source": {
          "PrimaryKeyame": 1,
          "DataList": [
            {
              "DataField1": "fieldValue1",
              "DataField2": "fieldValue2"
            },
            {
              "DataField2": "fieldValue4",
              "DataField1": "fieldValue3"
            }
          ]
        }
      },
      {
        "_index": "my-index-000001",
        "_id": "2",
        "_score": 1,
        "_source": {
          "PrimaryKeyame": "2",
          "DataList": [
            {
              "DataField1": "fieldValue3",
              "DataField2": "fieldValue4"
            }
          ]
        }
      }
    ]

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

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