我试图在Domain.class实体上进行简单搜索。我有两个测试。第一个应创建索引,第二个执行实际搜索。当我运行索引时,它只向DB运行25-30个select语句。当我进行搜索测试时,它只能获得13次点击。应该是大约6300次点击。
这是我的代码:
public class luceneTests {
DAOFacade dao = PMF.getTestDAO();
@Test(enabled = false)//only run once to fill the index
public void runFirstOnly() throws InterruptedException{
EntityManager em = dao.getEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
fullTextEntityManager.createIndexer(Domain.class).startAndWait();
}
@Test(enabled = true)
public void simpleSearchTest(){
EntityManager em = dao.getEntityManager();
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
em.getTransaction().begin();
// create native Lucene query unsing the query DSL
// alternatively you can write the Lucene query using the Lucene query parser
// or the Lucene programmatic API. The Hibernate Search DSL is recommended though
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity( Domain.class ).get();
org.apache.lucene.search.Query query = qb
.keyword().wildcard()
.onField("domain")
// .andField("state")
.matching("domaindom*")
.createQuery();
// wrap Lucene query in a javax.persistence.Query
javax.persistence.Query persistenceQuery =
fullTextEntityManager.createFullTextQuery(query, Domain.class).;
// execute search
List result = persistenceQuery.setFirstResult(0).setMaxResults(100).getResultList();
System.out.println("SIZE : " +result.size());
for(Object o : result){
if(o instanceof Domain)
System.out.println(" Object: "+o + ((Domain)o).getDomainId() + " " + ((Domain)o).getNameservers());
}
em.getTransaction().commit();
em.close();
}
}
域名实体
@Entity
@Indexed
@Table(name="Domain", schema="domains")
@DiscriminatorValue(value="DOMAIN")
public class Domain implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="domainId", nullable=false)
public String domainId;
@Column(name="domain", nullable=false)
@Field(index = Index.UN_TOKENIZED, store= Store.NO)
public String domain;
// there is alot more in this entity but I dont belive its needed.
关于我在这里做错什么的任何建议?
答案 0 :(得分:0)
如何在DAO中创建EntityManager?即。
EntityManager em = dao.getEntityManager();
也许这个EntityManager不会看到之前所做的更改......