Karol Pokomeda
Karol Pokomeda

Reputation: 141

@Field annotation for array

In my application I have to store entities in the Infinispan cache and run user-provided search queries on this type of in-memory repo. Problem is, that my entity has an array of Strings, let's say it is something like this:

@Indexed(index = "bookIndex")
public class Book {
    @Field
    private Long id;
    @Field
    private String[] authors;
}

My problem is that apparently you cannot index an array just like that. The obvious solution is to create Author object and have embedded index, however this is not desired option (performance issues - the dto has to be as small as possible).

Is there any other way to index such field?

Upvotes: 0

Views: 374

Answers (1)

yrodiere
yrodiere

Reputation: 9977

EDIT: Starting with Hibernate Search 6, you don't need any workaround: the following mapping will work as expected, indexing elements of the array.

    @Indexed(index = "bookIndex")
    public class Book {
        @Field
        private Long id;
        @FullTextField
        private String[] authors;
    }

As to Hibernate Search 5...

Just use @IndexedEmbedded on the array property and Hibernate will understand that you mean to index the array elements and not the array itself:

    @Indexed(index = "bookIndex")
    public class Book {
        @Field
        private Long id;
        @Field
        @IndexedEmbedded
        private String[] authors;
    }

The feature is undocumented because it can lead to weird behavior with non-primitive array elements, but in your case you should be good.

Alternatively, you can write your own bridge: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#section-custom-bridges

Upvotes: 2

Related Questions