Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?

DDD
Release: 2024-10-24 07:08:02
Original
987 people have browsed it

Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?

Search Entities with Prefixed Names in Google App Engine Datastore

Question:

Is it feasible to retrieve entities from Datastore where a specific field begins with a given string? Despite attempting the following query, I am unable to obtain the desired results:

q = datastore.NewQuery("Places").Filter("Name >", "a")
Copy after login

Answer:

Querying entities based on a field prefix is indeed supported in Datastore, but it necessitates the conjunction of two inequality filters.

For instance, to retrieve Places with names starting with "li," the query should specify that the Name field is greater than (or equal to) "li" and less than "lj." This is because "lj" is the lexicographically subsequent prefix.

In GQL, this query would appear as:

SELECT * FROM Places WHERE Name > 'li' AND Name < 'lj'
Copy after login

In Go code, the query takes the form:

q = datastore.NewQuery("Places").Filter("Name >", "li").Filter("Name <", "lj")
Copy after login

This query will yield Places with names like:

liam
lisotto
lizst
Copy after login

However, it will exclude names resembling:

abc
ljoi
lj
qwerty
Copy after login

Note that upper- and lowercase letters have distinct lexicographical orders. Thus, "List" precedes "li," while "list" follows "li."

The above is the detailed content of Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!