简体   繁体   中英

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:

    @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:

    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; Entity 1 have field annotated with name "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).

When you use @IndexedEmbedded, you include the nested fields in your Lucene document with a dotted notation.

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. 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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM