如何将嵌套对象中的字段从嵌套对象移出到弹性索引中的单独对象中

如何解决如何将嵌套对象中的字段从嵌套对象移出到弹性索引中的单独对象中

我的索引中有一个包含多个对象的嵌套字段。

 "customFields" : [
            {
              "objectTypeId" : 17,"Value" : "","description" : "The original author of the document","Name" : "Document Author"
            },{
              "objectTypeId" : 17,"description" : "Source document number","Name" : "Legacy document number"
            },.
.
.
]

我想创建一个脚本,可以将字段从 customFields 对象移出到单独的对象中,如下所示:

"Document_Author": {
"Description": "The original author of the document","Value": "Some value"
"ObjectTypeId": 17
},"Legacy document number": {
"Description": "Source document number",.
.
.

我试过这样的脚本,我对弹性搜索和脚本很陌生,所以这不起作用。

POST /new_document-20/_update_by_query
 {
  "script" : { "inline": "for (int i = 0; i < ctx._source.customFields.length; ++i) { ctx._source.add(\"customFields[i].Name\" : { \"Value\" : \"customFields[i].Value\",\"Description\" : \"customFields[i].description\",\"objectTypeId\" : \"customFields[i].objectTypeId\"}) }","query": {
         "bool": {
           "must": [
             {
               "exists": {
                 "field": "customFields.Name"
          }
        }
      ]
    }
  }
  }
}

我从这个指向 customFields[i].Name 得到编译错误,就像这样:

"error": {
    "root_cause": [
      {
        "type": "script_exception","reason": "compile error","script_stack": [
          "... d(\"customFields[i].Name\" : { \"Value\" : \"customFiel ...","                             ^---- HERE"

如何创建一个脚本来帮助我将字段从嵌套对象中移出?

解决方法

您可以只执行一次 ctx._source 写操作 per loop 以防止出现 "The maximum number of statements that can be executed in a loop has been reached." 错误。

话虽如此,我建议:

  1. 复制原来的_source
  2. 提取 customFields 列表
  3. 迭代提取的列表并调整哈希映射以符合所需的格式
  4. 将新形成的哈希映射设置到复制的 source
  5. 完全替换原来的 _source

实际上:

POST /new_document-20/_update_by_query
{
  "script": {
    "inline": """
      def source_copy = ctx._source;
      def customFields = source_copy.remove('customFields');
      
      for (int i = 0; i < customFields.length; i++) {
        // store the current iteratee
        def current = customFields[i];
        
        // remove AND return the name
        def name = current.remove('Name');
        
        // set in the _source
        source_copy[name] = current;
      }
      
      // replace the original source completely
      ctx._source = source_copy;
    ""","query": {
      "bool": {
        "must": [
          {
            "exists": {
              "field": "customFields.Name"
            }
          }
        ]
      }
    }
  }
}

并作为内联脚本字符串:

"\n      def source_copy = ctx._source;\n      def customFields = source_copy.remove('customFields');\n      \n      for (int i = 0; i < customFields.length; i++) {\n        // store the current iteratee\n        def current = customFields[i];\n        \n        // remove AND return the name\n        def name = current.remove('Name');\n        \n        // set in the _source\n        source_copy[name] = current;\n      }\n      \n      // replace the original source completely\n      ctx._source = source_copy;\n    "

顺便说一下,hash maps in Painless 是通过 new HashMap 调用或通过(有点令人困惑)[:] 运算符实例化的,即:

def entries_map_without_name = [
   "Value" : current.Value,"Description" : current.description,"objectTypeId" : current.objectTypeId
];

附言从嵌套的对象列表到一堆散列映射的转换 您尝试执行的操作有其优点和缺点,尤其是。当涉及到映射大小膨胀和非常有限的聚合可能性时。

无耻的插件——我只是讨论我的Elasticsearch Handbook,特别是在this sub-chapter

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?