我正在使用Spring和Hibernate项目,我正在使用Hibernate Lucene Search。它搜索得很好但是在显示结果时它会显示如下(即如果我搜索标题)
[id: 10 | title:easylib, id: 11 | title:IBM, id: 12 | title:Wipro]
但我想要的是它应该只展示'Wipro',而不是它的id或其他东西
这是我的代码(在标题的POJO类中):
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
@Column(name = "title", nullable = false, length = 150)
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
这是我的DAO课程
private void doIndex() throws InterruptedException {
Session session = getSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
}
private List<CatalogueBase> searchTitle(String queryString) {
Session session = getSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(CatalogueBase.class).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("subTitle","publishedplace","title").matching(queryString).createQuery();
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, CatalogueBase.class);
List<CatalogueBase> contactList = fullTextQuery.list();
return contactList;
}
@Override
public List<CatalogueBase> getSearchDao(String search) throws InterruptedException {
doIndex();
List<CatalogueBase> result = searchTitle(search);
return result;
}
答案 0 :(得分:1)
我得到了答案,我的错误是
在:
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("id: ").append(this.getId()).append(" | title:").append(this.getTitle());
return stringBuilder.toString();
}
后:
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder().append(this.getTitle());
return stringBuilder.toString();
}
答案 1 :(得分:0)
查询代码未呈现您正在“显示”的输出。我想你可能只是在生成的List<CatalogueBase>
上调用 toString()方法。您说您从查询中获得了正确的结果,现在只需转换CatalogueBase实例。
加载CatalogueBase实例的另一种方法是使用投影:
org.hibernate.Query fullTextQuery = fts.createFullTextQuery(luceneQuery, CatalogueBase.class);
fullTextQuery.setProjection("title");
List contactList = fullTextQuery.list();
答案 2 :(得分:0)
确保在您的字段中添加此注释@Field(store = Store.YES)
,然后使用fullTextQuery.setProjection("title");
,然后才能使用它。