#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,44 @@
/*
* 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.List;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;
/**
* Document representing a Product and its attributes that are propagated to the solr schema on first save of entity.
*
* @author Christoph Strobl
*/
@SolrDocument(solrCoreName = "collection1")
@Data
public class ManagedProduct {
private @Id String id;
private @Indexed(type = "text_general") String name;
private @Indexed(name = "cat", type = "string") List<String> category;
private @Indexed boolean inStock;
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", category=" + category + ", inStock=" + inStock + "]";
}
}

View File

@@ -0,0 +1,27 @@
/*
* 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 org.springframework.data.repository.CrudRepository;
/**
* Repository definition for {@link ManagedProduct}.
*
* @author Christoph Strobl
*/
public interface ProductRepository extends CrudRepository<ManagedProduct, String> {
}

View File

@@ -0,0 +1,58 @@
/*
* 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 org.junit.ClassRule;
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;
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;
/**
* Adds missing fields to the schema. <br />
* By default the fields {@literal id} and {@literal _version_} are present. <br />
* Check fields using <a
* href="http://localhost:8983/solr/collection1/schema/fields">../solr/collection1/schema/fields</a> <br />
* <br />
* <strong>NOTE</strong>: requires Solr to run in managed schema mode.
*/
@Test
public void triggerSchemaUpdateOnFirstSave() {
ManagedProduct p = new ManagedProduct();
p.setId("p-1");
repo.save(p);
Iterable<ManagedProduct> all = repo.findAll();
for (ManagedProduct product : all) {
System.out.println(product);
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* 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.PreDestroy;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
/**
* {@link Configuration} class enabling schema support for solr.<br />
* <br />
* <strong>NOTE</strong>: Requires solr to run in managed schema mode. run with
* {@code solr/example $ java -Dsolr.solr.home=example-schemaless/solr -jar start.jar}.
*
* @author Christoph Strobl
*/
@Configuration
@EnableSolrRepositories(schemaCreationSupport = true, multicoreSupport = true)
public class SolrTestConfiguration {
@Autowired ProductRepository repo;
@Bean
public SolrServer solrServer() {
return new HttpSolrServer("http://localhost:8983/solr");
}
/**
* Remove test data when context is shut down.
*/
@PreDestroy
public void deleteDocumentsOnShutdown() {
repo.deleteAll();
}
}

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" level="debug" />
<root level="warn">
<appender-ref ref="console" />
</root>
</configuration>