How to query all entities those array type field contains at least one element of the given array using java API?

huangapple go评论109阅读模式
英文:

How to query all entities those array type field contains at least one element of the given array using java API?

问题

我有一个具有数组类型字段的GCP数据存储实体,其中包含一个元素列表,类似于[1,2,3]

示例:

@Entity
public class TestEntity{

   @Id
   String id;

   List<String> data;
}

我需要使用GCP Java API构建一个查询,以选择所有具有给定数组中至少一个元素的实体。

示例:
两个实体:
1:[1,2,3]
2:[1,4,5]
对于输入,我希望选择:
[1,2] - 1, 2;
[2] - 1

  EntityQuery.Builder queryBuilder =
                    Query.newEntityQueryBuilder().setKind("testEntity");
  queryBuilder.setFilter(....)

我在API中没有看到类似于“包含”或“在内”的过滤器。如何构建这样的查询?

英文:

I have a GCP datastore entity that has an array type field that contains a list of elements, like [1,2,3]

Example:

@Entity
public class TestEntity{

   @Id
   String id;

   List&lt;String&gt; data;
}

I need to build a query using GCP java API to select all entities that have at least one element of the given array in the field.

Example:
two entities:
1: [1,2,3]
2: [1,4,5]
I expect to select for input:
[1,2] - 1, 2;
[2] - 1

  EntityQuery.Builder queryBuilder =
                    Query.newEntityQueryBuilder().setKind(&quot;testEntity&quot;);
  queryBuilder.setFilter(....)

I do not see a filter like "contains" or "is in" in the API. How can I build such a query?

答案1

得分: 0

Datastore模式对每个索引每次仅索引每个唯一的数组属性值。因此,要查询数组是否包含某个值,请使用等值筛选器。

.setFilter(PropertyFilter.eq(property,value))

英文:

Datastore mode indexes each unique array property value once per index. Thus to query if an array contains a value use an equality filter. [1]

.setFilter(PropertyFilter.eq(property,value))

[1] https://cloud.google.com/datastore/docs/concepts/queries#array_values

huangapple
  • 本文由 发表于 2020年8月16日 15:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63434292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定