Search in sources :

Example 1 with Product

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();
    }
}
Also used : UncategorizedSolrException(org.springframework.data.solr.UncategorizedSolrException) Product(com.nixmash.blog.solr.model.Product) PagedListHolder(org.springframework.beans.support.PagedListHolder) HighlightPage(org.springframework.data.solr.core.query.result.HighlightPage)

Example 2 with Product

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;
}
Also used : FacetFieldEntry(org.springframework.data.solr.core.query.result.FacetFieldEntry) ProductCategory(com.nixmash.blog.mvc.containers.ProductCategory) Product(com.nixmash.blog.solr.model.Product)

Example 3 with Product

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;
}
Also used : Product(com.nixmash.blog.solr.model.Product)

Example 4 with Product

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;
}
Also used : FacetFieldEntry(org.springframework.data.solr.core.query.result.FacetFieldEntry) PageRequest(org.springframework.data.domain.PageRequest) Product(com.nixmash.blog.solr.model.Product)

Example 5 with Product

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;
}
Also used : ArrayList(java.util.ArrayList) IProduct(com.nixmash.blog.solr.model.IProduct) Product(com.nixmash.blog.solr.model.Product) HighlightEntry(org.springframework.data.solr.core.query.result.HighlightEntry)

Aggregations

Product (com.nixmash.blog.solr.model.Product)20 Test (org.junit.Test)10 Point (org.springframework.data.geo.Point)6 PageRequest (org.springframework.data.domain.PageRequest)5 Distance (org.springframework.data.geo.Distance)5 FacetFieldEntry (org.springframework.data.solr.core.query.result.FacetFieldEntry)4 GeoLocationException (com.nixmash.blog.solr.exceptions.GeoLocationException)2 ArrayList (java.util.ArrayList)2 SimpleQuery (org.springframework.data.solr.core.query.SimpleQuery)2 SimpleStringCriteria (org.springframework.data.solr.core.query.SimpleStringCriteria)2 Post (com.nixmash.blog.jpa.model.Post)1 ProductCategory (com.nixmash.blog.mvc.containers.ProductCategory)1 IProduct (com.nixmash.blog.solr.model.IProduct)1 PostDoc (com.nixmash.blog.solr.model.PostDoc)1 MessageFormat (java.text.MessageFormat)1 PagedListHolder (org.springframework.beans.support.PagedListHolder)1 Box (org.springframework.data.geo.Box)1 UncategorizedSolrException (org.springframework.data.solr.UncategorizedSolrException)1 Criteria (org.springframework.data.solr.core.query.Criteria)1 Query (org.springframework.data.solr.core.query.Query)1