简体   繁体   English

休眠搜索不起作用

[英]Hibernate search is not working

I am developping a java/jee application in which i am using spring boot and hibernate as frameworks i used hibernate search for full text searching but unfortunately i always got an empty list as result.I am using hibernate version 5.1 and hibernate search orm version 5.5.3.Final.Here is my code :我正在开发一个 java/jee 应用程序,其中我使用 spring boot 和 hibernate 作为框架我使用 hibernate search 进行全文搜索,但不幸的是我总是得到一个空列表作为结果。我使用 hibernate 5.1 版和 hibernate search orm 5.5 版.3.Final.Here 是我的代码:

 public void search() {
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(this.em);

            try {
                fullTextEntityManager.createIndexer().startAndWait();
                // 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 queryBuilder = fullTextEntityManager.getSearchFactory() 
                          .buildQueryBuilder()
                          .forEntity(Application.class)
                          .get();
                org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().wildcard().onField("reference").matching("di*") 
                        .createQuery();

                // wrap Lucene query in a javax.persistence.Query
                javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery,
                        Application.class);

                // execute search
                List<Application> result = jpaQuery.getResultList();
                System.out.println("your result list is "+result);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

and here is my entity这是我的实体

package biz.picosoft.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;


@Entity
@Indexed
@Table(name = "application",uniqueConstraints = {@UniqueConstraint(columnNames = "reference")})
public class Application implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
    @Column(name = "reference") 
    private String  reference;
    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
    @Column(name = "creationDate")
    private Date creationDate;
    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
    @Column(name = "status")
    private String status;
    @Field(index = Index.YES,  analyze = Analyze.NO,store = Store.YES)
    @Column(name = "deadLine")
    private Date deadLine;
    @Field(index = Index.YES,  analyze = Analyze.NO,store = Store.YES)
    @Column(name = "appType")
    private String appType;
    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
    @Column(name = "projectId")
    private  Long projectId;


    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "App_files", joinColumns = { @JoinColumn(name = "idApp") }, inverseJoinColumns = {
            @JoinColumn(name = "idFile") })
    private List<FileMetadata> listePiecesJointes = new ArrayList<FileMetadata>();
    public Long getId() {
        return id;
    }

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

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getDeadLine() {
        return deadLine;
    }

    public void setDeadLine(Date deadLine) {
        this.deadLine = deadLine;
    }


    public String getAppType() {
        return appType;
    }

    public void setAppType(String appType) {
        this.appType = appType;
    }

    public Application() {
        super();
    }



    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }




    public List<FileMetadata> getListePiecesJointes() {
        return listePiecesJointes;
    }

    public void setListePiecesJointes(List<FileMetadata> listePiecesJointes) {
        this.listePiecesJointes = listePiecesJointes;
    }

    public Application(String reference, Date creationDate, String status, Date deadLine, String appType,Long projectId) {
        super();
        this.reference = reference;
        this.creationDate = creationDate;
        this.status = status;
        this.deadLine = deadLine;
        this.appType = appType;
        this.projectId=projectId;

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((appType == null) ? 0 : appType.hashCode());
        result = prime * result + ((creationDate == null) ? 0 : creationDate.hashCode());
        result = prime * result + ((deadLine == null) ? 0 : deadLine.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((projectId == null) ? 0 : projectId.hashCode());
        result = prime * result + ((reference == null) ? 0 : reference.hashCode());
        result = prime * result + ((status == null) ? 0 : status.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Application other = (Application) obj;
        if (appType == null) {
            if (other.appType != null)
                return false;
        } else if (!appType.equals(other.appType))
            return false;
        if (creationDate == null) {
            if (other.creationDate != null)
                return false;
        } else if (!creationDate.equals(other.creationDate))
            return false;
        if (deadLine == null) {
            if (other.deadLine != null)
                return false;
        } else if (!deadLine.equals(other.deadLine))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (projectId == null) {
            if (other.projectId != null)
                return false;
        } else if (!projectId.equals(other.projectId))
            return false;
        if (reference == null) {
            if (other.reference != null)
                return false;
        } else if (!reference.equals(other.reference))
            return false;
        if (status == null) {
            if (other.status != null)
                return false;
        } else if (!status.equals(other.status))
            return false;
        return true;
    }



    @Override
    public String toString() {
        return "Application [id=" + id + ", reference=" + reference + ", creationDate=" + creationDate + ", status="
                + status + ", deadLine=" + deadLine + ", appType=" + appType + ", projectId=" + projectId + "]";
    }




}

我已经解决了我的问题。这是因为没有索引的子类。

Did you make sure your entities are indexed before searching?在搜索之前,您是否确保您的实体已编入索引?

If you are using a pre-existing database, or if you initialized your database without using Hibernate ORM APIs (by using SQL directly, or by restoring a dump), your entities are probably not indexed yet.如果您正在使用预先存在的数据库,或者如果您在不使用 Hibernate ORM API 的情况下初始化您的数据库(通过直接使用 SQL,或通过恢复转储),您的实体可能尚未被索引。

You should have a look at the mass indexer, which makes it easy to index a lot of entities: https://docs.jboss.org/hibernate/search/5.5/reference/en-US/html_single/#search-batchindex-massindexer你应该看看质量索引器,它可以很容易地索引很多实体: https : //docs.jboss.org/hibernate/search/5.5/reference/en-US/html_single/#search-batchindex-质量指标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM