带有过滤器的AppEngine数据存储区查询从不返回结果(Go)

If I don't use a filter, I get results. If I use a filter (this record definitely exists), I don't get any results. It might be the lack of an index defined for that property, but, as I understand it, simple indices should automatically be created in the development server (and an index.yaml file created and populated with it). This is not happening.

query = datastore.NewQuery("UserAccount").Filter("email =", "test@example.com")

ua := UserAccount{}
t := query.Run(ctx)
for ; ; {
    if _, err = t.Next(&ua); err == nil {
        log.Debugf(ctx, "Current: %s", ua)
    } else if err == datastore.Done {
        break
    } else {
        panic(err)
    }
}

When the development server terminates, it states that it's "saving search indexes":

INFO     2016-08-08 05:09:52,894 api_server.py:651] Saving search indexes

However, since an "index.yaml" file doesn't appear, I'm assuming that no indices needed to be created, which means that my query must not've had the desired effect?

What am I missing?

Edit:

Note that the record was previously created and the application stopped and started many times since. I sincerely doubt this is a eventual-consistency thing.

Edit 2:

For the purpose of testing, I've created the following model with the following code. They both exhibit the same behavior as my original model and code.

Definition:

type TestEntity struct {
    Email string
}

Code:

log.Debugf(ctx, "Putting.")

email := "anothertest@a.b"

te := &TestEntity{
        Email: email,
}

k := datastore.NewKey(ctx, "TestEntity", "123", 0, nil)
_, err = datastore.Put(ctx, k, te)
if err != nil {
    panic(err)
}

log.Debugf(ctx, "Waiting.")
time.Sleep(1 * time.Second)

query := datastore.NewQuery("TestEntity")

var results []TestEntity
_, err = query.GetAll(ctx, &results)
log.Debugf(ctx, "GetAll: %s", results)

log.Debugf(ctx, "Running query.")

query = datastore.NewQuery("TestEntity").Filter("email =", email)

te = &TestEntity{}
t := query.Run(ctx)
for ; ; {
    if _, err = t.Next(te); err == nil {
        log.Debugf(ctx, "Found: [%s]", te.Email)
    } else if err == datastore.Done {
        log.Debugf(ctx, "Done.")
        break
    } else {
        panic(err)
    }
}

Results:

2016/08/09 02:11:36 DEBUG: Putting.
2016/08/09 02:11:36 DEBUG: Waiting.
2016/08/09 02:11:37 DEBUG: GetAll: [{anothertest@a.b}]
2016/08/09 02:11:37 DEBUG: Running query.
2016/08/09 02:11:37 DEBUG: Done.

Screenshot of Viewer:

Screenshot of Viewer

The property name in the datastore is "Email" with capital E, not "email".

It's case-sensitive, must be queried with capital E:

query = datastore.NewQuery("UserAccount").Filter("Email =", "test@example.com")

If you would want it to be saved / retrieved with small e, you could use tags to do the mapping, e.g.:

type UserAccount struct {
    Email string `datastore:"email"`
    // other fields...
}

To be able to find an entity in the result of a query/filter by a property that property must have had indexing enabled (in the model definition) at the time when the entity was created and the datastore's background indexing task(s) must be completed for it.

If you enable indexing for that property after the entity was created you need to re-write the property so that the indexing tasks are triggered for it, see: https://stackoverflow.com/a/34583510/4495081.

The "Saving search indexes" message is a generic one printed before the operation takes place, it doesn't mean the result of the operation will be non-empty.

You don't need to worry that your index.yaml file is empty. Not all indexes need to be present in the index.yaml file, only the more complex/composite ones need to. Your filter can be matched using the built-in indexes (after the above-mentioned indexing requirements are met). From Indexes:

There are two types of indexes:

  • Built-in indexes

    By default, Cloud Datastore automatically predefines an index for each property of each entity kind. These single property indexes are suitable for simple types of queries.

  • Composite indexes

    Composite indexes index multiple property values per indexed entity. Composite indexes support complex queries and are defined in an index configuration file (index.yaml).