Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 1fd40e1

Browse files
committed
elastic search module fix jooby-project#90
```java { use(new ElasticSearch("/search")); } ``` Elastic search will listen under the ```/search``` path. Here are some examples: Creating a index: ```bash curl -XPUT 'localhost:8080/search/customer?pretty' ``` Indexing a doc: ```bash curl -XPUT 'localhost:8080/search/customer/external/1?pretty' -d ' { "name": "John Doe" }' ``` Getting a doc: ```bash curl 'localhost:8080/search/customer/external/1?pretty' ``` The module exposes a ```Client``` and ```Node``` instances, so it is possible to manage the index inside programmatically. ```java post("/:id", req -> { // index a document Client client = req.require(Client.class); Map<String, Object>; json = new HashMap<>(); json.put("user", "kimchy"); json.put("message", "trying out Elasticsearch"); return client.prepareIndex("twitter", "tweet", req.param("id").value()) .setSource(json) .execute() .actionGet(); }); get("/:id", req -> { // get a doc Client client = req.require(Client.class); return client.prepareGet("twitter", "tweet", req.param("id").value()) .execute() .actionGet() .getSource(); }); delete("/:id", req -> { // delete a doc Client client = req.require(Client.class); return client.prepareDelete("twitter", "tweet", req.param("id").value()) .execute() .actionGet(); }); ``` If it possible to setup or configure [Elastic Search](https://github.com/elastic/elasticsearch) via ```application.conf```, just make sure to prefix the property with ```elasticsearch```: ```properties elasticsearch.http.jsonp.enable = true ``` or programmatically: ```java { use(new ElasticSearch().doWith(settings -> { settings.put(..., ...); }); } ``` HTTP is disabled and isn't possible to change this value. What does it mean? [Jooby](http://jooby.org) setup a custom handler which makes it possible to use a [Jooby](http://jooby.org) server to serve Elastic Search requests and avoid the need of starting up another server running in a different port. Most of the ```http.*``` properties has no sense in Jooby. Path data is set to a temporary directory: ```${application.tmpdir}/es/data```. It is useful for development, but if you need want to make sure the index is persistent between server restarts, please make sure to setup this path to something else. Node name is set to: ```application.name```. Cluster name is set to: ```${application.name}-cluster```. That's all folks! Enjoy it!!! ```properties elasticsearch { cluster.name: ${application.name}-cluster node.name: ${application.name} node.local: true path.data: ${application.tmpdir}/es/data path.work: ${application.tmpdir}/es/work network.host: ${application.host} http.detailed_errors.enabled: true } ```
1 parent 6105ce7 commit 1fd40e1

File tree

32 files changed

+2820
-41
lines changed

32 files changed

+2820
-41
lines changed

coverage-report/data/elasticsearch/nodes/0/node.lock

Whitespace-only changes.

coverage-report/data/elasticsearch/nodes/1/node.lock

Whitespace-only changes.

coverage-report/data/elasticsearch/nodes/2/node.lock

Whitespace-only changes.

coverage-report/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<source>${project.parent.basedir}/jooby-jedis/src/main/java</source>
5050
<source>${project.parent.basedir}/jooby-mongodb/src/main/java</source>
5151
<source>${project.parent.basedir}/jooby-morphia/src/main/java</source>
52+
<source>${project.parent.basedir}/jooby-elasticsearch/src/main/java</source>
5253
</sources>
5354
</configuration>
5455
</execution>
@@ -76,6 +77,7 @@
7677
<source>${project.parent.basedir}/jooby-jedis/src/test/java</source>
7778
<source>${project.parent.basedir}/jooby-mongodb/src/test/java</source>
7879
<source>${project.parent.basedir}/jooby-morphia/src/test/java</source>
80+
<source>${project.parent.basedir}/jooby-elasticsearch/src/test/java</source>
7981
</sources>
8082
</configuration>
8183
</execution>
@@ -210,6 +212,12 @@
210212
<version>${project.version}</version>
211213
</dependency>
212214

215+
<dependency>
216+
<groupId>org.jooby</groupId>
217+
<artifactId>jooby-elasticsearch</artifactId>
218+
<version>${project.version}</version>
219+
</dependency>
220+
213221
<!-- H2 database -->
214222
<dependency>
215223
<groupId>com.h2database</groupId>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.jooby.elasticsearch;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.elasticsearch.client.Client;
7+
import org.jooby.elasticsearch.ElasticSearch;
8+
import org.jooby.json.Jackson;
9+
import org.jooby.test.ServerFeature;
10+
import org.junit.Test;
11+
12+
public class ElasticSearchClientAPIFeature extends ServerFeature {
13+
14+
{
15+
16+
use(new Jackson());
17+
18+
use(new ElasticSearch());
19+
20+
post("/:id", req -> {
21+
Client client = req.require(Client.class);
22+
Map<String, Object> json = new HashMap<>();
23+
json.put("user", "kimchy");
24+
json.put("message", "trying out Elasticsearch");
25+
return client.prepareIndex("twitter", "tweet", req.param("id").value())
26+
.setSource(json)
27+
.execute()
28+
.actionGet();
29+
});
30+
31+
get("/:id", req -> {
32+
Client client = req.require(Client.class);
33+
return client.prepareGet("twitter", "tweet", req.param("id").value())
34+
.execute()
35+
.actionGet().getSource();
36+
});
37+
38+
delete("/:id", req -> {
39+
Client client = req.require(Client.class);
40+
return client.prepareDelete("twitter", "tweet", req.param("id").value())
41+
.execute()
42+
.actionGet();
43+
});
44+
}
45+
46+
@Test
47+
public void es() throws Exception {
48+
request()
49+
.post("/1")
50+
.expect(
51+
"{\"context\":{\"empty\":true},\"headers\":[],\"index\":\"twitter\",\"id\":\"1\",\"type\":\"tweet\",\"version\":1,\"created\":true,\"contextEmpty\":true}");
52+
53+
request()
54+
.get("/1")
55+
.expect("{\"message\":\"trying out Elasticsearch\",\"user\":\"kimchy\"}");
56+
57+
request()
58+
.delete("/1")
59+
.expect(
60+
"{\"context\":{\"empty\":true},\"headers\":[],\"index\":\"twitter\",\"id\":\"1\",\"type\":\"tweet\",\"version\":2,\"found\":true,\"contextEmpty\":true}");
61+
}
62+
63+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.jooby.elasticsearch;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class ElasticSearchFeature extends ServerFeature {
7+
8+
{
9+
10+
use(new ElasticSearch("/es"));
11+
12+
}
13+
14+
@Test
15+
public void es() throws Exception {
16+
// either 200 or 400
17+
request()
18+
.delete("/es/customer")
19+
.expect(rsp -> {
20+
});
21+
22+
request()
23+
.put("/es/customer")
24+
.expect(200);
25+
26+
request()
27+
.put("/es/customer/external/1")
28+
.body("{\"name\": \"John Doe\"}", "application/json")
29+
.expect(201);
30+
31+
request()
32+
.get("/es/customer/external/1")
33+
.expect(
34+
"{\"_index\":\"customer\",\"_type\":\"external\",\"_id\":\"1\",\"_version\":1,\"found\":true,\"_source\":{\"name\": \"John Doe\"}}");
35+
}
36+
37+
}

0 commit comments

Comments
 (0)