如何通过弹性搜索添加不存在的嵌套文档

时间:2020-01-06 17:37:48

标签: elasticsearch-7

鉴于以下文档,我将如何以嵌套类型添加新文档。仅当文档不存在时,才应添加该文档。

{
  "mappings": {
    "properties": {
      "isn" : { "type":"text" },
      "title" : { "type":"text" },
      "article": {
        "type": "nested" 
      }
    }
  }
}

PUT journal/_doc/1
{
  "isn" : "11223344",
  "article" : [
    {
      "id" : 1,
      "title" : "first article title",
      "author" :  "John"
    },
    {
      "id" : 2,
      "title" : "second article title",
      "author" :  "Carl"
    }
  ]
}

此脚本将不断重复添加...仅当(id = 3)不存在时,如何添加。

  "script": {
    "source": "ctx._source.articles.add(params.article)",
    "params": {
      "article": {
        "id": 3,
        "title": "new article tile",
        "author": "Leo"
      }
    }
  }
}

0 个答案:

没有答案