简体   繁体   English

如何使用NEST更新ElasticSearch索引内的现有文档?

[英]How to update an existing document inside an ElasticSearch index using NEST?

im attempting to do an index update of documents within my elasticsearch index. 我正在尝试对我的Elasticsearch索引中的文档进行索引更新。

a job is run periodically during the day that identifies database records that have been updated since the last time the job ran. 作业在一天中定期运行,以标识自上次运行作业以来已更新的数据库记录。 i want to be able to update these specific records in the index. 我希望能够更新索引中的这些特定记录。 any of the fields may have changed in the record. 记录中的任何字段都可能已更改。

so i populate a dataset and then loop through the records to populate an instance of my class with all the properties from the database. 所以我填充数据集,然后遍历记录以使用数据库中的所有属性填充类的实例。

each time i want to update the corresponding record in the index or add it if it doesnt currently exist... 每次我想更新索引中的相应记录或如果它当前不存在添加它...

within my loop im trying some code like this to do the update... 在我的循环中,我正在尝试像这样的代码来进行更新...

client.Update<MyContentClass>(u => u
                .Id("AU-7zORce3_kxnyDoVLv")
                .Index("qubecontent")
                //.Doc(new MyContentClass { ESUniqueKey = MyContentClassInstance.ESUniqueKey })
                .DocAsUpsert()
                .Refresh()
                );

im not sure what Id is referencing? 我不确定ID是指什么? is this the id that elasticsearch autogenerates for each indexed record? 这是Elasticsearch为每个索引记录自动生成的ID吗? I do generate an additional unique id within my class but not sure how i reference this? 我的确在班级内生成了一个额外的唯一ID,但不确定如何引用它?

Can anyone advise how i would perform this index update for a changed record? 谁能建议我如何对更改的记录执行此索引更新?

The ID field in the upsert is indeed referencing the internal ElasticSearch ID (think of it as the primary key for ES). upsert中的ID字段确实引用了内部ElasticSearch ID(将其视为ES的主键)。 If you already have your own unique primary key you can use that as the primary key in ES as well. 如果您已经拥有自己的唯一主键,则也可以将其用作ES中的主键。 Take these examples: 请看以下示例:

Example 1: Let ES generate it's own ID 示例1:让ES生成自己的ID

POST test/type1
{
  "f1": "record 1",
  "f2": "2000-01-01"
}

Result 1: 结果1:

{
   "_index": "test",
   "_type": "type1",
   "_id": "AU--Dz-Kl6g2APRJ9y7l",
   "_version": 1,
   "created": true
}

You can see that ES generated it's own primary key of "AU--Dz-Kl6g2APRJ9y7l" 您可以看到ES生成了它自己的主键“ AU--Dz-Kl6g2APRJ9y7l”

Example 2: Epecify your own ID 示例2:确认您自己的ID

POST test/type1/thisIsMyID
{
  "f1": "record 1",
  "f2": "2000-01-01"
}

Result 2: 结果2:

{
   "_index": "test",
   "_type": "type1",
   "_id": "thisIsMyID",
   "_version": 1,
   "created": true
}

Notice how in Example 2 it used the ID that I specified. 请注意,在示例2中它如何使用我指定的ID。 Once you are using the same primary key as ES then you can run your upsert statement. 一旦使用与ES相同的主键,就可以运行upsert语句。

NOTE: If you are regenerating the whole document and what you really want to do is overwrite the old vs upsert the old. 注意:如果您要重新生成整个文档,而您真正想要做的就是覆盖旧文档,然后覆盖旧文档。 Then you can just POST again with the same ID and it will overwrite the old record with the new record. 然后,您可以再次使用相同的ID进行POST,它将用新记录覆盖旧记录。 Overwriting will be WAY faster than upserting. 覆写便一路比upserting更快。

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

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