Revision 8872
Added by Jing Tao about 10 years ago
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/EmbeddedSolrQueryService.java | ||
---|---|---|
21 | 21 |
import java.io.IOException; |
22 | 22 |
import java.io.InputStream; |
23 | 23 |
import java.io.StringWriter; |
24 |
import java.util.ArrayList; |
|
24 | 25 |
import java.util.List; |
25 | 26 |
import java.util.Map; |
26 | 27 |
import java.util.Set; |
... | ... | |
30 | 31 |
import org.apache.solr.client.solrj.SolrServerException; |
31 | 32 |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; |
32 | 33 |
import org.apache.solr.client.solrj.response.QueryResponse; |
34 |
import org.apache.solr.common.SolrDocumentList; |
|
33 | 35 |
import org.apache.solr.common.params.SolrParams; |
34 | 36 |
import org.apache.solr.common.util.XML; |
35 | 37 |
import org.apache.solr.core.CoreContainer; |
36 | 38 |
import org.apache.solr.core.SolrCore; |
39 |
import org.apache.solr.schema.IndexSchema; |
|
37 | 40 |
import org.apache.solr.schema.SchemaField; |
41 |
import org.apache.solr.servlet.SolrRequestParsers; |
|
38 | 42 |
import org.dataone.service.exceptions.NotFound; |
43 |
import org.dataone.service.exceptions.NotImplemented; |
|
39 | 44 |
import org.dataone.service.exceptions.UnsupportedType; |
45 |
import org.dataone.service.types.v1.Identifier; |
|
40 | 46 |
import org.dataone.service.types.v1.Subject; |
41 | 47 |
import org.xml.sax.SAXException; |
42 | 48 |
|
49 |
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseTransformer; |
|
43 | 50 |
|
51 |
|
|
44 | 52 |
/** |
45 | 53 |
*The query service of the embedded solr server. |
46 | 54 |
* @author tao |
... | ... | |
166 | 174 |
return solrSpecVersion; |
167 | 175 |
} |
168 | 176 |
} |
177 |
|
|
178 |
/** |
|
179 |
* If there is a solr doc for the given id. |
|
180 |
* @param id - the specified id. |
|
181 |
* @return true if there is a solr doc for this id. |
|
182 |
*/ |
|
183 |
public boolean hasSolrDoc(Identifier id) throws ParserConfigurationException, SolrServerException, IOException, SAXException{ |
|
184 |
boolean hasIt = false; |
|
185 |
if(id != null && id.getValue() != null && !id.getValue().trim().equals("") ) { |
|
186 |
SolrParams query = buildIdQuery(id.getValue()); |
|
187 |
coreContainer.reload(collectionName); |
|
188 |
QueryResponse response = solrServer.query(query); |
|
189 |
hasIt = hasResult(response); |
|
190 |
} |
|
191 |
return hasIt; |
|
192 |
} |
|
193 |
|
|
194 |
/** |
|
195 |
* Build a query for decide if the solr server has the id. |
|
196 |
* @param id |
|
197 |
* @return the query |
|
198 |
*/ |
|
199 |
public static SolrParams buildIdQuery(String id) { |
|
200 |
String query = "select?q=id:"+id; |
|
201 |
query = query.replaceAll("\\+", "%2B"); |
|
202 |
SolrParams solrParams = SolrRequestParsers.parseQueryString(query); |
|
203 |
return solrParams; |
|
204 |
} |
|
205 |
|
|
206 |
/** |
|
207 |
* Determine if the response has any solr documents. |
|
208 |
* @param response |
|
209 |
* @return true if it has at least one solr document; |
|
210 |
*/ |
|
211 |
public static boolean hasResult(QueryResponse response) { |
|
212 |
boolean hasResult = false; |
|
213 |
if(response != null) { |
|
214 |
SolrDocumentList list = response.getResults(); |
|
215 |
if(list != null && list.getNumFound() >0) { |
|
216 |
hasResult = true; |
|
217 |
} |
|
218 |
} |
|
219 |
return hasResult; |
|
220 |
} |
|
169 | 221 |
} |
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/HttpSolrQueryService.java | ||
---|---|---|
22 | 22 |
import java.io.InputStream; |
23 | 23 |
import java.net.MalformedURLException; |
24 | 24 |
import java.net.URL; |
25 |
import java.util.ArrayList; |
|
26 |
import java.util.HashMap; |
|
25 | 27 |
import java.util.List; |
26 | 28 |
import java.util.Map; |
27 | 29 |
import java.util.Set; |
30 |
import java.util.Vector; |
|
28 | 31 |
|
29 | 32 |
import javax.xml.parsers.DocumentBuilder; |
30 | 33 |
import javax.xml.parsers.DocumentBuilderFactory; |
31 | 34 |
import javax.xml.parsers.ParserConfigurationException; |
32 | 35 |
import javax.xml.xpath.XPathConstants; |
36 |
import javax.xml.xpath.XPathExpressionException; |
|
33 | 37 |
import javax.xml.xpath.XPathFactory; |
34 | 38 |
|
35 | 39 |
import org.apache.commons.codec.net.URLCodec; |
36 | 40 |
import org.apache.commons.logging.Log; |
37 | 41 |
import org.apache.commons.logging.LogFactory; |
42 |
import org.apache.solr.client.solrj.SolrServerException; |
|
38 | 43 |
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; |
44 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
39 | 45 |
import org.apache.solr.client.solrj.util.ClientUtils; |
40 | 46 |
import org.apache.solr.common.params.SolrParams; |
41 | 47 |
import org.apache.solr.core.SolrConfig; |
48 |
import org.apache.solr.schema.FieldType; |
|
42 | 49 |
import org.apache.solr.schema.IndexSchema; |
43 | 50 |
import org.apache.solr.schema.SchemaField; |
51 |
import org.apache.solr.schema.TextField; |
|
44 | 52 |
import org.dataone.configuration.Settings; |
45 | 53 |
import org.dataone.service.exceptions.NotFound; |
54 |
import org.dataone.service.exceptions.NotImplemented; |
|
55 |
import org.dataone.service.types.v1.Identifier; |
|
46 | 56 |
import org.dataone.service.types.v1.Subject; |
57 |
import org.w3c.dom.Attr; |
|
47 | 58 |
import org.w3c.dom.Document; |
59 |
import org.w3c.dom.Element; |
|
48 | 60 |
import org.w3c.dom.Node; |
49 | 61 |
import org.w3c.dom.NodeList; |
62 |
import org.xml.sax.Attributes; |
|
50 | 63 |
import org.xml.sax.InputSource; |
51 | 64 |
import org.xml.sax.SAXException; |
52 | 65 |
|
... | ... | |
306 | 319 |
return doc; |
307 | 320 |
} |
308 | 321 |
|
322 |
/** |
|
323 |
* If there is a solr doc for the given id. |
|
324 |
* @param id - the specified id. |
|
325 |
* @return true if there is a solr doc for this id. |
|
326 |
*/ |
|
327 |
public boolean hasSolrDoc(Identifier id) throws ParserConfigurationException, SolrServerException, IOException, SAXException { |
|
328 |
boolean hasIt = false; |
|
329 |
if(id != null && id.getValue() != null && !id.getValue().trim().equals("") ) { |
|
330 |
SolrParams query = EmbeddedSolrQueryService.buildIdQuery(id.getValue()); |
|
331 |
QueryResponse response = httpSolrServer.query(query); |
|
332 |
hasIt = EmbeddedSolrQueryService.hasResult(response); |
|
333 |
} |
|
334 |
return hasIt; |
|
335 |
} |
|
336 |
|
|
309 | 337 |
|
310 | 338 |
} |
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/SolrQueryServiceController.java | ||
---|---|---|
21 | 21 |
import java.io.IOException; |
22 | 22 |
import java.io.InputStream; |
23 | 23 |
import java.net.MalformedURLException; |
24 |
import java.util.List; |
|
24 | 25 |
import java.util.Map; |
25 | 26 |
import java.util.Set; |
26 | 27 |
|
... | ... | |
30 | 31 |
import org.apache.solr.client.solrj.SolrServerException; |
31 | 32 |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; |
32 | 33 |
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; |
34 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
33 | 35 |
import org.apache.solr.common.params.SolrParams; |
34 | 36 |
import org.apache.solr.core.CoreContainer; |
35 | 37 |
import org.apache.solr.schema.IndexSchema; |
... | ... | |
37 | 39 |
import org.dataone.service.exceptions.NotFound; |
38 | 40 |
import org.dataone.service.exceptions.NotImplemented; |
39 | 41 |
import org.dataone.service.exceptions.UnsupportedType; |
42 |
import org.dataone.service.types.v1.Identifier; |
|
40 | 43 |
import org.dataone.service.types.v1.Subject; |
41 | 44 |
import org.xml.sax.SAXException; |
42 | 45 |
|
... | ... | |
158 | 161 |
} |
159 | 162 |
|
160 | 163 |
} |
164 |
|
|
165 |
/** |
|
166 |
* If the solr client was configured as an embedded solr server. |
|
167 |
* @return true if it is; false otherwise. |
|
168 |
*/ |
|
169 |
public boolean isEmbeddedSolrServer() { |
|
170 |
return isEmbeddedSolrServer; |
|
171 |
} |
|
172 |
|
|
173 |
|
|
174 |
/** |
|
175 |
* If the solr server has at least one solr doc for the given id. |
|
176 |
* @param id |
|
177 |
* @return true if the server has at lease one solr doc; false otherwise. |
|
178 |
*/ |
|
179 |
public boolean hasSolrDoc(Identifier id) throws ParserConfigurationException, SolrServerException, IOException, SAXException{ |
|
180 |
if(isEmbeddedSolrServer) { |
|
181 |
return embeddedQueryService.hasSolrDoc(id); |
|
182 |
} else{ |
|
183 |
return httpQueryService.hasSolrDoc(id); |
|
184 |
} |
|
185 |
} |
|
161 | 186 |
|
162 | 187 |
|
163 | 188 |
} |
Also available in: Unified diff
Add code to determine if a id exists in a solr server.