elasticsearch js api upsert,多个字段匹配

时间:2016-12-14 15:01:22

标签: node.js elasticsearch

我在节点环境中使用elasticsearch。 这是我的数据模型:

"_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”,“reviewer”和“logic”已经存在时,我想更新“nb”属性,而不是在“comments”数组中创建一个新项目。

我以为我会首先进行搜索,但我找不到一种方法来进行搜索,以匹配具有这三个值的数组“注释”的任何元素。

我真的希望你们能帮我一把,这已经很久了,我正在寻找。

提前致谢

1 个答案:

答案 0 :(得分:0)

考虑到文档的结构,我会将注释字段定义为嵌套对象:请参阅与您的用例非常接近的文档示例:https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects

考虑到你在参数值中有参数,我会使用嵌套查询和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"
                    }
                }]
            }
        }
    }
}}