Google App Engine: Is it possible to do a Gql LIKE query?
Asked Answered
U

12

125

Simple one really. In SQL, if I want to search a text field for a couple of characters, I can do:

SELECT blah FROM blah WHERE blah LIKE '%text%'

The documentation for App Engine makes no mention of how to achieve this, but surely it's a common enough problem?

Unguiculate answered 6/9, 2008 at 20:5 Comment(1)
The continued problem revolves around people trying to use GAE Datastore as if it were a Relational/~SQL database. By Google introducing GQL, it further leads people into thinking in terms of SQL systems. However I understand that Google is trying to make the transition for everyone much easier even though I'm not sure this is the right approach.Ker
E
82

BigTable, which is the database back end for App Engine, will scale to millions of records. Due to this, App Engine will not allow you to do any query that will result in a table scan, as performance would be dreadful for a well populated table.

In other words, every query must use an index. This is why you can only do =, > and < queries. (In fact you can also do != but the API does this using a a combination of > and < queries.) This is also why the development environment monitors all the queries you do and automatically adds any missing indexes to your index.yaml file.

There is no way to index for a LIKE query so it's simply not available.

Have a watch of this Google IO session for a much better and more detailed explanation of this.

Eleanoreleanora answered 6/9, 2008 at 20:29 Comment(0)
C
77

i'm facing the same problem, but i found something on google app engine pages:

Tip: Query filters do not have an explicit way to match just part of a string value, but you can fake a prefix match using inequality filters:

db.GqlQuery("SELECT * FROM MyModel WHERE prop >= :1 AND prop < :2",
            "abc",
            u"abc" + u"\ufffd")

This matches every MyModel entity with a string property prop that begins with the characters abc. The unicode string u"\ufffd" represents the largest possible Unicode character. When the property values are sorted in an index, the values that fall in this range are all of the values that begin with the given prefix.

http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html

maybe this could do the trick ;)

Chord answered 8/7, 2009 at 8:9 Comment(1)
+1 Tho it's worth pointing out this is case-sensitive. Luckily the data in the field I'm querying is converted to lowercase prior to storing.Admissive
B
12

Altough App Engine does not support LIKE queries, have a look at the properties ListProperty and StringListProperty. When an equality test is done on these properties, the test will actually be applied on all list members, e.g., list_property = value tests if the value appears anywhere in the list.

Sometimes this feature might be used as a workaround to the lack of LIKE queries. For instance, it makes it possible to do simple text search, as described on this post.

Bacteroid answered 25/1, 2010 at 22:43 Comment(1)
the post doesn't exist anymoreLombardi
C
9

You need to use search service to perform full text search queries similar to SQL LIKE.

Gaelyk provides domain specific language to perform more user friendly search queries. For example following snippet will find first ten books sorted from the latest ones with title containing fern and the genre exactly matching thriller:

def documents = search.search {
    select all from books
    sort desc by published, SearchApiLimits.MINIMUM_DATE_VALUE
    where title =~ 'fern'
    and genre =  'thriller'
    limit 10
}

Like is written as Groovy's match operator =~. It supports functions such as distance(geopoint(lat, lon), location) as well.

Cowherd answered 25/11, 2013 at 22:21 Comment(0)
I
4

App engine launched a general-purpose full text search service in version 1.7.0 that supports the datastore.

Details in the announcement.

More information on how to use this: https://cloud.google.com/appengine/training/fts_intro/lesson2

Ironhanded answered 2/10, 2012 at 19:55 Comment(0)
G
3

Have a look at Objectify here , it is like a Datastore access API. There is a FAQ with this question specifically, here is the answer

How do I do a like query (LIKE "foo%")
You can do something like a startWith, or endWith if you reverse the order when stored and searched. You do a range query with the starting value you want, and a value just above the one you want.

String start = "foo";
    ... = ofy.query(MyEntity.class).filter("field >=", start).filter("field <", start + "\uFFFD");
Glomma answered 1/6, 2011 at 17:1 Comment(1)
it will search "starts with" not "Contains".Charbonnier
G
1

Just follow here: init.py#354">http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/search/init.py#354

It works!

class Article(search.SearchableModel):
    text = db.TextProperty()
    ...

  article = Article(text=...)
  article.save()

To search the full text index, use the SearchableModel.all() method to get an
instance of SearchableModel.Query, which subclasses db.Query. Use its search()
method to provide a search query, in addition to any other filters or sort
orders, e.g.:

  query = article.all().search('a search query').filter(...).order(...)
Goltz answered 20/6, 2012 at 2:35 Comment(0)
U
1

I tested this with GAE Datastore low-level Java API. Me and works perfectly

    Query q = new Query(Directorio.class.getSimpleName());

    Filter filterNombreGreater = new FilterPredicate("nombre", FilterOperator.GREATER_THAN_OR_EQUAL, query);
    Filter filterNombreLess = new FilterPredicate("nombre", FilterOperator.LESS_THAN, query+"\uFFFD");
    Filter filterNombre =  CompositeFilterOperator.and(filterNombreGreater, filterNombreLess);

    q.setFilter(filter);
Unorthodox answered 25/6, 2014 at 16:59 Comment(1)
this works for prefix, but what if I want to match from the end of the string? For e.g - i want to search abc in sdfdsabc, then it should return sdfdsabcJaqitsch
F
1

In general, even though this is an old post, a way to produce a 'LIKE' or 'ILIKE' is to gather all results from a '>=' query, then loop results in python (or Java) for elements containing what you're looking for.

Let's say you want to filter users given a q='luigi'

users = []
qry = self.user_model.query(ndb.OR(self.user_model.name >= q.lower(),self.user_model.email >= q.lower(),self.user_model.username >= q.lower()))

for _qry in qry:
 if q.lower() in _qry.name.lower() or q.lower() in _qry.email.lower() or q.lower() in _qry.username.lower():
      users.append(_qry)
Fraunhofer answered 19/3, 2015 at 18:9 Comment(0)
I
1

It is not possible to do a LIKE search on datastore app engine, how ever creating an Arraylist would do the trick if you need to search a word in a string.

@Index
    public ArrayList<String> searchName;

and then to search in the index using objectify.

List<Profiles> list1 = ofy().load().type(Profiles.class).filter("searchName =",search).list();

and this will give you a list with all the items that contain the world you did on the search

Inhibitory answered 12/8, 2016 at 20:30 Comment(0)
I
0

If the LIKE '%text%' always compares to a word or a few (think permutations) and your data changes slowly (slowly means that it's not prohibitively expensive - both price-wise and performance-wise - to create and updates indexes) then Relation Index Entity (RIE) may be the answer.

Yes, you will have to build additional datastore entity and populate it appropriately. Yes, there are some constraints that you will have to play around (one is 5000 limit on the length of list property in GAE datastore). But the resulting searches are lightning fast.

For details see my RIE with Java and Ojbectify and RIE with Python posts.

Immortality answered 23/9, 2011 at 13:7 Comment(0)
L
0

"Like" is often uses as a poor-man's substitute for text search. For text search, it is possible to use Whoosh-AppEngine.

Lindseylindsley answered 26/11, 2011 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.