DATAES-407 - Support for HighLevelRestClient via ElasticsearchRestTemplate
Original pull request: #216
This commit is contained in:
committed by
Artur Konczak
parent
9c2f876bde
commit
2f0b9b718b
@@ -1,55 +1,63 @@
|
||||
/*
|
||||
* Copyright 2013 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 org.springframework.data.elasticsearch.config;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("namespace.xml")
|
||||
public class ElasticsearchNamespaceHandlerTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void shouldCreateTransportClient() {
|
||||
assertThat(context.getBean(TransportClientFactoryBean.class), is(notNullValue()));
|
||||
assertThat(context.getBean(TransportClientFactoryBean.class), is(instanceOf(TransportClientFactoryBean.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateRepository() {
|
||||
assertThat(context.getBean(TransportClientFactoryBean.class), is(notNullValue()));
|
||||
assertThat(context.getBean(SampleElasticsearchRepository.class),
|
||||
is(instanceOf(SampleElasticsearchRepository.class)));
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2013-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
|
||||
*
|
||||
* 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 org.springframework.data.elasticsearch.config;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.elasticsearch.client.RestClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Don Wellington
|
||||
*/
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("namespace.xml")
|
||||
public class ElasticsearchNamespaceHandlerTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void shouldCreateTransportClient() {
|
||||
assertThat(context.getBean(TransportClientFactoryBean.class), is(notNullValue()));
|
||||
assertThat(context.getBean(TransportClientFactoryBean.class), is(instanceOf(TransportClientFactoryBean.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateRepository() {
|
||||
assertThat(context.getBean(TransportClientFactoryBean.class), is(notNullValue()));
|
||||
assertThat(context.getBean(SampleElasticsearchRepository.class),
|
||||
is(instanceOf(SampleElasticsearchRepository.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateRestClient() {
|
||||
assertThat(context.getBean(RestClientFactoryBean.class), is(notNullValue()));
|
||||
assertThat(context.getBean(RestClientFactoryBean.class), is(instanceOf(RestClientFactoryBean.class)));
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -21,6 +21,7 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
@@ -45,13 +46,19 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @author Mohsin Husen
|
||||
* @author Keivn Leturc
|
||||
* @author Nordine Bittich
|
||||
* @author Don Wellington
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class MappingBuilderTests {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
private String xContentBuilderToString(XContentBuilder builder) {
|
||||
builder.close();
|
||||
ByteArrayOutputStream bos = (ByteArrayOutputStream) builder.getOutputStream();
|
||||
return bos.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotFailOnCircularReference() {
|
||||
@@ -63,43 +70,43 @@ public class MappingBuilderTests {
|
||||
|
||||
@Test
|
||||
public void testInfiniteLoopAvoidance() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
|
||||
"type\":\"text\",\"index\":false," +
|
||||
"\"analyzer\":\"standard\"}}}}";
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\""
|
||||
+ "type\":\"text\",\"index\":false," + "\"analyzer\":\"standard\"}}}}";
|
||||
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null);
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
assertThat(xContentBuilderToString(xContentBuilder), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseValueFromAnnotationType() throws IOException {
|
||||
//Given
|
||||
// Given
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}";
|
||||
|
||||
//When
|
||||
// When
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id", null);
|
||||
|
||||
//Then
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
// Then
|
||||
assertThat(xContentBuilderToString(xContentBuilder), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAddStockPriceDocumentToIndex() throws IOException {
|
||||
//Given
|
||||
// Given
|
||||
|
||||
//When
|
||||
// When
|
||||
elasticsearchTemplate.deleteIndex(StockPrice.class);
|
||||
elasticsearchTemplate.createIndex(StockPrice.class);
|
||||
elasticsearchTemplate.putMapping(StockPrice.class);
|
||||
String symbol = "AU";
|
||||
double price = 2.34;
|
||||
String id = "abc";
|
||||
elasticsearchTemplate.index(buildIndex(StockPrice.builder().id(id).symbol(symbol).price(new BigDecimal(price)).build()));
|
||||
elasticsearchTemplate
|
||||
.index(buildIndex(StockPrice.builder().id(id).symbol(symbol).price(new BigDecimal(price)).build()));
|
||||
elasticsearchTemplate.refresh(StockPrice.class);
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
|
||||
List<StockPrice> result = elasticsearchTemplate.queryForList(searchQuery, StockPrice.class);
|
||||
//Then
|
||||
// Then
|
||||
assertThat(result.size(), is(1));
|
||||
StockPrice entry = result.get(0);
|
||||
assertThat(entry.getSymbol(), is(symbol));
|
||||
@@ -110,7 +117,7 @@ public class MappingBuilderTests {
|
||||
public void shouldCreateMappingForSpecifiedParentType() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"_parent\":{\"type\":\"parentType\"},\"properties\":{}}}";
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(MinimalEntity.class, "mapping", "id", "parentType");
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
assertThat(xContentBuilderToString(xContentBuilder), is(expected));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -118,13 +125,12 @@ public class MappingBuilderTests {
|
||||
*/
|
||||
@Test
|
||||
public void shouldBuildMappingWithSuperclass() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
|
||||
"type\":\"text\",\"index\":false,\"analyzer\":\"standard\"}" +
|
||||
",\"createdDate\":{\"store\":false," +
|
||||
"\"type\":\"date\",\"index\":false}}}}";
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\""
|
||||
+ "type\":\"text\",\"index\":false,\"analyzer\":\"standard\"}" + ",\"createdDate\":{\"store\":false,"
|
||||
+ "\"type\":\"date\",\"index\":false}}}}";
|
||||
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleInheritedEntity.class, "mapping", "id", null);
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
assertThat(xContentBuilderToString(xContentBuilder), is(expected));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -132,21 +138,22 @@ public class MappingBuilderTests {
|
||||
*/
|
||||
@Test
|
||||
public void shouldAddSampleInheritedEntityDocumentToIndex() throws IOException {
|
||||
//Given
|
||||
// Given
|
||||
|
||||
//When
|
||||
// When
|
||||
elasticsearchTemplate.deleteIndex(SampleInheritedEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleInheritedEntity.class);
|
||||
elasticsearchTemplate.putMapping(SampleInheritedEntity.class);
|
||||
Date createdDate = new Date();
|
||||
String message = "msg";
|
||||
String id = "abc";
|
||||
elasticsearchTemplate.index(new SampleInheritedEntityBuilder(id).createdDate(createdDate).message(message).buildIndex());
|
||||
elasticsearchTemplate
|
||||
.index(new SampleInheritedEntityBuilder(id).createdDate(createdDate).message(message).buildIndex());
|
||||
elasticsearchTemplate.refresh(SampleInheritedEntity.class);
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
|
||||
List<SampleInheritedEntity> result = elasticsearchTemplate.queryForList(searchQuery, SampleInheritedEntity.class);
|
||||
//Then
|
||||
// Then
|
||||
assertThat(result.size(), is(1));
|
||||
SampleInheritedEntity entry = result.get(0);
|
||||
assertThat(entry.getCreatedDate(), is(createdDate));
|
||||
@@ -155,13 +162,13 @@ public class MappingBuilderTests {
|
||||
|
||||
@Test
|
||||
public void shouldBuildMappingsForGeoPoint() throws IOException {
|
||||
//given
|
||||
// given
|
||||
|
||||
//when
|
||||
// when
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(GeoEntity.class, "mapping", "id", null);
|
||||
|
||||
//then
|
||||
final String result = xContentBuilder.string();
|
||||
// then
|
||||
final String result = xContentBuilderToString(xContentBuilder);
|
||||
|
||||
assertThat(result, containsString("\"pointA\":{\"type\":\"geo_point\""));
|
||||
assertThat(result, containsString("\"pointB\":{\"type\":\"geo_point\""));
|
||||
@@ -174,40 +181,40 @@ public class MappingBuilderTests {
|
||||
*/
|
||||
@Test
|
||||
public void shouldHandleReverseRelationship() {
|
||||
//given
|
||||
// given
|
||||
elasticsearchTemplate.createIndex(User.class);
|
||||
elasticsearchTemplate.putMapping(User.class);
|
||||
elasticsearchTemplate.createIndex(Group.class);
|
||||
elasticsearchTemplate.putMapping(Group.class);
|
||||
//when
|
||||
// when
|
||||
|
||||
//then
|
||||
// then
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapBooks() {
|
||||
//given
|
||||
// given
|
||||
elasticsearchTemplate.createIndex(Book.class);
|
||||
elasticsearchTemplate.putMapping(Book.class);
|
||||
//when
|
||||
//then
|
||||
// when
|
||||
// then
|
||||
|
||||
}
|
||||
|
||||
@Test // DATAES-420
|
||||
public void shouldUseBothAnalyzer() {
|
||||
//given
|
||||
// given
|
||||
elasticsearchTemplate.deleteIndex(Book.class);
|
||||
elasticsearchTemplate.createIndex(Book.class);
|
||||
elasticsearchTemplate.putMapping(Book.class);
|
||||
|
||||
//when
|
||||
// when
|
||||
Map mapping = elasticsearchTemplate.getMapping(Book.class);
|
||||
Map descriptionMapping = (Map) ((Map) mapping.get("properties")).get("description");
|
||||
Map prefixDescription = (Map) ((Map) descriptionMapping.get("fields")).get("prefix");
|
||||
|
||||
//then
|
||||
// then
|
||||
assertThat(prefixDescription.size(), is(3));
|
||||
assertThat(prefixDescription.get("type"), equalTo("text"));
|
||||
assertThat(prefixDescription.get("analyzer"), equalTo("stop"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-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.
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
@@ -26,6 +27,7 @@ import org.springframework.data.elasticsearch.entities.SampleDateMappingEntity;
|
||||
/**
|
||||
* @author Jakub Vavrik
|
||||
* @author Mohsin Husen
|
||||
* @author Don Wellington
|
||||
*/
|
||||
public class SimpleElasticsearchDateMappingTests {
|
||||
|
||||
@@ -37,6 +39,9 @@ public class SimpleElasticsearchDateMappingTests {
|
||||
@Test
|
||||
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id", null);
|
||||
Assert.assertEquals(EXPECTED_MAPPING, xContentBuilder.string());
|
||||
xContentBuilder.close();
|
||||
ByteArrayOutputStream bos = (ByteArrayOutputStream) xContentBuilder.getOutputStream();
|
||||
String result = bos.toString();
|
||||
Assert.assertEquals(EXPECTED_MAPPING, result);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 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
|
||||
*
|
||||
* 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 org.springframework.data.elasticsearch.repositories;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.entities.SampleEntity;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Don Wellington
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration("classpath:custom-method-repository-rest-test.xml")
|
||||
public class CustomMethodRepositoryRestTests extends CustomMethodRepositoryBaseTests {
|
||||
@Autowired private ElasticsearchRestTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.putMapping(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user