This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation of SolrIndexer
- Loading branch information
Ye Cao
authored and
Andrew Woods
committed
Oct 22, 2013
1 parent
3a9e0b4
commit 8d8c307
Showing
8 changed files
with
3,255 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
fcrepo-jms-indexer-core/src/main/java/org/fcrepo/indexer/SolrIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
fcrepo-jms-indexer-core/src/test/java/org/fcrepo/indexer/SolrIndexerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
fcrepo-jms-indexer-core/src/test/java/org/fcrepo/indexer/SolrIndexerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
} |
Oops, something went wrong.