0

我想创建一个(url,visits)索引,elasticsearch每次调用 function 时都会在该索引上保存特定 url 的访问次数myfunction。我怎样才能做到这一点?到目前为止,我已经设法创建了插入,但我无法增加计数器。

def myfunction():
    a = elasticsearch.index(index='test_index_1' , doc_type='test_doc_type' , id="url" , body={'data' : {'visits' : 1} , 'doc_as_upsert' : True})
    app.logger.info(a)
    b = elasticsearch.update(index='test_index_1' , doc_type='test_doc_type' , id="url" , body={
        'script' : 'ctx._source.data.visits += visit',
        'params' : {
            'visit' : 1
        }
    })
    app.logger.info(b)
4

1 回答 1

0

You should use the upsert parameter. The document with id:url should be created with a default visits:1 if it doesn’t already exist:

POST /test_index_1/test_doc_type/{id}/_update
{
   "script" : "ctx._source.visits+=1",
   "upsert": {
       "visits": 1
   }
}
于 2016-03-13T03:17:04.477 回答