DATAES-50 - Enabled support for scripts for node client, added missing groovy lib

This commit is contained in:
Artur Konczak 2015-12-31 02:04:44 +00:00
parent d6603f8edf
commit 7dbab75cf5
9 changed files with 73 additions and 29 deletions

View File

@ -128,6 +128,13 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.5</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -17,14 +17,21 @@ package org.springframework.data.elasticsearch.client;
import static org.elasticsearch.node.NodeBuilder.*; import static org.elasticsearch.node.NodeBuilder.*;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
/** /**
* NodeClientFactoryBean * NodeClientFactoryBean
@ -41,6 +48,7 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
private String clusterName; private String clusterName;
private NodeClient nodeClient; private NodeClient nodeClient;
private String pathData; private String pathData;
private String pathConfiguration;
NodeClientFactoryBean() { NodeClientFactoryBean() {
} }
@ -67,14 +75,25 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder() ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
.put(loadConfig())
.put("http.enabled", String.valueOf(this.enableHttp)) .put("http.enabled", String.valueOf(this.enableHttp))
.put("path.data", this.pathData); .put("path.data", this.pathData);
nodeClient = (NodeClient) nodeBuilder().settings(settings).clusterName(this.clusterName).local(this.local).node() nodeClient = (NodeClient) nodeBuilder().settings(settings).clusterName(this.clusterName).local(this.local).node()
.client(); .client();
} }
private Settings loadConfig() {
if (StringUtils.isNotBlank(pathConfiguration)) {
try {
return ImmutableSettings.builder().loadFromUrl(new ClassPathResource(pathConfiguration).getURL()).build();
} catch (IOException e) {
logger.error(String.format("Unable to read node configuration from file [%s]", pathConfiguration), e);
}
}
return ImmutableSettings.builder().build();
}
public void setLocal(boolean local) { public void setLocal(boolean local) {
this.local = local; this.local = local;
} }
@ -91,6 +110,10 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
this.pathData = pathData; this.pathData = pathData;
} }
public void setPathConfiguration(String configuration) {
this.pathConfiguration = configuration;
}
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
try { try {

View File

@ -44,6 +44,7 @@ public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name")); builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
builder.addPropertyValue("enableHttp", Boolean.valueOf(element.getAttribute("http-enabled"))); builder.addPropertyValue("enableHttp", Boolean.valueOf(element.getAttribute("http-enabled")));
builder.addPropertyValue("pathData", element.getAttribute("path-data")); builder.addPropertyValue("pathData", element.getAttribute("path-data"));
builder.addPropertyValue("pathConfiguration", element.getAttribute("path-configuration"));
} }
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,

View File

@ -87,7 +87,6 @@ import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Mapping; import org.springframework.data.elasticsearch.annotations.Mapping;
import org.springframework.data.elasticsearch.annotations.Setting; import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter; import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.facet.FacetRequest; import org.springframework.data.elasticsearch.core.facet.FacetRequest;
@ -842,15 +841,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
} }
} }
if (!searchQuery.getScriptFields().isEmpty()) { if (!searchQuery.getScriptFields().isEmpty()) {
searchRequest.addField("_source"); searchRequest.addField("_source");
} for (ScriptField scriptedField : searchQuery.getScriptFields()) {
searchRequest.addScriptField(scriptedField.fieldName(), scriptedField.script(), scriptedField.params());
}
}
for (ScriptField scriptedField : searchQuery.getScriptFields()) { if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
searchRequest.addScriptField(scriptedField.fieldName(), scriptedField.script(), scriptedField.params());
}
if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
for (FacetRequest facetRequest : searchQuery.getFacets()) { for (FacetRequest facetRequest : searchQuery.getFacets()) {
FacetBuilder facet = facetRequest.getFacet(); FacetBuilder facet = facetRequest.getFacet();
if (facetRequest.applyQueryFilter() && searchQuery.getFilter() != null) { if (facetRequest.applyQueryFilter() && searchQuery.getFilter() != null) {

View File

@ -6,26 +6,26 @@ import java.util.Map;
* @author Ryan Murfitt * @author Ryan Murfitt
*/ */
public class ScriptField { public class ScriptField {
private final String fieldName;
private final String script;
private final Map<String, Object> params;
public ScriptField(String fieldName, String script, Map<String, Object> params) { private final String fieldName;
this.fieldName = fieldName; private final String script;
this.script = script; private final Map<String, Object> params;
this.params = params;
}
public String fieldName() { public ScriptField(String fieldName, String script, Map<String, Object> params) {
return fieldName; this.fieldName = fieldName;
} this.script = script;
this.params = params;
}
public String script() { public String fieldName() {
return script; return fieldName;
} }
public Map<String, Object> params() { public String script() {
return params; return script;
} }
public Map<String, Object> params() {
return params;
}
} }

View File

@ -74,6 +74,13 @@
</xsd:documentation> </xsd:documentation>
</xsd:annotation> </xsd:annotation>
</xsd:attribute> </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:extension>
</xsd:complexContent> </xsd:complexContent>
</xsd:complexType> </xsd:complexType>

View File

@ -19,6 +19,7 @@ import lombok.*;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version; import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.ScriptedField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.geo.GeoPoint;
/** /**
@ -39,6 +40,8 @@ public class SampleEntity {
private String type; private String type;
private String message; private String message;
private int rate; private int rate;
@ScriptedField
private Long scriptedRate;
private boolean available; private boolean available;
private String highlightedMessage; private String highlightedMessage;

View File

@ -5,7 +5,9 @@
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd 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"> 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()}" http-enabled="false" path-data="target/elasticsearchTestData"/> <elasticsearch:node-client id="client" local="true" cluster-name="#{T(java.util.UUID).randomUUID().toString()}"
http-enabled="false" path-data="target/elasticsearchTestData"
path-configuration="node-client-configuration.yml"/>
<!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300" />--> <!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300" />-->

View File

@ -0,0 +1,3 @@
#enabled scripts - this require groovy
script.inline: on
script.indexed: on