#6 - Add samples for Spring Data Solr.

Added sample using deep pagination with cursor and one for managed schema support. Added JUnit rule checking that Solr is up and running.

Original pull request: #12.
This commit is contained in:
Christoph Strobl
2014-09-02 09:08:36 +02:00
committed by Oliver Gierke
parent 91565e3e21
commit de7c2c5522
20 changed files with 700 additions and 2 deletions

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2014 the original author or authors.
*
* 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 example.springdata.solr;
import java.util.Iterator;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.query.result.Cursor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import example.springdata.solr.test.util.RequiresSolrServer;
/**
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SolrTestConfiguration.class })
public class SolrRepositoryTests {
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
@Autowired ProductRepository repo;
/**
* Finds all entries using a single request.
*/
@Test
public void findAll() {
Iterator<Product> iterator = repo.findAll().iterator();
printResult(iterator);
}
/**
* Pages through all entries using cursor marks. Have a look at the Solr console output to see iteration steps.
*/
@Test
public void findAllUsingDeepPagination() {
Cursor<Product> cursor = repo.findAllUsingCursor();
printResult(cursor);
}
private void printResult(Iterator<Product> it) {
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2014 the original author or authors.
*
* 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 example.springdata.solr;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.solr.core.SolrTemplate;
/**
* @author Christoph Strobl
*/
@Configuration
@EnableAutoConfiguration
public class SolrTestConfiguration {
@Autowired CrudRepository<Product, String> repo;
@Bean
public SolrTemplate solrTemplate() {
return new SolrTemplate(new HttpSolrServer("http://localhost:8983/solr"), "collection1");
}
/**
* Remove test data when context is shut down.
*/
@PreDestroy
public void deleteDocumentsOnShutdown() {
repo.deleteAll();
}
/**
* Initialize Solr instance with test data once context has started.
*/
@PostConstruct
public void initWithTestData() {
for (int i = 0; i < 100; i++) {
Product p = new Product();
p.setId("p-" + i);
p.setName("foobar");
repo.save(p);
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.data.solr.core.SolrTemplate" level="debug" />
<root level="warn">
<appender-ref ref="console" />
</root>
</configuration>