Home >Backend Development >Golang >How to Perform Prefix String Searches in Google App Engine Datastore?

How to Perform Prefix String Searches in Google App Engine Datastore?

DDD
DDDOriginal
2024-10-24 06:45:02309browse

How to Perform Prefix String Searches in Google App Engine Datastore?

Prefix String Search in Google App Engine Datastore

Question:

Can Datastore search for entities whose names begin with a specific string?

Answer:

Yes, it is possible to perform prefix string searches in Datastore.

Details:

Datastore does not support a direct prefix search operator. However, you can achieve this functionality using a combination of inequality filters.

To list entities with names starting with a prefix, you need to specify two filters:

  • A filter to select entities greater than or equal to the prefix.
  • A filter to select entities less than the next string in lexicographical order after the prefix.

Example:

Suppose you want to search for Places with the "li" prefix. The corresponding query would be:

<code class="go">q = datastore.NewQuery("Places").Filter("Name >=", "li").Filter("Name <", "lj")</code>

This query will return Places with names such as:

liam
lisotto
lizst

But it will exclude names like:

abc
ljoi
lj
qwerty

Note: Capital and lowercase letters are treated differently in lexicographical order. For instance, "List" is less than "li" in lexicographical order.

The above is the detailed content of How to Perform Prefix String Searches in Google App Engine Datastore?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn