Commit d9001c21 authored by Quxl's avatar Quxl

x

parent 42ebb51a
...@@ -79,6 +79,11 @@ ...@@ -79,6 +79,11 @@
<groupId>org.apache.solr</groupId> <groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId> <artifactId>solr-solrj</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -20,29 +20,33 @@ import com.egolm.common.StringUtil; ...@@ -20,29 +20,33 @@ import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx; import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.Page; import com.egolm.common.jdbc.Page;
import com.egolm.search.config.XRException; import com.egolm.search.config.XRException;
import com.google.common.collect.Lists;
public abstract class AbstractSolrApi implements SolrApi { public abstract class AbstractSolrApi implements SolrApi {
public abstract HttpSolrClient getSolrClient(); public abstract HttpSolrClient getSolrClient();
@Override @Override
public Map<String, Object> query(Page page, String queryKey, String[] keywords, String[] orders, String... groupNames) { public Map<String, Object> query(Page page, String queryKey, String[] keywords, String[] orders,
logger.info("收到商品检索请求:" + Rjx.json().set("page", page).set("queryKey", queryKey).set("keywords", keywords).set("orders", orders).set("groupNames", groupNames).toString()); String... groupNames) {
if(page == null) { 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); page = new Page(1L, 20L);
} }
queryKey = queryKey.toUpperCase(); queryKey = queryKey.toUpperCase();
Rjx result = Rjx.json(); Rjx result = Rjx.json();
SolrQuery solrQuery = new SolrQuery(queryKey); SolrQuery solrQuery = new SolrQuery(queryKey);
if(keywords != null) { if (keywords != null) {
for(String keyword : keywords) { for (String keyword : keywords) {
solrQuery.addFilterQuery(keyword); solrQuery.addFilterQuery(keyword);
} }
} }
if(orders != null) { if (orders != null) {
for(String order : orders) { for (String order : orders) {
String[] kv = order.split(":", 2); String[] kv = order.split(":", 2);
ORDER sc = kv[1] == null || kv[1].trim().length() == 0 || kv[1].equalsIgnoreCase("asc") ? ORDER.asc : ORDER.desc; ORDER sc = kv[1] == null || kv[1].trim().length() == 0 || kv[1].equalsIgnoreCase("asc") ? ORDER.asc
: ORDER.desc;
solrQuery.addSort(kv[0], sc); solrQuery.addSort(kv[0], sc);
} }
} }
...@@ -56,15 +60,15 @@ public abstract class AbstractSolrApi implements SolrApi { ...@@ -56,15 +60,15 @@ public abstract class AbstractSolrApi implements SolrApi {
List<FacetField> facetFields = resp.getFacetFields(); List<FacetField> facetFields = resp.getFacetFields();
for (FacetField facet : facetFields) { for (FacetField facet : facetFields) {
String fieldName = facet.getName(); String fieldName = facet.getName();
if(StringUtil.isNotBlank(fieldName)) { if (StringUtil.isNotBlank(fieldName)) {
List<Count> values = facet.getValues(); List<Count> values = facet.getValues();
for(int i = 0; i < values.size(); i++) { for (int i = 0; i < values.size(); i++) {
Count count = values.get(i); Count count = values.get(i);
List<String> list = groups.get(fieldName); List<String> list = groups.get(fieldName);
if(list == null) { if (list == null) {
list = new ArrayList<String>(); list = new ArrayList<String>();
} }
if(count.getCount() > 0) { if (count.getCount() > 0) {
list.add(count.getName()); list.add(count.getName());
} else { } else {
break; break;
...@@ -76,9 +80,9 @@ public abstract class AbstractSolrApi implements SolrApi { ...@@ -76,9 +80,9 @@ public abstract class AbstractSolrApi implements SolrApi {
SolrDocumentList docList = resp.getResults(); SolrDocumentList docList = resp.getResults();
page.setTotal(docList.getNumFound()); page.setTotal(docList.getNumFound());
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(SolrDocument doc : docList) { for (SolrDocument doc : docList) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
for(String key : doc.keySet()) { for (String key : doc.keySet()) {
Object objValue = doc.get(key); Object objValue = doc.get(key);
map.put(key, objValue); map.put(key, objValue);
} }
...@@ -90,29 +94,24 @@ public abstract class AbstractSolrApi implements SolrApi { ...@@ -90,29 +94,24 @@ public abstract class AbstractSolrApi implements SolrApi {
throw new XRException("SolrQuery:" + queryKey + "," + JSON.toJSONString(keywords), 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) {
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
for(Map<String, Object> map : list) {
SolrInputDocument solrInputDocument = new SolrInputDocument();
for(String key : map.keySet()) {
if(key != null && key.trim().length() > 0) {
solrInputDocument.addField(key, StringUtil.toString(map.get(key)));
}
}
docs.add(solrInputDocument);
}
int size = 1000;
List<SolrInputDocument> tmp = new ArrayList<SolrInputDocument>();
HttpSolrClient solrClient = getSolrClient(); HttpSolrClient solrClient = getSolrClient();
try { try {
for(int docIndex = 0; docIndex < docs.size(); docIndex++) { List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
tmp.add(docs.get(docIndex)); for (Map<String, Object> map : list) {
if((docIndex%size == 0 || docIndex == docs.size() - 1) && docIndex > 0) { SolrInputDocument solrInputDocument = new SolrInputDocument();
solrClient.add(tmp); for (String key : map.keySet()) {
tmp = new ArrayList<SolrInputDocument>(); if (key != null && key.trim().length() > 0) {
} solrInputDocument.addField(key, StringUtil.toString(map.get(key)));
}
}
docs.add(solrInputDocument);
}
List<List<SolrInputDocument>> doclist = Lists.partition(docs, 1000);
for(List<SolrInputDocument> tmp : doclist) {
solrClient.add(tmp);
} }
solrClient.commit(); solrClient.commit();
} catch (Exception e) { } catch (Exception e) {
...@@ -124,12 +123,12 @@ public abstract class AbstractSolrApi implements SolrApi { ...@@ -124,12 +123,12 @@ public abstract class AbstractSolrApi implements SolrApi {
throw new XRException("重建索引失败", e); throw new XRException("重建索引失败", e);
} }
} }
@Override @Override
public void deleteById(List<String> documentIds) { public void deleteById(List<String> documentIds) {
HttpSolrClient solrClient = getSolrClient(); HttpSolrClient solrClient = getSolrClient();
try { try {
if(documentIds != null && documentIds.size() > 0) { if (documentIds != null && documentIds.size() > 0) {
solrClient.deleteById(documentIds); solrClient.deleteById(documentIds);
solrClient.commit(); solrClient.commit();
} }
...@@ -142,7 +141,7 @@ public abstract class AbstractSolrApi implements SolrApi { ...@@ -142,7 +141,7 @@ public abstract class AbstractSolrApi implements SolrApi {
throw new XRException("SolrDelete:" + JSON.toJSONString(documentIds), e); throw new XRException("SolrDelete:" + JSON.toJSONString(documentIds), e);
} }
} }
@Override @Override
public void clear() { public void clear() {
HttpSolrClient solrClient = getSolrClient(); HttpSolrClient solrClient = getSolrClient();
......
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