mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 05:02:11 +00:00
DATAES-50 - Enabled support for scripts for node client, added missing groovy lib
This commit is contained in:
parent
d6603f8edf
commit
7dbab75cf5
7
pom.xml
7
pom.xml
@ -128,6 +128,13 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>2.3.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -17,14 +17,21 @@ package org.springframework.data.elasticsearch.client;
|
||||
|
||||
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.node.NodeClient;
|
||||
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* NodeClientFactoryBean
|
||||
@ -41,6 +48,7 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
|
||||
private String clusterName;
|
||||
private NodeClient nodeClient;
|
||||
private String pathData;
|
||||
private String pathConfiguration;
|
||||
|
||||
NodeClientFactoryBean() {
|
||||
}
|
||||
@ -67,14 +75,25 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
|
||||
.put(loadConfig())
|
||||
.put("http.enabled", String.valueOf(this.enableHttp))
|
||||
.put("path.data", this.pathData);
|
||||
|
||||
|
||||
nodeClient = (NodeClient) nodeBuilder().settings(settings).clusterName(this.clusterName).local(this.local).node()
|
||||
.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) {
|
||||
this.local = local;
|
||||
}
|
||||
@ -91,6 +110,10 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
|
||||
this.pathData = pathData;
|
||||
}
|
||||
|
||||
public void setPathConfiguration(String configuration) {
|
||||
this.pathConfiguration = configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
try {
|
||||
|
@ -44,6 +44,7 @@ public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser
|
||||
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
|
||||
builder.addPropertyValue("enableHttp", Boolean.valueOf(element.getAttribute("http-enabled")));
|
||||
builder.addPropertyValue("pathData", element.getAttribute("path-data"));
|
||||
builder.addPropertyValue("pathConfiguration", element.getAttribute("path-configuration"));
|
||||
}
|
||||
|
||||
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
|
||||
|
@ -87,7 +87,6 @@ import org.springframework.data.elasticsearch.ElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Mapping;
|
||||
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.MappingElasticsearchConverter;
|
||||
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
|
||||
@ -842,15 +841,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
||||
}
|
||||
}
|
||||
|
||||
if (!searchQuery.getScriptFields().isEmpty()) {
|
||||
searchRequest.addField("_source");
|
||||
}
|
||||
if (!searchQuery.getScriptFields().isEmpty()) {
|
||||
searchRequest.addField("_source");
|
||||
for (ScriptField scriptedField : searchQuery.getScriptFields()) {
|
||||
searchRequest.addScriptField(scriptedField.fieldName(), scriptedField.script(), scriptedField.params());
|
||||
}
|
||||
}
|
||||
|
||||
for (ScriptField scriptedField : searchQuery.getScriptFields()) {
|
||||
searchRequest.addScriptField(scriptedField.fieldName(), scriptedField.script(), scriptedField.params());
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
|
||||
if (CollectionUtils.isNotEmpty(searchQuery.getFacets())) {
|
||||
for (FacetRequest facetRequest : searchQuery.getFacets()) {
|
||||
FacetBuilder facet = facetRequest.getFacet();
|
||||
if (facetRequest.applyQueryFilter() && searchQuery.getFilter() != null) {
|
||||
|
@ -6,26 +6,26 @@ import java.util.Map;
|
||||
* @author Ryan Murfitt
|
||||
*/
|
||||
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) {
|
||||
this.fieldName = fieldName;
|
||||
this.script = script;
|
||||
this.params = params;
|
||||
}
|
||||
private final String fieldName;
|
||||
private final String script;
|
||||
private final Map<String, Object> params;
|
||||
|
||||
public String fieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
public ScriptField(String fieldName, String script, Map<String, Object> params) {
|
||||
this.fieldName = fieldName;
|
||||
this.script = script;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public String script() {
|
||||
return script;
|
||||
}
|
||||
public String fieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
public Map<String, Object> params() {
|
||||
return params;
|
||||
}
|
||||
public String script() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public Map<String, Object> params() {
|
||||
return params;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,6 +74,13 @@
|
||||
</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>
|
||||
|
@ -19,6 +19,7 @@ import lombok.*;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
|
||||
/**
|
||||
@ -39,6 +40,8 @@ public class SampleEntity {
|
||||
private String type;
|
||||
private String message;
|
||||
private int rate;
|
||||
@ScriptedField
|
||||
private Long scriptedRate;
|
||||
private boolean available;
|
||||
private String highlightedMessage;
|
||||
|
||||
|
@ -5,7 +5,9 @@
|
||||
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">
|
||||
|
||||
<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" />-->
|
||||
|
||||
|
3
src/test/resources/node-client-configuration.yml
Normal file
3
src/test/resources/node-client-configuration.yml
Normal file
@ -0,0 +1,3 @@
|
||||
#enabled scripts - this require groovy
|
||||
script.inline: on
|
||||
script.indexed: on
|
Loading…
x
Reference in New Issue
Block a user