Revision 8872
Added by Jing Tao over 9 years ago
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 |
} |
Also available in: Unified diff
Add code to determine if a id exists in a solr server.