More Related Content Similar to Java clients for elasticsearch (20) More from Florian Hopf (12) Java clients for elasticsearch3. Elasticsearch?
„Elasticsearch is a distributed,
JSON-based search and
analytics engine, designed for
horizontal scalability,
maximum reliability, and easy
management.“
4. Elasticsearch?
„Elasticsearch is a distributed,
JSON-based search and
analytics engine, designed for
horizontal scalability,
maximum reliability, and easy
management.“
6. Installation
# download archive
wget https://artifacts.elastic.co/downloads/
elasticsearch/elasticsearch-5.0.0.zip
unzip elasticsearch-5.0.0.zip
# on windows: elasticsearch.bat
elasticsearch-5.0.0/bin/elasticsearch
7. Accessing Elaticsearch
curl -XGET "http://localhost:9200"
{
"name" : "LI8ZN-t",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "UvbMAoJ8TieUqugCGw7Xrw",
"version" : {
"number" : "5.0.0",
"build_hash" : "253032b",
"build_date" : "2016-10-26T04:37:51.531Z",
"build_snapshot" : false,
"lucene_version" : "6.2.0"
},
"tagline" : "You Know, for Search"
}
10. Search
curl -XGET
"http://localhost:9200/food/dish/_search?q=chicken"
...
{"total":2,"max_score":0.3666863,"hits":
[{"_index":"food","_type":"dish","_id":"AVg9cMwARrBlrY9
tYBqX","_score":0.3666863,"_source":
{
"food": "Hainanese Chicken Rice",
"tags": ["chicken", "rice"],
"favorite": {
"location": "Tian Tian",
"price": 5.00
}
}},
...
11. Search using Query DSL
curl -XPOST "http://localhost:9200/food/dish/_search" -d'
{
"query": {
"bool": {
"must": {
"match": {
"_all": "rice"
}
},
"filter": {
"term": {
"tags.keyword": "chicken"
}
}
}
}
}'
12. Elasticsearch?
„Elasticsearch is a distributed,
JSON-based search and
analytics engine, designed for
horizontal scalability,
maximum reliability, and
easy management.“
15. Recap
●
Java based search server
●
HTTP and JSON
●
Search and Filtering Query-DSL
●
Faceting, Highlighting, Suggestions, …
●
Nodes can form a cluster
19. Search using Query DSL
curl -XPOST "http://localhost:9200/food/dish/_search" -d'
{
"query": {
"bool": {
"must": {
"match": {
"_all": "rice"
}
},
"filter": {
"term": {
"tags.keyword": "chicken"
}
}
}
}
}'
20. Search using Search Builders
SearchResponse searchResponse = client
.prepareSearch("food")
.setQuery(
boolQuery().
must(matchQuery("_all", "rice")).
filter(
termQuery("tags.keyword", "chicken")))
.execute().actionGet();
assertEquals(1, searchResponse.getHits().getTotalHits());
SearchHit hit = searchResponse.getHits().getAt(0);
String food = hit.getSource().get("food").toString();
21. Indexing
XContentBuilder builder = jsonBuilder()
.startObject()
.field("food", "Roti Prata")
.array("tags", new String [] {"curry"})
.startObject("favorite")
.field("location", "Tiong Bahru")
.field("price", 2.00)
.endObject()
.endObject();
IndexResponse resp = client.prepareIndex("food","dish")
.setSource(builder)
.execute()
.actionGet();
26. Sniffing
●
State of the cluster can be requested from
elasticsearch
●
Client side loadbalancing
TransportAddress address =
new InetSocketTransportAddress(
InetAddress.getByName("localhost"), 9300);
Settings settings = Settings.builder()
.put("client.transport.sniff", true)
.build();
Client client = new PreBuiltTransportClient(settings)
addTransportAddress(address);
32. Elasticsearch 5 REST-Client
+--- org.apache.httpcomponents:httpclient:4.5.2
+--- org.apache.httpcomponents:httpcore:4.4.5
+--- org.apache.httpcomponents:httpasyncclient:4.1.2
+--- org.apache.httpcomponents:httpcore-nio:4.4.5
+--- commons-codec:commons-codec:1.10
--- commons-logging:commons-logging:1.1.3
34. Elasticsearch 5 REST-Client
HttpEntity entity = new NStringEntity(
"{ "query": { "match_all": {}}}",
ContentType.APPLICATION_JSON);
// alternative: performRequestAsync
Response response = restClient.performRequest("POST",
"/_search", emptyMap(), entity);
String json = toString(response.getEntity());
// ...
35. Elasticsearch 5 REST-Client
●
Less dependent on elasticsearch version
●
Clean separation network application/cluster
●
Minimal dependency
●
Sniffing
●
Error handling, timeout config, basic auth,
headers, …
●
No query support (for now)
38. Client
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost:9200")
.multiThreaded(true)
.build());
JestClient client = factory.getObject();
39. Searching Jest
String query = jsonStringThatMagicallyAppears;
Search search = new Search.Builder(query)
.addIndex("library")
.build();
SearchResult result = client.execute(search);
assertEquals(Integer.valueOf(1), result.getTotal());
40. Searching Jest
JsonObject jsonObject = result.getJsonObject();
JsonObject hitsObj = jsonObject.getAsJsonObject("hits");
JsonArray hits = hitsObj.getAsJsonArray("hits");
JsonObject hit = hits.get(0).getAsJsonObject();
// ... more boring code
41. Searching Jest
public class Dish {
private String food;
private List<String> tags;
private Favorite favorite;
@JestId
private String id;
// ... getters and setters
}
42. Suche mit Jest
Dish dish = result.getFirstHit(Dish.class).source;
assertEquals("Roti Prata", dish.getFood());
45. Spring Data
●
Abstractions for different data stores
●
Speciality of each store available
●
Dynamic Repository implementations
●
Popular modules
●
Spring Data JPA
●
Spring Data MongoDB
50. Indexing
Dish mie = new Dish();
mie.setId("hokkien-prawn-mie");
mie.setFood("Hokkien Prawn Mie");
mie.setTags(Arrays.asList("noodles", "prawn"));
repository.save(Arrays.asList(hokkienPrawnMie));
52. Repository
public interface DishRepository
extends ElasticsearchCrudRepository<Dish, String> {
List<Dish> findByFood(String food);
List<Dish> findByTagsAndFavoriteLocation(String tag,
String location);
List<Dish> findByFavoritePriceLessThan(Double price);
@Query("{"query": {"match_all": {}}}")
List<Dish> customFindAll();
}