[英]Indexing in Hibernate Search not working properly
我正在嘗試將Hibernate Search集成到我的后端,但是我無法使其按預期運行。
我的目的是獲取以下所有model.Course
只標記“Java”的元組:
目前,我只得到了model.Course
與id
= 26,這意味着Lucene的工作,但我不知道如何創建執行我想要的操作所需的查詢。
這是我的CourseService
代碼:
private Session session;
private FullTextSession fts;
public CourseService(){
SessionFactory sf = SessionFactoryManager.getInstance(); // Singleton
this.session = sf.openSession();
this.fts = Search.getFullTextSession(this.session);
}
public List<Course> searchCourses(String token) {
try {
this.fts.createIndexer().startAndWait();
} catch (InterruptedException e) {
e.printStackTrace();
}
Transaction tx = this.fts.beginTransaction();
QueryBuilder queryBuilder = this.fts.getSearchFactory()
.buildQueryBuilder()
.forEntity(Course.class)
.get();
org.apache.lucene.search.Query query = queryBuilder.simpleQueryString()
.onField("description").andField("name")
.matching(token)
.createQuery();
org.hibernate.query.Query hibQuery = fts.createFullTextQuery(query, Course.class);
List result = hibQuery.getResultList();
tx.commit();
return result;
}
這是我的模型。 model.Course
代碼:
@Entity
@Indexed
@Inheritance(strategy = InheritanceType.JOINED)
public class Course implements Comparable<Course>{
@Id
@DocumentId
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
@Field (termVector = TermVector.YES)
private String name;
@Field
private String description;
/*...*/
protected Course(String name, String description, ...) {
/*...*/
}
重要編輯:我最初認為這是查詢問題,但實際上是索引問題。
我不知道如何解決這類問題。 我在聽建議。 細節:
SessionFactory
使用單例(我不是在編輯之前) org.hibernate:hibernate-search-orm:5.8.2.Final
看起來像是一個分析問題,例如,分析人員選擇不將字段拆分為令牌。
有點奇怪的是,默認分析器(StandardAnalyzer)將字段拆分為令牌,因此它應該開箱即用。
您是否設置了其他默認分析器(例如KeywordAnalyzer)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.