Commit d9001c21 authored by Quxl's avatar Quxl

x

parent 42ebb51a
......@@ -79,6 +79,11 @@
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -20,29 +20,33 @@ 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.google.common.collect.Lists;
public abstract class AbstractSolrApi implements SolrApi {
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) {
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) {
if (keywords != null) {
for (String keyword : keywords) {
solrQuery.addFilterQuery(keyword);
}
}
if(orders != null) {
for(String order : orders) {
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;
ORDER sc = kv[1] == null || kv[1].trim().length() == 0 || kv[1].equalsIgnoreCase("asc") ? ORDER.asc
: ORDER.desc;
solrQuery.addSort(kv[0], sc);
}
}
......@@ -56,15 +60,15 @@ public abstract class AbstractSolrApi implements SolrApi {
List<FacetField> facetFields = resp.getFacetFields();
for (FacetField facet : facetFields) {
String fieldName = facet.getName();
if(StringUtil.isNotBlank(fieldName)) {
if (StringUtil.isNotBlank(fieldName)) {
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);
List<String> list = groups.get(fieldName);
if(list == null) {
if (list == null) {
list = new ArrayList<String>();
}
if(count.getCount() > 0) {
if (count.getCount() > 0) {
list.add(count.getName());
} else {
break;
......@@ -76,9 +80,9 @@ public abstract class AbstractSolrApi implements SolrApi {
SolrDocumentList docList = resp.getResults();
page.setTotal(docList.getNumFound());
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>();
for(String key : doc.keySet()) {
for (String key : doc.keySet()) {
Object objValue = doc.get(key);
map.put(key, objValue);
}
......@@ -92,27 +96,22 @@ public abstract class AbstractSolrApi implements SolrApi {
}
public void update(List<Map<String, Object>> list) {
HttpSolrClient solrClient = getSolrClient();
try {
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
for(Map<String, Object> map : list) {
for (Map<String, Object> map : list) {
SolrInputDocument solrInputDocument = new SolrInputDocument();
for(String key : map.keySet()) {
if(key != null && key.trim().length() > 0) {
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();
try {
for(int docIndex = 0; docIndex < docs.size(); docIndex++) {
tmp.add(docs.get(docIndex));
if((docIndex%size == 0 || docIndex == docs.size() - 1) && docIndex > 0) {
List<List<SolrInputDocument>> doclist = Lists.partition(docs, 1000);
for(List<SolrInputDocument> tmp : doclist) {
solrClient.add(tmp);
tmp = new ArrayList<SolrInputDocument>();
}
}
solrClient.commit();
} catch (Exception e) {
......@@ -129,7 +128,7 @@ public abstract class AbstractSolrApi implements SolrApi {
public void deleteById(List<String> documentIds) {
HttpSolrClient solrClient = getSolrClient();
try {
if(documentIds != null && documentIds.size() > 0) {
if (documentIds != null && documentIds.size() > 0) {
solrClient.deleteById(documentIds);
solrClient.commit();
}
......
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