DATAES-407 - Support for HighLevelRestClient via ElasticsearchRestTemplate

Original pull request: #216
This commit is contained in:
Don Wellington 2018-06-14 22:58:31 -07:00 committed by Artur Konczak
parent 9c2f876bde
commit 2f0b9b718b
23 changed files with 6028 additions and 1895 deletions

16
pom.xml
View File

@ -19,7 +19,7 @@
<properties> <properties>
<commonscollections>3.2.1</commonscollections> <commonscollections>3.2.1</commonscollections>
<commonslang>2.6</commonslang> <commonslang>2.6</commonslang>
<elasticsearch>6.2.2</elasticsearch> <elasticsearch>6.3.0</elasticsearch>
<log4j>2.9.1</log4j> <log4j>2.9.1</log4j>
<springdata.commons>2.2.0.BUILD-SNAPSHOT</springdata.commons> <springdata.commons>2.2.0.BUILD-SNAPSHOT</springdata.commons>
<java-module-name>spring.data.elasticsearch</java-module-name> <java-module-name>spring.data.elasticsearch</java-module-name>
@ -79,6 +79,18 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId> <artifactId>log4j-over-slf4j</artifactId>
@ -169,7 +181,7 @@
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>${lombok}</version> <version>${lombok}</version>
<scope>test</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,95 @@
/*
* 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.client;
import static org.apache.commons.lang.StringUtils.*;
import lombok.extern.slf4j.Slf4j;
import java.net.URL;
import java.util.ArrayList;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* RestClientFactoryBean
*
* @author Don Wellington
*/
@Slf4j
public class RestClientFactoryBean implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
private RestHighLevelClient client;
private String hosts = "http://localhost:9200";
static final String COMMA = ",";
@Override
public void destroy() throws Exception {
try {
log.info("Closing elasticSearch client");
if (client != null) {
client.close();
}
} catch (final Exception e) {
log.error("Error closing ElasticSearch client: ", e);
}
}
@Override
public void afterPropertiesSet() throws Exception {
buildClient();
}
@Override
public RestHighLevelClient getObject() throws Exception {
return client;
}
@Override
public Class<?> getObjectType() {
return RestHighLevelClient.class;
}
@Override
public boolean isSingleton() {
return false;
}
protected void buildClient() throws Exception {
Assert.hasText(hosts, "[Assertion Failed] At least one host must be set.");
ArrayList<HttpHost> httpHosts = new ArrayList<HttpHost>();
for (String host : split(hosts, COMMA)) {
URL hostUrl = new URL(host);
httpHosts.add(new HttpHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol()));
}
client = new RestHighLevelClient(RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()])));
}
public void setHosts(String hosts) {
this.hosts = hosts;
}
public String getHosts() {
return this.hosts;
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,8 +25,8 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
* *
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Don Wellington
*/ */
public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport { public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport {
@Override @Override
@ -37,5 +37,6 @@ public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("repositories", parser); registerBeanDefinitionParser("repositories", parser);
registerBeanDefinitionParser("node-client", new NodeClientBeanDefinitionParser()); registerBeanDefinitionParser("node-client", new NodeClientBeanDefinitionParser());
registerBeanDefinitionParser("transport-client", new TransportClientBeanDefinitionParser()); registerBeanDefinitionParser("transport-client", new TransportClientBeanDefinitionParser());
registerBeanDefinitionParser("rest-client", new RestClientBeanDefinitionParser());
} }
} }

View File

@ -0,0 +1,47 @@
/*
* 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.config;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.elasticsearch.client.RestClientFactoryBean;
import org.w3c.dom.Element;
/**
* @author Don Wellington
*/
public class RestClientBeanDefinitionParser extends AbstractBeanDefinitionParser {
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RestClientFactoryBean.class);
setConfigurations(element, builder);
return getSourcedBeanDefinition(builder, element, parserContext);
}
private void setConfigurations(Element element, BeanDefinitionBuilder builder) {
builder.addPropertyValue("hosts", element.getAttribute("hosts"));
}
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
ParserContext context) {
AbstractBeanDefinition definition = builder.getBeanDefinition();
definition.setSource(context.extractSource(source));
return definition;
}
}

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,11 +43,6 @@ public interface ElasticsearchOperations {
*/ */
ElasticsearchConverter getElasticsearchConverter(); ElasticsearchConverter getElasticsearchConverter();
/**
* @return elasticsearch client
*/
Client getClient();
/** /**
* Create an index for a class * Create an index for a class
* *

View File

@ -136,7 +136,7 @@ import org.springframework.util.StringUtils;
* @author Ted Liang * @author Ted Liang
* @author Jean-Baptiste Nizet * @author Jean-Baptiste Nizet
*/ */
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware { public class ElasticsearchTemplate implements ElasticsearchOperations, EsClient<Client>, ApplicationContextAware {
private static final Logger QUERY_LOGGER = LoggerFactory.getLogger("org.springframework.data.elasticsearch.core.QUERY"); private static final Logger QUERY_LOGGER = LoggerFactory.getLogger("org.springframework.data.elasticsearch.core.QUERY");
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class); private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class);

View File

@ -0,0 +1,26 @@
/*
* 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.core;
/**
* EsClient interface. Specify what client an ElasticSearchTemplate will return from getClient().
*
* @author Don Wellington
* @param <C>
*/
public interface EsClient<C> {
C getClient();
}

View File

@ -0,0 +1,34 @@
/*
* 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.core.client.support;
import lombok.Data;
import lombok.Getter;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown=true)
@Data
public class AliasData {
String filter = null;
String routing = null;
String search_routing = null;
String index_routing= null;
}

View File

@ -1,2 +1,4 @@
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.1.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.1.xsd
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd
http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.1.xsd

View File

@ -0,0 +1,172 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.springframework.org/schema/data/elasticsearch"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:repository="http://www.springframework.org/schema/data/repository"
targetNamespace="http://www.springframework.org/schema/data/elasticsearch"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:import namespace="http://www.springframework.org/schema/data/repository"
schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd"/>
<xsd:element name="repositories">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="repository:repositories">
<xsd:attributeGroup ref="repository:repository-attributes"/>
<xsd:attribute name="elasticsearch-template-ref" type="elasticsearchTemplateRef"
default="elasticsearchTemplate"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="elasticsearchTemplateRef">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to type="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:union memberTypes="xsd:string"/>
</xsd:simpleType>
<xsd:element name="node-client">
<xsd:annotation>
<xsd:documentation/>
<xsd:appinfo>
<tool:assignable-to type="org.elasticsearch.client.Client"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="local" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation>
<![CDATA[local here means local on the JVM (well, actually class loader) level, meaning that two local servers started within the same JVM will discover themselves and form a cluster]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cluster-name" type="xsd:string" default="elasticsearch">
<xsd:annotation>
<xsd:documentation>
<![CDATA[Name of the cluster in which this instance of node client will connect to]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="http-enabled" type="xsd:boolean" default="true">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ to enable or disable http port ]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="path-data" type="xsd:string" default="">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ path to the data folder for node client ]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="path-home" type="xsd:string" default="">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ path to the home folder for node client ]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="path-configuration" type="xsd:string" default="">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ path to configuration file for node client ]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="transport-client">
<xsd:annotation>
<xsd:documentation/>
<xsd:appinfo>
<tool:assignable-to type="org.elasticsearch.client.Client"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="cluster-nodes" type="xsd:string" default="127.0.0.1:9300">
<xsd:annotation>
<xsd:documentation>
<![CDATA[The comma delimited list of host:port entries to use for elasticsearch cluster.]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="cluster-name" type="xsd:string" default="elasticsearch">
<xsd:annotation>
<xsd:documentation>
<![CDATA[Name of the cluster in which this instance of node client will connect to]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="client-transport-sniff" type="xsd:boolean" default="true">
<xsd:annotation>
<xsd:documentation>
<![CDATA[The client allows to sniff the rest of the cluster, and add those into its list of machines to use.]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="client-transport-ignore-cluster-name" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation>
<![CDATA[Set to true to ignore cluster name validation of connected nodes. (since 0.19.4)]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="client-transport-ping-timeout" type="xsd:string" default="5s">
<xsd:annotation>
<xsd:documentation>
<![CDATA[The time to wait for a ping response from a node. Defaults to 5s.]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="client-transport-nodes-sampler-interval" type="xsd:string" default="5s">
<xsd:annotation>
<xsd:documentation>
<![CDATA[How often to sample / ping the nodes listed and connected. Defaults to 5s.]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="rest-client">
<xsd:annotation>
<xsd:documentation/>
<xsd:appinfo>
<tool:assignable-to type="org.elasticsearch.client.RestHighLevelClient"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="hosts" type="xsd:string" default="http://127.0.0.1:9200">
<xsd:annotation>
<xsd:documentation>
<![CDATA[The comma delimited list of host:port entries to use for elasticsearch cluster.]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.elasticsearch.client.RestClientFactoryBean;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository; import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -31,6 +32,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* @author Rizwan Idrees * @author Rizwan Idrees
* @author Mohsin Husen * @author Mohsin Husen
* @author Don Wellington
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ -52,4 +54,10 @@ public class ElasticsearchNamespaceHandlerTests {
assertThat(context.getBean(SampleElasticsearchRepository.class), assertThat(context.getBean(SampleElasticsearchRepository.class),
is(instanceOf(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)));
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.junit.Assert.*;
import static org.springframework.data.elasticsearch.utils.IndexBuilder.*; import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
@ -45,13 +46,19 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Mohsin Husen * @author Mohsin Husen
* @author Keivn Leturc * @author Keivn Leturc
* @author Nordine Bittich * @author Nordine Bittich
* @author Don Wellington
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-test.xml") @ContextConfiguration("classpath:elasticsearch-template-test.xml")
public class MappingBuilderTests { public class MappingBuilderTests {
@Autowired @Autowired private ElasticsearchTemplate elasticsearchTemplate;
private ElasticsearchTemplate elasticsearchTemplate;
private String xContentBuilderToString(XContentBuilder builder) {
builder.close();
ByteArrayOutputStream bos = (ByteArrayOutputStream) builder.getOutputStream();
return bos.toString();
}
@Test @Test
public void shouldNotFailOnCircularReference() { public void shouldNotFailOnCircularReference() {
@ -63,43 +70,43 @@ public class MappingBuilderTests {
@Test @Test
public void testInfiniteLoopAvoidance() throws IOException { public void testInfiniteLoopAvoidance() throws IOException {
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" + final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\""
"type\":\"text\",\"index\":false," + + "type\":\"text\",\"index\":false," + "\"analyzer\":\"standard\"}}}}";
"\"analyzer\":\"standard\"}}}}";
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null); XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null);
assertThat(xContentBuilder.string(), is(expected)); assertThat(xContentBuilderToString(xContentBuilder), is(expected));
} }
@Test @Test
public void shouldUseValueFromAnnotationType() throws IOException { public void shouldUseValueFromAnnotationType() throws IOException {
//Given // Given
final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}"; final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}";
//When // When
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id", null); XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id", null);
//Then // Then
assertThat(xContentBuilder.string(), is(expected)); assertThat(xContentBuilderToString(xContentBuilder), is(expected));
} }
@Test @Test
public void shouldAddStockPriceDocumentToIndex() throws IOException { public void shouldAddStockPriceDocumentToIndex() throws IOException {
//Given // Given
//When // When
elasticsearchTemplate.deleteIndex(StockPrice.class); elasticsearchTemplate.deleteIndex(StockPrice.class);
elasticsearchTemplate.createIndex(StockPrice.class); elasticsearchTemplate.createIndex(StockPrice.class);
elasticsearchTemplate.putMapping(StockPrice.class); elasticsearchTemplate.putMapping(StockPrice.class);
String symbol = "AU"; String symbol = "AU";
double price = 2.34; double price = 2.34;
String id = "abc"; 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); elasticsearchTemplate.refresh(StockPrice.class);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
List<StockPrice> result = elasticsearchTemplate.queryForList(searchQuery, StockPrice.class); List<StockPrice> result = elasticsearchTemplate.queryForList(searchQuery, StockPrice.class);
//Then // Then
assertThat(result.size(), is(1)); assertThat(result.size(), is(1));
StockPrice entry = result.get(0); StockPrice entry = result.get(0);
assertThat(entry.getSymbol(), is(symbol)); assertThat(entry.getSymbol(), is(symbol));
@ -110,7 +117,7 @@ public class MappingBuilderTests {
public void shouldCreateMappingForSpecifiedParentType() throws IOException { public void shouldCreateMappingForSpecifiedParentType() throws IOException {
final String expected = "{\"mapping\":{\"_parent\":{\"type\":\"parentType\"},\"properties\":{}}}"; final String expected = "{\"mapping\":{\"_parent\":{\"type\":\"parentType\"},\"properties\":{}}}";
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(MinimalEntity.class, "mapping", "id", "parentType"); 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 @Test
public void shouldBuildMappingWithSuperclass() throws IOException { public void shouldBuildMappingWithSuperclass() throws IOException {
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" + final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\""
"type\":\"text\",\"index\":false,\"analyzer\":\"standard\"}" + + "type\":\"text\",\"index\":false,\"analyzer\":\"standard\"}" + ",\"createdDate\":{\"store\":false,"
",\"createdDate\":{\"store\":false," + + "\"type\":\"date\",\"index\":false}}}}";
"\"type\":\"date\",\"index\":false}}}}";
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleInheritedEntity.class, "mapping", "id", null); 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 @Test
public void shouldAddSampleInheritedEntityDocumentToIndex() throws IOException { public void shouldAddSampleInheritedEntityDocumentToIndex() throws IOException {
//Given // Given
//When // When
elasticsearchTemplate.deleteIndex(SampleInheritedEntity.class); elasticsearchTemplate.deleteIndex(SampleInheritedEntity.class);
elasticsearchTemplate.createIndex(SampleInheritedEntity.class); elasticsearchTemplate.createIndex(SampleInheritedEntity.class);
elasticsearchTemplate.putMapping(SampleInheritedEntity.class); elasticsearchTemplate.putMapping(SampleInheritedEntity.class);
Date createdDate = new Date(); Date createdDate = new Date();
String message = "msg"; String message = "msg";
String id = "abc"; 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); elasticsearchTemplate.refresh(SampleInheritedEntity.class);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
List<SampleInheritedEntity> result = elasticsearchTemplate.queryForList(searchQuery, SampleInheritedEntity.class); List<SampleInheritedEntity> result = elasticsearchTemplate.queryForList(searchQuery, SampleInheritedEntity.class);
//Then // Then
assertThat(result.size(), is(1)); assertThat(result.size(), is(1));
SampleInheritedEntity entry = result.get(0); SampleInheritedEntity entry = result.get(0);
assertThat(entry.getCreatedDate(), is(createdDate)); assertThat(entry.getCreatedDate(), is(createdDate));
@ -155,13 +162,13 @@ public class MappingBuilderTests {
@Test @Test
public void shouldBuildMappingsForGeoPoint() throws IOException { public void shouldBuildMappingsForGeoPoint() throws IOException {
//given // given
//when // when
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(GeoEntity.class, "mapping", "id", null); XContentBuilder xContentBuilder = MappingBuilder.buildMapping(GeoEntity.class, "mapping", "id", null);
//then // then
final String result = xContentBuilder.string(); final String result = xContentBuilderToString(xContentBuilder);
assertThat(result, containsString("\"pointA\":{\"type\":\"geo_point\"")); assertThat(result, containsString("\"pointA\":{\"type\":\"geo_point\""));
assertThat(result, containsString("\"pointB\":{\"type\":\"geo_point\"")); assertThat(result, containsString("\"pointB\":{\"type\":\"geo_point\""));
@ -174,40 +181,40 @@ public class MappingBuilderTests {
*/ */
@Test @Test
public void shouldHandleReverseRelationship() { public void shouldHandleReverseRelationship() {
//given // given
elasticsearchTemplate.createIndex(User.class); elasticsearchTemplate.createIndex(User.class);
elasticsearchTemplate.putMapping(User.class); elasticsearchTemplate.putMapping(User.class);
elasticsearchTemplate.createIndex(Group.class); elasticsearchTemplate.createIndex(Group.class);
elasticsearchTemplate.putMapping(Group.class); elasticsearchTemplate.putMapping(Group.class);
//when // when
//then // then
} }
@Test @Test
public void shouldMapBooks() { public void shouldMapBooks() {
//given // given
elasticsearchTemplate.createIndex(Book.class); elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class); elasticsearchTemplate.putMapping(Book.class);
//when // when
//then // then
} }
@Test // DATAES-420 @Test // DATAES-420
public void shouldUseBothAnalyzer() { public void shouldUseBothAnalyzer() {
//given // given
elasticsearchTemplate.deleteIndex(Book.class); elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class); elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class); elasticsearchTemplate.putMapping(Book.class);
//when // when
Map mapping = elasticsearchTemplate.getMapping(Book.class); Map mapping = elasticsearchTemplate.getMapping(Book.class);
Map descriptionMapping = (Map) ((Map) mapping.get("properties")).get("description"); Map descriptionMapping = (Map) ((Map) mapping.get("properties")).get("description");
Map prefixDescription = (Map) ((Map) descriptionMapping.get("fields")).get("prefix"); Map prefixDescription = (Map) ((Map) descriptionMapping.get("fields")).get("prefix");
//then // then
assertThat(prefixDescription.size(), is(3)); assertThat(prefixDescription.size(), is(3));
assertThat(prefixDescription.get("type"), equalTo("text")); assertThat(prefixDescription.get("type"), equalTo("text"));
assertThat(prefixDescription.get("analyzer"), equalTo("stop")); assertThat(prefixDescription.get("analyzer"), equalTo("stop"));

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import java.beans.IntrospectionException; import java.beans.IntrospectionException;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -26,6 +27,7 @@ import org.springframework.data.elasticsearch.entities.SampleDateMappingEntity;
/** /**
* @author Jakub Vavrik * @author Jakub Vavrik
* @author Mohsin Husen * @author Mohsin Husen
* @author Don Wellington
*/ */
public class SimpleElasticsearchDateMappingTests { public class SimpleElasticsearchDateMappingTests {
@ -37,6 +39,9 @@ public class SimpleElasticsearchDateMappingTests {
@Test @Test
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException { public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id", null); 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);
} }
} }

View File

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

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<import resource="elasticsearch-rest-template-test.xml"/>
<elasticsearch:repositories
base-package="org.springframework.data.elasticsearch.repositories.custom"/>
</beans>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="infrastructure.xml"/>
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate">
<constructor-arg name="client" ref="restClient"/>
</bean>
<elasticsearch:rest-client id="restClient"/>
</beans>

View File

@ -6,7 +6,7 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:node-client id="client" local="true" cluster-name="#{T(java.util.UUID).randomUUID().toString()}" <elasticsearch:node-client id="client" local="true" cluster-name="#{T(java.util.UUID).randomUUID().toString()}"
http-enabled="false" path-data="target/elasticsearchTestData" path-home="src/test/resources/test-home-dir" http-enabled="true" path-data="target/elasticsearchTestData" path-home="src/test/resources/test-home-dir"
path-configuration="node-client-configuration.yml"/> path-configuration="node-client-configuration.yml"/>
<!-- ip4 --> <!-- ip4 -->

View File

@ -2,7 +2,7 @@
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:node-client id="client" local="true" <elasticsearch:node-client id="client" local="true"
@ -19,4 +19,6 @@
<elasticsearch:repositories <elasticsearch:repositories
base-package="org.springframework.data.elasticsearch.repositories.sample"/> base-package="org.springframework.data.elasticsearch.repositories.sample"/>
<elasticsearch:rest-client id="restClient"/>
</beans> </beans>

View File

@ -37,7 +37,7 @@ classname=org.elasticsearch.script.expression.ExpressionPlugin
java.version=1.8 java.version=1.8
# #
# 'elasticsearch.version': version of elasticsearch compiled against # 'elasticsearch.version': version of elasticsearch compiled against
elasticsearch.version=6.2.2 elasticsearch.version=6.3.0
### optional elements for plugins: ### optional elements for plugins:
# #
# 'extended.plugins': other plugins this plugin extends through SPI # 'extended.plugins': other plugins this plugin extends through SPI
@ -47,4 +47,4 @@ extended.plugins=
has.native.controller=false has.native.controller=false
# #
# 'requires.keystore': whether or not the plugin needs the elasticsearch keystore be created # 'requires.keystore': whether or not the plugin needs the elasticsearch keystore be created
requires.keystore=false #requires.keystore=false