Search code examples
javagoogle-app-enginejdoql

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


I am using Google App Engine for a project and I need to do some queries on the 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.


Solution

  • 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");