elasticsearch - elasticsearch:在嵌套对象(HTTP)中更新

标签 elasticsearch nested

我正在尝试使用脚本更新部分嵌套的对象。但是我写的是不正确的。

我有这个对象:

{
  "_index" : "test_7",
  "_type" : "testField",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "view" : {
      "hit" : 3,
      "pages" : [
        {
          "name" : "A",
          "hit" : 1
        },
        {
          "name" : "B",
          "hit" : 1
        },
        {
          "name" : "C",
          "hit" : 1
        }
      ]
    }
  }
}

我想在页面上添加3个匹配。

使用名为“B”的现有页面:
  {
    "view" : {
      "hit" : 6,
      "pages" : [
        {
          "name" : "A",
          "hit" : 1
        },
        {
          "name" : "B",
          "hit" : 4
        },
        {
          "name" : "C",
          "hit" : 1
        }
      ]
    }
  }

带有一个名为“D”的新页面:
  {
    "view" : {
      "hit" : 6,
      "pages" : [
        {
          "name" : "A",
          "hit" : 1
        },
        {
          "name" : "B",
          "hit" : 1
        },
        {
          "name" : "C",
          "hit" : 1
        },
        {
          "name" : "D",
          "hit" : 3
        }
      ]
    }
  }

请告诉我如何编写HTTP请求?
此外,在哪里可以阅读有关“ctx._source”的更多文档?

谢谢。

最佳答案

ctx_source就是您提交的用于对该特定文档建立索引的原始JSON文档。
您需要在_update API中使用脚本支持

curl -XPOST 'http://localhost:9200/data/data/X_UucUuYTOqdB8eo2H-XWw/_update' -d '{
  "script": "increment",
  "params": {
    "newPage": {
      "name": "A",
      "hit": 40
    }
  }
}'

增量脚本
source = ctx._source
Boolean isAdded = false;
for(page in source.view.pages){
    if(page.name == newPage.name){
        page.hit += newPage.hit
        isAdded = true
    }
}
if(!isAdded){
    source.view.pages += newPage
}

在此处,在params部分中指定count和pageName。如果pageName存在,则该值将增加或附加一个新值。

希望对您有所帮助。

关于elasticsearch - elasticsearch:在嵌套对象(HTTP)中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27703649/

相关文章:

maven - Elasticsearch ICU插件-找不到分析器

javascript - 获取嵌套对象的总大小

Javascript 正则表达式 : Find all URLs outside <a> tags - Nested Tags

ElasticSearch:索引文档基准测试

SQL查询查找表中出现次数最多的值,无需嵌套

javascript - 使用 jquery 读取嵌套 json 返回未定义

sql - 从 2 个表更新查询

search - 在 ElasticSearch 中找到文本的返回字段

elasticsearch - Logstash Grok解析问题

elasticsearch - elasticsearch edge n-gram token 生成器:在 token 中包含符号