Revision 7548
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.embedded.EmbeddedSolrServer; |
|
51 | 52 |
import org.apache.solr.client.solrj.impl.HttpSolrServer; |
52 | 53 |
import org.apache.solr.client.solrj.response.UpdateResponse; |
53 | 54 |
import org.apache.solr.common.SolrInputDocument; |
55 |
import org.apache.solr.core.CoreContainer; |
|
54 | 56 |
import org.dataone.cn.indexer.XMLNamespaceConfig; |
55 | 57 |
import org.dataone.cn.indexer.XPathDocumentParser; |
56 | 58 |
import org.dataone.cn.indexer.parser.IDocumentSubprocessor; |
... | ... | |
68 | 70 |
*/ |
69 | 71 |
public class SolrIndex { |
70 | 72 |
|
71 |
//private static final String INPUT_ENCODING = "UTF-8"; |
|
72 |
public static final String METACATPIDFIELD = "id"; |
|
73 |
private static final String SOLRHOMEPATH = "/Users/tao/Downloads/apache-solr-3.6.2/example/solr"; |
|
74 |
|
|
75 |
private static final String SOLRHOME = "solr.solr.home"; |
|
76 |
private static final String SOLRSERVERNAME = "metacat-core"; |
|
77 |
|
|
78 |
|
|
73 | 79 |
private List<IDocumentSubprocessor> subprocessors = null; |
74 | 80 |
private SolrServer solrServer = null; |
75 | 81 |
private XMLNamespaceConfig xmlNamespaceConfig = null; |
... | ... | |
96 | 102 |
|
97 | 103 |
/** |
98 | 104 |
* Constructor |
105 |
* @throws SAXException |
|
106 |
* @throws IOException |
|
99 | 107 |
*/ |
100 | 108 |
public SolrIndex(List<SolrField> sysmetaSolrFields, XMLNamespaceConfig xmlNamespaceConfig) |
101 |
throws XPathExpressionException, ParserConfigurationException { |
|
102 |
this.xmlNamespaceConfig = xmlNamespaceConfig;
|
|
103 |
this.sysmetaSolrFields = sysmetaSolrFields;
|
|
104 |
solrServer = new HttpSolrServer("http://localhost:8080/solr/");
|
|
105 |
init();
|
|
109 |
throws XPathExpressionException, ParserConfigurationException, IOException, SAXException {
|
|
110 |
this.xmlNamespaceConfig = xmlNamespaceConfig; |
|
111 |
this.sysmetaSolrFields = sysmetaSolrFields; |
|
112 |
initSolrServer();
|
|
113 |
init(); |
|
106 | 114 |
} |
107 | 115 |
|
116 |
private void initSolrServer() throws IOException, ParserConfigurationException, SAXException { |
|
117 |
System.setProperty(SOLRHOME, SOLRHOMEPATH); |
|
118 |
CoreContainer.Initializer init = new CoreContainer.Initializer(); |
|
119 |
CoreContainer c = init.initialize(); |
|
120 |
solrServer = new EmbeddedSolrServer(c, "collection1"); |
|
121 |
} |
|
122 |
|
|
108 | 123 |
private void init() throws ParserConfigurationException, XPathExpressionException { |
109 | 124 |
xpath.setNamespaceContext(xmlNamespaceConfig); |
110 | 125 |
initExpressions(); |
... | ... | |
271 | 286 |
if(field != null) { |
272 | 287 |
String value = field.getValue(); |
273 | 288 |
String name = field.getName(); |
274 |
//System.out.println("add name/value pair - "+name+"/"+value);
|
|
289 |
System.out.println("add name/value pair - "+name+"/"+value); |
|
275 | 290 |
solrDoc.addField(name, value); |
276 | 291 |
} |
277 | 292 |
} |
... | ... | |
281 | 296 |
if(!solrDoc.isEmpty()) { |
282 | 297 |
UpdateResponse response = solrServer.add(solrDoc); |
283 | 298 |
solrServer.commit(); |
284 |
//System.out.println("=================the response is:\n"+response.toString());
|
|
299 |
System.out.println("=================the response is:\n"+response.toString()); |
|
285 | 300 |
} |
286 | 301 |
} |
287 | 302 |
} |
... | ... | |
294 | 309 |
* @throws SolrServerException |
295 | 310 |
*/ |
296 | 311 |
public void remove(String pid) throws IOException, SolrServerException { |
297 |
solrServer.deleteByQuery(METACATPIDFIELD + ":" + pid); |
|
312 |
solrServer.deleteById(pid); |
|
313 |
solrServer.commit(); |
|
298 | 314 |
|
299 | 315 |
} |
300 | 316 |
} |
Also available in: Unified diff
Use the EmbeddedSolrServer to replace HttpSolrServer. Now it still has some issues.