From 49104de56b35950b4de7570eb8574613da38fed7 Mon Sep 17 00:00:00 2001 From: christophstrobl Date: Thu, 2 Jan 2014 13:46:25 +0100 Subject: [PATCH] STEP-4 - Use Highlight Annotation to mark keywords Using Highlight annotation to accentuate search input in result documents. Highlighting options can be configured via various annotation attribtes. By default all document fields will be target for highlighting. --- .../data/solr/showcase/product/ProductRepository.java | 6 ++++-- src/main/webapp/WEB-INF/views/search.jsp | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/solr/showcase/product/ProductRepository.java b/src/main/java/org/springframework/data/solr/showcase/product/ProductRepository.java index 14e8708..d91e1da 100644 --- a/src/main/java/org/springframework/data/solr/showcase/product/ProductRepository.java +++ b/src/main/java/org/springframework/data/solr/showcase/product/ProductRepository.java @@ -17,11 +17,12 @@ import java.util.Collection; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.solr.core.query.Query.Operator; import org.springframework.data.solr.core.query.result.FacetPage; +import org.springframework.data.solr.core.query.result.HighlightPage; import org.springframework.data.solr.repository.Facet; +import org.springframework.data.solr.repository.Highlight; import org.springframework.data.solr.repository.Query; import org.springframework.data.solr.repository.SolrCrudRepository; import org.springframework.data.solr.showcase.product.model.Product; @@ -31,10 +32,11 @@ */ interface ProductRepository extends SolrCrudRepository { + @Highlight(prefix = "", postfix = "") @Query(fields = { SearchableProductDefinition.ID_FIELD_NAME, SearchableProductDefinition.NAME_FIELD_NAME, SearchableProductDefinition.PRICE_FIELD_NAME, SearchableProductDefinition.FEATURES_FIELD_NAME, SearchableProductDefinition.AVAILABLE_FIELD_NAME }, defaultOperator = Operator.AND) - Page findByNameIn(Collection names, Pageable page); + HighlightPage findByNameIn(Collection names, Pageable page); @Facet(fields = { SearchableProductDefinition.NAME_FIELD_NAME }) FacetPage findByNameStartsWith(Collection nameFragments, Pageable pagebale); diff --git a/src/main/webapp/WEB-INF/views/search.jsp b/src/main/webapp/WEB-INF/views/search.jsp index 3bf35e3..8bdaa6c 100644 --- a/src/main/webapp/WEB-INF/views/search.jsp +++ b/src/main/webapp/WEB-INF/views/search.jsp @@ -34,6 +34,10 @@ ${item.features[0]}
Price:
Available: ${item.available?"yes":"no"}
+
+ + ${highlight.getSnipplets()} +