0

I am using Hibernate Search 6 configured to an elasticsearch backend.

@Indexed
    public class Book extends PanacheEntity{
      @FullTextField
      public String title;
      @GenericField
      public Double price;
    } 

I can filter on the price field for books in a price range, as follows:

  Search.session(entityManager)
                .search(Book.class)
                .predicate(f -> f.bool(b -> {
    b.must(f.range().field("price").lessThan(50));
    }));

I would like to be able to sort the search results by price as well. For this I changed the field definition to

@GenericField(sortable = Sortable.YES)
    private Double price;

I am doing the sort as follows:

 .sort(f -> f.composite(b -> {
b.add(f.field("price");
 } ) ) 

I am getting the error as follows, which looks to have been thrown from Elastic

Can\u0027t load fielddata on [offer.price] because fielddata is unsupported on fields of type [double]. Use doc values instead.

I have seen Can you sort and search on same field with Hibernate/Lucene? but I dont see equivalent annotations in Hibernate search 6

bobby
  • 2,629
  • 5
  • 30
  • 56

1 Answers1

1

Your code with the sortable = Sortable.YES should work as you expect.

The error returned by Elasticsearch leads me to think your schema (the "mapping" registered to the Elasticsearch cluster) might be obsolete.

You can check that by starting Hibernate Search with the validate lifecycle strategy. If you get an error, it means your schema is out of date. The easiest solution to update it is to drop your Elasticsearch indexes and let Hibernate Search re-create them.

yrodiere
  • 9,280
  • 1
  • 13
  • 35
  • I had done a reindex after making the field sortable, but looks like a drop and reindex was needed – bobby Mar 10 '20 at 15:54