java - How to create a query using wildcards and projections with hibernate search? -
i trying create queries using wildcards , projections hibernate search, i'm failing. users can use or not wildcard on queries. example, want search terms starting "book", use "book*" search.
the method i've implemented, below.
public list<employee> filtrar(string term) throws daoexception { try { fulltextentitymanager fulltextem = search.getfulltextentitymanager(this.entitymanager); query query = new wildcardquery(new term("*", term)); fulltextquery fulltextquery = fulltextem.createfulltextquery(query, employee.class); fulltextquery.setprojection("id", "name", "phone", "email"); sort sortfield = new sort(new sortfield("name_order", sortfield.string)); fulltextquery.setsort(sortfield); return fulltextquery.getresultlist(); } catch (exception e) { logger.error("error: " + term, e); throw new daoexception(e); } } the string term passed, parameter method, used search on fields(id, name, phone , email). nothing returned. what's wrong in method?
edit: entity
@entity @indexed @table(name = "vw_employee") public class employee implements serializable { private static final long serialversionuid = 244555315052436669l; @id @column(name = "id", insertable = false, updatable = false) @field(store = store.yes, index = index.yes, analyze = analyze.no) private long id; @fields({ @field(name = "name_order", store = store.yes, index = index.yes, analyze = analyze.no), @field(name = "name", store = store.yes, index = index.yes, analyze = analyze.yes) }) @column(name = "name", insertable = false, updatable = false) private string name; @field(store = store.yes, index = index.yes, analyze = analyze.yes) @column(name = "phone", insertable = false, updatable = false) private string phone; @field(store = store.yes, index = index.yes, analyze = analyze.yes) @column(name = "email", insertable = false, updatable = false) private string email; //getters , setters ommited } indexing method:
public void index() throws daoexception { fulltextentitymanager fulltextentitymanager = search.getfulltextentitymanager(this.entitymanager); try { fulltextentitymanager.createindexer(employee.class).purgeallonstart(boolean.true).optimizeonfinish(boolean.true).startandwait(); logger.info("indexing...."); } catch (interruptedexception e) { logger.error("error", e); throw new daoexception(e); } }
this doesn't work.
query query = new wildcardquery(new term("*", term)); if want search across fields, you'll either need create term each field need search, , combine them (a disjunctionmaxquery best that):
query query = new disjunctionmaxquery(0); query.add(new termquery(new term("field1", term))); query.add(new termquery(new term("field2", term))); query.add(new termquery(new term("field3", term))); multifieldqueryparser tool this.
alternatively, useful pattern add searchable fields 1 unstored catch-all field @ index time, , when want match against in index, query field instead of enumerate subqueries on searchable fields.
also, looks want add wildcard query here, haven't done so. wildcardquery won't assume wildcards aren't there in term.
query query = new wildcardquery(new term("field", term + "*")); since need trailing wildcard, though, use prefixquery, standard way of handling trailing wildcard.
query query = new prefixquery(new term("field", term));
Comments
Post a Comment