如何使用Hibernate Lucene Search访问实体中的外键排序字段名称?

时间:2016-07-21 03:47:00

标签: java hibernate lucene 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”的字段。但是,当我尝试访问上面的字段时,如下例所示:

主类:

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;

但它不起作用。

需要在实体中实施哪些注释或主程序中的任何更改才能访问字段进行排序?

2 个答案:

答案 0 :(得分:1)

您正在使用@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对象。我想它是副本/牧师。

我建议你先阅读一下Hibernate Search如何构建索引:这样你就可以更容易理解这类内容了。

答案 1 :(得分:0)

我不确定这对你有用但我们试试看: 将以下注释添加到类name中的属性Yesh

@Fields( {
            @Field,
            @Field(name = "name_sort", analyze = Analyze.NO, store = Store.YES)
            } )
private String name;

按字段排序查询需要取消分析字段。如果想要通过此属性中的单词进行搜索并仍对其进行排序,则需要对其进行两次索引 - 一次分析后再进行未分析。

查询中的第二个应用您想要的排序:

ftem.createFullTextQuery(query, Kash.class, Yesh.class);

fullTextQuery.setSort(new Sort(new SortField("name_sort", SortField.STRING, true)));