2017-06-06 74 views
0

兩年前有人問了how to do upserts when you don't know a document's id。 (未被接受)的答案引用了feature request ,這導致了_update_by_query API基於查詢的Elasticsearch upsert

但是,如果沒有匹配存在,_update_by_query不允許插入,所以它不是真正的upsert,而只是另一種更新方式。

有沒有辦法做一個upsert沒有_id呢?我知道我的查詢將始終返回一個或零個結果。還是我不得不做多個請求(並保持自己的獨特性)?

回答

0

現在看起來不可能。 _update提供了一個upsert屬性,但這不適用於_update_by_query。以下只是給你一個錯誤Unknown key for a START_OBJECT in [upsert].

POST website/doc/_update_by_query?conflicts=proceed 
{ 
    "query": { 
    "term": { 
     "url": "http://foo.com" 
    } 
    }, 
    "script": { 
    "inline": "ctx._source.views+=1", 
    "lang": "painless" 
    }, 
    "upsert": { 
     "views": 1, 
     "url": "http://foo.com" 
    } 
}