簡體   English   中英

如何使用Hibernate Lucene搜索訪問實體中外鍵的排序字段名稱?

[英]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;
    }
    }

實體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;

但它不起作用。

需要在實體中實現哪些注釋或對主程序進行任何更改才能訪問該字段以進行排序?

您正在使用@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如何構建索引的信息:這樣您就可以更輕松地了解這種情況。

我不確定這是否對您有用,但讓我們嘗試一下:在Yesh類的屬性name中添加以下注釋:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM