Skip to content

Commit

Permalink
STEP-4 - Use Highlight Annotation to mark keywords
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
christophstrobl committed Apr 18, 2014
1 parent 27fa1a3 commit 49104de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Expand Up @@ -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;
Expand All @@ -31,10 +32,11 @@
*/
interface ProductRepository extends SolrCrudRepository<Product, String> {

@Highlight(prefix = "<b>", postfix = "</b>")
@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<Product> findByNameIn(Collection<String> names, Pageable page);
HighlightPage<Product> findByNameIn(Collection<String> names, Pageable page);

@Facet(fields = { SearchableProductDefinition.NAME_FIELD_NAME })
FacetPage<Product> findByNameStartsWith(Collection<String> nameFragments, Pageable pagebale);
Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/WEB-INF/views/search.jsp
Expand Up @@ -34,6 +34,10 @@
<c:if test="${fn:length(item.features) > 0}">${item.features[0]} <br /></c:if>
<c:if test="${item.price != null}">Price: <fmt:formatNumber value="${item.price}" type="currency"/><br /></c:if>
Available: ${item.available?"yes":"no"}<br />
<br />
<c:forEach var="highlight" items="${page.getHighlights(item) }" varStatus="highlightStatus">
${highlight.getSnipplets()}
</c:forEach>
</div>
</c:forEach>
<br />
Expand Down

0 comments on commit 49104de

Please sign in to comment.