Lucene 只找到了一些命中

发布于 2024-12-13 14:26:07 字数 2577 浏览 0 评论 0原文

我尝试对 Domain.class 实体进行简单搜索。我有两个测试。第一个应该创建索引,第二个进行实际搜索。当我运行索引时,它只对数据库运行 25-30 个选择语句。当我进行搜索测试时,它只获得 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.

关于我在这里做错了什么有什么建议吗?

Im trying to do a simple search on a Domain.class entity. I have two test. The first should create the indexes and the second does a actual search. When I run the index it only runs 25-30 select statements to the DB. When I do my search test it only get 13 hits. It should be around 6300 hits.

This is my code:

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();
    }
}

domain entity

@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.

Any suggestions on what im doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-12-20 14:26:07

如何在 DAO 中创建 EntityManager? IE

EntityManager em = dao.getEntityManager();

也许这个 EntityManager 没有看到之前所做的更改......

How do you create your EntityManager in the DAO? I.e.

EntityManager em = dao.getEntityManager();

Maybe this EntityManager doesn't see the changes made before ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文