Skip to content

Commit 4b3bbf9

Browse files
STEP-1 - Configuration, Repository, Derived Queries
Initial project setup including all required components to bootstrap the application and set up a basic search box using SolrCrudRepository.
1 parent 674bbf0 commit 4b3bbf9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+25490
-1
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target/
2+
.settings/
3+
.project
4+
.classpath
5+
.springBeans
6+
.sonar4clipse
7+
*.sonar4clipseExternals
8+
.idea
9+
spring-data-solr.iml
10+
.DS_Store

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
spring-data-solr-showcase
1+
Spring Data Solr Showcase
22
=========================
3+
4+
This example shows basic usage concepts of [Spring Data Solr](http://projects.spring.io/spring-data-solr).
5+
6+
The commit order from initial to latest guides you through:
7+
8+
1. STEP 1: Initial setup and configuration.
9+
2. STEP 2: Custom `Repository` implementation for autocomplete.
10+
3. STEP 3: `@Facet` annotation as alternative to custom implementation.
11+
4. STEP 4: Highlighting of search terms in result.
12+
5. STEP 5: Type mapping for custom types.
13+
6. STEP 6: Use multicore support to enable automatic template configuration.
14+
15+
In order to run this example a 4+ [Solr Server](http://lucene.apache.org/solr/downloads.html) and [Maven](http://maven.apache.org/download.cgi) are required.
16+
17+
The application is meant to work with the example data provided by the Solr distribution.
18+
19+
### Running Solr
20+
```emacs
21+
:solr> cd example
22+
:example> java -jar start.jar
23+
:example> cd exampledocs
24+
:exampledocs> java -jar post.jar *.xml
25+
```
26+
27+
Access via [localhost:8983/solr/](http://localhost:8983/solr/#/collection1)
28+
29+
### Running Showcase
30+
```emacs
31+
:spring-data-solr-showcase> mvn spring-boot:run
32+
```
33+
34+
Access via [localhost:8080/search](http://localhost:8080/search)

pom.xml

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.springframework.data</groupId>
5+
<artifactId>spring-data-solr-showcase</artifactId>
6+
<version>1.0.0.BUILD-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<inceptionYear>2013</inceptionYear>
9+
<url>https://github.com/christophstrobl/spring-data-solr-showcase</url>
10+
<name>Spring Data Solr Showcase</name>
11+
<description>Spring Data Solr - Spring MVC Application Showcase</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>1.0.1.RELEASE</version>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-test</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<!-- SOLR -->
31+
<dependency>
32+
<groupId>org.springframework.data</groupId>
33+
<artifactId>spring-data-solr</artifactId>
34+
<version>${spring-data-solr.verion}</version>
35+
</dependency>
36+
37+
<!-- JSON -->
38+
<dependency>
39+
<groupId>org.codehaus.jackson</groupId>
40+
<artifactId>jackson-mapper-asl</artifactId>
41+
<version>1.9.11</version>
42+
</dependency>
43+
44+
<!-- WEB -->
45+
<dependency>
46+
<groupId>javax.servlet</groupId>
47+
<artifactId>jstl</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.apache.tomcat.embed</groupId>
51+
<artifactId>tomcat-embed-jasper</artifactId>
52+
<scope>provided</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
<properties>
57+
<start-class>org.springframework.data.solr.showcase.Application</start-class>
58+
<java.version>1.7</java.version>
59+
60+
<!-- -->
61+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
62+
<junit.version>4.10</junit.version>
63+
<spring-data-solr.verion>1.1.0.RELEASE</spring-data-solr.verion>
64+
<org.springframework.version>4.0.2.RELEASE</org.springframework.version>
65+
<springhateoas>0.9.0.RELEASE</springhateoas>
66+
<servlet-api.version>3.0.1</servlet-api.version>
67+
</properties>
68+
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
79+
<licenses>
80+
<license>
81+
<name>The Apache Software License, Version 2.0</name>
82+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
83+
<distribution>repo</distribution>
84+
<comments>
85+
Copyright 2010 the original author or authors.
86+
87+
Licensed under the Apache License, Version 2.0 (the "License");
88+
you may not use this file except in compliance with the License.
89+
You may obtain a copy of the License at
90+
91+
http://www.apache.org/licenses/LICENSE-2.0
92+
93+
Unless required by applicable law or agreed to in writing, software
94+
distributed under the License is distributed on an "AS IS" BASIS,
95+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
96+
implied.
97+
See the License for the specific language governing permissions and
98+
limitations under the License.
99+
</comments>
100+
</license>
101+
</licenses>
102+
103+
<scm>
104+
<url>https://github.com/christophstrobl/spring-data-solr-showcase</url>
105+
<connection>scm:git:git://github.com/christophstrobl/spring-data-solr-showcase.git</connection>
106+
<developerConnection>scm:git:ssh://[email protected]:christophstrobl/spring-data-solr-showcase.git</developerConnection>
107+
</scm>
108+
109+
110+
<developers>
111+
<developer>
112+
<id>christophstrobl</id>
113+
<name>Christoph Strobl</name>
114+
<timezone>+1</timezone>
115+
</developer>
116+
</developers>
117+
118+
<repositories>
119+
<repository>
120+
<id>spring-snapshots</id>
121+
<url>http://repo.springsource.org/libs-snapshot</url>
122+
</repository>
123+
<repository>
124+
<id>spring-releases</id>
125+
<url>http://repo.springsource.org/libs-release</url>
126+
</repository>
127+
</repositories>
128+
129+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.solr.showcase;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
20+
import org.springframework.context.annotation.ComponentScan;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.context.annotation.Import;
23+
import org.springframework.data.solr.showcase.config.SearchContext;
24+
import org.springframework.data.solr.showcase.config.WebContext;
25+
26+
/**
27+
* @author Christoph Strobl
28+
*/
29+
@Configuration
30+
@ComponentScan
31+
@EnableAutoConfiguration
32+
@Import({ WebContext.class, SearchContext.class })
33+
public class Application {
34+
35+
public static void main(String[] args) {
36+
SpringApplication.run(Application.class, args);
37+
}
38+
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012 - 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.solr.showcase.config;
17+
18+
import org.apache.solr.client.solrj.SolrServer;
19+
import org.apache.solr.client.solrj.impl.HttpSolrServer;
20+
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.data.solr.core.SolrTemplate;
24+
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
25+
import org.springframework.data.solr.server.SolrServerFactory;
26+
import org.springframework.data.solr.server.support.MulticoreSolrServerFactory;
27+
28+
/**
29+
* @author Christoph Strobl
30+
*/
31+
@Configuration
32+
@EnableSolrRepositories(basePackages = { "org.springframework.data.solr.showcase.product" })
33+
public class SearchContext {
34+
35+
@Bean
36+
public SolrServer solrServer(@Value("${solr.host}") String solrHost) {
37+
return new HttpSolrServer(solrHost);
38+
}
39+
40+
@Bean
41+
public SolrServerFactory solrServerFactory(SolrServer solrServer) {
42+
return new MulticoreSolrServerFactory(solrServer);
43+
}
44+
45+
@Bean
46+
public SolrTemplate solrTemplate(SolrServerFactory solrServerFactory) {
47+
return new SolrTemplate(solrServerFactory);
48+
}
49+
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.solr.showcase.config;
17+
18+
import java.util.List;
19+
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
23+
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
24+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
25+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
26+
27+
/**
28+
* @author Christoph Strobl
29+
*/
30+
@Configuration
31+
public class WebContext {
32+
33+
@Bean
34+
public WebMvcConfigurerAdapter mvcViewConfigurer() {
35+
36+
return new WebMvcConfigurerAdapter() {
37+
38+
@Override
39+
public void addViewControllers(ViewControllerRegistry registry) {
40+
41+
registry.addViewController("/").setViewName("search");
42+
registry.addViewController("/monitor").setViewName("monitor");
43+
}
44+
45+
@Override
46+
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
47+
argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
48+
}
49+
};
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012 - 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.solr.showcase.product;
17+
18+
import java.util.Collection;
19+
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.Pageable;
22+
import org.springframework.data.solr.core.query.Query.Operator;
23+
import org.springframework.data.solr.repository.Query;
24+
import org.springframework.data.solr.repository.SolrCrudRepository;
25+
import org.springframework.data.solr.showcase.product.model.Product;
26+
27+
/**
28+
* @author Christoph Strobl
29+
*/
30+
interface ProductRepository extends SolrCrudRepository<Product, String> {
31+
32+
@Query(fields = { SearchableProductDefinition.ID_FIELD_NAME, SearchableProductDefinition.NAME_FIELD_NAME,
33+
SearchableProductDefinition.PRICE_FIELD_NAME, SearchableProductDefinition.FEATURES_FIELD_NAME,
34+
SearchableProductDefinition.AVAILABLE_FIELD_NAME }, defaultOperator = Operator.AND)
35+
Page<Product> findByNameIn(Collection<String> names, Pageable page);
36+
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012 - 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.solr.showcase.product;
17+
18+
import org.springframework.data.domain.Page;
19+
import org.springframework.data.domain.Pageable;
20+
import org.springframework.data.solr.showcase.product.model.Product;
21+
22+
/**
23+
* @author Christoph Strobl
24+
*/
25+
public interface ProductService {
26+
27+
int DEFAULT_PAGE_SIZE = 3;
28+
29+
Page<Product> findByName(String searchTerm, Pageable pageable);
30+
31+
Product findById(String id);
32+
33+
}

0 commit comments

Comments
 (0)