【问题标题】:How to update the Elasticsearch document with Python?如何使用 Python 更新 Elasticsearch 文档?
【发布时间】:2022-04-25 09:15:40
【问题描述】:

我正在使用下面的代码向 Elasticsearch 添加数据:

from elasticsearch import Elasticsearch
es = Elasticsearch()
es.cluster.health()
records = [
    {'Name': 'Dr. Christopher DeSimone', 'Specialised and Location': 'Health'},
    {'Name': 'Dr. Tajwar Aamir (Aamir)', 'Specialised and Location': 'Health'},
    {'Name': 'Dr. Bernard M. Aaron', 'Specialised and Location': 'Health'}
]
es.indices.create(index='my-index_1', ignore=400)
    
for record in records:
    # es.indices.update(index="my-index_1", body=record)
    es.index(index="my-index_1", body=record)
    
    # Retrieve the data
    es.search(index='my-index_1')['hits']['hits']

但是如何更新文档呢?

records = [
    {'Name': 'Dr. Messi', 'Specialised and Location': 'Health'},
    {'Name': 'Dr. Christiano', 'Specialised and Location': 'Health'},
    {'Name': 'Dr. Bernard M. Aaron', 'Specialised and Location': 'Health'}
]

这里Dr. Messi, Dr. Christiano 必须更新索引,Dr. Bernard M. Aaron 不应更新,因为它已经存在于索引中。

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    在 Elasticsearch 中,当数据在没有提供自定义 ID 的情况下被索引时,Elasticsearch 将为您索引的每个文档创建一个新 ID。

    因此,由于您没有提供 ID,Elasticsearch 会自动生成它。

    但您还想检查Name 是否已经存在。有两种方法:

    1. 索引数据而不为每个文档传递_id。在此之后,您必须使用 Name 字段搜索以查看文档是否存在。
    2. 为每个文档使用您自己的_id 索引数据。然后用_id搜索。

    我将演示创建我们自己的 ID 的第二种方法。由于您正在搜索Name 字段,我将使用 MD5 对其进行散列以生成_id。 (任何哈希函数都可以工作。)

    第一个索引数据:

    import hashlib
    from elasticsearch import Elasticsearch
    es = Elasticsearch()
    es.cluster.health()
    records = [
        {'Name': 'Dr. Christopher DeSimone', 'Specialised and Location': 'Health'},
        {'Name': 'Dr. Tajwar Aamir (Aamir)', 'Specialised and Location': 'Health'},
        {'Name': 'Dr. Bernard M. Aaron', 'Specialised and Location': 'Health'}
    ]
    
    index_name="my-index_1"
    es.indices.create(index=index_name, ignore=400)
    
    for record in records:
        #es.indices.update(index="my-index_1", body=record)
        es.index(index=index_name, body=record,id=hashlib.md5(record['Name'].encode()).hexdigest())
    

    输出:

    [{'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '1164c423bc4e2fcb75697c3031af9ef1',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Christopher DeSimone',
       'Specialised and Location': 'Health'}},
     {'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '672ae14197a135c39eab759be8b0597f',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Tajwar Aamir (Aamir)',
       'Specialised and Location': 'Health'}},
     {'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '85702447f9e9ea010054eaf0555ce79c',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Bernard M. Aaron',
       'Specialised and Location': 'Health'}}]
    

    下一步:索引新数据

    records = [
        {'Name': 'Dr. Messi', 'Specialised and Location': 'Health'},
        {'Name': 'Dr. Christiano', 'Specialised and Location': 'Health'},
        {'Name': 'Dr. Bernard M. Aaron', 'Specialised and Location': 'Health'}]
    
    for record in records:
        try:
            es.get(index=index_name, id=hashlib.md5(record['Name'].encode()).hexdigest())
        except NotFoundError:
            print("Record Not found")
            es.index(index=index_name, body=record,id=hashlib.md5(record['Name'].encode()).hexdigest())
    
    

    输出:

    [{'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '1164c423bc4e2fcb75697c3031af9ef1',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Christopher DeSimone',
       'Specialised and Location': 'Health'}},
     {'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '672ae14197a135c39eab759be8b0597f',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Tajwar Aamir (Aamir)',
       'Specialised and Location': 'Health'}},
     {'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '85702447f9e9ea010054eaf0555ce79c',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Bernard M. Aaron',
       'Specialised and Location': 'Health'}},
     {'_index': 'my-index_1',
      '_type': '_doc',
      '_id': 'e2e0f463145568471097ff027b18b40d',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Messi', 'Specialised and Location': 'Health'}},
     {'_index': 'my-index_1',
      '_type': '_doc',
      '_id': '23bb4f1a3a41efe7f4cab8a80d766708',
      '_score': 1.0,
      '_source': {'Name': 'Dr. Christiano', 'Specialised and Location': 'Health'}}]
    

    如您所见,Dr. Bernard M. Aaron 记录没有被索引,因为它已经存在

    【讨论】:

    • 我相信,由于 OP 还希望更新现有文档,因此对所有文档调用 index 可能更正确,无论它们是否已经存在于索引中。如果 ID 已存在,Elastic 将自动对文档执行更新。否则几乎是正确的。
    • OP提供的场景让我采取了这种方法。否则,正如您所说,索引所有文档
    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2016-05-21
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多