1
pom.xml
1
pom.xml
@@ -30,7 +30,6 @@
|
||||
<module>neo4j</module>
|
||||
<module>rest</module>
|
||||
<module>redis</module>
|
||||
<module>solr</module>
|
||||
<module>web</module>
|
||||
</modules>
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
# Spring Data Solr - Examples
|
||||
|
||||
In order to run this example a 6.5+ [Solr Server](http://lucene.apache.org/solr/downloads.html) and [Maven](http://maven.apache.org/download.cgi) are required.
|
||||
|
||||
### Running Solr
|
||||
```emacs
|
||||
:solr> ./bin/solr start -e techproducts
|
||||
```
|
||||
|
||||
Access via [localhost:8983/solr/](http://localhost:8983/solr/#/techproducts)
|
||||
@@ -1,23 +0,0 @@
|
||||
<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 https://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>2.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>
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.product;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
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;
|
||||
import org.springframework.data.solr.repository.Score;
|
||||
|
||||
/**
|
||||
* Document representing a {@link Product} and its attributes matching the fields defined in the <a
|
||||
* href="http://localhost:8983/solr/techproducts/schema">example solr schema</a>.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
@SolrDocument(collection = "techproducts")
|
||||
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 String description;
|
||||
private @Indexed boolean inStock;
|
||||
private @Indexed Integer popularity;
|
||||
private @Score Float score;
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.product;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.solr.core.query.result.HighlightPage;
|
||||
import org.springframework.data.solr.repository.Boost;
|
||||
import org.springframework.data.solr.repository.Highlight;
|
||||
import org.springframework.data.solr.repository.Query;
|
||||
|
||||
/**
|
||||
* Repository definition for {@link Product}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public interface ProductRepository extends ProductRepositoryCustom, CrudRepository<Product, String> {
|
||||
|
||||
/**
|
||||
* Find documents with matching description, highlighting context within a 20 char range around the hit.
|
||||
*
|
||||
* @param description
|
||||
* @param page
|
||||
* @return
|
||||
*/
|
||||
@Highlight(fragsize = 20, snipplets = 3)
|
||||
HighlightPage<Product> findByDescriptionStartingWith(String description, Pageable page);
|
||||
|
||||
/**
|
||||
* Find the first 10 documents with a match in name or description. Boosting score for search hits in name by 2 sorts
|
||||
* documents by relevance.
|
||||
*
|
||||
* @param name
|
||||
* @param description
|
||||
* @return
|
||||
*/
|
||||
@Query
|
||||
List<Product> findTop10ByNameOrDescription(@Boost(2) String name, String description);
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.product;
|
||||
|
||||
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
|
||||
*/
|
||||
interface ProductRepositoryCustom {
|
||||
|
||||
/**
|
||||
* Use a {@link Cursor} to scroll through documents in index. <br />
|
||||
* <strong>NOTE:</strong> Requires at least Solr 4.7.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Cursor<Product> findAllUsingCursor();
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.product;
|
||||
|
||||
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
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
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("techproducts", new SimpleQuery("*:*").addSort(Sort.by("id")), Product.class);
|
||||
}
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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 static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.solr.core.query.Criteria.*;
|
||||
import static org.springframework.data.solr.core.query.ExistsFunction.*;
|
||||
|
||||
import example.springdata.solr.product.Product;
|
||||
import example.springdata.solr.product.ProductRepository;
|
||||
import example.springdata.solr.test.util.RequiresSolrServer;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.solr.core.SolrOperations;
|
||||
import org.springframework.data.solr.core.query.Function;
|
||||
import org.springframework.data.solr.core.query.Query;
|
||||
import org.springframework.data.solr.core.query.SimpleQuery;
|
||||
import org.springframework.data.solr.core.query.result.HighlightPage;
|
||||
import org.springframework.data.solr.repository.Boost;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SolrTestConfiguration.class)
|
||||
public class AdvancedSolrRepositoryTests {
|
||||
|
||||
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("techproducts");
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
@PostConstruct
|
||||
protected void doInitTestData(CrudRepository<Product, String> repository) {
|
||||
|
||||
Product playstation = Product.builder().id("id-1").name("Playstation")
|
||||
.description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build();
|
||||
Product playstation2 = Product.builder().id("id-2").name("Playstation Two")
|
||||
.description("Playstation two is the successor of playstation in 2000.").build();
|
||||
Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build();
|
||||
Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build();
|
||||
|
||||
repository.saveAll(Arrays.asList(playstation, playstation2, superNES, nintendo64));
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired ProductRepository repository;
|
||||
@Autowired SolrOperations operations;
|
||||
|
||||
/**
|
||||
* {@link HighlightPage} holds next to the entities found also information about where a match was found within the
|
||||
* document. This allows to fine grained display snipplets of data containing the matching term in context.
|
||||
*/
|
||||
@Test
|
||||
public void annotationBasedHighlighting() {
|
||||
|
||||
HighlightPage<Product> products = repository.findByDescriptionStartingWith("play", PageRequest.of(0, 10));
|
||||
|
||||
products.getHighlighted().forEach(entry -> entry.getHighlights().forEach(highligh -> System.out
|
||||
.println(entry.getEntity().getId() + " | " + highligh.getField() + ":\t" + highligh.getSnipplets())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Using {@link Boost} allows to influence scoring at query time. In this case we want hits in {@code Product#name} to
|
||||
* count twice as much as such in {@code Product#description}.
|
||||
*/
|
||||
@Test
|
||||
public void annotationBasedBoosting() {
|
||||
repository.findTop10ByNameOrDescription("Nintendo", "Nintendo").forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* Using {@link Function} in queries has no influence on restricting results as all documents will match the function.
|
||||
* Though it does influence document score. In this sample documents not having popularity assigned will be sorted to
|
||||
* the end of the list.
|
||||
*/
|
||||
@Test
|
||||
public void influcenceScoreWithFunctions() {
|
||||
|
||||
Query query = new SimpleQuery(where(exists("popularity"))).addProjectionOnFields("*", "score");
|
||||
|
||||
operations.queryForPage("techproducts", query, Product.class).forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* Using {@link SolrOperations#getById(java.io.Serializable, Class)} allows reading uncommitted documents from the
|
||||
* update log.
|
||||
*/
|
||||
@Test
|
||||
public void useRealtimeGetToReadUncommitedDocuments() throws InterruptedException {
|
||||
|
||||
Product xbox = Product.builder().id("id-5").name("XBox").description("Microsift XBox").popularity(2).build();
|
||||
Query query = new SimpleQuery(where("id").is(xbox.getId()));
|
||||
|
||||
// add document but delay commit for 3 seconds
|
||||
operations.saveBean("techproducts", xbox, Duration.ofSeconds(3));
|
||||
|
||||
// document will not be returned hence not yet committed to the index
|
||||
assertThat(operations.queryForObject("techproducts", query, Product.class), is(Optional.empty()));
|
||||
|
||||
// realtime-get fetches uncommitted document
|
||||
assertThat(operations.getById("techproducts", xbox.getId(), Product.class), notNullValue());
|
||||
|
||||
// wait a little so that changes get committed to the index - normal query will now be able to find the document.
|
||||
Thread.sleep(3010);
|
||||
assertThat(operations.queryForObject("techproducts", query, Product.class).isPresent(), is(true));
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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 example.springdata.solr.product.ProductRepository;
|
||||
import example.springdata.solr.test.util.RequiresSolrServer;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SolrTestConfiguration.class)
|
||||
public class BasicSolrRepositoryTests {
|
||||
|
||||
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("techproducts");
|
||||
|
||||
@Autowired ProductRepository repository;
|
||||
|
||||
/**
|
||||
* Finds all entries using a single request.
|
||||
*/
|
||||
@Test
|
||||
public void findAll() {
|
||||
repository.findAll().forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pages through all entries using cursor marks. Have a look at the Solr console output to see iteration steps.
|
||||
*/
|
||||
@Test
|
||||
public void findAllUsingDeepPagination() {
|
||||
repository.findAllUsingCursor().forEachRemaining(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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 example.springdata.solr.product.Product;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.apache.solr.client.solrj.impl.HttpSolrClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.solr.core.SolrTemplate;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SolrTestConfiguration {
|
||||
|
||||
@Autowired CrudRepository<Product, String> repo;
|
||||
|
||||
public @Bean SolrTemplate solrTemplate() {
|
||||
return new SolrTemplate(new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove test data when context is shut down.
|
||||
*/
|
||||
public @PreDestroy void deleteDocumentsOnShutdown() {
|
||||
repo.deleteAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize Solr instance with test data once context has started.
|
||||
*/
|
||||
public @PostConstruct void initWithTestData() {
|
||||
doInitTestData(repo);
|
||||
}
|
||||
|
||||
protected void doInitTestData(CrudRepository<Product, String> repository) {
|
||||
|
||||
IntStream.range(0, 100)
|
||||
.forEach(index -> repository.save(Product.builder().id("p-" + index).name("foobar").build()));
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
logging.level.root=WARN
|
||||
@@ -1,11 +0,0 @@
|
||||
# Spring Data Solr - Managed Schema Examples
|
||||
|
||||
In order to run this example a 6.5+ [Solr Server](http://lucene.apache.org/solr/downloads.html) and [Maven](http://maven.apache.org/download.cgi) are required.
|
||||
|
||||
### Running Solr
|
||||
```emacs
|
||||
:solr> ./bin/solr start -e schemaless
|
||||
```
|
||||
|
||||
Access via [localhost:8983/solr/](http://localhost:8983/solr/#/gettingstarted).
|
||||
Fields available at [../schema/fields](http://localhost:8983/solr/gettingstarted/schema/fields)
|
||||
@@ -1,23 +0,0 @@
|
||||
<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 https://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>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-data-solr-managed-schema-example</artifactId>
|
||||
<name>Spring Data Solr - Managed schema examples</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-solr-example-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.product;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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 = "gettingstarted")
|
||||
@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 + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.product;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* Repository definition for {@link ManagedProduct}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public interface ProductRepository extends CrudRepository<ManagedProduct, String> {
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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 example.springdata.solr.product.ManagedProduct;
|
||||
import example.springdata.solr.product.ProductRepository;
|
||||
import example.springdata.solr.test.util.RequiresSolrServer;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SolrRepositoryTests {
|
||||
|
||||
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("gettingstarted");
|
||||
|
||||
@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/gettingstarted/schema/fields">../solr/gettingstarted/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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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 example.springdata.solr.product.ProductRepository;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.apache.solr.client.solrj.SolrClient;
|
||||
import org.apache.solr.client.solrj.impl.HttpSolrClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
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 Apache Solr.<br />
|
||||
* <br />
|
||||
* <strong>NOTE</strong>: Requires solr to run in managed schema mode. Run Solr with
|
||||
* {@code ./bin/solr start -e schemaless}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableSolrRepositories(schemaCreationSupport = true)
|
||||
public class SolrTestConfiguration {
|
||||
|
||||
@Autowired ProductRepository repo;
|
||||
|
||||
public @Bean SolrClient solrClient() {
|
||||
return new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove test data when context is shut down.
|
||||
*/
|
||||
public @PreDestroy void deleteDocumentsOnShutdown() {
|
||||
repo.deleteAll();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
logging.level.root=WARN
|
||||
32
solr/pom.xml
32
solr/pom.xml
@@ -1,32 +0,0 @@
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-solr-examples</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data Solr - Examples</name>
|
||||
<description>Sample projects for Spring Data Solr</description>
|
||||
<url>https://projects.spring.io/spring-data-solr</url>
|
||||
|
||||
<modules>
|
||||
<module>util</module>
|
||||
<module>example</module>
|
||||
<module>managed-schema</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-solr</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,20 +0,0 @@
|
||||
<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 https://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>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-data-solr-example-utils</artifactId>
|
||||
<name>Spring Data Solr - Example utilities</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2018 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
|
||||
*
|
||||
* https://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.test.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.hamcrest.core.Is;
|
||||
import org.junit.Assume;
|
||||
import org.junit.AssumptionViolatedException;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link TestRule} implementation using {@link CloseableHttpClient} to check if Solr is running by sending
|
||||
* {@literal GET} request to {@literal /admin/info/system}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class RequiresSolrServer implements TestRule {
|
||||
|
||||
private static final String PING_PATH = "/admin/info/system";
|
||||
|
||||
private final String baseUrl;
|
||||
private final @Nullable String collection;
|
||||
|
||||
private RequiresSolrServer(String baseUrl) {
|
||||
this(baseUrl, null);
|
||||
}
|
||||
|
||||
private RequiresSolrServer(String baseUrl, @Nullable String collection) {
|
||||
|
||||
this.baseUrl = baseUrl;
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public static RequiresSolrServer onLocalhost() {
|
||||
return new RequiresSolrServer("http://localhost:8983/solr");
|
||||
}
|
||||
|
||||
public RequiresSolrServer withCollection(String collection) {
|
||||
return new RequiresSolrServer(baseUrl, collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
return new Statement() {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
|
||||
checkServerRunning();
|
||||
base.evaluate();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void checkServerRunning() {
|
||||
|
||||
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
|
||||
|
||||
String url = StringUtils.hasText(collection) ? baseUrl + "/" + collection + "/select?q=*:*" : baseUrl + PING_PATH;
|
||||
|
||||
CloseableHttpResponse response = client.execute(new HttpGet(url));
|
||||
if (response != null && response.getStatusLine() != null) {
|
||||
Assume.assumeThat(response.getStatusLine().getStatusCode(), Is.is(200));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new AssumptionViolatedException("SolrServer does not seem to be running", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user