开发者

Problem with JDOQL to obtain results with a "contains" request

开发者 https://www.devze.com 2023-01-17 10:43 出处:网络
I am using Google App Engine for a project and I need to do some queries on t开发者_如何学Gohe database. I use the JDOQL to ask the database. In my case I want to obtain the university that contains t

I am using Google App Engine for a project and I need to do some queries on t开发者_如何学Gohe database. I use the JDOQL to ask the database. In my case I want to obtain the university that contains the substring "array". I think my query has a mistake because it returns the name of universities in the alphabetical order and not the ones containing the substring.

Query query = pm.newQuery("SELECT FROM " + University.class.getName() + " WHERE name.contains("+array+") ORDER BY name RANGE 0, 5");

Could someone tell me what's wrong in my query?

Thank you for your help!

EDIT

I have a list of universities store and I have a suggestbox where we can request a university by his name. And I want to autocomplete the requested name.


App engine does not support full-text searches, you should star issue 217. However, A partial workaround is possible. And in your case I think it is a good fit.

First thing, adjust your model such that there is a lower (or upper case) version of the name as well -- I will assume it is called lname. Unless you want your queries to be case-sensitive.

Then you query like this:

Query query = pm.newQuery(University.class);
query.setFilter("lname >= startNameParam");
query.setFilter("lname < stopNameParam");
query.setOrdering("lname asc");
query.declareParameters("String startNameParam");
query.declareParameters("String stopNameParam");
query.setRange(0, 5);
List<University> results = (List<University>) query.execute(search_value, search_value + "z");


The correct way to do this is like this -

Query query = pm.newQuery(University.class,":p.contains(name)");
query.setOrdering("name asc");
query.setRange(0, 5);
List univs = q.execute(Arrays.asList(array)); 

(note- In this case the :p is an implicit param name you can replace with any name)

0

精彩评论

暂无评论...
验证码 换一张
取 消