如何使用过滤器脚本迭代elasticsearch中的嵌套数组?

Lea*_*ira 5 elasticsearch

我正在尝试过滤 Elasticsearch 中的嵌套字段。嗯,我需要根据某些规则归还某些文件。要重现我收到的错误,您可以参考以下示例:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "user": {
        "type": "nested" 
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
PUT my-index-000001/_doc/1
{
  "group": "fans",
  "user": [
    {
      "first": "John",
      "last": "Smith"
    },
    {
      "first": "Alice",
      "last": "White"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

可以看出,我们有一个对象数组(嵌套)。

我需要在嵌套字段上应用一个脚本,我可以在其中遍历用户数组。

例如我尝试过这个:

GET my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "filter": [
            {
              "script": {
                "script": {
                  "inline": """
                  def users = doc['user'];
                  for ( int i = 0; i < users.size(); i++ ) {
                    
                  }
                  return true;
                  """
                }
              }
            }
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

{
  ...
          "script_stack" : [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
            "users = doc['user'];\n                  ",
            "            ^---- HERE"
          ],
          ...
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "No field found for [user] in mapping with types []"
          }
        }
      }
    ]
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

Run Code Online (Sandbox Code Playgroud)

Elasticsearch 版本 7.7

这可以吗?我已经查看了一些答案,但我不清楚。

Joe*_*ook 4

嵌套文档非常强大,因为您保留了某些属性连接,但存在无法迭代它们的缺点,如此处所述


话虽这么说,您可以users使用该copy_to 功能来展平属性,如下所示:

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "user__first_flattened": {
        "type": "keyword"
      },
      "user": {
        "type": "nested",
        "properties": {
          "first": {
            "type": "keyword",
            "copy_to": "user__first_flattened"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后

PUT my-index-000001/_doc/1
{
  "group": "fans",
  "user": [
    {
      "first": "John",
      "last": "Smith"
    },
    {
      "first": "Alice",
      "last": "White"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在您可以访问字段值并可以迭代它们(如果需要,还可以使用循环索引来帮助定位/识别正确的“嵌套”子文档。)这仅在您迭代字段的假设下有效它在每个嵌套子文档中表示,以便循环不会被缩短:

GET my-index-000001/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "script": {
            "script": {
              "inline": """
                  def users = doc.user__first_flattened;
                  // users == [Alice, John]
                  for ( int i = 0; i < users.size(); i++ ) {
                    
                  }
                  return true;
                  """
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我们不再执行nested查询,因为我们不在该上下文中,并且在根中获得了可用的扁平字段。


还值得知道的是,您可以替换copy_toinclude_in_rootwhich 在此处同样有用。