index_documents.py 876 B

1234567891011121314151617181920212223242526272829
  1. """
  2. Example script to index elasticsearch documents.
  3. """
  4. import argparse
  5. import json
  6. from elasticsearch import Elasticsearch
  7. from elasticsearch.helpers import bulk
  8. def load_dataset(path):
  9. with open(path) as f:
  10. return [json.loads(line) for line in f]
  11. def main(args):
  12. host = '192.168.20.69:30920'
  13. es = Elasticsearch([host])
  14. # 往es索引写数据,docs是一个json集合数据集
  15. docs = load_dataset(args.data)
  16. bulk(es, docs, index=args.index_name, chunk_size=1000, request_timeout=120)
  17. if __name__ == '__main__':
  18. parser = argparse.ArgumentParser(description='Indexing elasticsearch documents.')
  19. parser.add_argument('--data', default='documents.jsonl', help='Elasticsearch documents.')
  20. parser.add_argument('--index_name', default='fault_meter', help='Elasticsearch index name.')
  21. args = parser.parse_args()
  22. main(args)