Search code examples
javagoogle-cloud-platformgoogle-cloud-datastore

How to query all entities those array type field contains at least one element of the given array using java 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<String> 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("testEntity");
  queryBuilder.setFilter(....)

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


Solution

  • 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