use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.
the class SolrController method processFindForm.
@RequestMapping(value = "/products/list", method = RequestMethod.GET)
public String processFindForm(UserQuery userQuery, BindingResult result, Model model, HttpServletRequest request) {
List<Product> results = null;
Boolean isSimpleTermQuery = userQuery.getQuery().matches("[a-zA-Z_0-9 ]*");
if (StringUtils.isEmpty(userQuery.getQuery())) {
return "redirect:/products/search";
} else
try {
if (isSimpleTermQuery) {
HighlightPage<Product> highlightedResults = productService.findByHighlightedNameCriteria(userQuery.getQuery());
results = SolrUtils.highlightPagesToList(highlightedResults);
} else {
results = productService.getProductsWithUserQuery(userQuery.getQuery());
}
} catch (UncategorizedSolrException ex) {
logger.info(MessageFormat.format("Bad Query: {0}", userQuery.getQuery()));
result.rejectValue("query", "product.search.error", new Object[] { userQuery.getQuery() }, "not found");
return PRODUCT_SEARCH_VIEW;
}
if (results.size() < 1) {
result.rejectValue("query", "product.search.noresults", new Object[] { userQuery.getQuery() }, "not found");
return PRODUCT_SEARCH_VIEW;
}
if (results.size() > 1) {
PagedListHolder<Product> pagedListHolder = new PagedListHolder<Product>(results);
pagedListHolder.setPageSize(PRODUCT_LIST_PAGE_SIZE);
request.getSession().setAttribute(SESSION_ATTRIBUTE_PRODUCTLIST, pagedListHolder);
return "redirect:/products/page/1";
} else {
Product product = results.iterator().next();
return "redirect:/products/" + product.getId();
}
}
use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.
the class SolrController method productCategories.
@RequestMapping(value = "/products/categories", method = RequestMethod.GET)
public String productCategories(Model model) {
FacetPage<Product> catfacetPage = productService.getFacetedProductsCategory();
Page<FacetFieldEntry> catPage = catfacetPage.getFacetResultPage(Product.CATEGORY_FIELD);
List<ProductCategory> results = new ArrayList<ProductCategory>();
for (FacetFieldEntry entry : catPage) {
results.add(new ProductCategory(entry.getValue(), toIntExact(entry.getValueCount())));
}
model.addAttribute(MODEL_ATTRIBUTE_PRODUCT_CATEGORIES, results);
return PRODUCT_CATEGORIES_VIEW;
}
use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.
the class SolrController method productPage.
@RequestMapping(value = "/products/{id}", method = GET)
public String productPage(@PathVariable("id") String id, Model model) {
logger.debug("Showing product page for product with id: {}", id);
Product found = productService.getProduct(id);
logger.debug("Found product: {}", found);
model.addAttribute(MODEL_ATTRIBUTE_PRODUCT, found);
return PRODUCT_VIEW;
}
use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.
the class SolrController method autoComplete.
@ResponseBody
@RequestMapping(value = "/products/autocomplete", produces = "application/json")
public Set<String> autoComplete(Model model, @RequestParam("term") String query) {
if (StringUtils.isBlank(query)) {
return Collections.emptySet();
}
PageRequest pageRequest = new PageRequest(0, 1);
FacetPage<Product> result = productService.autocompleteNameFragment(query, pageRequest);
Set<String> titles = new LinkedHashSet<String>();
for (Page<FacetFieldEntry> page : result.getFacetResultPages()) {
for (FacetFieldEntry entry : page) {
if (entry.getValue().contains(query)) {
titles.add(entry.getValue());
}
}
}
return titles;
}
use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.
the class SolrUtils method highlightPagesToList.
public static List<Product> highlightPagesToList(HighlightPage<Product> productPage) {
List<Product> products = new ArrayList<Product>();
for (HighlightEntry<Product> highlightedProduct : productPage.getHighlighted()) {
Product product = new Product(highlightedProduct.getEntity().getId(), highlightedProduct.getEntity().getName());
products.add(product);
for (HighlightEntry.Highlight highlight : highlightedProduct.getHighlights()) {
for (String snippet : highlight.getSnipplets()) {
if (highlight.getField().getName().equals(IProduct.NAME_FIELD)) {
product.setName(snippet);
}
}
}
}
return products;
}
Aggregations