2017-08-10 41 views
0

我用戶存儲到Elasticsearch爲:如何處理以下/追隨者Elasticsearch

{ 
"name": "Alice", 
"following": ["Bob", "Charlie", etc...] 
} 

當我讀回,我想獲得:

{ 
"name": "Alice", 
"following": ["Bob", "Charlie", etc...], 
"followers": X 
} 

,其中X應該是跟隨Alice的其他用戶的數量。

回答

0

使用此Python腳本:

from elasticsearch import client, helpers 

es = client.Elasticsearch() 

INDEX='service' 
TYPE='user' 
PARAM='following' 

def _query(entries): 
    params = [dict(match={PARAM: entry}) for entry in entries] 
    return dict(size=0, query={'bool': {'should': params}}) 

def _result(query): 
    result = es.search(index=INDEX, body=query) 
    return result['hits']['total'] 

def _action(entry, result): 
    return { 
     '_op_type': 'update', 
     '_id': entry, 
     'script': { 
      'inline': 'ctx._source.followers = params.count', 
      'params': { 
       'count': result 
      } 
     } 
    } 

def _update(): 
    updates = entries = 0 

    scan = helpers.scan(es, index=INDEX, doc_type=TYPE) 
    for doc in scan: 
     user = doc['_id'] 
     src = doc['_source'] 
     count = src.get('followers', 0) 

     query = _query([user]) 
     result = _result(query) 
     if result != count: 
      updates += 1 
      yield _action(user, result) 

     entries += 1 
    print 'entries:', entries 
    print 'updates:', updates 

def run(): 
    helpers.bulk(es, _update(), index=INDEX, doc_type=TYPE) 

if __name__ == '__main__': 
    run()