How to filter Google Content Store data based on a property that is an array using Java library -


i have datastore 1 property contains array of strings. each entity can have different size of array. want make query , filter upon content of array in each entity.

i generate set of filters, 1 each required string in array. i'm building composite filter used in entityquery. filters generated this:

// arrayproperty name on property in datastore contains array of strings. list<propertyfilter> pathfilters = arrays.stream(new string[] {"a","b","c"})     .map(s -> propertyfilter.eq("arrayproperty", s))     .collect(collectors.tolist()); 

this match both entities arrayproperty = [a,b,c] , arrayproperty = [a,b,c,d].

can use google cloud datastore query java library in com.google.cloud.datastore filter out entities property value particular array exact same elements (no more, no less , order independent)? maybe sort of size filter or different query/filter. or use gql?

you cannot equality filter (or exact match) on array property. can query entities contain 1 or more individual elements. example data ([a, b, c] , [a, b, c, d]), following queries possible:

  • arrayproperty="a" - returns both entities
  • arrayproperty="a" , arrayproperty="b" - returns both entities
  • arrayproperty="a" , arrayproperty="b" , arrayproperty="c"- returns both entities
  • arrayproperty="a" , arrayproperty="b" , arrayproperty="c" , arrayproperty="d" - returns 1 entity
  • arrayproperty='x' - neither match

you can expect same behavior query/filter , gql.

one workaround store number of elements in array property in separate field, , create composite index on count property , array property. query like:

where count=3 , arrayproperty="a" , arrayproperty="b" , arrayproperty="c"  

this return entities arrayproperty has 3 elements , arrayproperty has a, b , c in order.

you have ensure new count property stays in sync when entities created/updated.

while should give desired result, index size more.

you may choose dump count original array field (as integer), assuming primary data in array of type string. approach, can avoid composite index.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -