Skip to content

Commit

Permalink
spring-projects#164 - Fix Elasticsearch example
Browse files Browse the repository at this point in the history
Remove Boot Autoconfiguration, in-memory index and facets.
GeoPoint instead of String for location.

Original pull request: spring-projects#165.
  • Loading branch information
christophstrobl authored and mp911de committed Mar 21, 2016
1 parent fc5da90 commit 09697b8
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand All @@ -46,18 +70,19 @@ public void insertDataSample() {

// 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());
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,34 +19,36 @@

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<String> 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<String> keywords) {
public Conference(String id, String name, String date, GeoPoint location, List<String> keywords) {

this.id = id;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,38 +15,29 @@
*/
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;

/**
* Test case to show Spring Data Elasticsearch functionality.
*
* @author Artur Konczak
* @author Oliver Gierke
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
Expand All @@ -61,8 +52,8 @@ public void textSearch() throws ParseException {

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<Conference> result = operations.queryForList(query, Conference.class);

Expand All @@ -77,58 +68,12 @@ public void textSearch() throws ParseException {
@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));

List<Conference> result = operations.queryForList(query, Conference.class);

assertThat(result, hasSize(2));
}

@Test
public void termFacet() {

String termField = "keywords";
String facetName = "all-keywords";

FacetedPage<Conference> 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<Conference> 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));
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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:
# <arbitrary name1>.jar <-- classes, resources, dependencies
# <arbitrary nameN>.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
#
Original file line number Diff line number Diff line change
@@ -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 "<<STANDARD>>";
// 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";
};

0 comments on commit 09697b8

Please sign in to comment.