質問

I am using Hibernate lucene for searching. Now I want to search with an entity which has a many to one relation

I have two class one is catalogueBase and another one is Subject, here subject has a many-to-one relation (it is one sided relation)

catalogueBase.java class :

@Indexed
@JsonAutoDetect
@Entity
@Table(name="catalogueBase")
public class CatalogueBase extends BaseObject implements Serializable {

    // some entities
    // ...
    private Subject subject; 

    // setter and get methods 
    // ...

    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
    @ManyToOne
    @NotFound(action= NotFoundAction.IGNORE)
    @JoinColumn(name = "subject1", insertable = true, updatable=true, nullable = true)
    @JsonProperty
    public Subject getSubject() {
        return subject;
    }
    public void setSubject(Subject subject) {
        this.subject = subject;
    }
}

Subject.java (what ever i want to search regarding subject it will be stored in description column) :

@Indexed
@JsonAutoDetect
@Entity
@Table(name="subject")
public class Subject implements java.io.Serializable {

    private String description;

    // ...
    @Column(name = "subjectname", nullable = false, length = 150)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    // ....
}

this is my DAO method :

private List<CatalogueBase> searchTitle(String queryString) throws InterruptedException {
    Session session = getSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session); 
    fullTextSession.createIndexer().startAndWait();
    org.hibernate.Query fullTextQuery = null;
    List<CatalogueBase> resultList = null;
    try{
        QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(CatalogueBase.class).get();
        org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("title","subject").matching(queryString).createQuery();     
        fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, CatalogueBase.class);

        List<CatalogueBase> contactList = fullTextQuery.list();

        resultList = new ArrayList<CatalogueBase>();;

        for (CatalogueBase catalogueBase : contactList) {
            catalogueBase.setNoOfCopiesBooks(getCopydetailsCount(catalogueBase.getId()));
            catalogueBase.setIssuedCount(getIssuedCount(catalogueBase.getId()));
            resultList.add(catalogueBase);
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    return resultList;
}

But it's giving an error like: SearchException: Unable to find field subject in com.easylib.elibrary.model.CatalogueBase

And I did something like this post, but error was the same.

役に立ちましたか?

解決

I got solution.

I will just post code...

@Indexed  // must
@JsonAutoDetect
@Entity
@Table(name="subject")
public class Subject implements java.io.Serializable {

    private String description;

    @ContainedIn   // must
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
    @Column(name = "subjectname", nullable = false, length = 150)
    public String getDescription() {
        return this.description;
    }
}

In catalogue:

    @ManyToOne
    @NotFound(action= NotFoundAction.IGNORE)
    @JoinColumn(name = "subject1", insertable = true, updatable=true, nullable = true)
    @JsonProperty
    @IndexedEmbedded   // must
    public Subject getSubject() {
        return subject;
    }
    public void setSubject(Subject subject) {
        this.subject = subject;
    }

And in the DAO, it must be:

org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("subject.description").matching(queryString).createQuery();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top