[英]How to access Sort Field name of Foreign Key in entity using Hibernate Lucene Search?
There are two entities and first entity is referred into second entity. 有两个实体,第一个实体称为第二个实体。
Entity 1: 实体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 + " ]";
}
}
Entity 2: 实体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;
}
}
There is no annotation in Entity 2 on reference Yesh; 实体2中没有关于引用Yesh的注释; Entity 1 have field annotated with name "YeshName_for_sort". 实体1的字段注释为名称“ YeshName_for_sort”。 But when i try to access above field as given in following example: 但是当我尝试访问以下示例中给出的字段时:
Main Class: 主类:
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());
}
Sorting does not work. 排序不起作用。 I also tried to change statements like: 我还尝试更改以下语句:
ftem.createFullTextQuery(query, Kash.class, Yesh.class); //added entity 1
or 要么
fullTextQuery.setSort(new Sort(new SortField("yes.name", SortField.STRING, true))); // Added property name for Yesh yes;
but it is not working. 但它不起作用。
What annotations need to be implemented in entities or any changes in main program to access the field for sorting? 需要在实体中实现哪些注释或对主程序进行任何更改才能访问该字段以进行排序?
You are using @IndexedEmbedded so you need to reference the field with its full path namely yes.YeshName_for_sort (or yesh if the yes was a typo). 您正在使用@IndexedEmbedded,因此您需要使用完整路径(即yes.YeshName_for_sort)(如果是,则是yesh)来引用该字段的完整路径。
When you use @IndexedEmbedded, you include the nested fields in your Lucene document with a dotted notation. 当您使用@IndexedEmbedded时,您会在Lucene文档中包含带点划线的嵌套字段。
Thus: 从而:
FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, Kash.class);
fullTextQuery.setSort(new Sort(new SortField("yes.YeshName_for_sort", SortField.STRING, true)));
should work. 应该管用。
Note that your code is not really consistent because you start by searching for Kash objects then you manipulate Yesh objects. 请注意,您的代码并不是真正一致的,因为您首先搜索Kash对象,然后再操作Yesh对象。 I suppose it's a copy/pasto. 我想这是副本/粘贴。
I recommend you to read a bit about how the indexes are built by Hibernate Search: it will then be easier for you to understand this sort of things. 我建议您阅读一些有关Hibernate Search如何构建索引的信息:这样您就可以更轻松地了解这种情况。
I m not sure that this is going to work for you but let s give it a try : add the following annotation to property name
in the class Yesh
: 我不确定这是否对您有用,但让我们尝试一下:在Yesh
类的属性name
中添加以下注释:
@Fields( {
@Field,
@Field(name = "name_sort", analyze = Analyze.NO, store = Store.YES)
} )
private String name;
sorting a query by field requires the field to be un-analyzed. 按字段对查询进行排序要求不对字段进行分析。 If one wants to search by words in this property and still sort it, one need to index it twice - once analyzed and once un-analyzed. 如果要通过该属性中的单词进行搜索并仍然对其进行排序,则需要对其进行两次索引-进行一次分析并进行一次未分析。
2nd in your query apply the sort you want: 查询中的第二个应用所需的排序:
ftem.createFullTextQuery(query, Kash.class, Yesh.class);
fullTextQuery.setSort(new Sort(new SortField("name_sort", SortField.STRING, true)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.