#hibernate #entities #lucene #hibernate-search
#спящий режим #сущности #lucene #спящий режим -поиск
Вопрос:
Я пытаюсь выполнить простой поиск в Domain.class сущность. У меня есть два теста. Первый должен создавать индексы, а второй выполняет фактический поиск. Когда я запускаю индекс, он запускает только 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.
Любые предложения о том, что я здесь делаю не так?
Ответ №1:
Как вы создаете свой EntityManager в DAO? То есть.
EntityManager em = dao.getEntityManager();
Возможно, этот EntityManager не видит изменений, внесенных ранее…
Комментарии:
1. Я проверил это, и с ними все должно быть в порядке. Спасибо за совет, хотя