有兩個實體和第一個實體被稱爲第二個實體。如何使用Hibernate Lucene Search訪問在實體中的外鍵的排序字段名稱?
實體1:
@Indexed
public abstract class Yesh implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Long id;
@Fields({ @Field(index = Index.YES, store = Store.NO), @Field(name = "YeshName_for_sort", index = Index.YES, analyzer = @Analyzer(definition = "customanalyzer")) })
@Column(name = "NAME", length = 100)
private String name;
public Yesh() {
}
public Yesh (Long id) {
this.id = id;
}
public Yesh (Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "com.Prac.Yesh[ id=" + id + " ]";
}
}
實體2:
public class Kash implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Long id;
@IndexedEmbedded
@ManyToOne
Yesh yes; //Contain reference to first entity
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Yesh getYes() {
return yes;
}
public void setId(Yesh yes) {
this.yes = yes;
}
}
有一個在實體2沒有註解對參考Yesh;實體1具有用名稱「YeshName_for_sort」註解的字段。但是當我嘗試在下面的例子中給出訪問上述領域:
主類:
FullTextEntityManager ftem = Search.getFullTextEntityManager(factory.createEntityManager());
QueryBuilder qb = ftem.getSearchFactory().buildQueryBuilder().forEntity(Kash.class).get();
org.apache.lucene.search.Query query = qb.all().getQuery();
FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, Kash.class);
//fullTextQuery.setSort(new Sort(new SortField("YeshName_for_sort", SortField.STRING, true)));
The above statement is not working and i have also tried to replace YeshName_for_sort with 'yes' reference but it is not working.
fullTextQuery.setFirstResult(0).setMaxResults(150);
int size = fullTextQuery.getResultSize();
List<Yesh> result = fullTextQuery.getResultList();
for (Yeshuser : result) {
logger.info("Yesh Name:" + user.getName());
}
排序不起作用。我也試圖改變像聲明:
ftem.createFullTextQuery(query, Kash.class, Yesh.class); //added entity 1
或
fullTextQuery.setSort(new Sort(new SortField("yes.name", SortField.STRING, true))); // Added property name for Yesh yes;
,但它無法正常工作。
需要在實體中實現哪些註釋或主程序中的任何更改以訪問該字段進行排序?
謝謝。展望合乎邏輯剛剛看到第一個實體(Yesh)是抽象的,因此它正在給 - >「」抽象類永遠不能插入索引文檔「」;它不工作的原因。我已經索引了Kash,但同樣的問題。想辦法解決它。將通過它的一些文檔。如果您有任何建議,請分享 – fatherazrael
您不能在抽象類中添加@索引,您必須在每個子類上定義@索引。這就是說,你可以搜索一個抽象類,因爲Hibernate Search支持多態。 –
謝謝。做過某事。我已經使用了Luke並檢查了索引,發現它正如你所提到的那樣將它存儲爲「yes.YeshName_for_sort」。我沒有索引抽象實體,但它仍然沒有顯示排序結果,可能是「analyzer = @Analyzer(definition =」customanalyzer「))」不適用於抽象類。仔細研究它。 – fatherazrael