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

首頁 > 學院 > 開發設計 > 正文

Hibernate Search使用以及搜索結果高亮顯示

2019-11-10 18:54:25
字體:
來源:轉載
供稿:網友

Hibernate Search使用以及搜索結果高亮顯視

1、首先說一下需求

有兩個實體:問題實體(Question)和選項實體(QuestionOption),兩個實體間是一對多關系,需求如下:根據問題(questionContent字段)或選項(questionOptionContent字段)進行搜索,并將搜索結果高亮顯示。

2、所需jar包(gradle項目)

'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、實體注解如下(具體參照hibernate search 官網):

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 私有方法(打包高亮問題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());		//存放高亮問題		String questionContent="";		try {          		questionContent = highlighter.getBestFragment(analyzer, str, question.getQuestionContent());         	} catch (InvalidTokenOffsetsException e) {              		e.printStackTrace();          	} catch (IOException e) {			e.printStackTrace();		}          	//重新封裝問題          	if(questionContent!=null&&questionContent.trim().length()>0){        		qjo.put("questionContent", questionContent);        	}else{        		qjo.put("questionContent", question.getQuestionContent());        	}				JSONArray array=new JSONArray();		int i=0;		//選項高亮		for(QuestionOption questionOption:question.getQuestionOptions()){			JSONObject jo=new JSONObject();			jo.put("order", (char)('A'+i));			jo.put("optionSn", questionOption.getQuestionOptionSn());							//存放高亮選項			String optionContent="";			try {  				optionContent = highlighter.getBestFragment(analyzer, str, questionOption.getQuestionOptionContent()); 		        } catch (InvalidTokenOffsetsException e) {  		            	e.printStackTrace();  		        } catch (IOException e) {				e.printStackTrace();			}  		        //重新封裝選項 		        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 全文檢索問題	 * @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;	}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐柏县| 茂名市| 青州市| 剑川县| 资源县| 平南县| 通榆县| 陆河县| 嘉禾县| 安顺市| 玉田县| 灵石县| 深水埗区| 阿拉善右旗| 屯昌县| 淮北市| 南乐县| 许昌市| 绍兴市| 离岛区| 双峰县| 湖州市| 梅州市| 江北区| 彭阳县| 丹凤县| 阿拉善右旗| 南投市| 碌曲县| 祁连县| 贵溪市| 临安市| 岳池县| 崇义县| 资溪县| 博罗县| 湛江市| 高雄市| 罗定市| 纳雍县| 莱芜市|