Search in sources :

Example 11 with Product

use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.

the class SolrProductControllerTests method createProduct.

private Product createProduct(int id) {
    Product product = new Product();
    product.setId(Integer.toString(id));
    product.setAvailable(id % 2 == 0);
    product.setName("product-" + id);
    product.setPopularity(id * 10);
    product.setPrice((float) id * 100);
    product.setWeight((float) id * 2);
    return product;
}
Also used : Product(com.nixmash.blog.solr.model.Product)

Example 12 with Product

use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.

the class ProductServiceImpl method getProductsByLocation.

@Override
public List<Product> getProductsByLocation(String LatLng) throws GeoLocationException {
    List<Product> found;
    try {
        Point point = GeoConverters.StringToPointConverter.INSTANCE.convert(LatLng);
        found = customProductRepository.findByLocationNear(new Point(point.getX(), point.getY()), new Distance(30));
    } catch (Exception e) {
        logger.debug("No location found with coordinates: {}", LatLng);
        throw new GeoLocationException("Error in mapping latLng: " + LatLng);
    }
    return found;
}
Also used : Product(com.nixmash.blog.solr.model.Product) Point(org.springframework.data.geo.Point) GeoLocationException(com.nixmash.blog.solr.exceptions.GeoLocationException) Distance(org.springframework.data.geo.Distance) GeoLocationException(com.nixmash.blog.solr.exceptions.GeoLocationException)

Example 13 with Product

use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.

the class SolrUI method runDemos.

private void runDemos(DEMO demo) {
    Query query = new SimpleQuery(new SimpleStringCriteria("doctype:post"));
    List<Post> posts = postService.getAllPublishedPosts();
    switch(demo) {
        case MORE_LIKE_THIS:
            PostDoc postDoc = postDocService.getPostDocByPostId(435L);
            printPost(postDoc);
            postDocList = postDocService.getMoreLikeThis(435L).subList(0, 2);
            printPosts(postDocList);
            break;
        case POPULATE_DATABASE_SINGLE_ENTRIES:
            solrOperations.delete(query);
            solrOperations.commit();
            System.out.println("Existing posts deleted...");
            for (Post post : posts) {
                System.out.println(String.format("Entering Post #%s : %s", post.getPostId(), post.getPostTitle()));
                postDocService.addToIndex(post);
            }
            System.out.println("All posts added to Solr Server at " + solrSettings.getSolrServerUrl());
            break;
        case POPULATE_DATABASE_AS_LIST:
            solrOperations.delete(query);
            solrOperations.commit();
            System.out.println("Existing posts deleted...");
            postDocService.addAllToIndex(posts);
            System.out.println("All posts added to Solr Server at " + solrSettings.getSolrServerUrl());
            break;
        case BY_LOCATION:
            try {
                productList = service.getProductsByLocation("35.10,-96.102");
            } catch (GeoLocationException e) {
                e.printStackTrace();
            }
            printProducts(productList);
            break;
        case HIGHLIGHT_SEARCH_CRITERIA:
            highlightProductPage = service.findByHighlightedNameCriteria("canon powershot");
            SolrUtils.processHighlights(highlightProductPage);
            printProducts(highlightProductPage);
            break;
        case HIGHLIGHT_SEARCH:
            String queryString = "Can on yep";
            Boolean matches = queryString.matches("[a-zA-Z_0-9 ]*");
            System.out.println("MATCHES: " + matches);
            // printProducts(highlightProductPage);
            break;
        case SIMPLE_QUERY:
            // productList = service.getProductsWithUserQuery("name:memory AND name:corsair) AND
            // popularity:[6 TO *]");
            // productList = service.getProductsWithUserQuery("name:Western+Digital AND inStock:TRUE");
            // productList = service.getProductsWithUserQuery("cat:memory");
            // productList = service.getProductsWithUserQuery("features::printer");
            productList = service.getProductsWithUserQuery("inStock:true");
            printProducts(productList);
            break;
        case FACET_ON_NAME:
            facetProductPage = service.autocompleteNameFragment("pr", new PageRequest(0, 1));
            Page<FacetFieldEntry> fnPage = facetProductPage.getFacetResultPage(Product.NAME_FIELD);
            for (FacetFieldEntry entry : fnPage) {
                System.out.println(String.format("%s:%s \t %s", entry.getField().getName(), entry.getValue(), entry.getValueCount()));
            }
            break;
        case FACET_ON_AVAILABLE:
            facetProductPage = service.getFacetedProductsAvailable();
            Page<FacetFieldEntry> avPage = facetProductPage.getFacetResultPage(Product.AVAILABLE_FIELD);
            for (FacetFieldEntry entry : avPage) {
                System.out.println(String.format("%s:%s \t %s", entry.getField().getName(), entry.getValue(), entry.getValueCount()));
            }
            break;
        case FACET_ON_CATEGORY:
            facetProductPage = service.getFacetedProductsCategory();
            Page<FacetFieldEntry> catPage = facetProductPage.getFacetResultPage(Product.CATEGORY_FIELD);
            for (FacetFieldEntry entry : catPage) {
                System.out.println(String.format("%s:%s \t %s", entry.getField().getName(), entry.getValue(), entry.getValueCount()));
            }
            break;
        case METHOD_NAME_QUERY:
            productList = service.getProductsByStartOfName("power cord");
            printProducts(productList);
            break;
        case ANNOTATED_QUERY:
            productList = service.getProductsByNameOrCategoryAnnotatedQuery("canon");
            printProducts(productList);
            break;
        case NAMED_QUERY:
            productIterable = service.getProductsByNameOrCategory("canon");
            printProducts(productIterable);
            break;
        case TEST_RECORDS:
            productListPage = service.getTestRecords();
            printProducts(productListPage);
            break;
        case AVAILABLE_PRODUCTS:
            productList = service.getAvailableProducts();
            printProducts(productList);
            break;
        case ALL_PRODUCTS:
            // productList = service.getProductsByFilter();
            // printProducts(productList);
            productList = service.getProducts();
            printProducts(productList);
            break;
        case ALL_RECORDS:
            Iterable<Product> allRecords = service.getAllRecords();
            List<Product> productList = new ArrayList<>();
            for (Product product : allRecords) {
                if (product.getDoctype().equals(SolrDocType.PRODUCT))
                    productList.add(product);
            }
            printProducts(productList);
            break;
        case UPDATE_RECORD:
            Product urProduct = service.getProduct(SOLR_RECORD_ID);
            System.out.println(String.format("Original Product Name: %s", urProduct.getName()));
            urProduct.setName("Solr, The Enterprisey Http Search Server");
            service.updateProductName(urProduct);
            Product urProductUpdated = service.getProduct(SOLR_RECORD_ID);
            System.out.println(String.format("New Product Name: %s", urProductUpdated.getName()));
            urProductUpdated.setName("Solr, The Enterprise Http Search Server");
            service.updateProductName(urProductUpdated);
            break;
        case CRITERIA_SEARCH:
            this.productList = service.searchWithCriteria("Canon Camera memory");
            printProducts(this.productList);
            break;
        default:
            break;
    }
}
Also used : FacetFieldEntry(org.springframework.data.solr.core.query.result.FacetFieldEntry) SimpleQuery(org.springframework.data.solr.core.query.SimpleQuery) Query(org.springframework.data.solr.core.query.Query) SimpleQuery(org.springframework.data.solr.core.query.SimpleQuery) Post(com.nixmash.blog.jpa.model.Post) ArrayList(java.util.ArrayList) Product(com.nixmash.blog.solr.model.Product) GeoLocationException(com.nixmash.blog.solr.exceptions.GeoLocationException) PageRequest(org.springframework.data.domain.PageRequest) SimpleStringCriteria(org.springframework.data.solr.core.query.SimpleStringCriteria) PostDoc(com.nixmash.blog.solr.model.PostDoc)

Example 14 with Product

use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.

the class SolrUI method printProducts.

private void printProducts(Iterable<? extends Product> products) {
    int i = 0;
    for (Product product : products) {
        MessageFormat mf = new MessageFormat("{0} | Popularity: {1} | Lng/Lat: {2},{3}");
        Object[] items = { product.getName(), product.getPopularity(), product.getPoint().getX(), product.getPoint().getY() };
        if (product.getPoint().getX() < 0) {
            mf = new MessageFormat("{0} | Popularity: {1}");
        }
        System.out.println(mf.format(items));
        i++;
    }
    System.out.println("\nTOTAL RECORDS: " + i);
    System.out.println("\n\n");
}
Also used : MessageFormat(java.text.MessageFormat) Product(com.nixmash.blog.solr.model.Product)

Example 15 with Product

use of com.nixmash.blog.solr.model.Product in project nixmash-blog by mintster.

the class SolrLocationTests method testFindByLocationCriteria.

@Test
public void testFindByLocationCriteria() {
    Point location = new Point(22.15, -90.85);
    Criteria criteria = new Criteria("store").near(location, new Distance(5));
    Page<Product> result = solrOperations.queryForPage(new SimpleQuery(criteria), Product.class);
    Assert.assertEquals(1, result.getTotalElements());
    Assert.assertEquals(locatedInYonkers.getId(), result.getContent().get(0).getId());
}
Also used : SimpleQuery(org.springframework.data.solr.core.query.SimpleQuery) Product(com.nixmash.blog.solr.model.Product) Point(org.springframework.data.geo.Point) SimpleStringCriteria(org.springframework.data.solr.core.query.SimpleStringCriteria) Criteria(org.springframework.data.solr.core.query.Criteria) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

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