簡體   English   中英

Elasticsearch中具有順序ID的批量索引數據

[英]Bulk Index data in Elasticsearch with sequential IDs

我正在使用此代碼使用python在Elasticsearch中批量索引所有數據:

from elasticsearch import Elasticsearch, helpers
import json
import os
import sys
import sys, json

es = Elasticsearch()   

def load_json(directory):
    for filename in os.listdir(directory):
        if filename.endswith('.json'):
            with open(filename,'r') as open_file:
                yield json.load(open_file)

helpers.bulk(es, load_json(sys.argv[1]), index='v1_resume', doc_type='candidate')

我知道,如果沒有提到ID,ES本身會給出一個20個字符長的ID,但是我希望它從ID = 1開始直到文檔數被索引。

我該如何實現?

在彈性的搜索,如果你不挑ID為您的文檔的ID將自動為您創建,檢查這里的彈性文檔

Autogenerated IDs are 20 character long, URL-safe, Base64-encoded GUID 
strings. These GUIDs are generated from a modified FlakeID scheme which 
allows multiple nodes to be generating unique IDs in parallel with 
essentially zero chance of collision.

如果您想擁有自定義ID,則需要使用類似的語法自行構建它們:

[
    {'_id': 1,
     '_index': 'index-name',
     '_type': 'document',
     '_source': {
          "title": "Hello World!",
          "body": "..."}

    },
    {'_id': 2,
     '_index': 'index-name',
     '_type': 'document',
     '_source': {
          "title": "Hello World!",
          "body": "..."}
    }
]

helpers.bulk(es, load_json(sys.argv[1])

由於您要在schematypeindex進行貼圖,因此不必在helpers.bulk()方法中進行操作。 您需要更改'load_json'的輸出以創建包含要保存在es中的字典(如上)的列表( python elastic client docs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM