1 package com.acumenvelocity.ath.solr;
2
3 import java.io.Closeable;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.stream.Collectors;
9
10 import org.apache.solr.client.solrj.SolrClient;
11 import org.apache.solr.client.solrj.SolrQuery;
12 import org.apache.solr.client.solrj.SolrRequest.METHOD;
13 import org.apache.solr.client.solrj.impl.HttpSolrClient;
14 import org.apache.solr.client.solrj.request.SolrPing;
15 import org.apache.solr.client.solrj.response.QueryResponse;
16 import org.apache.solr.client.solrj.response.UpdateResponse;
17 import org.apache.solr.common.SolrInputDocument;
18
19 import com.acumenvelocity.ath.common.AthUtil;
20 import com.acumenvelocity.ath.common.Const;
21 import com.acumenvelocity.ath.common.Log;
22 import com.acumenvelocity.ath.common.exception.AthException;
23
24 import net.sf.okapi.common.Util;
25
26 @SuppressWarnings("deprecation")
27 public class Solr implements Closeable {
28 public static final String REQUEST_HANDLER = "requestHandler";
29 public static final String INCLUDE_SCORE = "includeScore";
30 public static final String ROWS = "rows";
31 public static final String FIELDS = "fields";
32
33 private SolrClient solr;
34
35 public Solr() {
36 super();
37
38 solr = new HttpSolrClient.Builder(Const.ATH_SOLR_URL).build();
39 }
40
41 public void createOne(String coreName, Map<String, Object> doc)
42 throws Exception {
43 doCreateOne(coreName, doc);
44 solr.commit(coreName, false, false, true);
45 }
46
47 public long createMany(String coreName, List<Map<String, Object>> docs)
48 throws Exception {
49
50
51
52 if (Util.isEmpty(docs)) {
53 Log.warn(this.getClass(), "Empty list of docs is passed to createMany() in {}", coreName);
54 return 0;
55 }
56
57 int numDocs = docs.size();
58
59 docs = AthUtil.removeNulls(docs);
60
61 List<SolrInputDocument> solrDocs = docs.stream().map(doc -> {
62 SolrInputDocument solrDoc = new SolrInputDocument();
63 doc.forEach((name, value) -> solrDoc.addField(name, value));
64 return solrDoc;
65 }).collect(Collectors.toList());
66
67 solr.add(coreName, solrDocs);
68 solr.commit(coreName, false, false, true);
69 return numDocs;
70 }
71
72 private void doCreateOne(String coreName, Map<String, Object> doc)
73 throws AthException {
74 SolrInputDocument document = new SolrInputDocument();
75 doc.forEach((name, value) -> document.addField(name, value));
76
77 try {
78 solr.add(coreName, document);
79
80 } catch (Exception e) {
81
82
83 Log.warn(this.getClass(),
84 "Error storing to solr: {}\n\ndocument: {}\n\ndoc: {}",
85 e.getMessage(), document,
86 doc);
87 }
88 }
89
90 public void deleteById(String coreName, String id) throws Exception {
91 solr.deleteById(coreName, id);
92 solr.commit(coreName, false, false, true);
93 }
94
95 public void deleteByIds(String coreName, List<String> ids) throws Exception {
96 solr.deleteById(coreName, ids);
97 solr.commit(coreName, false, false, true);
98 }
99
100 public void deleteByQuery(String coreName, String query) throws Exception {
101 solr.deleteByQuery(coreName, query);
102 solr.commit(coreName, false, false, true);
103 }
104
105 public void commit(String coreName) throws Exception {
106 solr.commit(coreName, false, false, true);
107 }
108
109 public <Response> Response getMany(String coreName, String query, Map<String, Object> queryParams,
110 Class<Response> responseClass) throws Exception {
111
112 Log.trace(Solr.class, "--- getMany() query:'{}'", query);
113 SolrQuery solrQuery = new SolrQuery();
114 solrQuery.setQuery(query);
115
116 if (queryParams != null) {
117 queryParams.forEach((name, value) -> {
118 switch (name) {
119 case REQUEST_HANDLER:
120 Object rhParam = queryParams.get(REQUEST_HANDLER);
121 if (rhParam != null) {
122 solrQuery.setRequestHandler(rhParam.toString());
123 }
124 break;
125
126 case FIELDS:
127 solrQuery.setFields((String[]) queryParams.get(FIELDS));
128 break;
129
130 case INCLUDE_SCORE:
131 solrQuery.setIncludeScore((Boolean) queryParams.get(INCLUDE_SCORE));
132 break;
133
134 case ROWS:
135 solrQuery.setRows((Integer) queryParams.get(ROWS));
136 break;
137
138 default:
139
140 if (value instanceof String[]) {
141 solrQuery.setParam(name, (String[]) value);
142
143 } else if (value instanceof Boolean) {
144 solrQuery.setParam(name, (Boolean) value);
145 }
146 break;
147 }
148 });
149 }
150
151
152
153
154 QueryResponse response = solr.query(coreName, solrQuery, METHOD.POST);
155 return responseClass.cast(response);
156 }
157
158 public void close() throws IOException {
159 solr.close();
160 }
161
162 public void ping(String coreName) throws Exception {
163
164 new SolrPing().process(solr, coreName);
165 }
166
167 public long createMany(String coreName, Iterator<SolrInputDocument> solrDocs)
168 throws Exception {
169 solr.add(coreName, solrDocs);
170 UpdateResponse response = solr.commit(coreName, false, false, true);
171 return response.getResponse().size();
172 }
173
174 public SolrClient getClient() {
175 return solr;
176 }
177 }