diff --git a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java index 330c0976..d3b1e98a 100644 --- a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 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,26 +16,50 @@ package example.springdata.elasticsearch.conference; import java.util.Arrays; +import java.util.UUID; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import org.elasticsearch.client.Client; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.geo.GeoPoint; +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; /** * @author Artur Konczak * @author Oliver Gierke + * @author Christoph Strobl */ @Configuration -@EnableAutoConfiguration +@EnableElasticsearchRepositories class ApplicationConfiguration { @Autowired ElasticsearchOperations operations; @Autowired ConferenceRepository repository; + @Bean + public NodeClientFactoryBean client() { + + NodeClientFactoryBean bean = new NodeClientFactoryBean(true); + bean.setClusterName(UUID.randomUUID().toString()); + bean.setEnableHttp(false); + bean.setPathData("target/elasticsearchTestData"); + bean.setPathHome("src/test/resources/test-home-dir"); + + return bean; + } + + @Bean + public ElasticsearchTemplate elasticsearchTemplate(Client client) throws Exception { + return new ElasticsearchTemplate(client); + } + @PreDestroy public void deleteIndex() { operations.deleteIndex(Conference.class); @@ -46,18 +70,19 @@ class ApplicationConfiguration { // Remove all documents repository.deleteAll(); - operations.refresh(Conference.class, true); + operations.refresh(Conference.class); // Save data sample repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London") - .keywords(Arrays.asList("java", "spring")).location("51.500152, -0.126236").build()); + .keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build()); repository.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London") - .keywords(Arrays.asList("scala", "play", "java")).location("51.500152, -0.126236").build()); + .keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build()); repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin") - .keywords(Arrays.asList("java", "elasticsearch", "kibana")).location("52.5234051, 13.4113999").build()); + .keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999)) + .build()); repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014") - .keywords(Arrays.asList("cloud", "aws")).location("51.500152, -0.126236").build()); + .keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build()); repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow") - .keywords(Arrays.asList("java", "spring")).location("50.0646501, 19.9449799").build()); + .keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build()); } } diff --git a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/Conference.java b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/Conference.java index 37940005..cfa210b6 100644 --- a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/Conference.java +++ b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/Conference.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 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. @@ -19,34 +19,36 @@ import static org.springframework.data.elasticsearch.annotations.FieldType.*; import java.util.List; -import lombok.Data; -import lombok.experimental.Builder; - import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; -import org.springframework.data.elasticsearch.annotations.GeoPointField; +import org.springframework.data.elasticsearch.core.geo.GeoPoint; + +import lombok.Data; +import lombok.experimental.Builder; /** * @author Artur Konczak * @author Oliver Gierke + * @auhtor Christoph Strobl */ @Data @Builder -@Document(indexName = "conference-index", shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1") +@Document(indexName = "conference-index", type = "geo-class-point-type", shards = 1, replicas = 0, + refreshInterval = "-1") public class Conference { private @Id String id; private String name; private @Field(type = Date) String date; - private @GeoPointField String location; + private GeoPoint location; private List keywords; // do not remove it public Conference() {} // do not remove it - work around for lombok generated constructor for all params - public Conference(String id, String name, String date, String location, List keywords) { + public Conference(String id, String name, String date, GeoPoint location, List keywords) { this.id = id; this.name = name; diff --git a/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java b/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java index 7c8ed462..d00ef7b2 100644 --- a/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java +++ b/elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 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,31 +15,21 @@ */ package example.springdata.elasticsearch.conference; -import static org.elasticsearch.index.query.QueryBuilders.*; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import example.springdata.elasticsearch.conference.ApplicationConfiguration; -import example.springdata.elasticsearch.conference.Conference; - import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; -import java.util.concurrent.TimeUnit; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.FacetedPage; -import org.springframework.data.elasticsearch.core.facet.request.HistogramFacetRequestBuilder; -import org.springframework.data.elasticsearch.core.facet.request.TermFacetRequestBuilder; -import org.springframework.data.elasticsearch.core.facet.result.HistogramResult; -import org.springframework.data.elasticsearch.core.facet.result.TermResult; +import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.CriteriaQuery; -import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** @@ -47,6 +37,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * * @author Artur Konczak * @author Oliver Gierke + * @author Christoph Strobl */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = ApplicationConfiguration.class) @@ -61,8 +52,8 @@ public class ElasticsearchOperationsTest { String expectedDate = "2014-10-29"; String expectedWord = "java"; - CriteriaQuery query = new CriteriaQuery(new Criteria("_all").contains(expectedWord).and( - new Criteria("date").greaterThanEqual(expectedDate))); + CriteriaQuery query = new CriteriaQuery( + new Criteria("_all").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate))); List result = operations.queryForList(query, Conference.class); @@ -77,7 +68,7 @@ public class ElasticsearchOperationsTest { @Test public void geoSpatialSearch() { - String startLocation = "50.0646501,19.9449799"; + GeoPoint startLocation = new GeoPoint(50.0646501D, 19.9449799D); String range = "330mi"; // or 530km CriteriaQuery query = new CriteriaQuery(new Criteria("location").within(startLocation, range)); @@ -85,50 +76,4 @@ public class ElasticsearchOperationsTest { assertThat(result, hasSize(2)); } - - @Test - public void termFacet() { - - String termField = "keywords"; - String facetName = "all-keywords"; - - FacetedPage firstPage = operations.queryForPage(// - new NativeSearchQueryBuilder().// - withQuery(matchAllQuery()).// - withFacet(new TermFacetRequestBuilder(facetName).// - allTerms().// - descCount().// - fields(termField).build()).// - build(), Conference.class); - - TermResult facet = (TermResult) firstPage.getFacet(facetName); - - assertThat(facet.getTerms(), hasSize(8)); - - facet.getTerms().forEach( - term -> { - assertThat(term.getTerm(), - isOneOf("java", "spring", "scala", "play", "elasticsearch", "kibana", "cloud", "aws")); - }); - } - - @Test - public void histogramFacetOnDate() { - - String termField = "date"; - int interval = 30; - String facetName = "by-date"; - - FacetedPage firstPage = operations.queryForPage(new NativeSearchQueryBuilder() // - .withQuery(matchAllQuery()) // - .withFacet(new HistogramFacetRequestBuilder(facetName).// - timeUnit(TimeUnit.DAYS).// - interval(interval). // - field(termField).build()).// - build(), Conference.class); - - HistogramResult facet = (HistogramResult) firstPage.getFacet(facetName); - - assertThat(facet.getIntervalUnit(), hasSize(3)); - } } diff --git a/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/groovy-all-2.4.4-indy.jar b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/groovy-all-2.4.4-indy.jar new file mode 100644 index 00000000..3154c33b Binary files /dev/null and b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/groovy-all-2.4.4-indy.jar differ diff --git a/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/lang-groovy-2.2.0.jar b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/lang-groovy-2.2.0.jar new file mode 100644 index 00000000..08d9cc22 Binary files /dev/null and b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/lang-groovy-2.2.0.jar differ diff --git a/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/plugin-descriptor.properties b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/plugin-descriptor.properties new file mode 100644 index 00000000..6dfe2c18 --- /dev/null +++ b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/plugin-descriptor.properties @@ -0,0 +1,80 @@ +# Elasticsearch plugin descriptor file +# This file must exist as 'plugin-descriptor.properties' at +# the root directory of all plugins. +# +# A plugin can be 'site', 'jvm', or both. +# +### example site plugin for "foo": +# +# foo.zip <-- zip file for the plugin, with this structure: +# _site/ <-- the contents that will be served +# plugin-descriptor.properties <-- example contents below: +# +# site=true +# description=My cool plugin +# version=1.0 +# +### example jvm plugin for "foo" +# +# foo.zip <-- zip file for the plugin, with this structure: +# .jar <-- classes, resources, dependencies +# .jar <-- any number of jars +# plugin-descriptor.properties <-- example contents below: +# +# jvm=true +# classname=foo.bar.BazPlugin +# description=My cool plugin +# version=2.0.0-rc1 +# elasticsearch.version=2.0 +# java.version=1.7 +# +### mandatory elements for all plugins: +# +# 'description': simple summary of the plugin +description=Groovy scripting integration for Elasticsearch +# +# 'version': plugin's version +version=2.2.0 +# +# 'name': the plugin name +name=lang-groovy + +### mandatory elements for site plugins: +# +# 'site': set to true to indicate contents of the _site/ +# directory in the root of the plugin should be served. +site=false +# +### mandatory elements for jvm plugins : +# +# 'jvm': true if the 'classname' class should be loaded +# from jar files in the root directory of the plugin. +# Note that only jar files in the root directory are +# added to the classpath for the plugin! If you need +# other resources, package them into a resources jar. +jvm=true +# +# 'classname': the name of the class to load, fully-qualified. +classname=org.elasticsearch.script.groovy.GroovyPlugin +# +# 'java.version' version of java the code is built against +# use the system property java.specification.version +# version string must be a sequence of nonnegative decimal integers +# separated by "."'s and may have leading zeros +java.version=1.7 +# +# 'elasticsearch.version' version of elasticsearch compiled against +# You will have to release a new version of the plugin for each new +# elasticsearch release. This version is checked when the plugin +# is loaded so Elasticsearch will refuse to start in the presence of +# plugins with the incorrect elasticsearch.version. +elasticsearch.version=2.2.0 +# +### deprecated elements for jvm plugins : +# +# 'isolated': true if the plugin should have its own classloader. +# passing false is deprecated, and only intended to support plugins +# that have hard dependencies against each other. If this is +# not specified, then the plugin is isolated by default. +isolated=true +# diff --git a/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/plugin-security.policy b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/plugin-security.policy new file mode 100644 index 00000000..521c93bf --- /dev/null +++ b/elasticsearch/example/src/test/resources/test-home-dir/modules/lang-groovy/plugin-security.policy @@ -0,0 +1,57 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you 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. + */ + +grant { + // needed to generate runtime classes + permission java.lang.RuntimePermission "createClassLoader"; + // needed by IndyInterface + permission java.lang.RuntimePermission "getClassLoader"; + // needed by groovy engine + permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect"; + permission java.lang.reflect.ReflectPermission "suppressAccessChecks"; + // needed by GroovyScriptEngineService to close its classloader (why?) + permission java.lang.RuntimePermission "closeClassLoader"; + // Allow executing groovy scripts with codesource of /untrusted + permission groovy.security.GroovyCodeSourcePermission "/untrusted"; + + // Standard set of classes + permission org.elasticsearch.script.ClassPermission "<>"; + // groovy runtime + permission org.elasticsearch.script.ClassPermission "groovy.grape.GrabAnnotationTransformation"; + permission org.elasticsearch.script.ClassPermission "groovy.json.JsonOutput"; + permission org.elasticsearch.script.ClassPermission "groovy.lang.Binding"; + permission org.elasticsearch.script.ClassPermission "groovy.lang.GroovyObject"; + permission org.elasticsearch.script.ClassPermission "groovy.lang.GString"; + permission org.elasticsearch.script.ClassPermission "groovy.lang.Script"; + permission org.elasticsearch.script.ClassPermission "groovy.util.GroovyCollections"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.ast.builder.AstBuilderTransformation"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.reflection.ClassInfo"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.GStringImpl"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.powerassert.ValueRecorder"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.powerassert.AssertionRenderer"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.ScriptBytecodeAdapter"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.vmplugin.v7.IndyInterface"; + permission org.elasticsearch.script.ClassPermission "sun.reflect.ConstructorAccessorImpl"; + + permission org.elasticsearch.script.ClassPermission "groovy.lang.Closure"; + permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.GeneratedClosure"; + permission org.elasticsearch.script.ClassPermission "groovy.lang.MetaClass"; + permission org.elasticsearch.script.ClassPermission "groovy.lang.Range"; +};