1
votes

I am running into a very basic problem of querying my data from the Datastore Viewer console based on a simple WHERE <column> = 'xxx' type query.

Here is a snapshot of my dummy data:

enter image description here


From what I understand, I should be able to query for that record based on the "email" field, or any other field, using SELECT * FROM User WHERE email = 'a'. But that returns "No results in Empty namespace." as can be seen below:

enter image description here


I don't see what is different to the situation in this related question:

I have examined that record in the Entity editor, and can confirm that the value of the email field is "a", without any extra hidden spaces:

enter image description here


Update

Mihail suggested that I need to create an index for this query.

Following the Java Datastore Index Configuration docs, I created the file datastore-indexes.xml in /war/WEB-INF:

<?xml version="1.0" encoding="utf-8"?>
<datastore-indexes autoGenerate="false">
    <datastore-index kind="User" ancestor="false">
        <property name="email" direction="asc" />
    </datastore-index>
</datastore-indexes>

I was unable to deploy my project because it did not allow this index to be uploaded. The error message I got was:

Creating a composite index failed for entity_type: "User"ancestor: falseProperty { name: "email" direction: 1}: This index:entity_type: "User"ancestor: falseProperty { name: "email" direction: 1}is not necessary, since single-property indicies are built in. Please remove it from your index file and upgrade to the latest version of the SDK, if you haven't already.

1
Is the email property indexed? It will work just like you are describing when the property is not indexed. - Mihail Russu
@MihailR It does indeed look like an index problem... but for my personal curiosity, why wouldn't the error be "index needed" as it normally is in these cases? - Patrice
Assuming you are using NDB (or any other Datastore library) - that's what's taking care of the "Index needed" exception. Datastore itself doesn't. You can even misspell property name (i.e. email123 instead of email) and still won't get an error. - Mihail Russu

1 Answers

2
votes

It sounds like the email property is not indexed and you can't do any queries on unindexed properties, even as simple as this one, because if you had, say million users, this query would have to fetch all million entities to find the ones you need.

All you need to do is make the email property indexed but note that it will not take care of past entities. You are going to need to re-save all past entities in order for the new index to be applied.

To make a property indexed:

  • In Objectify the @Index annotation needs to be added to the property. More details here.

  • If using low-level Java Datastore API make sure to define the property with setProperty() and not with setUnindexedProperty(). More details here.

  • If using Python's NDB, make sure indexed attribute is not set to False since most properties are indexed by default. More details here.