Skip to content

Commit fd77f62

Browse files
authored
DATAES-897 - Add documentation for Highlight annotation.
Original PR: spring-projects#499
1 parent 99ed967 commit fd77f62

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/main/asciidoc/reference/elasticsearch-repositories.adoc

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,58 @@
33

44
This chapter includes details of the Elasticsearch repository implementation.
55

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+
629
include::elasticsearch-repository-queries.adoc[leveloffset=+1]
730

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+
858
[[elasticsearch.annotation]]
959
== Annotation based configuration
1060

@@ -40,7 +90,8 @@ class ProductService {
4090
}
4191
----
4292
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.
4495
<2> Provide a Bean named `elasticsearchTemplate` of type `ElasticsearchOperations` by using one of the configurations shown in the <<elasticsearch.operations>> chapter.
4596
<3> Let Spring inject the Repository bean into your class.
4697
====
@@ -145,5 +196,3 @@ Using the `Transport Client` or `Rest Client` element registers an instance of `
145196
</beans>
146197
----
147198
====
148-
149-
include::reactive-elasticsearch-repositories.adoc[leveloffset=+1]

0 commit comments

Comments
 (0)