Jest is a Java HTTP Rest client for Elasticsearch.It is actively developed and tested by Searchly.
A sample Java application using Jest can be found on GitHub
https://github.com/searchly/searchly-java-sample.
Installation
Ensure you have added Sonatype repository to your pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<
repositories
>
.
.
<
repository
>
<
id
>
sonatype
<
/
id
>
<
name
>
Sonatype
Groups
<
/
name
>
<
url
>
https
:
//oss.sonatype.org/content/groups/public/</url>
<
/
repository
>
.
.
<
/
repositories
>
|
With Maven add Jest dependency to your pom.xml
1
2
3
4
5
|
<
dependency
>
<
groupId
>
io
.
searchbox
<
/
groupId
>
<
artifactId
>
jest
<
/
artifactId
>
<
version
>
0.1.2
<
/
version
>
<
/
dependency
>
|
Install Jest via Maven
1
|
$
mvn
clean
install
|
Configuration
Create a Jest Client (Change apiKey with your api key):
1
2
3
4
5
6
7
8
9
10
|
// Construct a new Jest client according to configuration via factory
String
connectionUrl
=
"http://site:your-key@your-end-point.searchly.com"
;
JestClientFactory
factory
=
new
JestClientFactory
(
)
;
factory
.
setHttpClientConfig
(
new
HttpClientConfig
.
Builder
(
connectionUrl
)
.
multiThreaded
(
true
)
.
build
(
)
)
;
JestClient
client
=
factory
.
getObject
(
)
;
|
Indexing
Create an index via Jest with ease;
1
|
client
.
execute
(
new
CreateIndex
.
Builder
(
"articles"
)
.
build
(
)
)
;
|
Create new document.
1
2
3
|
Article
source
=
new
Article
(
)
;
source
.
setAuthor
(
"John Ronald Reuel Tolkien"
)
;
source
.
setContent
(
"The Lord of the Rings is an epic high fantasy novel"
)
;
|
Index article to “articles” index with “article” type.
1
2
|
Index
index
=
new
Index
.
Builder
(
source
)
.
index
(
"articles"
)
.
type
(
"article"
)
.
build
(
)
;
client
.
execute
(
index
)
;
|
Searching
Search queries can be either JSON String or ElasticSearch SearchSourceBuilder object.
With Json string;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
String
query
=
"{\n"
+
" \"query\": {\n"
+
" \"filtered\" : {\n"
+
" \"query\" : {\n"
+
" \"query_string\" : {\n"
+
" \"query\" : \"Lord\"\n"
+
" }\n"
+
" }\n"
+
" }\n"
+
" }\n"
+
"}"
;
Search
search
=
(
Search
)
new
Search
.
Builder
(
query
)
// multiple index or types can be added.
.
addIndex
(
"articles"
)
.
addType
(
"article"
)
.
build
(
)
;
JestResult
result
=
client
.
execute
(
search
)
;
|
With SearchSourceBuilder; (Add ElasticSearch as dependency if you want to use SearchSourceBuilder).
1
2
3
4
5
6
7
8
9
10
|
SearchSourceBuilder
searchSourceBuilder
=
new
SearchSourceBuilder
(
)
;
searchSourceBuilder
.
query
(
QueryBuilders
.
queryString
(
"Lord"
)
)
;
Search
search
=
(
Search
)
new
Search
.
Builder
(
searchSourceBuilder
.
toString
(
)
)
// multiple index or types can be added.
.
addIndex
(
"articles"
)
.
addType
(
"article"
)
.
build
(
)
;
JestResult
result
=
client
.
execute
(
search
)
;
|
Useful Resources
- Check out documentation of Jest here.