Question

Is it possible to use a wildcard with GQL? I would like to be able to pull all records that contain a particular word somewhere in their values. I don't believe there is a "LIKE" filter. Here is basically what I am trying to do:
Any advice you can give is greatly appreciated-

mentors_with_topic = db.GqlQuery("SELECT * FROM MentorServices WHERE Services LIKE 'Reading'").get()
Was it helpful?

Solution

For what you want to do, you're probably best served using the search API (added in 1.7).

There are docs available, but essentially you create a new "search" index and "documents" which you must add to the index by creating them from your existing data. The search index is a different, fully searchable type of index which but which is still stored in your datastore.


As a hackish alternative to this, if you store the free text you want to search against in a StringListProperty (you will probably have to update your model and migrate your data to be split into individual words), then you can "search" against this with a simple equality operator (=) which is roughly equivalent to an in in python:

search_string = "Reading"
q = MentorServices.all()
q.filter("Services =", search_string)
q.fetch()

I strongly suggest using the search API instead of this, because:

  • You have to migrate all of your TextProperty fields that you want to search to StringListProperty fields before this will work
  • Using StringListProperty fields is not efficient for storing text
  • The alternative search API was created precisely to avoid these kinds of solutions.

If you controlled how users entered the data, for example by having them enter a set of skills as tags, then they would be more easily saved as a StringListProperty and it would make more sense to search in this manner. For searching unstructured text, use the search API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top