2

I'm using java with app engine. SQL query:

select * from Person where name LIKE "a%"

How to write it on Google App Engine?

Niventh
  • 985
  • 2
  • 9
  • 23

4 Answers4

2

Assuming you're querying against an entity in the App Engine datastore, in Java you'd rewrite LIKE "a%" into something like

Query query = new Query("Person")
  .addFilter("name", QueryFilterOperater.GREATER_THAN_OR_EQUAL, "a")
  .addFilter("name", QueryFilterOperator.LESS_THAN, "b");

The rest is left as an exercise. See https://developers.google.com/appengine/docs/java/datastore/queries

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • This does not address the question (how to do LIKE) at all. Looking at the other answers, that seems to be non-trivial, not something "to be left as an exercise". – Thilo Mar 28 '13 at 01:59
  • 1
    Look again. This rewrights LIKE "a%" into >= "a" AND < "b". – Dave W. Smith Mar 28 '13 at 03:11
  • Okay, in this particular case (the pattern is just a case-sensitive fixed prefix), that would be an alternative to using LIKE. But I did not get that at all from your answer. Add a bit of explanation, and I'll remove the downvote. – Thilo Mar 28 '13 at 03:25
  • 1
    "This particular case" is the question asked. – Dave W. Smith Mar 28 '13 at 03:34
1

There's no support for the LIKE operator in Appengine's datastore.

Two solutions :

  • Use Google Cloud SQL if you want to keep SQL-like queries
  • Use Full Text Search with you entities in the datastore
David
  • 5,481
  • 2
  • 20
  • 33
1

To perform such query you have to use Google Cloud SQL.

If you're using Datastore, so it doesn't support fulltext search. You have to manually create all possible variations of name and use them to query entities. You can store them in special entity's multivalued field (it can be expensive) or use special Search API that perfectly fit this purpose (Search doesn't support substring matching so you have to use variations all the same).

It two words working with Search API is:

  1. List item Create Person entity in Datastore with some name

  2. Create corresponding Document in search Index named Persons, with fields possible_names and entity_id (also you can use Document.doc_id to store entity's id). In possible_names you store all useful variations of name divided by space.

  3. Query Persons index to find all documents where field possible_names matches your search string.

  4. Use field entity_id form result's Documents to retrieve entity from Datastore by id.

Here is little example.

Community
  • 1
  • 1
Denisigo
  • 640
  • 4
  • 13
  • LIKE is not full text search, though. – Thilo Mar 28 '13 at 01:57
  • Can you explain why? By `fulltext search` I mean searching some string by some pattern in some text without using some index. LIKE does exactly it, isn't it? – Denisigo Mar 28 '13 at 05:26
  • Fulltext search usually means something like Lucene, or that Search API you linked to (which, like you point out does not support substring matching). In particular, it generally does not match arbitrary (non-token) patterns that a LIKE search (which is substring matching) could. – Thilo Mar 28 '13 at 06:20
0

are you looking for this "select * from Person where name LIKE \"a%\""

Ankit
  • 6,554
  • 6
  • 49
  • 71