美文网首页
Elasticsearch 学习笔记

Elasticsearch 学习笔记

作者: 尼罗河上的小杀手 | 来源:发表于2017-10-18 01:21 被阅读30次
  • 用python在 elasticsearch 里面批量找出 我想要的某一个字段,方法跟sql 感觉完全不一样。 elasticsearch 有个专门的helper

Helpers:
Collection of simple helper functions that abstract some specifics or the raw API.

这个是定义。

  • 我这次主要用到它其中的一个叫scan的东西。
    具体的需求的是,我需要到elasticsearch 里的一个document 取一个字段的值 变成哈希 放到一个list中。
import hashlib
from hashlib import sha1
from binascii import hexlify
from elasticsearch import Elasticsearch, helpers

ESURL = 'localhost:9211'

def build_listing_hash(desc):  # 哈希函数
    return hexlify(sha1((desc).encode("UTF-8")).digest())
desc_result = []


def hello():
    desc = 'body'
    res = helpers.scan(client=es,
                       scroll='2m',
                       size=10,
                       query={"query": {"match_all": {}}, "_source": [desc]},
                       index='dump2_parser',
                       doc_type='document_name')
    for val in res:
        result = build_listing_hash(val['_source'].get(desc, None) or undefined)
        job_id = val['_id']
        # 这个document的哈希和job_id 变成一个tulp ,append到一个list中
        desc_result.append((result, job_id)) 

相关文章

网友评论

      本文标题:Elasticsearch 学习笔记

      本文链接:https://www.haomeiwen.com/subject/qrdhsxtx.html