Project

General

Profile

« Previous | Next » 

Revision 7548

Added by Jing Tao over 11 years ago

Use the EmbeddedSolrServer to replace HttpSolrServer. Now it still has some issues.

View differences:

metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/SolrIndexTest.java
16 16
    
17 17
    private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
18 18
    private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
19
    private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
19 20
    
20 21
    /**
21 22
     * Test building index for an insert.
......
25 26
       SolrIndex solrIndex = generateSolrIndex();
26 27
       InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
27 28
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));
28
       String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
29 29
       solrIndex.insert(id, systemInputStream, emlInputStream);
30 30
       
31 31
    }
32 32
    
33
    /**
34
     * Test building index for an insert.
35
     */
36
    @Test
37
    public void testDelete() throws Exception {
38
       SolrIndex solrIndex = generateSolrIndex();
39
       solrIndex.remove(id);
40
       
41
    }
42
    
33 43
    private SolrIndex generateSolrIndex() throws Exception {
34 44
        ApplicationController controller = new ApplicationController();
35 45
        List<SolrIndex> list = controller.getSolrIndexes();
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/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
}
metacat-index/pom.xml
44 44
    <dependency>
45 45
        <groupId>org.apache.solr</groupId>
46 46
        <artifactId>solr-solrj</artifactId>
47
        <version>4.1.0</version>
47
        <version>3.6.2</version>
48 48
    </dependency>
49
    <dependency>
50
        <groupId>org.apache.solr</groupId>
51
        <artifactId>solr-core</artifactId>
52
        <version>3.6.2</version>
53
    </dependency>
49 54
  </dependencies>
50 55
</project>

Also available in: Unified diff