Rev 37638 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.common.solr;import com.spice.profitmandi.common.enumuration.SchemeType;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.web.client.RestClient;import org.apache.commons.lang3.StringUtils;import org.apache.http.conn.HttpHostConnectException;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.json.JSONArray;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;import java.util.*;import java.util.stream.Collectors;@Service("solrServiceCommon")public class SolrService {private static final Logger logger = LogManager.getLogger(SolrService.class);@Value("${new.solr.url}")private String solrUrl;@Autowiredprivate RestClient restClient;public String getContent(String queryTerm, List<Integer> categoryIds, List<String> brands, int limit,boolean activeOnly) throws Exception {JSONArray docs = this.getContentDocs(queryTerm, categoryIds, brands, limit, activeOnly);return docs.toString();}public JSONArray getContentDocs(String queryTerm, List<Integer> categoryIds, List<String> brands, int limit,boolean activeOnly) throws Exception {Map<String, String> params = new HashMap<>();List<String> mandatoryQ = new ArrayList<>();if (queryTerm != null && !queryTerm.equals("null")) {mandatoryQ.add(String.format("+(%s)", "*" + queryTerm + "*"));} else {queryTerm = null;}params.put("q", StringUtils.join(mandatoryQ, " "));if (categoryIds != null && categoryIds.size() > 0) {params.put("q", params.get("q") + " +filter(categoryId_i:(" + StringUtils.join(categoryIds, " ") + "))");}if (brands.size() > 0) {brands = brands.stream().map(x -> x.replaceAll(" ", "\\\\ ")).collect(Collectors.toList());params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");}if (activeOnly) {params.put("q", params.get("q") + " AND active_b:true");}params.put("fl", "*");if (queryTerm == null) {params.put("sort", "create_s desc");}params.put("start", String.valueOf(0));if (limit == 0) {params.put("fl", "catalogId_i, title_s, active_b");params.put("rows", String.valueOf(5000));} else {params.put("rows", String.valueOf(limit));}params.put("wt", "json");String response = null;try {response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");JSONArray docs = solrResponseJSONObj.getJSONArray("docs");return docs;}/*** Same query as {@link #getContentDocs} but also surfaces Solr's* <code>response.numFound</code> so callers can show "showing X of Y" totals.* Returns a small wrapper with the docs array (still capped by {@code limit})* and the unlimited match total.*/public ContentSearchResult getContentWithTotal(String queryTerm, List<Integer> categoryIds, List<String> brands,int limit, boolean activeOnly) throws Exception {Map<String, String> params = new HashMap<>();List<String> mandatoryQ = new ArrayList<>();if (queryTerm != null && !queryTerm.equals("null")) {mandatoryQ.add(String.format("+(%s)", "*" + queryTerm + "*"));} else {queryTerm = null;}params.put("q", StringUtils.join(mandatoryQ, " "));if (categoryIds != null && categoryIds.size() > 0) {params.put("q", params.get("q") + " +filter(categoryId_i:(" + StringUtils.join(categoryIds, " ") + "))");}if (brands.size() > 0) {brands = brands.stream().map(x -> x.replaceAll(" ", "\\\\ ")).collect(Collectors.toList());params.put("q", params.get("q") + " AND brand_ss:(" + StringUtils.join(brands, " ") + ")");}if (activeOnly) {params.put("q", params.get("q") + " AND active_b:true");}params.put("fl", "*");if (queryTerm == null) {params.put("sort", "create_s desc");}params.put("start", String.valueOf(0));if (limit == 0) {params.put("fl", "catalogId_i, title_s");params.put("rows", String.valueOf(5000));} else {params.put("rows", String.valueOf(limit));}params.put("wt", "json");String response;try {response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");JSONArray docs = solrResponseJSONObj.getJSONArray("docs");long numFound = solrResponseJSONObj.optLong("numFound", docs.length());return new ContentSearchResult(docs, numFound);}/** Wrapper for {@link #getContentWithTotal}. */public static class ContentSearchResult {private final JSONArray docs;private final long totalCount;public ContentSearchResult(JSONArray docs, long totalCount) {this.docs = docs;this.totalCount = totalCount;}public JSONArray getDocs() { return docs; }public long getTotalCount() { return totalCount; }}public Map<Integer, JSONObject> getContentByCatalogIds(List<Integer> catalogIds) throws Exception {Map<Integer, JSONObject> documentMap = new HashMap<>();Map<String, String> params = new HashMap<>();params.put("q", "catalogId_i:" + StringUtils.join(catalogIds, " "));params.put("fl", "*");params.put("start", String.valueOf(0));params.put("rows", String.valueOf(100));params.put("wt", "json");String response = null;try {response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");JSONArray docs = solrResponseJSONObj.getJSONArray("docs");for (int i = 0; i < docs.length(); i++) {JSONObject doc = docs.getJSONObject(i);documentMap.put(doc.getInt("catalogId_i"), doc);}return documentMap;}// This method is the used to pull docs based on search and shall be used// interchangably for both the thingspublic static String brandExclusionFq(List<String> excludeBrands) {if (excludeBrands == null || excludeBrands.isEmpty()) {return null;}return "-brand_ss:(" + excludeBrands.stream().map(x -> "\"" + x + "\"").collect(Collectors.joining(" OR "))+ ")";}/*** Category facet over the hot-deal models: which categoryId_i values are present (and* how many models each) among the docs the hot-deal listing can show. Drives the* page's category filter chips.*/public Map<Integer, Integer> getHotDealCategoryFacet(List<Integer> hotDealCatalogIds) throws Exception {Map<Integer, Integer> counts = new LinkedHashMap<>();for (Map.Entry<String, Integer> bucket: hotDealFacet(hotDealCatalogIds, "categoryId_i", null).entrySet()) {counts.put(Integer.parseInt(bucket.getKey()), bucket.getValue());}return counts;}/*** Brand facet over the hot-deal models, driving the page's brand chips.** <p>Facets on oem_brand_s, NOT brand_ss. Every hot-deal doc reads "Hot Deal" in* brand_ss - that is the whole point of the brand - so faceting there would yield a* single useless bucket. oem_brand_s carries the brand the SKU had before the move* (Samsung, OnePlus, ...), which is what a partner actually wants to narrow by. It is* surfaced to partners as plain "Brand"; "OEM" is internal vocabulary.*//*** @param categoryId when set, counts only models in that category, so the chips match* the tab the partner is standing on. Without it the row is global:* the LED TVs tab would offer Samsung and Vivo chips even though* neither has a hot-deal TV, and tapping one returns nothing.*/public Map<String, Integer> getHotDealBrandFacet(List<Integer> hotDealCatalogIds, Integer categoryId)throws Exception {return hotDealFacet(hotDealCatalogIds, "oem_brand_s", categoryId);}/*** One facet query over the hot-deal doc set, keyed by raw bucket value. Shared by the* category and brand facets, which differ only in the field they count and whether* they narrow to a category.*/private Map<String, Integer> hotDealFacet(List<Integer> hotDealCatalogIds, String facetField,Integer categoryId) throws Exception {Map<String, Integer> counts = new LinkedHashMap<>();if (hotDealCatalogIds == null || hotDealCatalogIds.isEmpty()) {return counts;}String idClause = hotDealCatalogIds.stream().map(x -> "catalog" + x).collect(Collectors.joining(" OR "));// categoryId_i lives on the parent (catalog) doc, same as the id clause, so it// narrows the facet without needing a child-document join.String categoryClause = categoryId == null || categoryId <= 0? "" : " +categoryId_i:" + categoryId;Map<String, String> params = new HashMap<>();params.put("q", "+id:(" + idClause + ") +show_default_b:true +eol_no_stock_b:false"+ categoryClause);params.put("rows", "0");params.put("facet", "true");params.put("facet.field", facetField);params.put("facet.mincount", "1");params.put("wt", "json");String response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);JSONArray buckets = new JSONObject(response).getJSONObject("facet_counts").getJSONObject("facet_fields").getJSONArray(facetField);for (int i = 0; i + 1 < buckets.length(); i += 2) {counts.put(buckets.getString(i), buckets.getInt(i + 1));}return counts;}public JSONArray getSolrDocs(String queryTerm, String categoryId, int offset, int limit, String sort,String brand, int subCategoryId, boolean hotDeal, boolean group, boolean eol_filter) throws Throwable {return this.getSolrDocs(queryTerm, categoryId, offset, limit, sort, brand, subCategoryId, hotDeal, group,eol_filter, null);}public JSONArray getSolrDocs(String queryTerm, String categoryId, int offset, int limit, String sort,String brand, int subCategoryId, boolean hotDeal, boolean group, boolean eol_filter,List<String> excludeBrands) throws Throwable {return getSolrDocs(queryTerm, categoryId, offset, limit, sort, brand, subCategoryId, hotDeal, group,eol_filter, excludeBrands, null);}public JSONArray getSolrDocs(String queryTerm, String categoryId, int offset, int limit, String sort,String brand, int subCategoryId, boolean hotDeal, boolean group, boolean eol_filter,List<String> excludeBrands, List<Integer> hotDealCatalogIds) throws Throwable {List<String> parentFilter = new ArrayList<>();// Hot-deal listings are cross-category by default (the curated catalogId filter// is the scope); categoryId is applied there only when the caller explicitly// passes one (the page's category filter chips)if (hotDealCatalogIds == null || hotDealCatalogIds.isEmpty()|| (categoryId != null && !categoryId.trim().isEmpty())) {parentFilter.add("categoryId_i:" + categoryId);}parentFilter.add("show_default_b:true");List<String> childFilter = new ArrayList<>();childFilter.add("itemId_i:*");Map<String, String> params = new HashMap<>();if (queryTerm == null || queryTerm.equals("null")) {queryTerm = "";} else {queryTerm = "(" + queryTerm + ")";}// True when this query is scoped to hot deals by EITHER route below. The brand// filter keys off this rather than the hotDeal flag alone, because the caller that// resolves the catalog ids itself passes hotDeal = false - and that listing still// needs its brand chips routed to oem_brand_s.boolean hotDealScoped = hotDeal || (hotDealCatalogIds != null && !hotDealCatalogIds.isEmpty());if (hotDealCatalogIds != null && !hotDealCatalogIds.isEmpty()) {// Caller already resolved the hot-deal set from the brand: restrict parents to// those models; items stay the normal active setString idClause = hotDealCatalogIds.stream().map(x -> "catalog" + x).collect(java.util.stream.Collectors.joining(" OR "));parentFilter.add("id:(" + idClause + ")");childFilter.add("active_b:true");} else if (hotDeal) {// hot_deal_b is derived from the SKU's brand at index time (FofoSolr)childFilter.add("hot_deal_b:true");} else {childFilter.add("active_b:true");}if (subCategoryId != 0) {parentFilter.add("subCategoryId_i:" + subCategoryId);}if(eol_filter){parentFilter.add("eol_no_stock_b:false");}if (StringUtils.isNotBlank(brand)) {// Inside the hot-deal listing the brand chips carry the ORIGINAL brand, which// lives in oem_brand_s - every one of those docs reads "Hot Deal" in brand_ss,// so filtering there would match nothing.String brandField = hotDealScoped ? "oem_brand_s" : "brand_ss";parentFilter.add(brandField + ":" + "\\\"" + brand + "\\\"");}if (queryTerm == "") {params.put("sort", (sort == "" ? "" : sort + ", ") + "create_s desc");} else {parentFilter.addAll(Arrays.asList(queryTerm.split(" ")));}String parentFilterString = "\"" + String.join(" AND ", parentFilter) + "\"";String childFilterString = String.join(" AND ", childFilter);params.put("q", String.format("{!parent which=%s}%s", parentFilterString, childFilterString));params.put("fl",String.format("*, [child parentFilter=id:catalog* childFilter=%s]", "\"" + childFilterString + "\""));params.put("start", String.valueOf(offset));params.put("rows", String.valueOf(limit));params.put("wt", "json");String exclusionFq = brandExclusionFq(excludeBrands);if (exclusionFq != null) {params.put("fq", exclusionFq);}String groupByField = null;if (group) {groupByField = "superCatalog_s";params.put("group", String.valueOf(group));params.put("group.field", groupByField);params.put("group.limit", "1");if (!sort.isEmpty()) {params.put("group.sort", sort);}}logger.info("groupByField {}", groupByField);String response = null;try {response = restClient.get(SchemeType.HTTP, solrUrl, 8984, "solr/demo/select", params);} catch (HttpHostConnectException e) {throw new ProfitMandiBusinessException("", "", "Could not connect to host");}/*JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");JSONArray docs = solrResponseJSONObj.getJSONArray("docs");*/JSONObject solrResponseJSONObj = new JSONObject(response);JSONArray docs;if (group) {logger.info("Reached in if condition {}",groupByField);JSONObject grouped = solrResponseJSONObj.getJSONObject("grouped");JSONArray groups = grouped.getJSONObject(groupByField).getJSONArray("groups");docs = new JSONArray();for (int i = 0; i < groups.length(); i++) {JSONObject groupObj = groups.getJSONObject(i);JSONArray groupDocs = groupObj.getJSONObject("doclist").getJSONArray("docs");for (int j = 0; j < groupDocs.length(); j++) {docs.put(groupDocs.getJSONObject(j));}}} else {logger.info("Reached in else condition {}",groupByField);docs = solrResponseJSONObj.getJSONObject("response").getJSONArray("docs");}logger.info("Reached at end {}",docs);return docs;}}