1

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()
Matt Ellis
  • 119
  • 1
  • 2
  • 15
  • What are some examples of what you want your `like` to match against? You can't literally do a `like`, but if an index can be built on what you want to do, it will be possible. – John Lyon Sep 25 '13 at 21:40
  • Well, I would like for users to write a few sentence bio - and then have other users able to search by keywords that appears in their bio. – Matt Ellis Sep 25 '13 at 21:52
  • One idea I had is to convert the string (their bio) into a list ..and then possibly trying to match against each item in the list. Would that be possible? – Matt Ellis Sep 25 '13 at 22:00
  • If you used a `StringListProperty`, I'd say it could work – John Lyon Sep 25 '13 at 22:09

1 Answers1

0

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.

John Lyon
  • 9,978
  • 2
  • 34
  • 42
  • 1
    thanks jozzas, that helps a lot. I will look into using the search API, and my fallback will be your idea of using tags- – Matt Ellis Sep 26 '13 at 13:20