2016-12-14 68 views
0

我在節點環境中使用elasticsearch。 這是我的數據模型:elasticsearch js api upsert與多個字段匹配

"_source": { 
    "repo_id": "ecom-test", 
    "pr_id": "1", 
    "pr_status": "approved", 
    "date_created": "2016-12-14T12:55:50.317Z", 
    "date_modified": "2016-12-14T12:55:50.317Z", 
    "comments": [ 
     { 
     "tuple": { 
      "reviewee": "Max", 
      "reviewer": "Vic" 
     }, 
     "nb": "1", 
     "type": "typo" 
     }, 
     { 
     "tuple": { 
      "reviewee": "Antoine", 
      "reviewer": "Vic" 
     }, 
     "nb": "2", 
     "type": "logic" 
     } 
    ] 
} 

我添加使用下面的代碼註釋:

client.update({ 
    index: 'loreal', 
    type: 'reviews', 
    id: reviewID, 
    body: { 
     script: { 
      inline: "ctx._source.comments.add(params.comment)", 
      lang: "painless", 
      params: { 
       comment: { 
        tuple: { 
         reviewee: data.reviewee, 
         reviewer: data.reviewer 
        }, 
        nb: data.nb, 
        type: data.type 
       } 
      } 
     } 
    } 
}) 

時相同的「reviewee」,「審閱」和「邏輯」評論已經存在我可是想更新「nb」屬性而不是在「comments」數組中創建一個新項目。

我以爲我會先做一個搜索,但我找不到一種搜索方式來匹配任何具有這三個值的數組「註釋」元素。

我真的希望你們可以幫我一把,這已經很長一段時間了,我正在尋找= s。

在此先感謝

回答

0

鑑於您的文檔的結構,我會這樣定義一個嵌套對象的註釋字段:請參閱文檔例子是非常接近你的使用情況:https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects

考慮到你得到了param的reviewee值,我會使用嵌套查詢和bool查詢的組合。 https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-query.html

{ 
"query": { 
    "nested": { 
     "path": "comments", 
     "query": { 
      "bool": { 
       "must": [{ 
        "match": { 
         "comments.tuple.reviewee": "your reviewee" 
        } 
       }, { 
        "match": { 
         "comments.tuple.reviewer": "your reviewee" 
        } 
       }, { 
        "term": { 
         "comments.type": "logic" 
        } 
       }] 
      } 
     } 
    } 
} 

這裏的測試中,我寫道:

PUT test 
{ 
    "mappings": { 
    "item": { 
     "properties": { 
     "comments": { 
      "type": "nested" 
     } 
     } 
    } 
    } 
} 





POST test/item 
{ "comments": [ 
     { 
     "tuple": { 
      "reviewee": "Max", 
      "reviewer": "Max" 
     }, 
     "nb": "1", 
     "type": "logic" 
     }, 
     { 
     "tuple": { 
      "reviewee": "Antoine", 
      "reviewer": "Vic" 
     }, 
     "nb": "2", 
     "type": "logic" 
     } 
    ] 
} 

POST test/item 
{ 
    "comments": [ 
     { 
     "tuple": { 
      "reviewee": "Max", 
      "reviewer": "Max" 
     }, 
     "nb": "1", 
     "type": "test" 
     }, 
     { 
     "tuple": { 
      "reviewee": "Antoine", 
      "reviewer": "Vic" 
     }, 
     "nb": "2", 
     "type": "logic" 
     } 
    ] 
} 

POST test/item 
{ 
    "comments": [ 
     { 
     "tuple": { 
      "reviewee": "Max", 
      "reviewer": "Mac" 
     }, 
     "nb": "1", 
     "type": "logic" 
     }, 
     { 
     "tuple": { 
      "reviewee": "Antoine", 
      "reviewer": "Vic" 
     }, 
     "nb": "2", 
     "type": "logic" 
     } 
    ] 
} 

GET /test/item/_search 
{ 
"query": { 
    "nested": { 
     "path": "comments", 
     "query": { 
      "bool": { 
       "must": [{ 
        "match": { 
         "comments.tuple.reviewee": "Max" 
        } 
       }, { 
        "match": { 
         "comments.tuple.reviewer": "Max" 
        } 
       }, { 
        "term": { 
         "comments.type": "logic" 
        } 
       }] 
      } 
     } 
    } 
}} 
+0

非常感謝您的回答。不幸的是,這不是我正在尋找的東西,但它對另一個問題幫了我很大的忙。我終於決定創建一個新文檔,而不是更新前一個文檔。這樣我就不需要做複雜的更新,而且我可以保留文檔的歷史記錄。 – Mexos