#297 - Upgraded samples for Spring Data for Apache Solr to Kay.

Adapt to API changes in SolrOperations.
This commit is contained in:
Christoph Strobl
2017-05-05 13:50:03 +02:00
committed by Oliver Gierke
parent d319758bef
commit 5fcec44ec6
14 changed files with 44 additions and 49 deletions

View File

@@ -27,7 +27,7 @@
<module>neo4j</module>
<module>rest</module>
<module>redis</module>
<!--<module>solr</module>-->
<module>solr</module>
<module>web</module>
</modules>

View File

@@ -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)

View File

@@ -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 <a
* href="http://localhost:8983/solr/collection1/schema">example solr schema</a>.
* href="http://localhost:8983/solr/techproducts/schema">example solr schema</a>.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
@Value
@Builder
@SolrDocument(solrCoreName = "collection1")
@SolrDocument(collection = "techproducts")
public class Product {
private @Id String id;

View File

@@ -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.

View File

@@ -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.

View File

@@ -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<Product> 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);
}
}

View File

@@ -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));
}
}

View File

@@ -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<Product, String> 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());
}
/**

View File

@@ -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)
Access via [localhost:8983/solr/](http://localhost:8983/solr/#/gettingstarted).
Fields available at [../schema/fields](http://localhost:8983/solr/gettingstarted/schema/fields)

View File

@@ -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 {

View File

@@ -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. <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 />
* <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.
*/

View File

@@ -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.<br />
* {@link Configuration} class enabling schema support for Apache 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}.
* <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, 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();
}
/**

View File

@@ -32,12 +32,6 @@
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -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;