How do Hibernate Search sort the result of a search?

2

I have a indexed search with Hibernate Search but I can not get Hibernate Search sort the search result by the order of a particular column , such as the column name.

But there is a quirk . When the search is done using some term, the result is then sorted perfectly alphabetically. However, if I do not put any search term (I want it to return all results), then Hibernate Search no longer commands anything.

See the codes for the two searches:

Using a filter (works)

Below is my method that searches by filtering through a term. This search works perfectly and sorts by name (alphabetically) .

public List<Colaborador> filtrar(String termo) throws DAOException {
    try {
        FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(this.entityManager);
        QueryBuilder qb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Colaborador.class).get();
        Query query = qb.keyword().onFields("nome", "email", "usuario").matching(termo).createQuery();
        FullTextQuery fullTextQuery = fullTextEm.createFullTextQuery(query);

        // Ordenação ocorre corretamente aqui!
        Sort sortField = new Sort(new SortField("nome", SortField.STRING));

        fullTextQuery.setSort(sortField);
        return fullTextQuery.getResultList();
    }
    catch (Exception e) {
        logger.error("Erro ao filtrar colaboradores com termo: " + termo, e);
        throw new DAOException(e);
    }
}

Bringing all the records (sorting does not work)

Below is a method very similar to what I posted above, however the method below just does not correctly order the records that are returned by Hibernate Search. Notice that it is the same entity ( Colaborador.class ) and even then the order by name does not work correctly.

public List<Colaborador> listarColaboradores() throws DAOException {
    FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(this.entityManager);
    QueryBuilder qb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Colaborador.class).get();
    FullTextQuery fullTextQuery = fullTextEm.createFullTextQuery(qb.all().createQuery());

    // A mágica deveria ser feita aqui!
    Sort sortField = new Sort(new SortField("nome", SortField.STRING));

    fullTextQuery.setSort(sortField);
    return fullTextQuery.getResultList();
}

Has anyone ever gone through this? Do you know what might be happening?

    
asked by anonymous 21.03.2014 / 19:03

1 answer

0

The problem was in my entity.

@Entity
@Indexed
@Table(name = "my_view")
public class Colaborador implements Serializable {

    private static final long serialVersionUID = 244555315052436669L;

    @Id
    @Field(store = Store.YES, index = Index.YES, analyze = Analyze.YES)
    @Column(name = "id", insertable = false, updatable = false)
    private Long id;

    @Field(store = Store.YES, index = Index.YES, analyze = Analyze.YES)
    @Column(name = "name", insertable = false, updatable = false)
    private String nome;

    @Field(store = Store.YES, index = Index.YES, analyze = Analyze.YES)
    @Column(name = "email", insertable = false, updatable = false)
    private String email;

    @Field(store = Store.YES, index = Index.YES, analyze = Analyze.YES)
    @Column(name = "user", insertable = false, updatable = false)
    private String usuario;

    //Getters e Setters omitidos
}

Notice that the private String nome; attribute, in which I want to sort my search, is noted as follows:

@Field(store = Store.YES, index = Index.YES, analyze = Analyze.YES)
@Column(name = "nome", insertable = false, updatable = false)
private String nome;

Doing research and more research, I found a snippet in the Hibernate documentation Search that reads as follows:

  

Fields used for sorting should not be analyzed.

Then I changed the mapping of my entity to:

@Field(store = Store.YES, index = Index.YES, analyze = Analyze.NO)
@Column(name = "nome", insertable = false, updatable = false)
private String nome;

And I went to test my application. To my surprise the ordering worked, but the filter by the name attribute, stopped working . That is, I arranged one thing and broke another. And I returned my searches and ended up in the Hibernate Search documentation again.

I have found that the same attribute can be mapped twice to Hibernate Search. Then you can in a mapping use the Analyze.YES and run the query on top of that mapping and a second mapping with Analyze.NO to do the sorting. Sounds complicated? See the code:

@Fields({
    @Field(name = "nome_ordem", store = Store.YES, index = Index.YES, analyze = Analyze.NO),
    @Field(name = "nome", store = Store.YES, index = Index.YES, analyze = Analyze.YES)
})
@Column(name = "nome", insertable = false, updatable = false)
private String nome;

Notice that I create two indexes for the same attribute. One is used as a filter field and the other as an ordering. By doing so, I can then search for this attribute and also sort it!

    
26.03.2014 / 20:56