Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Datastore filter data which contains the items in a List

In our Google Cloud Datastore we've a property(companies property) which stores list of strings and while we are querying our datastore we would like to send a Java list to datastore API and get the data which companies property includes at least one of the items in the list. Our current query code is in below, however we were not able to find how to set list filter on this code. Does anyone have any idea about it?

public List<MAExchangeNews> getExchangeNews(IExchangeController controller,int count, String offsetVal, List<String> relatedCompanySymbols) {
    List<MAExchangeNews> result = null;
    if (controller != null) {
        EntityQuery.Builder builder = Query.newEntityQueryBuilder();
        builder.setKind(KIND_NAME);
        builder.setLimit(count);
        builder.setOrderBy(OrderBy.desc(FIELD_NEWS_TIME));
        if (offsetVal != null) {
            builder.setStartCursor(Cursor.fromUrlSafe(offsetVal));
        }
        if (relatedCompanySymbols != null) {

//This is the area we want to add our filtering code        //builder.setFilter(PropertyFilter.Operator.GREATER_THAN_OR_EQUAL_VALUE);
        }

        Query<Entity> query = builder.build();

        QueryResults<Entity> resultList = datastore.run(query);
        result = entitiesToNews(controller,resultList);         
        if (result != null) {
            this.nextOffset = resultList.getCursorAfter().toUrlSafe();
        }           
    }

    return result;
}

What we wanted to achieve as an example:

- Our DataStore Entities Company Properties :
  1. FROTO, KCHOL
  2. KCHOL, ALBRK
  3. AKBNK, GARAN
  4. ALBRK, ACSEL

- Our query List
  1. {KCHOL,GARAN}

- Expected Result:
  1. FROTO, KCHOL
  2. KCHOL, ALBRK
  3. AKBNK, GARAN

I think what we need is, a contains query with or statements in it (if possible), or an in query. But I dont how we can implement it with Google Cloud Datastore API.

Thanks

like image 651
tolgatanriverdi Avatar asked Jul 19 '19 12:07

tolgatanriverdi


People also ask

Is Google Datastore deprecated?

The Cloud Datastore Administration API v1beta1 is now deprecated. The Cloud Datastore Admin backup feature is being phased out in favor of the managed export and import for Cloud Datastore. Please migrate to the managed export and import functionality at your earliest convenience.

What is the query language we can use with Datastore?

The Python Datastore API provides two classes for preparing and executing queries: Query uses method calls to prepare the query. GqlQuery uses a SQL-like query language called GQL to prepare the query from a query string.


1 Answers

I am not familiar with java. But little bit with Google Cloud Datastore. Google Cloud Datastore have five types filter (=, <, <=, >, >=) on query So you can use only one out of filter.

For getting result of 1. {KCHOL,GARAN} you can try it with two query, first query in find data for KCHOl and second query in find data for GARAN and merge it.

There is not any like contains of In in Google Cloud Datastore. may be this guidance helps to find your solution.

like image 92
paras chauhan Avatar answered Oct 16 '22 05:10

paras chauhan