Skip to content

Commit e2d5fb3

Browse files
committed
Merge pull request #1 from mikedangelo/2.1-SNAPSHOT
updating to elasticsearch 2.1.1 and play framework 2.4.6
2 parents 74888c0 + 19181bc commit e2d5fb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+790
-366
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Module | Playframework | Elasticsearch | Comments | Diff
2828
0.8.2 | 2.2.1 | 0.90.12 | Small fixes
2929
1.1.0 | 2.2.1 | 1.1.0 | Upgrading the ES 1.1.0 (#47)
3030
1.4-SNAPSHOT | 2.2.1 | 1.4.1 | Upgrading the ES 1.4.1 (#59)
31+
2.1-SNAPSHOT | 2.4.6 | 2.1.1 | Upgrading the ES 2.1.1
3132

3233

3334
## Install
@@ -63,14 +64,23 @@ play.Project.playScalaSettings
6364
// resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
6465
```
6566

66-
## Activate the plugin
67+
## Activate the Plugin/Module
68+
69+
### Plugins were prior to 2.1-SNAPSHOT:
6770

6871
The Play2-elasticsearch module requires its plugin class to be declared in the conf/play.plugins file. If this file doesn't exist (it's not created by default when you create a new project),
6972
just create it in the conf directory first, and then add
7073
```
7174
9000:com.github.cleverage.elasticsearch.plugin.IndexPlugin
7275
```
7376

77+
### Modules were introduced in the 2.1-SNAPSHOT:
78+
79+
The Play2-elasticsearch module requires a Module class to be enabled inside of conf/application.conf
80+
```
81+
play.modules.enabled += "com.github.cleverage.elasticsearch.module.IndexModule"
82+
```
83+
7484
## Configuration
7585
You can configure the module in conf/application.conf (or in any configuration file included in your application.conf)
7686

module/app/com/github/cleverage/elasticsearch/AsyncUtils.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.github.cleverage.elasticsearch
2-
3-
import org.elasticsearch.client.Client
4-
5-
import concurrent.{Future, Promise}
6-
import org.elasticsearch.action.{ActionResponse, ActionRequest, ActionListener, ActionRequestBuilder}
2+
import org.elasticsearch.action.{ActionListener, ActionRequest, ActionRequestBuilder, ActionResponse}
73
import play.libs.F
84

5+
import scala.concurrent.{Future, Promise}
6+
97
/**
108
* Utils for managing Asynchronous tasks
119
*/
@@ -21,7 +19,7 @@ object AsyncUtils {
2119
* @param requestBuilder
2220
* @return
2321
*/
24-
def executeAsync[RQ <: ActionRequest[RQ],RS <: ActionResponse, RB <: ActionRequestBuilder[RQ,RS,RB,CL], CL <: Client](requestBuilder: ActionRequestBuilder[RQ,RS,RB,CL]): Future[RS] = {
22+
def executeAsync[RQ <: ActionRequest[RQ],RS <: ActionResponse, RB <: ActionRequestBuilder[RQ,RS,RB]](requestBuilder: ActionRequestBuilder[RQ,RS,RB]): Future[RS] = {
2523
val promise = Promise[RS]()
2624

2725
requestBuilder.execute(new ActionListener[RS] {
@@ -42,7 +40,7 @@ object AsyncUtils {
4240
* @param requestBuilder
4341
* @return
4442
*/
45-
def executeAsyncJava[RQ <: ActionRequest[RQ],RS <: ActionResponse, RB <: ActionRequestBuilder[RQ,RS,RB,CL], CL <: Client](requestBuilder: ActionRequestBuilder[RQ,RS,RB,CL]): F.Promise[RS] = {
43+
def executeAsyncJava[RQ <: ActionRequest[RQ],RS <: ActionResponse, RB <: ActionRequestBuilder[RQ,RS,RB]](requestBuilder: ActionRequestBuilder[RQ,RS,RB]): F.Promise[RS] = {
4644
F.Promise.wrap(executeAsync(requestBuilder))
4745
}
4846

module/app/com/github/cleverage/elasticsearch/Index.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.elasticsearch.action.delete.DeleteResponse;
55
import org.elasticsearch.action.index.IndexResponse;
66
import org.elasticsearch.action.update.UpdateResponse;
7-
import org.elasticsearch.index.query.FilterBuilder;
7+
import org.elasticsearch.index.query.QueryBuilder;
88
import org.elasticsearch.script.ScriptService;
99
import org.elasticsearch.search.SearchHit;
1010

@@ -95,20 +95,20 @@ public F.Promise<IndexResponse> indexAsync(String indexName) {
9595
return IndexService.indexAsync(getIndexPath(indexName), id, this);
9696
}
9797

98-
public UpdateResponse update(Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType){
99-
return IndexService.update(getIndexPath(), id, updateFieldValues, updateScript, scriptType);
98+
public UpdateResponse update(Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType, String lang){
99+
return IndexService.update(getIndexPath(), id, updateFieldValues, updateScript, scriptType, lang);
100100
}
101101

102-
public UpdateResponse update(String indexName, Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType){
103-
return IndexService.update(getIndexPath(indexName), id, updateFieldValues, updateScript, scriptType);
102+
public UpdateResponse update(String indexName, Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType, String lang){
103+
return IndexService.update(getIndexPath(indexName), id, updateFieldValues, updateScript, scriptType, lang);
104104
}
105105

106-
public F.Promise<UpdateResponse> updateAsync(Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType){
107-
return IndexService.updateAsync(getIndexPath(), id, updateFieldValues, updateScript, scriptType);
106+
public F.Promise<UpdateResponse> updateAsync(Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType, String lang){
107+
return IndexService.updateAsync(getIndexPath(), id, updateFieldValues, updateScript, scriptType, lang);
108108
}
109109

110-
public F.Promise<UpdateResponse> updateAsync(String indexName, Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType){
111-
return IndexService.updateAsync(getIndexPath(indexName), id, updateFieldValues, updateScript, scriptType);
110+
public F.Promise<UpdateResponse> updateAsync(String indexName, Map<String,Object> updateFieldValues , String updateScript, ScriptService.ScriptType scriptType, String lang){
111+
return IndexService.updateAsync(getIndexPath(indexName), id, updateFieldValues, updateScript, scriptType, lang);
112112
}
113113

114114
/**
@@ -224,7 +224,7 @@ public F.Promise<IndexResults<T>> searchAsync(IndexQuery<T> query) {
224224
return IndexService.searchAsync(queryPath, query, null);
225225
}
226226

227-
public F.Promise<IndexResults<T>> searchAsync(IndexQuery<T> query, FilterBuilder filter){
227+
public F.Promise<IndexResults<T>> searchAsync(IndexQuery<T> query, QueryBuilder filter){
228228
return IndexService.searchAsync(queryPath, query, filter);
229229
}
230230
}

module/app/com/github/cleverage/elasticsearch/IndexClient.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.github.cleverage.elasticsearch;
22

33
import org.elasticsearch.client.transport.TransportClient;
4-
import org.elasticsearch.common.settings.ImmutableSettings;
4+
import org.elasticsearch.common.settings.Settings;
55
import org.elasticsearch.common.settings.SettingsException;
66
import org.elasticsearch.common.transport.InetSocketTransportAddress;
77
import org.elasticsearch.node.NodeBuilder;
8-
import play.Application;
98
import play.Logger;
9+
import play.api.Configuration;
10+
import play.api.Environment;
11+
12+
import java.net.InetAddress;
13+
import java.nio.file.Paths;
1014

1115
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
1216

@@ -18,15 +22,15 @@ public class IndexClient {
1822

1923
public static IndexConfig config;
2024

21-
public IndexClient(Application application) {
25+
public IndexClient(Environment environment, Configuration configuration) {
2226
// ElasticSearch config load from application.conf
23-
this.config = new IndexConfig(application);
27+
this.config = new IndexConfig(environment, configuration);
2428
}
2529

2630
public void start() throws Exception {
2731

2832
// Load Elasticsearch Settings
29-
ImmutableSettings.Builder settings = loadSettings();
33+
Settings.Builder settings = loadSettings();
3034

3135
// Check Model
3236
if (this.isLocalMode()) {
@@ -38,7 +42,7 @@ public void start() throws Exception {
3842
Logger.info("ElasticSearch : Started in Local Mode");
3943
} else {
4044
Logger.info("ElasticSearch : Starting in Client Mode");
41-
TransportClient c = new TransportClient(settings);
45+
TransportClient c = TransportClient.builder().settings(settings).build();
4246
if (config.client == null) {
4347
throw new Exception("Configuration required - elasticsearch.client when local model is disabled!");
4448
}
@@ -51,7 +55,7 @@ public void start() throws Exception {
5155
throw new Exception("Invalid Host: " + host);
5256
}
5357
Logger.info("ElasticSearch : Client - Host: " + parts[0] + " Port: " + parts[1]);
54-
c.addTransportAddress(new InetSocketTransportAddress(parts[0], Integer.valueOf(parts[1])));
58+
c.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(parts[0]), Integer.valueOf(parts[1])));
5559
done = true;
5660
}
5761
if (!done) {
@@ -94,21 +98,21 @@ private boolean isLocalMode() {
9498
* @return
9599
* @throws Exception
96100
*/
97-
private ImmutableSettings.Builder loadSettings() throws Exception {
98-
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
101+
private Settings.Builder loadSettings() throws Exception {
102+
Settings.Builder settings = Settings.settingsBuilder();
99103

100104
// set default settings
101105
settings.put("client.transport.sniff", config.sniffing);
102106

103-
if (config.clusterName != null) {
107+
if (config.clusterName != null && !config.clusterName.isEmpty()) {
104108
settings.put("cluster.name", config.clusterName);
105109
}
106110

107111
// load settings
108-
if (config.localConfig != null) {
112+
if (config.localConfig != null && !config.localConfig.isEmpty()) {
109113
Logger.debug("Elasticsearch : Load settings from " + config.localConfig);
110114
try {
111-
settings.loadFromClasspath(config.localConfig);
115+
settings.loadFromPath(Paths.get(this.getClass().getClassLoader().getResource(config.localConfig).toURI()));
112116
} catch (SettingsException settingsException) {
113117
Logger.error("Elasticsearch : Error when loading settings from " + config.localConfig);
114118
throw new Exception(settingsException);

module/app/com/github/cleverage/elasticsearch/IndexConfig.java

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
import com.github.cleverage.elasticsearch.annotations.IndexMapping;
44
import com.github.cleverage.elasticsearch.annotations.IndexName;
55
import com.github.cleverage.elasticsearch.annotations.IndexType;
6+
import com.typesafe.config.ConfigValue;
67
import org.apache.commons.lang3.StringUtils;
78
import org.reflections.Reflections;
8-
import play.Application;
9-
import play.Configuration;
109
import play.Logger;
10+
import play.api.Configuration;
11+
import play.api.Environment;
1112
import play.libs.Json;
12-
import play.libs.ReflectionsCache;
13+
import play.libs.MyReflectionsCache;
14+
import scala.Option;
15+
import scala.Tuple2;
16+
import scala.collection.Iterator;
1317

14-
import java.util.*;
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.HashSet;
21+
import java.util.LinkedList;
22+
import java.util.Map;
23+
import java.util.Set;
24+
25+
;
1526

1627

1728
/**
@@ -94,24 +105,34 @@ public class IndexConfig {
94105
public boolean routingReqd = false;
95106

96107
/**
97-
* Play application
108+
* Play configuration
109+
*/
110+
public Configuration configuration;
111+
112+
/**
113+
* Play environment
98114
*/
99-
public Application application;
115+
public Environment environment;
116+
117+
private static final Option<scala.collection.immutable.Set<String>> empty = Option.apply(new scala.collection.immutable.HashSet<>());
118+
119+
public IndexConfig(Environment environment, Configuration configuration) {
120+
121+
this.environment = environment;
122+
this.configuration = configuration;
100123

101-
public IndexConfig(Application app) {
102-
this.application = app;
103-
this.client = app.configuration().getString("elasticsearch.client");
104-
this.sniffing = app.configuration().getBoolean("elasticsearch.sniff", true);
105-
this.local = app.configuration().getBoolean("elasticsearch.local");
106-
this.localConfig = app.configuration().getString("elasticsearch.config.resource");
107-
this.clusterName = app.configuration().getString("elasticsearch.cluster.name");
124+
this.client = (configuration.getString("elasticsearch.client", empty) == Option.apply((String)null)) ? "" : configuration.getString("elasticsearch.client", empty).get();
125+
this.clusterName = (configuration.getString("elasticsearch.cluster.name", empty) == Option.apply((String)null)) ? "" : configuration.getString("elasticsearch.cluster.name", empty).get();
126+
this.indexClazzs = (configuration.getString("elasticsearch.index.clazzs", empty) == Option.apply((String)null)) ? "" : configuration.getString("elasticsearch.index.clazzs", empty).get();
127+
String indexNameConf = (configuration.getString("elasticsearch.index.name", empty) == Option.apply((String)null)) ? "" : configuration.getString("elasticsearch.index.name", empty).get();
108128

129+
this.sniffing = (configuration.getBoolean("elasticsearch.sniff") == Option.apply(null)) ? false : (Boolean)configuration.getBoolean("elasticsearch.sniff").get();
130+
this.local = (configuration.getBoolean("elasticsearch.local") == Option.apply(null)) ? false : (Boolean)configuration.getBoolean("elasticsearch.local").get();
131+
this.localConfig = (configuration.getString("elasticsearch.config.resource", empty) == Option.apply((String)null)) ? "" : configuration.getString("elasticsearch.config.resource", empty).get();
109132

110-
this.showRequest = app.configuration().getBoolean("elasticsearch.index.show_request", false);
111-
this.dropOnShutdown = app.configuration().getBoolean("elasticsearch.index.dropOnShutdown", false);
112-
this.indexClazzs = app.configuration().getString("elasticsearch.index.clazzs");
133+
this.showRequest = (configuration.getBoolean("elasticsearch.index.show_request") == Option.apply(null)) ? false : (Boolean)configuration.getBoolean("elasticsearch.index.show_request").get();
134+
this.dropOnShutdown = (configuration.getBoolean("elasticsearch.index.dropOnShutdown") == Option.apply(null)) ? false : (Boolean)configuration.getBoolean("elasticsearch.index.dropOnShutdown").get();
113135

114-
String indexNameConf = app.configuration().getString("elasticsearch.index.name");
115136
if(indexNameConf != null) {
116137
LinkedList<String> indexNamesL = new LinkedList<String>();
117138
String[] indexNamesTab = indexNameConf.split(",");
@@ -136,7 +157,7 @@ public IndexConfig(Application app) {
136157
}
137158

138159
private void loadSettingsFromConfig(String indexName) {
139-
String setting = application.configuration().getString("elasticsearch." + indexName + ".settings");
160+
String setting = (configuration.getString("elasticsearch." + indexName + ".settings", empty) == Option.apply((String)null)) ? "" : configuration.getString("elasticsearch." + indexName + ".settings", empty).get();
140161
if(StringUtils.isNotEmpty(setting)) {
141162
indexSettings.put(indexName, setting);
142163
}
@@ -155,7 +176,7 @@ public void loadFromAnnotations() {
155176
// Loading class and annotation for set mapping if is present
156177
Logger.debug("ElasticSearch : Registering class " + aClass);
157178

158-
klass = Class.forName(aClass, true, application.classloader());
179+
klass = Class.forName(aClass, true, configuration.getClass().getClassLoader());
159180
Object o = klass.newInstance();
160181

161182
String indexType = getIndexType(o);
@@ -167,6 +188,7 @@ public void loadFromAnnotations() {
167188
indexMappings.put(path, indexMapping);
168189
}
169190
} catch (Throwable e) {
191+
e.printStackTrace();
170192
Logger.error(e.getMessage());
171193
}
172194
}
@@ -177,16 +199,22 @@ public void loadFromAnnotations() {
177199
* @param indexName
178200
*/
179201
private void loadMappingFromConfig(String indexName) {
180-
Configuration mappingConfig = application.configuration().getConfig("elasticsearch." + indexName + ".mappings");
202+
Configuration mappingConfig = (configuration.getConfig("elasticsearch." + indexName + ".mappings") == Option.apply((Configuration)null)) ? null : configuration.getConfig("elasticsearch." + indexName + ".mappings").get();
203+
181204
if (mappingConfig != null) {
182-
Map<String, Object> mappings = mappingConfig.asMap();
183-
for (String indexType : mappings.keySet()) {
205+
206+
Iterator<Tuple2<String, ConfigValue>> iter = mappingConfig.entrySet().iterator();
207+
208+
while (iter.hasNext()) {
209+
Tuple2<String, ConfigValue> mapping = iter.next();
210+
String indexType = mapping._1();
184211
IndexQueryPath indexQueryPath = new IndexQueryPath(indexName, indexType);
185-
if (mappings.get(indexType) instanceof String) {
186-
indexMappings.put(indexQueryPath, (String) mappings.get(indexType));
212+
213+
if (mapping._2().unwrapped() instanceof String) {
214+
indexMappings.put(indexQueryPath, (String) mapping._2().unwrapped());
187215
} else {
188216
try {
189-
indexMappings.put(indexQueryPath, Json.toJson(mappings.get(indexType)).toString());
217+
indexMappings.put(indexQueryPath, Json.toJson(mapping._2().unwrapped()).toString());
190218
} catch (Exception e) {
191219
Logger.warn("Incorrect value in elasticsearch.index.mappings", e);
192220
}
@@ -231,11 +259,14 @@ private Set<String> getClazzs() {
231259
for (String load : toLoad) {
232260
load = load.trim();
233261
if (load.endsWith(".*")) {
234-
Reflections reflections = ReflectionsCache.getReflections(application.classloader(), load.substring(0, load.length() - 2));
235-
for(Class c :reflections.getTypesAnnotatedWith(IndexName.class)){
262+
263+
//TODO: remove the "play.libs.{MyClasspath,MyReflectionsCache}" local patch when we upgrade to play 2.5.0
264+
Reflections reflections = MyReflectionsCache.getReflections(environment.classLoader(), load.substring(0, load.length() - 2));
265+
266+
for(Class c : reflections.getTypesAnnotatedWith(IndexName.class)){
236267
classes.add(c.getName());
237268
}
238-
for(Class c :reflections.getTypesAnnotatedWith(IndexType.class)){
269+
for(Class c : reflections.getTypesAnnotatedWith(IndexType.class)){
239270
classes.add(c.getName());
240271
}
241272
} else {

0 commit comments

Comments
 (0)