The new version of the API that was intended to use Lucene was too cumbersome, and finally decided to use Hibernate search to implement full-text indexing. This blog post is an example of what I've done in the past to achieve full-text retrieval.
1, modify hibernate configuration file , because my system uses SSH2 to develop, so I modified the spring configuration file
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name= "DataSource" ref= "DataSource" /> <property name= "Hibernateproperties" > < Props> <prop key= " Hibernate.dialect ">org.hibernate.dialect.MySQL5Dialect</prop> <prop key= "Hibernate.hbm2ddl.auto" >update</prop> <prop key= "Hibernate.show_sql" > True</prop> <prop key= " Hibernate.search.default.directory_provider ">filesystem</prop> <p rop key= "Hibernate.search.default.indexBase" >E:/index</prop> </props> </property> <property name= "Mappingresources" > <list> <value>cn/harmel/blog/domain/user.hbm.xml </value> <value>cn/harmel/ blog/domain/category.hbm.xml</value> <value>cn/harmel/blog/domain/Article.hbm.xml</value> <value>cn/harmel/blog/domain/Comment.hbm.xml</value> <value>cn/harmel/blog/domain/ attachment.hbm.xml</value> </list> </property></bean>
In fact, the following two properties are configured:
Hibernate.search.default.directory_provider = FileSystem
Hibernate.search.default.indexBase = Index Store Directory
2, annotation to the entity
package cn.harmel.blog.domain;import java.util.date;import java.util.hashset;import java.util.set;import org.hibernate.search.annotations.analyzer;import org.hibernate.search.annotations.documentid;import org.hibernate.search.annotations.field;import org.hibernate.search.annotations.indexed;import org.wltea.analyzer.lucene.ikanalyzer;/** * Articles * * @author harmel * */@Indexed @analyzer (impl = Ikanalyzer.class) public class article { @DocumentId private Long id; @Field private string title; @Field private String content; @Field private String description; Private date posttime; private date lastedittime; private int viewcount; private Category category; private set<comment> comments = new HashSet<Comment> ( private set<attachment> attachments); = new HashSet<Attachment> (); // Some getter and setter methods are omitted here //...}
Note Description:
@Indexed: Let the entity support the index
@Analyzer : Set the word breaker, I'm using an open source IK Chinese word breaker
@DocumentID: Index Document ID
@Field : The indexed field, the default property value for the annotation is
store=store.no: Whether the data is stored in the index, whether store=store.no or store=store.yes is not affected by the experiment will not affect the final search. If the store=store.no value is obtained through the database, if the Store=store.yes value is obtained directly from the index document.
Index=index.yes: whether to index
analyze=analyze.yes: whether participle
The index is automatically generated or modified when the annotated entity is saved and updated.
3. Query Index
Public pagemodel<article> searcharticle (int pagenum, int pagesize, string keyword) { fulltextsession fts = search.getfulltextsession ( Sessionfactory.getcurrentsession ()); querybuilder qb = Fts.getsearchfactory (). Buildquerybuilder (). forentity (Article.class). Get (); query Lucenequery = qb.keyword (). Onfields ("title", "Content", "description"). Matching (keyword). CreateQuery (); fulltextquery query = fts.createfulltextquery (luceneQuery , article.class); query.setfirstresult (pagenum - 1) * pageSize); query.setmaxresults (pageSize); list<article> data = query.list (); //Package Paging Data PageModel< Article> model = new pagEmodel<> (Pagenum, pagesize, data.size ()); //Highlight Data model.setdata (Searchutils.hightlight (lucenequery, data, "title", "Content", " description ")); return model;}
The data Highlighting tool method
/*** highlighting article * * @param query {@link org.apache.lucene.search.Query}* @param data data * @param fields fields to be highlighted * @return highlight Data */public static List<article> hightlight (query query, list<article> data, string... Fields) { List<Article> result = new ArrayList< Article> (); formatter formatter = new simplehtmlformatter ("<b style=\ "color:red\" > ", " </b> ") queryscorer queryscorer = new queryscorer (query); highlighter highlighter = new Highlighter (formatter, queryscorer); // use IK chinese word Analyzer analyzer = new ikanalyzer (); for (Article a : data) { // build new objects to return to avoid page confusion (my page is garbled) article article = new article (); for (string fieldname : fields) { // get the field value and assign a value to the new Article object Object fieldValue = ReflectionUtils .invokemethod (Beanutils.getpropertydescriptor (article.class, fieldname). Getreadmethod (), a); reflectionutils.invokemethod ( Beanutils.getpropertydescriptor (Article.class, fieldname). Getwritemethod (), article, fieldvalue); string hightlightfieldvalue = null; try { &nbSp; hightlightfieldvalue = highlighter.getbestfragment (analyzer, Fieldname, string.valueof (fieldvalue)); } catch (exception e) { throw New runtimeexception ("Highlight keyword failed", e); } // re-assign value if highlight is successful if (Hightlightfieldvalue != null) { reflectionutils.invokemethod (Beanutils.getpropertydescriptor (Article.class, fieldName). Getwritemethod (), article,hightlightfieldvalue); } } // Assignment Value id Reflectionutils.invokemethod (Beanutils.getpropertydescriptor (article.class, "id"). GetWriteMethod (), article, a.getid ()); result.add (article); } return result;}
4. Page Iteration Display
<s:iterator value= "#request. Pagemodel.data" ><div class= "article" > <div class= "Article_title_area" > <span class= "Article_title" ><a href= "${pagecontext.request.contextpath }/article/show.action?" id=${id } ">${title }</a></span> < span class= "Article_date" > Release date: <s:date name= "Posttime" format= "YYYY-MM-DD&NBSP;HH:MM:SS"/ ></span> </div> <div class= "Article_ Content ">${description }</div> <div class=" Article_count_info " > <span> reading (${viewcount }) </span> <span> Reviews (${comments.size () }) </span> </div></div></s:iterator>
Integrate hibernate search into existing projects for full-text indexing