gpt4 book ai didi

lucene - 如何使用休眠 lucene 搜索具有多对一关系的实体

转载 作者:行者123 更新时间:2023-12-04 04:37:41 26 4
gpt4 key购买 nike

我正在使用 Hibernate lucene 进行搜索。现在我想搜索一个具有多对一关系的实体

我有两个类(class)是catalogueBase另一个是 Subject ,这里主题有一个多对一的关系(这是单方面的关系)

catalogueBase.java 类:

@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(我想搜索的主题将存储在描述列中):

@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;
}

// ....
}

这是我的 DAO 方法:

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

但它给出了一个错误,如: SearchException: Unable to find field subject in com.easylib.elibrary.model.CatalogueBase
我做了类似 this post 的事情,但错误是一样的。

最佳答案

我得到了解决方案。

我只会贴代码...

@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;
}
}

在目录中:

    @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;
}

而在 DAO 中,它必须是:

org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("subject.description").matching(queryString).createQuery();

关于lucene - 如何使用休眠 lucene 搜索具有多对一关系的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19444543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com