use of com.salesmanager.core.model.search.SearchEntry in project shopizer by shopizer-ecommerce.
the class SearchServiceImpl method search.
public com.salesmanager.core.model.search.SearchResponse search(MerchantStore store, String languageCode, String term, int entriesCount, int startIndex) throws ServiceException {
try {
StringBuilder collectionName = new StringBuilder();
collectionName.append(PRODUCT_INDEX_NAME).append(UNDERSCORE).append(languageCode).append(UNDERSCORE).append(store.getCode().toLowerCase());
SearchRequest request = new SearchRequest();
request.addCollection(collectionName.toString());
request.setSize(entriesCount);
request.setStart(startIndex);
request.setMatch(term);
SearchResponse response = searchService.search(request);
com.salesmanager.core.model.search.SearchResponse resp = new com.salesmanager.core.model.search.SearchResponse();
resp.setTotalCount(0);
if (response != null) {
resp.setTotalCount(response.getCount());
List<SearchEntry> entries = new ArrayList<SearchEntry>();
Collection<SearchHit> hits = response.getSearchHits();
if (!CollectionUtils.isEmpty(hits)) {
for (SearchHit hit : hits) {
SearchEntry entry = new SearchEntry();
// Map<String,Object> metaEntries = hit.getMetaEntries();
Map<String, Object> metaEntries = hit.getItem();
IndexProduct indexProduct = new IndexProduct();
Object desc = metaEntries.get("description");
if (desc instanceof JsonNull == false) {
indexProduct.setDescription((String) metaEntries.get("description"));
}
Object hl = metaEntries.get("highlight");
if (hl instanceof JsonNull == false) {
indexProduct.setHighlight((String) metaEntries.get("highlight"));
}
indexProduct.setId((String) metaEntries.get("id"));
indexProduct.setLang((String) metaEntries.get("lang"));
Object nm = metaEntries.get("name");
if (nm instanceof JsonNull == false) {
indexProduct.setName(((String) metaEntries.get("name")));
}
Object mf = metaEntries.get("manufacturer");
if (mf instanceof JsonNull == false) {
indexProduct.setManufacturer(((String) metaEntries.get("manufacturer")));
}
indexProduct.setPrice(Double.valueOf(((String) metaEntries.get("price"))));
indexProduct.setStore(((String) metaEntries.get("store")));
entry.setIndexProduct(indexProduct);
entries.add(entry);
/**
* no more support for highlighted
*/
}
resp.setEntries(entries);
// Map<String,List<FacetEntry>> facets = response.getFacets();
Map<String, Facet> facets = response.getFacets();
if (facets != null && facets.size() > 0) {
Map<String, List<SearchFacet>> searchFacets = new HashMap<String, List<SearchFacet>>();
for (String key : facets.keySet()) {
Facet f = facets.get(key);
List<com.shopizer.search.services.Entry> ent = f.getEntries();
// List<FacetEntry> f = facets.get(key);
List<SearchFacet> fs = searchFacets.computeIfAbsent(key, k -> new ArrayList<>());
for (com.shopizer.search.services.Entry facetEntry : ent) {
SearchFacet searchFacet = new SearchFacet();
searchFacet.setKey(facetEntry.getName());
searchFacet.setName(facetEntry.getName());
searchFacet.setCount(facetEntry.getCount());
fs.add(searchFacet);
}
}
resp.setFacets(searchFacets);
}
}
}
return resp;
} catch (Exception e) {
LOGGER.error("Error while searching keywords " + term, e);
throw new ServiceException(e);
}
}
use of com.salesmanager.core.model.search.SearchEntry in project shopizer by shopizer-ecommerce.
the class SearchFacadeImpl method convertToSearchProductList.
@Override
public SearchProductList convertToSearchProductList(SearchResponse searchResponse, MerchantStore merchantStore, int start, int count, Language language) {
SearchProductList returnList = new SearchProductList();
List<SearchEntry> entries = searchResponse.getEntries();
if (CollectionUtils.isNotEmpty(entries)) {
List<Long> ids = entries.stream().map(SearchEntry::getIndexProduct).map(IndexProduct::getId).map(Long::parseLong).collect(Collectors.toList());
ProductCriteria searchCriteria = new ProductCriteria();
searchCriteria.setMaxCount(count);
searchCriteria.setStartIndex(start);
searchCriteria.setProductIds(ids);
searchCriteria.setAvailable(true);
ProductList productList = productService.listByStore(merchantStore, language, searchCriteria);
List<ReadableProduct> readableProducts = productList.getProducts().stream().map(product -> convertProductToReadableProduct(product, merchantStore, language)).collect(Collectors.toList());
returnList.getProducts().addAll(readableProducts);
returnList.setProductCount(productList.getProducts().size());
}
// Facets
Map<String, List<SearchFacet>> facets = Optional.ofNullable(searchResponse.getFacets()).orElse(Collections.emptyMap());
List<ReadableCategory> categoryProxies = getCategoryFacets(merchantStore, language, facets);
returnList.setCategoryFacets(categoryProxies);
List<SearchFacet> manufacturersFacets = facets.entrySet().stream().filter(e -> MANUFACTURER_FACET_NAME.equals(e.getKey())).findFirst().map(Entry::getValue).orElse(Collections.emptyList());
if (CollectionUtils.isNotEmpty(manufacturersFacets)) {
// TODO add manufacturer facets
}
return returnList;
}
Aggregations