und die Java-Welt
Florian Hopf
@fhopf
Elasticsearch?
„Elasticsearch is a distributed,
open source search and
analytics engine, designed for
horizontal scalabili...
Elasticsearch?
„Elasticsearch is a distributed,
open source search and
analytics engine, designed for
horizontal scalabili...
Elasticsearch?
Installation
# download archive
wget https://download.elastic.co/elasticsearch
/elasticsearch/elasticsearch-1.7.2.zip
# zi...
Zugriff per HTTP
curl -XGET "http://localhost:9200"
{
"status": 200,
"name": "Ultron",
"cluster_name": "elasticsearch",
"v...
Indizierung
curl -XPOST "http://localhost:9200/library/book" -d'
{
"title": "Elasticsearch in Action",
"author": [ "Radu G...
Suche
curl -XPOST "http://localhost:9200/library/book/_search
?q=elasticsearch"
{
[...]
"hits": {
"hits": [
{
"_index": "l...
Suche per Query DSL
curl -XPOST "http://localhost:9200/library/book/_search"
-d'
{
"query": {
"filtered": {
"query": {
"ma...
Recap
●
Auf Java basierender Suchserver
●
HTTP und JSON
●
Suche und Filterung über Query-DSL
●
Facettierung über Aggregati...
Elasticsearch?
„Elasticsearch is a distributed,
open source search and
analytics engine, designed for
horizontal scalabili...
Verteilung
Verteilung
Verteilung
Suche im Cluster
Suche im Cluster
Recap
●
Knoten können Cluster bilden
●
Datenverteilung durch Sharding
●
Replicas zur Lastverteilung und
Ausfallsicherheit
...
Standard-Client
Java!
dependencies {
compile group: 'org.elasticsearch',
name: 'elasticsearch',
version: '1.7.2'
}
Java!
TransportAddress address =
new InetSocketTransportAddress("localhost", 9300);
Client client = new TransportClient()....
Suche per Query DSL
curl -XPOST "http://localhost:9200/library/book/_search"
-d'
{
"query": {
"filtered": {
"query": {
"ma...
Suche über Java-Client
SearchResponse searchResponse = client
.prepareSearch("library")
.setQuery(filteredQuery(
matchQuer...
Indizierung über Java-Client
XContentBuilder jsonBuilder = jsonBuilder()
.startObject()
.field("title", "Elasticsearch")
....
Indizierung über Java-Client
Transport-Client
●
Verbindet sich mit bestehendem Cluster
TransportAddress address =
new InetSocketTransportAddress("local...
Transport-Client
Node-Client
Node node = nodeBuilder().client(true).node();
Client client = node.client();
Node-Client
●
Startet eigenen Knoten
●
Anwendung wird Teil des Clusters
Node node = nodeBuilder().client(true).node();
Cli...
Node-Client
Standard-Client
●
Volle API-Unterstützung
●
Effiziente Kommunikation
●
Node-Client
●
Cluster-State spart unnötige Hops
Standard-Client Gotchas
●
Elasticsearch-Version Client == Server
●
Node-Client
●
Bei Start und Stop wird Cluster-State übe...
Jest – Http Client
Jest
Jest
dependencies {
compile group: 'io.searchbox',
name: 'jest',
Version: '0.1.6'
}
Client
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("htt...
Suche mit Jest
String query = jsonStringThatMagicallyAppears;
Search search = new Search.Builder(query)
.addIndex("library...
Suche mit Jest
JsonObject jsonObject = result.getJsonObject();
JsonObject hitsObj = jsonObject.getAsJsonObject("hits");
Js...
Suche mit Jest
public class Book {
private String title;
private List<String> author;
private Publisher publisher;
public ...
Suche mit Jest
Book book = result.getFirstHit(Book.class).source;
assertEquals("Elasticsearch in Action", book.getTitle());
Jest
●
Alternative HTTP-Implementierung
●
Queries als String oder per Elasticsearch-
Builder
●
Indizieren und Suchen per J...
Spring Data Elasticsearch
Spring Data
●
Abstraktionen für Data-Stores
●
Spezialitäten bleiben erhalten
●
Dynamische Repository-Implementierung
●
Pop...
Dependency
dependencies {
compile group: 'org.springframework.data',
name: 'spring-data-elasticsearch',
version: '1.2.0.RE...
Entity
@Document(
indexName = "library",
type = "book")
public class Book {
private String id;
private String title;
priva...
Repository
public interface BookRepository
extends ElasticsearchCrudRepository<Book, String> {
public Iterable<Book> findB...
Configuration
<elasticsearch:node-client id="client" />
<bean name="elasticsearchTemplate"
class="o.s.d.elasticsearch.core...
Indexing
Book book = new Book();
book.setTitle("Elasticsearch");
book.setAuthor(Arrays.asList("Florian Hopf"));
book.setPu...
Searching
Iterable<Book> books = repository.findAll();
books = repository.findByTitle("Elasticsearch");
Recap
●
Abstraktion über Elasticsearch
●
Entity-Beans
●
Dynamische Repository-Implementierung
●
Junges Projekt
Recap
●
Standard-Client
●
Node- oder Transport-Client
●
Jest
●
Http-Client mit Java-Bean-Unterstützung
●
Spring-Data-Elast...
JVM?
●
Clojure: Elastisch
●
Standard-Client oder HTTP
●
Groovy: elasticsearch-groovy
●
Standard-Client, Closure-Unterstütz...
Links
●
https://www.found.no/foundation/java-
clients-for-elasticsearch/
●
http://elastic.co
●
https://github.com/searchbo...
Weitere Infos
Nächste SlideShare
Wird geladen in …5
×

Elasticsearch und die Java-Welt

1.172 Aufrufe

Veröffentlicht am

Talk at Java Forum Nord 06.10.2015 about integrating Elasticsearch into Java applications.

Veröffentlicht in: Software
0 Kommentare
2 Gefällt mir
Statistik
Notizen
  • Als Erste(r) kommentieren

Keine Downloads
Aufrufe
Aufrufe insgesamt
1.172
Auf SlideShare
0
Aus Einbettungen
0
Anzahl an Einbettungen
54
Aktionen
Geteilt
0
Downloads
9
Kommentare
0
Gefällt mir
2
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie

Elasticsearch und die Java-Welt

  1. 1. und die Java-Welt Florian Hopf @fhopf
  2. 2. Elasticsearch? „Elasticsearch is a distributed, open source search and analytics engine, designed for horizontal scalability, reliability, and easy management.“
  3. 3. Elasticsearch? „Elasticsearch is a distributed, open source search and analytics engine, designed for horizontal scalability, reliability, and easy management.“
  4. 4. Elasticsearch?
  5. 5. Installation # download archive wget https://download.elastic.co/elasticsearch /elasticsearch/elasticsearch-1.7.2.zip # zip is for windows and linux unzip elasticsearch-1.7.2.zip # on windows: elasticsearch.bat elasticsearch-1.7.2/bin/elasticsearch
  6. 6. Zugriff per HTTP curl -XGET "http://localhost:9200" { "status": 200, "name": "Ultron", "cluster_name": "elasticsearch", "version": { "number" : "1.7.2", "build_hash" : "e43676b1385b8125...", "build_timestamp" : "2015-09-14T09:49:53Z", "build_snapshot" : false, "lucene_version" : "4.10.4" }, "tagline": "You Know, for Search" }
  7. 7. Indizierung curl -XPOST "http://localhost:9200/library/book" -d' { "title": "Elasticsearch in Action", "author": [ "Radu Gheorghe", "Matthew Lee Hinman", "Roy Russo" ], "published": "2015-06-30T00:00:00.000Z", "publisher": { "name": "Manning", "country": "USA" } }'
  8. 8. Suche curl -XPOST "http://localhost:9200/library/book/_search ?q=elasticsearch" { [...] "hits": { "hits": [ { "_index": "library", "_type": "book", "_source": { "title": "Elasticsearch in Action", [...]
  9. 9. Suche per Query DSL curl -XPOST "http://localhost:9200/library/book/_search" -d' { "query": { "filtered": { "query": { "match": { "title": "elasticsearch" } }, "filter": { "term": { "publisher.name": "manning" } } } } }'
  10. 10. Recap ● Auf Java basierender Suchserver ● HTTP und JSON ● Suche und Filterung über Query-DSL ● Facettierung über Aggregations ● Highlighting, Suggestions, ...
  11. 11. Elasticsearch? „Elasticsearch is a distributed, open source search and analytics engine, designed for horizontal scalability, reliability, and easy management.“
  12. 12. Verteilung
  13. 13. Verteilung
  14. 14. Verteilung
  15. 15. Suche im Cluster
  16. 16. Suche im Cluster
  17. 17. Recap ● Knoten können Cluster bilden ● Datenverteilung durch Sharding ● Replicas zur Lastverteilung und Ausfallsicherheit ● Verteilte Suche ● Cluster-Zustand auf jedem Knoten
  18. 18. Standard-Client
  19. 19. Java! dependencies { compile group: 'org.elasticsearch', name: 'elasticsearch', version: '1.7.2' }
  20. 20. Java! TransportAddress address = new InetSocketTransportAddress("localhost", 9300); Client client = new TransportClient(). addTransportAddress(address);
  21. 21. Suche per Query DSL curl -XPOST "http://localhost:9200/library/book/_search" -d' { "query": { "filtered": { "query": { "match": { "title": "elasticsearch" } }, "filter": { "term": { "publisher.name": "manning" } } } } }'
  22. 22. Suche über Java-Client SearchResponse searchResponse = client .prepareSearch("library") .setQuery(filteredQuery( matchQuery("title", "elasticsearch"), termFilter("publisher.name", "manning"))) .execute().actionGet(); assertEquals(1, searchResponse.getHits().getTotalHits()); SearchHit hit = searchResponse.getHits().getAt(0); assertEquals("Elasticsearch in Action", hit.getSource().get("title"));
  23. 23. Indizierung über Java-Client XContentBuilder jsonBuilder = jsonBuilder() .startObject() .field("title", "Elasticsearch") .field("author", "Florian Hopf") .startObject("publisher") .field("name", "dpunkt") .endObject() .endObject(); client.prepareIndex("library", "book") .setSource(jsonBuilder) .execute().actionGet();
  24. 24. Indizierung über Java-Client
  25. 25. Transport-Client ● Verbindet sich mit bestehendem Cluster TransportAddress address = new InetSocketTransportAddress("localhost", 9300); Client client = new TransportClient(). addTransportAddress(address);
  26. 26. Transport-Client
  27. 27. Node-Client Node node = nodeBuilder().client(true).node(); Client client = node.client();
  28. 28. Node-Client ● Startet eigenen Knoten ● Anwendung wird Teil des Clusters Node node = nodeBuilder().client(true).node(); Client client = node.client();
  29. 29. Node-Client
  30. 30. Standard-Client ● Volle API-Unterstützung ● Effiziente Kommunikation ● Node-Client ● Cluster-State spart unnötige Hops
  31. 31. Standard-Client Gotchas ● Elasticsearch-Version Client == Server ● Node-Client ● Bei Start und Stop wird Cluster-State übertragen ● Speicherverbrauch ● Elasticsearch-Dependency
  32. 32. Jest – Http Client
  33. 33. Jest
  34. 34. Jest dependencies { compile group: 'io.searchbox', name: 'jest', Version: '0.1.6' }
  35. 35. Client JestClientFactory factory = new JestClientFactory(); factory.setHttpClientConfig(new HttpClientConfig .Builder("http://localhost:9200") .multiThreaded(true) .build()); JestClient client = factory.getObject();
  36. 36. Suche mit Jest String query = jsonStringThatMagicallyAppears; Search search = new Search.Builder(query) .addIndex("library") .build(); SearchResult result = client.execute(search); assertEquals(Integer.valueOf(1), result.getTotal());
  37. 37. Suche mit Jest JsonObject jsonObject = result.getJsonObject(); JsonObject hitsObj = jsonObject.getAsJsonObject("hits"); JsonArray hits = hitsObj.getAsJsonArray("hits"); JsonObject hit = hits.get(0).getAsJsonObject(); // ... more boring code
  38. 38. Suche mit Jest public class Book { private String title; private List<String> author; private Publisher publisher; public Publisher getPublisher() { return publisher; } public void setPublisher(Publisher publisher) { this.publisher = publisher; } // more code }
  39. 39. Suche mit Jest Book book = result.getFirstHit(Book.class).source; assertEquals("Elasticsearch in Action", book.getTitle());
  40. 40. Jest ● Alternative HTTP-Implementierung ● Queries als String oder per Elasticsearch- Builder ● Indizieren und Suchen per Java-Beans
  41. 41. Spring Data Elasticsearch
  42. 42. Spring Data ● Abstraktionen für Data-Stores ● Spezialitäten bleiben erhalten ● Dynamische Repository-Implementierung ● Populäre Implementierungen ● Spring Data JPA ● Spring Data MongoDB
  43. 43. Dependency dependencies { compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '1.2.0.RELEASE' }
  44. 44. Entity @Document( indexName = "library", type = "book") public class Book { private String id; private String title; private List<String> author; private Publisher publisher; // more code }
  45. 45. Repository public interface BookRepository extends ElasticsearchCrudRepository<Book, String> { public Iterable<Book> findByTitle(String title); }
  46. 46. Configuration <elasticsearch:node-client id="client" /> <bean name="elasticsearchTemplate" class="o.s.d.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"/> </bean> <elasticsearch:repositories base-package="de.fhopf.elasticsearch.springdata" />
  47. 47. Indexing Book book = new Book(); book.setTitle("Elasticsearch"); book.setAuthor(Arrays.asList("Florian Hopf")); book.setPublisher(new Publisher("dpunkt")); repository.save(book);
  48. 48. Searching Iterable<Book> books = repository.findAll(); books = repository.findByTitle("Elasticsearch");
  49. 49. Recap ● Abstraktion über Elasticsearch ● Entity-Beans ● Dynamische Repository-Implementierung ● Junges Projekt
  50. 50. Recap ● Standard-Client ● Node- oder Transport-Client ● Jest ● Http-Client mit Java-Bean-Unterstützung ● Spring-Data-Elasticsearch ● Annotationzentrierte Konfiguration, dynamische Repository-Implementierungen
  51. 51. JVM? ● Clojure: Elastisch ● Standard-Client oder HTTP ● Groovy: elasticsearch-groovy ● Standard-Client, Closure-Unterstützung ● Scala: große Auswahl, z.B. elastic4s ● Standard-Client, Type-Safety
  52. 52. Links ● https://www.found.no/foundation/java- clients-for-elasticsearch/ ● http://elastic.co ● https://github.com/searchbox-io/Jest ● https://github.com/spring-projects/spring- data-elasticsearch ● http://blog.florian-hopf.de
  53. 53. Weitere Infos

×