Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Add initial implementation of SolrIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ye Cao authored and Andrew Woods committed Oct 22, 2013
1 parent 3a9e0b4 commit 8d8c307
Show file tree
Hide file tree
Showing 8 changed files with 3,255 additions and 4 deletions.
67 changes: 63 additions & 4 deletions fcrepo-jms-indexer-core/pom.xml
Expand Up @@ -17,6 +17,8 @@
<spring.version>3.2.0.RELEASE</spring.version>
<activemq.version>5.7.0</activemq.version>
<abdera.version>1.1.3</abdera.version>
<!-- Solr version should match lucene version of modeshape -->
<solr.version>3.6.2</solr.version>
</properties>

<dependencies>
Expand All @@ -38,11 +40,11 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
Expand Down Expand Up @@ -165,7 +167,64 @@
<version>0.2.7</version>
<scope>test</scope>
</dependency>

<!-- Start of Solr Indexer libs -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solr.version}</version>
<exclusions>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>${solr.version}</version>
<exclusions>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
<exclusion>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-test-framework</artifactId>
<version>${solr.version}</version>
</dependency>
<!-- HttpClient are used for create standardalone SolrIndexer Server client.
They seem to be included with Solr 3.6.2?
Ver 4.2.5 aim to fit JENA included version -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.5</version>
</dependency>
<!-- End of Solr Indexer libs -->
</dependencies>

<build>
Expand Down
@@ -0,0 +1,91 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.indexer;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
* A Solr Indexer (stub) implementation that adds some basic information to
* a Solr index server.
*
* @author yecao
*
*/
public class SolrIndexer implements Indexer {

private final SolrServer server;

private final Logger logger = LoggerFactory.getLogger(SolrIndexer.class);

/**
* @Autowired solrServer instance is auto-@Autowired in indexer-core.xml
*/
@Autowired
public SolrIndexer(final SolrServer solrServer) {
this.server = solrServer;
}

@Override
public void update(final String pid, final String doc) {
try {
final SolrInputDocument inputDoc = new SolrInputDocument();
inputDoc.addField("id", pid);
inputDoc.addField("content", doc);
final UpdateResponse resp = server.add(inputDoc);
if (resp.getStatus() == 0) {
logger.debug("update request was successful for pid: {}", pid);
server.commit();
} else {
logger.warn(
"update request has error, code: {} for pid: {}",
resp.getStatus(), pid);
}
} catch (final SolrServerException | IOException e) {
logger.warn("Update Exception: {}", e);
}

}

/**
* (non-Javadoc)
* @see org.fcrepo.indexer.Indexer#remove(java.lang.String)
*/
@Override
public void remove(final String pid) throws IOException {
try {
final UpdateResponse resp = server.deleteById(pid);
if (resp.getStatus() == 0) {
logger.debug("remove request was successful for pid: {}", pid);
server.commit();
} else {
logger.warn("remove request has error, code: {} for pid: {}",
resp.getStatus(), pid);
}
} catch (final SolrServerException | IOException e) {
logger.warn("Delete Exception: {}", e);
}

}

}
@@ -0,0 +1,79 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.indexer;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


/**
* @author yecao
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/spring-test/test-container.xml"})
public class SolrIndexerIT {

@Autowired
private SolrIndexer solrIndexer;
@Autowired
private SolrServer server;


/**
* Test method for
* {@link org.fcrepo.indexer.SolrIndexer#update(java.lang.String, java.lang.String)}
* .
*
* @throws SolrServerException
*/
@Test
public void testUpdate() throws SolrServerException {
solrIndexer.update("123", "some content");
final SolrParams params = new SolrQuery("content");
final QueryResponse response = server.query(params);
assertEquals("123", response.getResults().get(0).get("id"));
}

/**
* Test method for
* {@link org.fcrepo.indexer.SolrIndexer#remove(java.lang.String)}.
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testRemove() throws IOException, SolrServerException {
solrIndexer.update("345", "some content");
solrIndexer.remove("345");
final SolrParams params = new SolrQuery("content");
final QueryResponse response = server.query(params);
assertEquals(0, response.getResults().getNumFound());
}

}
@@ -0,0 +1,96 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.indexer;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.util.AbstractSolrTestCase;
import org.junit.Before;
import org.junit.Test;


/**
* @author yecao
*
*/
public class SolrIndexerTest extends AbstractSolrTestCase {

private final String solrHome = "./target/test-classes/solr";

private SolrIndexer indexer;

private SolrServer server;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
server =
new EmbeddedSolrServer(h.getCoreContainer(), h.getCore()
.getName());
indexer = new SolrIndexer(server);
}

/**
* Test method for
* {@link org.fcrepo.indexer.SolrIndexer#update(java.lang.String, java.lang.String)}
* .
*
* @throws SolrServerException
*/
@Test
public void testUpdate() throws SolrServerException {
indexer.update("123", "some content");
final SolrParams params = new SolrQuery("content");
final QueryResponse response = server.query(params);
assertEquals("123", response.getResults().get(0).get("id"));
}

/**
* Test method for
* {@link org.fcrepo.indexer.SolrIndexer#remove(java.lang.String)}.
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testRemove() throws IOException, SolrServerException {
indexer.update("123", "some content");
indexer.remove("123");
final SolrParams params = new SolrQuery("content");
final QueryResponse response = server.query(params);
assertEquals(0, response.getResults().getNumFound());
}

@Override
public String getSchemaFile() {
return solrHome + "/conf/schema.xml";
}

@Override
public String getSolrConfigFile() {
return solrHome + "/conf/solrconfig.xml";
}

}

0 comments on commit 8d8c307

Please sign in to comment.