elasticsearch js api upsert与多个字段匹配
问题描述:
我在节点环境中使用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。
在此先感谢
答
鉴于您的文档的结构,我会这样定义一个嵌套对象的注释字段:请参阅文档例子是非常接近你的使用情况: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"
}
}]
}
}
}
}}
非常感谢您的回答。不幸的是,这不是我正在寻找的东西,但它对另一个问题帮了我很大的忙。我终于决定创建一个新文档,而不是更新前一个文档。这样我就不需要做复杂的更新,而且我可以保留文档的历史记录。 – Mexos