如何使用 Hibernate Lucene Search 访问实体中外键的排序字段名称?
How to access Sort Field name of Foreign Key in entity using 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;
}
}
引用 Yesh 的实体 2 中没有注释;实体 1 的字段用名称 "YeshName_for_sort" 注释。但是当我尝试访问上面的字段时,如下例所示:
主要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());
}
排序无效。我还尝试更改如下语句:
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;
但它不起作用。
需要在实体中实现哪些注解或在主程序中进行任何更改才能访问字段进行排序?
我不确定这是否适合您,但让我们试一试:
在 class Yesh
中的 属性 name
添加以下注释:
@Fields( {
@Field,
@Field(name = "name_sort", analyze = Analyze.NO, store = Store.YES)
} )
private String name;
按字段对查询排序需要未分析的字段。如果要在此 属性 中按单词搜索并仍然对其进行排序,则需要对其进行两次索引 - 一次分析和一次未分析。
查询中的第 2 个应用您想要的排序:
ftem.createFullTextQuery(query, Kash.class, Yesh.class);
fullTextQuery.setSort(new Sort(new SortField("name_sort", SortField.STRING, true)));
您正在使用 @IndexedEmbedded,因此您需要使用完整路径引用该字段,即 yes.YeshName_for_sort(如果是错字,则为 yesh)。
当您使用@IndexedEmbedded 时,您在 Lucene 文档中使用点分符号包含嵌套字段。
因此:
FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, Kash.class);
fullTextQuery.setSort(new Sort(new SortField("yes.YeshName_for_sort", SortField.STRING, true)));
应该可以。
请注意,您的代码并不真正一致,因为您首先搜索 Kash 对象,然后操作 Yesh 对象。我想是 copy/pasto.
我建议您阅读一些有关 Hibernate 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;
}
}
引用 Yesh 的实体 2 中没有注释;实体 1 的字段用名称 "YeshName_for_sort" 注释。但是当我尝试访问上面的字段时,如下例所示:
主要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());
}
排序无效。我还尝试更改如下语句:
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;
但它不起作用。
需要在实体中实现哪些注解或在主程序中进行任何更改才能访问字段进行排序?
我不确定这是否适合您,但让我们试一试:
在 class Yesh
中的 属性 name
添加以下注释:
@Fields( {
@Field,
@Field(name = "name_sort", analyze = Analyze.NO, store = Store.YES)
} )
private String name;
按字段对查询排序需要未分析的字段。如果要在此 属性 中按单词搜索并仍然对其进行排序,则需要对其进行两次索引 - 一次分析和一次未分析。
查询中的第 2 个应用您想要的排序:
ftem.createFullTextQuery(query, Kash.class, Yesh.class);
fullTextQuery.setSort(new Sort(new SortField("name_sort", SortField.STRING, true)));
您正在使用 @IndexedEmbedded,因此您需要使用完整路径引用该字段,即 yes.YeshName_for_sort(如果是错字,则为 yesh)。
当您使用@IndexedEmbedded 时,您在 Lucene 文档中使用点分符号包含嵌套字段。
因此:
FullTextQuery fullTextQuery = ftem.createFullTextQuery(query, Kash.class);
fullTextQuery.setSort(new Sort(new SortField("yes.YeshName_for_sort", SortField.STRING, true)));
应该可以。
请注意,您的代码并不真正一致,因为您首先搜索 Kash 对象,然后操作 Yesh 对象。我想是 copy/pasto.
我建议您阅读一些有关 Hibernate Search 是如何构建索引的内容:这样您会更容易理解这类事情。