|
3 | 3 |
|
4 | 4 | This chapter includes details of the Elasticsearch repository implementation.
|
5 | 5 |
|
| 6 | +.The sample `Book` entity |
| 7 | +==== |
| 8 | +[source,java] |
| 9 | +---- |
| 10 | +@Document(indexName="books") |
| 11 | +class Book { |
| 12 | + @Id |
| 13 | + private String id; |
| 14 | +
|
| 15 | + @Field(type = FieldType.text) |
| 16 | + private String name; |
| 17 | +
|
| 18 | + @Field(type = FieldType.text) |
| 19 | + private String summary; |
| 20 | +
|
| 21 | + @Field(type = FieldType.Integer) |
| 22 | + private Integer price; |
| 23 | +
|
| 24 | + // getter/setter ... |
| 25 | +} |
| 26 | +---- |
| 27 | +==== |
| 28 | + |
6 | 29 | include::elasticsearch-repository-queries.adoc[leveloffset=+1]
|
7 | 30 |
|
| 31 | +include::reactive-elasticsearch-repositories.adoc[leveloffset=+1] |
| 32 | + |
| 33 | +[[elasticsearch.repositories.annotations]] |
| 34 | +== Annotations for repository methods |
| 35 | + |
| 36 | +=== @Highlight |
| 37 | + |
| 38 | +The `@Highlight` annotation on a repository method defines for which fields of the returned entity highlighting should be included. To search for some text in a `Book` 's name or summary and have the found data highlighted, the following repository method can be used: |
| 39 | + |
| 40 | +==== |
| 41 | +[source,java] |
| 42 | +---- |
| 43 | +interface BookRepository extends Repository<Book, String> { |
| 44 | +
|
| 45 | + @Highlight(fields = { |
| 46 | + @HighlightField(name = "name"), |
| 47 | + @HighlightField(name = "summary") |
| 48 | + }) |
| 49 | + List<SearchHit<Book>> findByNameOrSummary(String text, String summary); |
| 50 | +} |
| 51 | +---- |
| 52 | +==== |
| 53 | + |
| 54 | +It is possible to define multiple fields to be highlighted like above, and both the `@Highlight` and the `@HighlightField` annotation can further be customized with a `@HighlightParameters` annotation. Check the Javadocs for the possible configuration options. |
| 55 | + |
| 56 | +In the search results the highlight data can be retrieved from the `SearchHit` class. |
| 57 | + |
8 | 58 | [[elasticsearch.annotation]]
|
9 | 59 | == Annotation based configuration
|
10 | 60 |
|
@@ -40,7 +90,8 @@ class ProductService {
|
40 | 90 | }
|
41 | 91 | ----
|
42 | 92 |
|
43 |
| -<1> The `EnableElasticsearchRepositories` annotation activates the Repository support. If no base package is configured, it will use the one of the configuration class it is put on. |
| 93 | +<1> The `EnableElasticsearchRepositories` annotation activates the Repository support. |
| 94 | +If no base package is configured, it will use the one of the configuration class it is put on. |
44 | 95 | <2> Provide a Bean named `elasticsearchTemplate` of type `ElasticsearchOperations` by using one of the configurations shown in the <<elasticsearch.operations>> chapter.
|
45 | 96 | <3> Let Spring inject the Repository bean into your class.
|
46 | 97 | ====
|
@@ -145,5 +196,3 @@ Using the `Transport Client` or `Rest Client` element registers an instance of `
|
145 | 196 | </beans>
|
146 | 197 | ----
|
147 | 198 | ====
|
148 |
| - |
149 |
| -include::reactive-elasticsearch-repositories.adoc[leveloffset=+1] |
|
0 commit comments