Revision 7547
Added by Jing Tao almost 11 years ago
SolrIndex.java | ||
---|---|---|
48 | 48 |
import org.apache.commons.logging.LogFactory; |
49 | 49 |
import org.apache.solr.client.solrj.SolrServer; |
50 | 50 |
import org.apache.solr.client.solrj.SolrServerException; |
51 |
import org.apache.solr.client.solrj.impl.HttpSolrServer; |
|
52 |
import org.apache.solr.client.solrj.response.UpdateResponse; |
|
51 | 53 |
import org.apache.solr.common.SolrInputDocument; |
52 | 54 |
import org.dataone.cn.indexer.XMLNamespaceConfig; |
53 | 55 |
import org.dataone.cn.indexer.XPathDocumentParser; |
... | ... | |
67 | 69 |
public class SolrIndex { |
68 | 70 |
|
69 | 71 |
//private static final String INPUT_ENCODING = "UTF-8"; |
70 |
|
|
72 |
public static final String METACATPIDFIELD = "id"; |
|
71 | 73 |
private List<IDocumentSubprocessor> subprocessors = null; |
72 | 74 |
private SolrServer solrServer = null; |
73 | 75 |
private XMLNamespaceConfig xmlNamespaceConfig = null; |
... | ... | |
95 | 97 |
/** |
96 | 98 |
* Constructor |
97 | 99 |
*/ |
98 |
public SolrIndex(XMLNamespaceConfig xmlNamespaceConfig, List<SolrField> sysmetaSolrFields)
|
|
100 |
public SolrIndex(List<SolrField> sysmetaSolrFields, XMLNamespaceConfig xmlNamespaceConfig)
|
|
99 | 101 |
throws XPathExpressionException, ParserConfigurationException { |
100 | 102 |
this.xmlNamespaceConfig = xmlNamespaceConfig; |
101 | 103 |
this.sysmetaSolrFields = sysmetaSolrFields; |
104 |
solrServer = new HttpSolrServer("http://localhost:8080/solr/"); |
|
102 | 105 |
init(); |
103 | 106 |
} |
104 | 107 |
|
... | ... | |
261 | 264 |
if(doc != null) { |
262 | 265 |
List<SolrElementField> list = doc.getFieldList(); |
263 | 266 |
if(list != null) { |
267 |
//solrDoc.addField(METACATPIDFIELD, pid); |
|
264 | 268 |
Iterator<SolrElementField> iterator = list.iterator(); |
265 | 269 |
while (iterator.hasNext()) { |
266 | 270 |
SolrElementField field = iterator.next(); |
267 | 271 |
if(field != null) { |
268 | 272 |
String value = field.getValue(); |
269 | 273 |
String name = field.getName(); |
274 |
//System.out.println("add name/value pair - "+name+"/"+value); |
|
270 | 275 |
solrDoc.addField(name, value); |
271 | 276 |
} |
272 | 277 |
} |
... | ... | |
274 | 279 |
} |
275 | 280 |
} |
276 | 281 |
if(!solrDoc.isEmpty()) { |
277 |
solrServer.add(solrDoc); |
|
282 |
UpdateResponse response = solrServer.add(solrDoc); |
|
283 |
solrServer.commit(); |
|
284 |
//System.out.println("=================the response is:\n"+response.toString()); |
|
278 | 285 |
} |
279 | 286 |
} |
280 | 287 |
} |
281 | 288 |
} |
289 |
|
|
290 |
/** |
|
291 |
* Remove the indexed associated with specified pid. |
|
292 |
* @param pid the pid which the indexes are associated with |
|
293 |
* @throws IOException |
|
294 |
* @throws SolrServerException |
|
295 |
*/ |
|
296 |
public void remove(String pid) throws IOException, SolrServerException { |
|
297 |
solrServer.deleteByQuery(METACATPIDFIELD + ":" + pid); |
|
298 |
|
|
299 |
} |
|
282 | 300 |
} |
Also available in: Unified diff
Add junit test class to test the SolrIndex class.