Skip to content

Commit

Permalink
STEP-2 - Custom Repository Implementation
Browse files Browse the repository at this point in the history
Extend SolrRepository by custom implementation. Use Solr Faceting
capablities to provide autocomple for search box. Extend the application
by jquery autocomplete and return application/json using Spring MVC json
support via ResponseBody.
  • Loading branch information
christophstrobl committed Apr 18, 2014
1 parent 4b3bbf9 commit 3044165
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1 deletion.
Expand Up @@ -27,7 +27,7 @@
/**
* @author Christoph Strobl
*/
interface ProductRepository extends SolrCrudRepository<Product, String> {
interface ProductRepository extends ProductRepositoryCustom, SolrCrudRepository<Product, String> {

@Query(fields = { SearchableProductDefinition.ID_FIELD_NAME, SearchableProductDefinition.NAME_FIELD_NAME,
SearchableProductDefinition.PRICE_FIELD_NAME, SearchableProductDefinition.FEATURES_FIELD_NAME,
Expand Down
@@ -0,0 +1,31 @@
/*
* Copyright 2012 - 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.solr.showcase.product;

import java.util.Collection;

import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.FacetPage;
import org.springframework.data.solr.showcase.product.model.Product;

/**
* @author Christoph Strobl
*/
interface ProductRepositoryCustom {

FacetPage<Product> findByNameStartsWith(Collection<String> nameFragments, Pageable pagebale);

}
@@ -0,0 +1,55 @@
/*
* Copyright 2012 - 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.solr.showcase.product;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.SolrOperations;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.FacetOptions;
import org.springframework.data.solr.core.query.FacetQuery;
import org.springframework.data.solr.core.query.SimpleFacetQuery;
import org.springframework.data.solr.core.query.result.FacetPage;
import org.springframework.data.solr.showcase.product.model.Product;

/**
* @author Christoph Strobl
*/
class ProductRepositoryImpl implements ProductRepositoryCustom {

private SolrOperations operations;

@Override
public FacetPage<Product> findByNameStartsWith(Collection<String> nameFragments, Pageable pagebale) {
Criteria criteria = new Criteria(Product.NAME_FIELD_NAME);
for (String s : nameFragments) {
criteria.startsWith(s);
}

FacetQuery query = new SimpleFacetQuery(criteria).setFacetOptions(new FacetOptions(Product.NAME_FIELD_NAME));
query.setPageRequest(pagebale);

return operations.queryForFacetPage(query, Product.class);
}

@Autowired
public void setOperations(SolrOperations operations) {
this.operations = operations;
}

}
Expand Up @@ -17,6 +17,7 @@

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.FacetPage;
import org.springframework.data.solr.showcase.product.model.Product;

/**
Expand All @@ -30,4 +31,6 @@ public interface ProductService {

Product findById(String id);

FacetPage<Product> autocompleteNameFragment(String fragment, Pageable pageable);

}
Expand Up @@ -17,13 +17,16 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.FacetPage;
import org.springframework.data.solr.core.query.result.SolrResultPage;
import org.springframework.data.solr.showcase.product.model.Product;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -51,6 +54,14 @@ public Product findById(String id) {
return productRepository.findOne(id);
}

@Override
public FacetPage<Product> autocompleteNameFragment(String fragment, Pageable pageable) {
if (StringUtils.isBlank(fragment)) {
return new SolrResultPage<Product>(Collections.<Product> emptyList());
}
return productRepository.findByNameStartsWith(splitSearchTermAndRemoveIgnoredCharacters(fragment), pageable);
}

private Collection<String> splitSearchTermAndRemoveIgnoredCharacters(String searchTerm) {
String[] searchTerms = StringUtils.split(searchTerm, " ");
List<String> result = new ArrayList<String>(searchTerms.length);
Expand Down
Expand Up @@ -15,18 +15,28 @@
*/
package org.springframework.data.solr.showcase.product.web;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.result.FacetFieldEntry;
import org.springframework.data.solr.core.query.result.FacetPage;
import org.springframework.data.solr.showcase.product.ProductService;
import org.springframework.data.solr.showcase.product.model.Product;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author Christoph Strobl
Expand All @@ -48,6 +58,27 @@ public String search(Model model, @RequestParam(value = "q", required = false) S
return "search";
}

@ResponseBody
@RequestMapping(value = "/autocomplete", produces = "application/json")
public Set<String> autoComplete(Model model, @RequestParam("term") String query,
@PageableDefault(page = 0, size = 1) Pageable pageable) {
if (!StringUtils.hasText(query)) {
return Collections.emptySet();
}

FacetPage<Product> result = productService.autocompleteNameFragment(query, pageable);

Set<String> titles = new LinkedHashSet<String>();
for (Page<FacetFieldEntry> page : result.getFacetResultPages()) {
for (FacetFieldEntry entry : page) {
if (entry.getValue().contains(query)) { // we have to do this as we do not use terms vector or a string field
titles.add(entry.getValue());
}
}
}
return titles;
}

@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
Expand Down
12 changes: 12 additions & 0 deletions src/main/webapp/WEB-INF/fragments/searchbox.jsp
Expand Up @@ -3,5 +3,17 @@
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form name="searchform" action="/search" method="GET">
<img src="<c:url value="/resources/images/showcase-logo.png" />" height="50px" title="sping-data-solr-showcase"/> Search for: <input id="queryField" name="q" size="12" type="text" value="${query}">
<script type="text/javascript">
$(function() {
$( "#queryField" ).autocomplete({
source: '<c:url value="/autocomplete" />',
minLength: 1,
select: function( event, ui ) {
$( "#queryField" ).value=ui.item.value;
document.searchform.submit();
}
});
});
</script>
<input type="submit" value="Search" />
</form:form>

0 comments on commit 3044165

Please sign in to comment.