国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Hibernate Search使用以及搜索結(jié)果高亮顯示

2019-11-10 18:09:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Hibernate Search使用以及搜索結(jié)果高亮顯視

1、首先說(shuō)一下需求

有兩個(gè)實(shí)體:?jiǎn)栴}實(shí)體(Question)和選項(xiàng)實(shí)體(QuestionOption),兩個(gè)實(shí)體間是一對(duì)多關(guān)系,需求如下:根據(jù)問(wèn)題(questionContent字段)或選項(xiàng)(questionOptionContent字段)進(jìn)行搜索,并將搜索結(jié)果高亮顯示。

2、所需jar包(gradle項(xiàng)目)

'org.hibernate:hibernate-core:5.0.9.Final','org.hibernate:hibernate-java8:5.0.9.Final','org.hibernate:hibernate-ehcache:5.0.9.Final','org.hibernate:hibernate-entitymanager:5.0.9.Final','org.hibernate:hibernate-search-engine:5.5.4.Final','org.hibernate:hibernate-search-orm:5.5.4.Final','org.apache.lucene:lucene-core:5.3.1','org.apache.lucene:lucene-analyzers-smartcn:5.3.1','org.apache.lucene:lucene-highlighter:5.3.1'

3、實(shí)體注解如下(具體參照hibernate search 官網(wǎng)):

import java.util.HashSet;import java.util.Set;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import static javax.persistence.GenerationType.IDENTITY;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.OneToMany;import javax.persistence.Table;import javax.persistence.UniqueConstraint;import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;import org.hibernate.search.annotations.Analyze;import org.hibernate.search.annotations.Analyzer;import org.hibernate.search.annotations.Field;import org.hibernate.search.annotations.Index;import org.hibernate.search.annotations.Indexed;import org.hibernate.search.annotations.IndexedEmbedded;import org.hibernate.search.annotations.Store;/** * Question generated by hbm2java * updated by 馬輝 2016-11-29 21:46 */@Indexed@Entity@Table(name = "question", catalog = "mryt", uniqueConstraints = @UniqueConstraint(columnNames = "question_sn"))@Analyzer(impl=SmartChineseAnalyzer.class)//分詞器public class Question implements java.io.Serializable {	PRivate static final long serialVersionUID = 4651161939667149753L;	private Integer id;	private String questionSn;	private String questionContent;	private Set<QuestionOption> questionOptions = new HashSet<QuestionOption>(0);	@Id	@GeneratedValue(strategy = IDENTITY)	@Column(name = "id", unique = true, nullable = false)	public Integer getId() {		return this.id;	}	public void setId(Integer id) {		this.id = id;	}	@Column(name = "question_sn", unique = true, length = 50)	public String getQuestionSn() {		return this.questionSn;	}	public void setQuestionSn(String questionSn) {		this.questionSn = questionSn;	}	@Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)	@Column(name = "questionContent", length = 65535)	public String getQuestionContent() {		return this.questionContent;	}	public void setQuestionContent(String questionContent) {		this.questionContent = questionContent;	}	@IndexedEmbedded	@OneToMany(targetEntity=QuestionOption.class, fetch = FetchType.LAZY, mappedBy = "question")	public Set<QuestionOption> getQuestionOptions() {		return this.questionOptions;	}	public void setQuestionOptions(Set<QuestionOption> questionOptions) {		this.questionOptions = questionOptions;	}}
import java.util.HashSet;import java.util.Set;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Table;import javax.persistence.UniqueConstraint;import org.hibernate.search.annotations.Analyze;import org.hibernate.search.annotations.Field;import org.hibernate.search.annotations.Index;import org.hibernate.search.annotations.Indexed;import org.hibernate.search.annotations.Store;/** * QuestionOption generated by hbm2java * updated by 馬輝 2016-11-29 21:48 */@Indexed@Entity@Table(name = "question_option", catalog = "mryt", uniqueConstraints = @UniqueConstraint(columnNames = "question_option_sn"))public class QuestionOption implements java.io.Serializable {	private static final long serialVersionUID = 3055805431841974260L;	private int id;	private Question question;	private String questionOptionSn;	private String questionOptionContent;	private boolean isAnswer;	private Set<EmployeeQuestionOption> employeeQuestionOptions = new HashSet<EmployeeQuestionOption>(0);	@Id	@Column(name = "id", unique = true, nullable = false)	public int getId() {		return this.id;	}	public void setId(int id) {		this.id = id;	}	@ManyToOne(targetEntity=Question.class,fetch = FetchType.LAZY)	@JoinColumn(name = "question_sn",referencedColumnName="question_sn", nullable = false)	public Question getQuestion() {		return this.question;	}	public void setQuestion(Question question) {		this.question = question;	}	@Column(name = "question_option_sn", unique = true, nullable = false, length = 60)	public String getQuestionOptionSn() {		return this.questionOptionSn;	}	public void setQuestionOptionSn(String questionOptionSn) {		this.questionOptionSn = questionOptionSn;	}	@Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)	@Column(name = "question_option_content", nullable = false, length = 65535)	public String getQuestionOptionContent() {		return this.questionOptionContent;	}	public void setQuestionOptionContent(String questionOptionContent) {		this.questionOptionContent = questionOptionContent;	}	@Column(name = "is_answer", nullable = false)	public boolean getIsAnswer() {		return isAnswer;	}	public void setIsAnswer(boolean isAnswer) {		this.isAnswer = isAnswer;	}	@OneToMany(targetEntity=EmployeeQuestionOption.class, fetch = FetchType.LAZY, mappedBy = "questionOption")	public Set<EmployeeQuestionOption> getEmployeeQuestionOptions() {		return this.employeeQuestionOptions;	}	public void setEmployeeQuestionOptions(Set<EmployeeQuestionOption> employeeQuestionOptions) {		this.employeeQuestionOptions = employeeQuestionOptions;	}}

4、搜索以及高亮 

/**	 * @method 私有方法(打包高亮問(wèn)題json)	 * @param question	 * @author mahui	 * @return JSONObject	 */	private JSONObject packSearchQuestion(Question question,Query luceneQuery,String str){  		SimpleHTMLFormatter formatter = new SimpleHTMLFormatter( "<span style='color:red;'>", "</span>");        	QueryScorer qs = new QueryScorer(luceneQuery);          	Highlighter highlighter = new Highlighter( formatter, qs);         	Analyzer analyzer = new SmartChineseAnalyzer();        		JSONObject jsonObject=new JSONObject();				JSONObject qjo=new JSONObject();		qjo.put("questionSn", question.getQuestionSn());		//存放高亮問(wèn)題		String questionContent="";		try {          		questionContent = highlighter.getBestFragment(analyzer, str, question.getQuestionContent());         	} catch (InvalidTokenOffsetsException e) {              		e.printStackTrace();          	} catch (IOException e) {			e.printStackTrace();		}          	//重新封裝問(wèn)題          	if(questionContent!=null&&questionContent.trim().length()>0){        		qjo.put("questionContent", questionContent);        	}else{        		qjo.put("questionContent", question.getQuestionContent());        	}				JSONArray array=new JSONArray();		int i=0;		//選項(xiàng)高亮		for(QuestionOption questionOption:question.getQuestionOptions()){			JSONObject jo=new JSONObject();			jo.put("order", (char)('A'+i));			jo.put("optionSn", questionOption.getQuestionOptionSn());							//存放高亮選項(xiàng)			String optionContent="";			try {  				optionContent = highlighter.getBestFragment(analyzer, str, questionOption.getQuestionOptionContent()); 		        } catch (InvalidTokenOffsetsException e) {  		            	e.printStackTrace();  		        } catch (IOException e) {				e.printStackTrace();			}  		        //重新封裝選項(xiàng) 		        if(optionContent!=null&&optionContent.trim().length()>0){		        	jo.put("optionContent", optionContent);		        }else{		        	jo.put("optionContent", questionOption.getQuestionOptionContent());		        }				jo.put("isAnswer", questionOption.getIsAnswer());				array.add(jo);				i++;			}		jsonObject.put("question", qjo);		jsonObject.put("options", array);		return jsonObject;	}		/**	 * @method 全文檢索問(wèn)題	 * @param str	 * @author mahui	 * @return JSONArray	 */	@SuppressWarnings("unchecked")	@Override	public JSONArray fullTextQuery(String str) {		FullTextsession fullTextSession = Search.getFullTextSession(getSession());		List<Question> list=new ArrayList<Question>();		try {			fullTextSession.createIndexer().startAndWait();		} catch (InterruptedException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		QueryBuilder qb=fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Question.class).get();		org.apache.lucene.search.Query luceneQuery = qb				  .keyWord()				  .onFields("questionContent", "questionOptions.questionOptionContent")				  .matching(str)				  .createQuery();		FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery, Question.class);		list=query.setMaxResults(2).list();		JSONArray array=new JSONArray();		for(Question question:list){			array.add(packSearchQuestion(question,luceneQuery,str));		}               		return array;	}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 准格尔旗| 武夷山市| 敦化市| 伽师县| 延川县| 清远市| 岳西县| 镇沅| 谢通门县| 闻喜县| 葫芦岛市| 芜湖市| 丹寨县| 千阳县| 安福县| 喀喇沁旗| 阳城县| 武宁县| 江阴市| 法库县| 江达县| 庄浪县| 仁寿县| 景洪市| 闻喜县| 镇赉县| 霞浦县| 扶风县| 台北县| 五寨县| 大竹县| 阳朔县| 深水埗区| 兰溪市| 白沙| 平舆县| 拉萨市| 永胜县| 织金县| 宾阳县| 灌云县|