简体   繁体   中英

java - Google Cloud Datastore - Inequality returning “no matching index found”

In Android Studio , I'm trying to use Google Clood Datastore from an Android app.

With the Google example https://cloud.google.com/datastore/docs/concepts/indexes#Datastore_Indexes_and_properties I am able to add Tom (32 years old) and Lucy (30 years old) as Person and as children of the Company Acme. I can see these two entries are in the datastore in the Google Developers Console.

  • I can retrieve children from Company Acme if I set a query filter as below.

     Key acmeKey = makeKey("Company", "Acme").build(); makeFilter("__key__", PropertyFilter.Operator.HAS_ANCESTOR, makeValue(acmeKey)).build())); 
    • If I want to retrieve only Person where age is equal to 32 I add to the filter above makeFilter("age", PropertyFilter.Operator.EQUAL, makeValue(32)).build() Then, I get the Person Tom.

  • But, if, instead of Operator.EQUAL I put an inequality filter like Operator.LESS_THAN or Operator.GREATER_THAN I get the error " no matching index found ".

I think it's a problem of indexes, but I really don't understand how can I set an index. I use Android Studio and I don't use at all the App Engine. I just want to retrieve data as I do with MySQL with a simple WHERE age > 32 .

Btw, I tried with GQL and I get exactly the exact same problem, work with EQUAL , but not with GREATER_THAN (and same in https://developers.google.com/apis-explorer ).

So, does anybody know how I can fix this problem ? Thanks !!

Index configuration for Cloud Datastore is handled using the gcd.sh tool.

It's easiest if you're able to run your queries against the local datastore provided by gcd.sh . The tool can determine exactly which indexes you'll need, and then you can use the updateindexes command to configure them on your production app.

If not, you can also configure indexes by hand:

  1. gcd.sh create <project> -d <project>
  2. Create a file at <project>/WEB-INF/datastore-indexes.xml with the following content:

<?xml version="1.0" encoding="utf-8"?> <datastore-indexes autoGenerate="true"> <datastore-index kind="Company" ancestor="true"> <property name="age" direction="asc" /> </datastore-index> </datastore-indexes>

  1. gcd.sh updateindexes <project>

The exact content of the file will depend on the queries you want to run. This page gives additional details on the format of the indexes file.

So, as Ed Davisson just said, everything is done with gcd.

It took me a lot of time to understand what to do, so I put my experience below, maybe it will be helpful to someone. I'm working with Ubuntu , I don't know about other OS.

  1. Download gcd-v1beta2-rev1-2.1.1.zip in the Downloads folder
  2. Unzip the downloaded file
  3. In the console, go to the unziped folder cd Downloads/gcd-v1beta2-rev1-2.1.1
  4. Then, ./gcd.sh create -d myDataset myProjectName where myDataset is the project ID in Google Developers Console (it is composed with 2 random nouns and a number)
  5. Go to the unziped folder gcd-v1beta2-rev1-2.1.1 and you should see a folder myProjectName . In this myProjectName folder, in the folder WEB-INF create a file datastore-indexes.xml
  6. As I wanted to retrieve Person who have the ancestor Company and older than 20, I had to add the index as Ed Davisson said

     <?xml version="1.0" encoding="utf-8"?> <datastore-indexes autoGenerate="true"> <datastore-index kind="Person" ancestor="true"> <property name="age" direction="asc" /> </datastore-index> </datastore-indexes> 

  7. In your console, ./gcd.sh updateindexes myProjectName

Now, in Google Developers Console you can see this new index under Cloud Datastore -> Index

And finally you can retrieve Person where age > 20

Key acmeKey = makeKey("Company", "Acme").build();
Query.Builder query = Query.newBuilder();
query.addKindBuilder().setName("Person");
query.setFilter(makeFilter(

    makeFilter("__key__",
        HAS_ANCESTOR,
        makeValue(acmeKey)).build(),

    makeFilter("age",
        GREATER_THAN,
        makeValue(20)).build()));

 query.addOrder(makeOrder("age", ASCENDING));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM