DynamoDB:查询 "Contains" 的列表类型属性时的 FilterExpression

DynamoDB: FilterExpression when querying List Type Attribute for "Contains"

我找遍了所有地方,但只能找到关于使用 FilterExpressions 过滤列表属性中包含给定值的记录的最少文档和示例。我能够找到的几乎所有其他资源都只是针对潜在匹配列表过滤非列表类型。 这个 AWS comparison operator function reference only mentions that the target can be a Set type, but I do see in the Condition API reference it says contains is supported for list, and the only caveat is that for (a contains b) a can be a list, but b cant. This is also noted in ,它是我能找到的唯一代码示例。我自己试过这个,但我无法让它工作。即使我知道我正在搜索的项目包含在列表中,它也会成功并返回空响应。

例如,对于下面的情况,如果我在没有过滤器的情况下对 partition/sort 关键条件发出相同的请求 - 我会根据我的已知数据得到正确输出的成功响应。然后我 运行 相同的查询,试图只过滤包含“ENTITY123”的记录(我知道应该根据没有过滤器的输出进行匹配)。根据文档,这似乎应该可以工作,因为它在 List 类型上使用 contains,并检查非列表类型 (String)。

设置过滤器表达式的代码(filterExpressions 是转换为过滤器表达式字符串的字符串列表 - 在这种情况下,这是下面日志中反映的唯一表达式):

attributeNames.put("#entities", "entities");
attributeValues.put(":entities", new AttributeValue().withS("ENTITY123"));
filterExpressions.add("contains(:entities, #entities)");

输出日志记录显示了正确创建的表达式(而且我知道这种方法通常应该有效,因为我有几个其他属性可以出现在过滤器表达式中,并且它们都工作得很好,所以日志记录应该反映出什么是实际进入查询:

14 May 2022 00:32:14,303 [INFO] Creating query with hash key value: USER123#US
14 May 2022 00:32:14,303 [INFO] Adding range key condition: {AttributeValueList: [{S: 2021-01-01T00:00:00Z,}],ComparisonOperator: GT}
14 May 2022 00:32:14,303 [INFO] Added FilterExpression: contains(:entities, #entities)
14 May 2022 00:32:14,303 [INFO] Added Attribute Names: {#entities=entities}
14 May 2022 00:32:14,303 [INFO] Added Attribute Values: {:entities={S: ENTITY123,}}

起初我认为它可能与 Attribute Values: {:entities={S: ENTITY123,}} 中属性值末尾的逗号有关,但它似乎总是存在,即使在我的其他工作过滤器表达式中也是如此。这是我的一条记录中此属性的实际 DDB json 的示例:

  "entities": {
    "L": [
      {
        "S": "ENTITY123"
      },
      {
        "S": "ENTITY345"
      }
    ]
  }

任何人都可以指出 querying/filtering 针对 DDB 列表类型的更具体的文档或示例吗?这看起来应该有效吗,还是我在这里遗漏了什么?谢谢!

交换 contains 函数中的元素。 “名称”(路径)在“值”(操作数)之前:

filterExpressions.add("contains(#entities, :entities)");

你是对的,filter expression docscontainssetstring 上运行。但是,我可以确认 contains 过滤器表达式 returns 应用于 list.

时的预期结果