Commit 1edaab12 authored by Quxl's avatar Quxl

x

parent e29fcaf8
package com.egolm.search.schedules; package com.egolm.search.schedules;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputDocument;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.egolm.common.StringUtil; import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.Page;
import com.egolm.search.config.XRException; import com.egolm.search.config.XRException;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
...@@ -26,75 +16,6 @@ public abstract class AbstractSolrApi implements SolrApi { ...@@ -26,75 +16,6 @@ public abstract class AbstractSolrApi implements SolrApi {
public abstract HttpSolrClient getSolrClient(); public abstract HttpSolrClient getSolrClient();
@Override
public Map<String, Object> query(Page page, String queryKey, String[] keywords, String[] orders,
String... groupNames) {
logger.info("收到商品检索请求:" + Rjx.json().set("page", page).set("queryKey", queryKey).set("keywords", keywords)
.set("orders", orders).set("groupNames", groupNames).toString());
if (page == null) {
page = new Page(1L, 20L);
}
queryKey = queryKey.toUpperCase();
Rjx result = Rjx.json();
SolrQuery solrQuery = new SolrQuery(queryKey);
if (keywords != null) {
for (String keyword : keywords) {
solrQuery.addFilterQuery(keyword);
}
}
if (orders != null) {
for (String order : orders) {
String[] kv = order.split(":", 2);
ORDER sc = kv[1] == null || kv[1].trim().length() == 0 || kv[1].equalsIgnoreCase("asc") ? ORDER.asc
: ORDER.desc;
solrQuery.addSort(kv[0], sc);
}
}
solrQuery.setFacet(true);
solrQuery.addFacetField(groupNames);
solrQuery.setRows(page.getLimit().intValue());
solrQuery.setStart(page.getFirstRowNumber().intValue() - 1);
try {
QueryResponse resp = getSolrClient().query(solrQuery);
Map<String, List<String>> groups = new HashMap<String, List<String>>();
List<FacetField> facetFields = resp.getFacetFields();
for (FacetField facet : facetFields) {
String fieldName = facet.getName();
if (StringUtil.isNotBlank(fieldName)) {
List<Count> values = facet.getValues();
for (int i = 0; i < values.size(); i++) {
Count count = values.get(i);
List<String> list = groups.get(fieldName);
if (list == null) {
list = new ArrayList<String>();
}
if (count.getCount() > 0) {
list.add(count.getName());
} else {
break;
}
groups.put(fieldName, list);
}
}
}
SolrDocumentList docList = resp.getResults();
page.setTotal(docList.getNumFound());
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (SolrDocument doc : docList) {
Map<String, Object> map = new HashMap<String, Object>();
for (String key : doc.keySet()) {
Object objValue = doc.get(key);
map.put(key, objValue);
}
list.add(map);
}
result.set("datas", list).set("page", page).set("groups", groups);
return result.getMap();
} catch (Exception e) {
throw new XRException("SolrQuery:" + queryKey + "," + JSON.toJSONString(keywords), e);
}
}
public void update(List<Map<String, Object>> list) { public void update(List<Map<String, Object>> list) {
HttpSolrClient solrClient = getSolrClient(); HttpSolrClient solrClient = getSolrClient();
try { try {
......
...@@ -5,15 +5,11 @@ import java.util.Map; ...@@ -5,15 +5,11 @@ import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.egolm.common.jdbc.Page;
public interface SolrApi { public interface SolrApi {
Logger logger = Logger.getLogger(SolrApi.class); Logger logger = Logger.getLogger(SolrApi.class);
public Map<String, Object> query(Page page, String queryKey, String[] keywords, String[] orders, String... groupsNames);
public void update(List<Map<String, Object>> datas); public void update(List<Map<String, Object>> datas);
public void deleteById(List<String> idList); public void deleteById(List<String> idList);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment