From 5fcec44ec698f6e0dcc8851e77cae4179b6b41ae Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 5 May 2017 13:50:03 +0200 Subject: [PATCH] #297 - Upgraded samples for Spring Data for Apache Solr to Kay. Adapt to API changes in SolrOperations. --- pom.xml | 2 +- solr/example/Readme.md | 7 +++---- .../springdata/solr/product/Product.java | 6 +++--- .../solr/product/ProductRepository.java | 2 +- .../solr/product/ProductRepositoryCustom.java | 2 +- .../solr/product/ProductRepositoryImpl.java | 4 ++-- .../solr/AdvancedSolrRepositoryTests.java | 17 ++++++++++------- .../springdata/solr/SolrTestConfiguration.java | 4 ++-- solr/managed-schema/Readme.md | 9 ++++----- .../springdata/solr/product/ManagedProduct.java | 8 ++++---- .../springdata/solr/SolrRepositoryTests.java | 4 ++-- .../springdata/solr/SolrTestConfiguration.java | 14 +++++++------- solr/pom.xml | 6 ------ .../solr/test/util/RequiresSolrServer.java | 8 ++++---- 14 files changed, 44 insertions(+), 49 deletions(-) diff --git a/pom.xml b/pom.xml index df91b553..26997398 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ neo4j rest redis - + solr web diff --git a/solr/example/Readme.md b/solr/example/Readme.md index 1682ca7c..0701b69a 100644 --- a/solr/example/Readme.md +++ b/solr/example/Readme.md @@ -1,11 +1,10 @@ # 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. +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> cd example -:example> java -jar start.jar +:solr> ./bin/solr start -e techproducts ``` -Access via [localhost:8983/solr/](http://localhost:8983/solr/#/collection1) +Access via [localhost:8983/solr/](http://localhost:8983/solr/#/techproducts) diff --git a/solr/example/src/main/java/example/springdata/solr/product/Product.java b/solr/example/src/main/java/example/springdata/solr/product/Product.java index dfd70b99..3f1bba03 100644 --- a/solr/example/src/main/java/example/springdata/solr/product/Product.java +++ b/solr/example/src/main/java/example/springdata/solr/product/Product.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 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. @@ -28,14 +28,14 @@ import org.springframework.data.solr.repository.Score; /** * Document representing a {@link Product} and its attributes matching the fields defined in the example solr schema. + * href="http://localhost:8983/solr/techproducts/schema">example solr schema. * * @author Christoph Strobl * @author Oliver Gierke */ @Value @Builder -@SolrDocument(solrCoreName = "collection1") +@SolrDocument(collection = "techproducts") public class Product { private @Id String id; diff --git a/solr/example/src/main/java/example/springdata/solr/product/ProductRepository.java b/solr/example/src/main/java/example/springdata/solr/product/ProductRepository.java index f504ba06..13623592 100644 --- a/solr/example/src/main/java/example/springdata/solr/product/ProductRepository.java +++ b/solr/example/src/main/java/example/springdata/solr/product/ProductRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 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. diff --git a/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryCustom.java b/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryCustom.java index ce71156f..4b64dec3 100644 --- a/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryCustom.java +++ b/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryCustom.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 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. diff --git a/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryImpl.java b/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryImpl.java index 534262ff..2728cf56 100644 --- a/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryImpl.java +++ b/solr/example/src/main/java/example/springdata/solr/product/ProductRepositoryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 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. @@ -39,6 +39,6 @@ class ProductRepositoryImpl implements ProductRepositoryCustom { public Cursor findAllUsingCursor() { // NOTE: Using Cursor requires to sort by an unique field - return solrTemplate.queryForCursor(new SimpleQuery("*:*").addSort(new Sort("id")), Product.class); + return solrTemplate.queryForCursor("techproducts", new SimpleQuery("*:*").addSort(Sort.by("id")), Product.class); } } diff --git a/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java b/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java index 93e23d7b..1ad7ebf2 100644 --- a/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java +++ b/solr/example/src/test/java/example/springdata/solr/AdvancedSolrRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 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. @@ -15,7 +15,7 @@ */ package example.springdata.solr; -import static org.hamcrest.core.IsNull.*; +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.*; @@ -24,7 +24,9 @@ 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 org.junit.ClassRule; import org.junit.Test; @@ -45,6 +47,7 @@ import org.springframework.test.context.junit4.SpringRunner; /** * @author Christoph Strobl * @author Oliver Gierke + * @author Mark Paluch */ @RunWith(SpringRunner.class) @SpringBootTest @@ -104,7 +107,7 @@ public class AdvancedSolrRepositoryTests { Query query = new SimpleQuery(where(exists("popularity"))).addProjectionOnFields("*", "score"); - operations.queryForPage(query, Product.class).forEach(System.out::println); + operations.queryForPage("techproducts", query, Product.class).forEach(System.out::println); } /** @@ -118,16 +121,16 @@ public class AdvancedSolrRepositoryTests { Query query = new SimpleQuery(where("id").is(xbox.getId())); // add document but delay commit for 3 seconds - operations.saveBean(xbox, 3000); + operations.saveBean("techproducts", xbox, Duration.ofSeconds(3)); // document will not be returned hence not yet committed to the index - assertThat(operations.queryForObject(query, Product.class), nullValue()); + assertThat(operations.queryForObject("techproducts", query, Product.class), is(Optional.empty())); // realtime-get fetches uncommitted document - assertThat(operations.getById(xbox.getId(), Product.class), notNullValue()); + 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(query, Product.class), notNullValue()); + assertThat(operations.queryForObject("techproducts", query, Product.class).isPresent(), is(true)); } } diff --git a/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java b/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java index 66784456..f36b7126 100644 --- a/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java +++ b/solr/example/src/test/java/example/springdata/solr/SolrTestConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 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. @@ -39,7 +39,7 @@ public class SolrTestConfiguration { @Autowired CrudRepository repo; public @Bean SolrTemplate solrTemplate() { - return new SolrTemplate(new HttpSolrClient("http://localhost:8983/solr"), "collection1"); + return new SolrTemplate(new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build()); } /** diff --git a/solr/managed-schema/Readme.md b/solr/managed-schema/Readme.md index a2774c42..3f3ca624 100644 --- a/solr/managed-schema/Readme.md +++ b/solr/managed-schema/Readme.md @@ -1,12 +1,11 @@ # 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. +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> cd example -:example> java -Dsolr.solr.home=example-schemaless/solr -jar start.jar +:solr> ./bin/solr start -e schemaless ``` -Access via [localhost:8983/solr/](http://localhost:8983/solr/#/collection1). -Fields available at [../schema/fields](http://localhost:8983/solr/collection1/schema/fields) \ No newline at end of file +Access via [localhost:8983/solr/](http://localhost:8983/solr/#/gettingstarted). +Fields available at [../schema/fields](http://localhost:8983/solr/gettingstarted/schema/fields) \ No newline at end of file diff --git a/solr/managed-schema/src/main/java/example/springdata/solr/product/ManagedProduct.java b/solr/managed-schema/src/main/java/example/springdata/solr/product/ManagedProduct.java index 0a3c4eb1..ec54ece7 100644 --- a/solr/managed-schema/src/main/java/example/springdata/solr/product/ManagedProduct.java +++ b/solr/managed-schema/src/main/java/example/springdata/solr/product/ManagedProduct.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2017 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. @@ -15,10 +15,10 @@ */ package example.springdata.solr.product; -import java.util.List; - 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; @@ -28,7 +28,7 @@ import org.springframework.data.solr.core.mapping.SolrDocument; * * @author Christoph Strobl */ -@SolrDocument(solrCoreName = "collection1") +@SolrDocument(solrCoreName = "gettingstarted") @Data public class ManagedProduct { diff --git a/solr/managed-schema/src/test/java/example/springdata/solr/SolrRepositoryTests.java b/solr/managed-schema/src/test/java/example/springdata/solr/SolrRepositoryTests.java index 6ebb1d89..2789fab1 100644 --- a/solr/managed-schema/src/test/java/example/springdata/solr/SolrRepositoryTests.java +++ b/solr/managed-schema/src/test/java/example/springdata/solr/SolrRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 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. @@ -41,7 +41,7 @@ public class SolrRepositoryTests { * Adds missing fields to the schema.
* By default the fields {@literal id} and {@literal _version_} are present.
* Check fields using - * ../solr/collection1/schema/fields
+ * ../solr/gettingstarted/schema/fields
*
* NOTE: requires Solr to run in managed schema mode. */ diff --git a/solr/managed-schema/src/test/java/example/springdata/solr/SolrTestConfiguration.java b/solr/managed-schema/src/test/java/example/springdata/solr/SolrTestConfiguration.java index 9127076f..af2e26c0 100644 --- a/solr/managed-schema/src/test/java/example/springdata/solr/SolrTestConfiguration.java +++ b/solr/managed-schema/src/test/java/example/springdata/solr/SolrTestConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 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. @@ -28,21 +28,21 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.solr.repository.config.EnableSolrRepositories; /** - * {@link Configuration} class enabling schema support for solr.
+ * {@link Configuration} class enabling schema support for Apache Solr.
*
- * NOTE: Requires solr to run in managed schema mode. run with - * {@code solr/example $ java -Dsolr.solr.home=example-schemaless/solr -jar start.jar}. + * NOTE: Requires solr to run in managed schema mode. Run Solr with + * {@code ./bin/solr start -e schemaless}. * * @author Christoph Strobl */ @SpringBootApplication -@EnableSolrRepositories(schemaCreationSupport = true, multicoreSupport = true) +@EnableSolrRepositories(schemaCreationSupport = true) public class SolrTestConfiguration { @Autowired ProductRepository repo; - public @Bean SolrClient solrServer() { - return new HttpSolrClient("http://localhost:8983/solr"); + public @Bean SolrClient solrClient() { + return new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build(); } /** diff --git a/solr/pom.xml b/solr/pom.xml index b6e74398..460e6f13 100644 --- a/solr/pom.xml +++ b/solr/pom.xml @@ -32,12 +32,6 @@ joda-time joda-time - - - org.apache.solr - solr-solrj - 5.3.1 - diff --git a/solr/util/src/main/java/example/springdata/solr/test/util/RequiresSolrServer.java b/solr/util/src/main/java/example/springdata/solr/test/util/RequiresSolrServer.java index 7eccbd1a..5082e9d8 100644 --- a/solr/util/src/main/java/example/springdata/solr/test/util/RequiresSolrServer.java +++ b/solr/util/src/main/java/example/springdata/solr/test/util/RequiresSolrServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2017 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. @@ -23,20 +23,20 @@ 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.internal.AssumptionViolatedException; +import org.junit.AssumptionViolatedException; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; /** * {@link TestRule} implementation using {@link CloseableHttpClient} to check if Solr is running by sending - * {@literal GET} request to {@literal /admin/ping}. + * {@literal GET} request to {@literal /admin/info/system}. * * @author Christoph Strobl */ public class RequiresSolrServer implements TestRule { - private static final String PING_PATH = "/admin/ping"; + private static final String PING_PATH = "/admin/info/system"; private final String baseUrl;