forked from spring-projects/spring-data-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projects#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: spring-projects#12.
- Loading branch information
1 parent
91565e3
commit de7c2c5
Showing
20 changed files
with
700 additions
and
2 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
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,11 @@ | ||
# Spring Data Solr - Examples | ||
|
||
In order to run this example a 4.7+ [Solr Server](http://lucene.apache.org/solr/downloads.html) and [Maven](http://maven.apache.org/download.cgi) are required. | ||
|
||
### Running Solr | ||
```emacs | ||
:solr> cd example | ||
:example> java -jar start.jar | ||
``` | ||
|
||
Access via [localhost:8983/solr/](http://localhost:8983/solr/#/collection1) |
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,23 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.springframework.data.examples</groupId> | ||
<artifactId>spring-data-solr-examples</artifactId> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>spring-data-solr-example</artifactId> | ||
<name>Spring Data Solr - Example</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>spring-data-solr-example-utils</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
48 changes: 48 additions & 0 deletions
48
solr/example/src/main/java/example/springdata/solr/Product.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,48 @@ | ||
/* | ||
* 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.geo.Point; | ||
import org.springframework.data.solr.core.mapping.Indexed; | ||
import org.springframework.data.solr.core.mapping.SolrDocument; | ||
|
||
/** | ||
* Document representing a Product and its attributes matching the fieldes defined in the <a | ||
* href="http://localhost:8983/solr/collection1/schema">example solr schema</a>. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
@Data | ||
@SolrDocument(solrCoreName = "collection1") | ||
public class Product { | ||
|
||
private @Id String id; | ||
private @Indexed String name; | ||
private @Indexed(name = "cat") List<String> category; | ||
private @Indexed(name = "store") Point location; | ||
private @Indexed boolean inStock; | ||
|
||
@Override | ||
public String toString() { | ||
return "Product [id=" + id + ", name=" + name + ", category=" + category + ", location=" + location + ", inStock=" | ||
+ inStock + "]"; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
solr/example/src/main/java/example/springdata/solr/ProductRepository.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,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 Product}. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
public interface ProductRepository extends ProductRepositoryCustom, CrudRepository<Product, String> { | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
solr/example/src/main/java/example/springdata/solr/ProductRepositoryCustom.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,29 @@ | ||
/* | ||
* 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.Repository; | ||
import org.springframework.data.solr.core.query.result.Cursor; | ||
|
||
/** | ||
* Custom repository implementation to show special solr functions without {@link Repository} abstraction. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
public interface ProductRepositoryCustom { | ||
|
||
Cursor<Product> findAllUsingCursor(); | ||
} |
43 changes: 43 additions & 0 deletions
43
solr/example/src/main/java/example/springdata/solr/ProductRepositoryImpl.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,43 @@ | ||
/* | ||
* 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.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.solr.core.SolrTemplate; | ||
import org.springframework.data.solr.core.query.SimpleQuery; | ||
import org.springframework.data.solr.core.query.result.Cursor; | ||
|
||
/** | ||
* Implementation of {@link ProductRepositoryCustom}. | ||
* | ||
* @author Christoph Strobl | ||
*/ | ||
public class ProductRepositoryImpl implements ProductRepositoryCustom { | ||
|
||
@Autowired SolrTemplate solrTemplate; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see example.springdata.solr.ProductRepositoryCustom#findAllUsingCursor() | ||
*/ | ||
@Override | ||
public Cursor<Product> findAllUsingCursor() { | ||
|
||
// NOTE: Using Cursor requires to sort by an unique field | ||
return solrTemplate.queryForCursor(new SimpleQuery("*:*").addSort(new Sort("id")), Product.class); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
solr/example/src/test/java/example/springdata/solr/SolrRepositoryTests.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,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()); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.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,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); | ||
} | ||
} | ||
} |
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,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> |
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,12 @@ | ||
# Spring Data Solr - Managed Schema Examples | ||
|
||
In order to run this example a 4.7+ [Solr Server](http://lucene.apache.org/solr/downloads.html) and [Maven](http://maven.apache.org/download.cgi) are required. | ||
|
||
### Running Solr | ||
```emacs | ||
:solr> cd example | ||
:example> java -Dsolr.solr.home=example-schemaless/solr -jar start.jar | ||
``` | ||
|
||
Access via [localhost:8983/solr/](http://localhost:8983/solr/#/collection1). | ||
Fields available at [../schema/fields](http://localhost:8983/solr/collection1/schema/fields) |
Oops, something went wrong.