gpt4 book ai didi

java - 如何使用 QueryBuilder、Lucene Query 和 FullTextQuery 显示 hibernate 搜索结果?

转载 作者:行者123 更新时间:2023-11-29 11:24:35 24 4
gpt4 key购买 nike

如何使用仅包含 order 子句的 FullTextEntity Manager 显示结果。

  EntityManager em = factory.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity( Desh.class ).get();

org.apache.lucene.search.Query query = new org.apache.lucene.search.MatchAllDocsQuery();
//Also used qb.all().getQuery(); but no results

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Desh.class);
System.out.println(""+ query);
System.out.println(""+ query.toString());

fullTextQuery.setSort(new Sort(new SortField("DeshName_for_sort", SortField.STRING, true)));
//Also used DESH but no results

fullTextQuery.setFirstResult(0).setMaxResults(60);
System.out.println("" + fullTextQuery);
System.out.println("" + fullTextQuery.getParameters());

int size = fullTextQuery.getResultSize();
List<Desh> result = fullTextQuery.getResultList();
System.out.println("Size e -> "+ size);
System.out.println("Size e -> "+ result.size());
for (Deshuser : result) {
System.out.println("Id: " + user.getId());
System.out.println("Person Id:" + user.getName());
}

Desh - 实体类:

@Entity
@Table(name = "DESH")
@XmlRootElement
@Indexed

public class Desh implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Long id;

@Fields({ @Field(index = Index.YES, store = Store.NO), @Field(name = "DeshName_for_sort", index = Index.YES, analyze = Analyze.NO) })
@Column(name = "NAME", length = 100)
private String name;

public Desh () {
}

public Desh (Long id) {
this.id = id;
}

public Desh (Long id) {
this.id = id;

}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}



public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


@Override
public String toString() {
return "in.hibprac.hibernatepractice.Desh[ id=" + id + " ]";
}

}

当我运行上面的程序时,它不返回任何大小。但是当我使用 Namedquery 和 Native Query 返回时,它执行得很好。即使我删除代码中的排序语句,它也不会返回任何内容。谁能指导我问题出在哪里?

最佳答案

试试这个:

FullTextEntityManager ftem=Search.getFullTextEntityManager(entityManager);
ftem.createIndexer().startAndWait();

启动针对所有用@Indexed注释的实体的索引

关于java - 如何使用 QueryBuilder、Lucene Query 和 FullTextQuery 显示 hibernate 搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com