Hibernate Search JPA + Lucene Simple Application Example

Source: Internet
Author: User

Let's say there's a scenario for this. A large number of articles are stored in the database, and we want to retrieve the relevant articles from the database by entering keywords.

1. Establish article Entity:

public class Article {private String ID;         Idprivate String title;       Title Private String content; Content private String ispublication; Publication status public String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;} Public String getispublication () {return ispublication;} public void Setispublication (String ispublication) {this.ispublication = ispublication;}}


2. Build the index:

Import Javax.persistence.entitymanager;import Javax.persistence.persistencecontext;import Org.hibernate.search.jpa.fulltextentitymanager;import Org.hibernate.search.jpa.search;public class IndexArticle {@ persistencecontextprotected entitymanager entitymanager;public void Indexarticle () {//... The specific encoding is not finely written//list<article> articles = articleservice.findlist (null, NULL, NULL, first, count);//for (article article : articless) {//index (article);//}}public void index (article article) {//hibernate Search Jpafulltextentitymanager Fulltextentitymanager = Search.getfulltextentitymanager (Entitymanager); Fulltextentitymanager.index (articles);}}


3. Search Keywords:

Import Java.util.list;import Javax.persistence.entitymanager;import Javax.persistence.persistencecontext;import Org.apache.lucene.index.term;import Org.apache.lucene.queryparser.parseexception;import Org.apache.lucene.queryparser.queryparser;import Org.apache.lucene.search.booleanclause.occur;import Org.apache.lucene.search.booleanquery;import Org.apache.lucene.search.fuzzyquery;import Org.apache.lucene.search.query;import Org.apache.lucene.search.termquery;import org.apache.lucene.util.Version; Import Org.hibernate.search.jpa.fulltextentitymanager;import Org.hibernate.search.jpa.fulltextquery;import Org.hibernate.search.jpa.search;import Org.wltea.analyzer.lucene.ikanalyzer;import Com.dowik.dwshop.mall.entity.article;public class Searcharticle {@PersistenceContextprotected Entitymanager Entitymanager;public list<article> searchresult (String keyword) {try {//keyword to search keyword = Queryparser.escape ( keyword);//parser, Chinese word breaker queryparser titleparser = new Queryparser (version.lucene_35, "title", new Ikanalyzer ()); Titleparser.setdefaultoperator (Queryparser.and_operator); Query titlequery = titleparser.parse (keyword);//Fuzzy search title Fuzzyquery titlefuzzyquery = new Fuzzyquery (New term ("title", keyword), 0.5F);//Search by entry query contentquery = new Termquery (New term ("content", keyword);//Search by Entry Publish status query Ispublicationquery = new Termquery (New term ("Ispublication", "true"));//Full Text Search (Title Search + content Search) Booleanquery textquery = new Booleanquery (); Textquery.add (Titlequery, occur.should); Textquery.add (Titlefuzzyquery, Occur.SHOULD); Textquery.add (Contentquery, occur.should);//Publish Status + full-text search booleanquery query = new Booleanquery (); Query.add ( Ispublicationquery, Occur.must); Query.add (Textquery, occur.must);//hibernate Search Jpafulltextentitymanager Fulltextentitymanager = Search.getfulltextentitymanager (Entitymanager); FullTextQuery fulltextquery = fulltextentitymanager.createfulltextquery (query, Articles.class); list<article> resultlist = Fulltextquery.getresultlist (); return resultlist;} catch (ParseException E) {e.printstacktrace ();} return null;}}


The above three steps build a simple application example of Hibernate Search JPA + Lucene.

Hibernate Search JPA + Lucene Simple Application Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.