java - Can you sort and search on same field with Hibernate/Lucene? -


i have following annotated class trying sort results lucene/hibernate search query. have query working seems when implement necessary annotations (seen on jobstatus) sort column, makes impossible search column. basing off instructions found here on google. have been having issues figuring whole hibernate search , sort thing out, figured out how sort , search need able them together.

@entity @table(name="jobreq") @indexed public class jobreq {  @id @documentid @generatedvalue(strategy=generationtype.identity) private integer id;  @field(index = index.yes) @column(name="jobid", nullable=false, unique=true) private string jobid;  @field(index = index.yes) @column(name="jobtitle", nullable=false) private string jobtitle;  @field(index = index.yes) @column(name="jobcontract", nullable=false) private string contract;  @field(index = index.yes) @column(name="jobproject", nullable=true) private string project;  @field(index = index.yes) @column(name="joblaborcategory", nullable=false) private string laborcategory;  @field(index = index.yes) @column(name="jobsummary", nullable=false) private string summary;  @field(index = index.yes) @column(name="jobdescription", nullable=false) private string jobdescription;  @fields({@field, @field(analyze  = analyze.no, name = "jobstatus")}) @column(name="jobstatus", nullable=false) private string status;  @field(index = index.yes) @column(name="ttonumber", nullable=false) private string ttonumber;  @field(index = index.yes) @column(name="jobposteddate", nullable=false) @type(type="date") private date posteddate; 

and snippet search function

field[] allfields = this.type.getdeclaredfields(); sortfield field =new sortfield(sortcolumn, sortfield.string, reversesort); sort sort = new sort(field); hibquery = fulltextsession.createfulltextquery(bq, this.type).setsort(sort); results = hibquery.list(); 

hibernate search documentation provides solution problem similar adam's solution.

http://docs.jboss.org/hibernate/search/4.5/reference/en-us/html_single/#d0e3165

basically use @fields annotation index field twice once analyze no sorting , once analyze yes searching.

@entity @indexed(index = "book") public class book { @fields( {         @field,         @field(name = "summary_forsort", analyze = analyze.no, store = store.yes)         } )  public string getsummary() {     return summary;  }   ... } 

analyze: determines whether property analyzed (analyze.yes) or not (analyze.no). default value analyze.yes. tip whether or not want analyze property depends on whether wish search element is, or words contains. make sense analyze text field, not date field. tip fields used sorting or faceting must not analyzed.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -