Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elasticsearch

 Elasticsearch

An introductory talk about Elasticsearch I gave on MaceióDEVMeetup#5

Event: http://www.meetup.com/maceio-dev-meetup/events/220830067/

Tony Messias

March 27, 2015
Tweet

More Decks by Tony Messias

Other Decks in Programming

Transcript

  1. what is “search”? “A search is the organized pursuit of

    information (...) that you want to find, but you have no idea where it is.” (NASA).
  2. how do we add search to our apps? ➔ SQL

    queries (not just LIKE queries); ➔ Elasticsearch; ➔ Lucene; ➔ Solr; ➔ …
  3. how do we add search to our apps? ➔ SQL

    queries (not just LIKE queries); ➔ Elasticsearch; ➔ Lucene; ➔ Solr; ➔ …
  4. some analogy * Relational ES database index table type row

    document column field schema mapping index everything SQL QueryDSL
  5. { "email": "[email protected]", "first_name": "John", "last_name": "Smith", "info": { "bio":

    "Eco-warrior and defender of the weak", "age": 25, "interests": [ "dolphins", "whales" ] }, "join_date": "2014/05/01" }
  6. $ curl -XPOST localhost:9200/my-app/users -d ‘{ "email": "[email protected]", "first_name": "John",

    "last_name": "Smith", "info": { "bio": "Eco-warrior and defender of the weak", "age": 25, "interests": [ "dolphins", "whales" ] }, "join_date": "2014/05/01" }’
  7. $ curl -XGET localhost:9200/my-app/users/AUxOBeJbue-jeR3jNcHj { "_index": "my-app", "_type": "users", "_id":

    "AUxOJe-4ue-jeR3jNcHo", "_version": 2, "found": true, "_source": { "email": "[email protected]", "first_name": "John" } }
  8. { ..., "hits": { "total": 4, "max_score": 0.30685282, "hits": [

    { "_index": "my-app", "_type": "users", "_id": "AUxOJWlCue-jeR3jNcHn", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Doe", "info": { "bio": "Like rock music", "age": 35, "interests": [ "dolphins", "whales" ] }, "join_date": "2014/05/01" } },...
  9. $ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "filtered": {

    "query": { "match": { "first_name": "John" } }, "filter": { "range": { "info.age": { "gt": 34 } } } } } }’
  10. { "took": 6, "timed_out": false, "_shards": { "total": 5, "successful":

    5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.30685282, "hits": [ { "_index": "my-app", "_type": "users", "_id": "AUxOJWlCue-jeR3jNcHn", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Doe", "info": { "bio": "Like rock music", "age": 35, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } } ] } }
  11. { "took": 9, "timed_out": false, "_shards": {...}, "hits": { "total":

    2, "max_score": 0.2169777, "hits": [ { "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_score": 0.2169777, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Kennedy", "info": { "bio": "I love rock climbing!", "age": 29, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } }, { "_index": "my-app", "_type": "users", "_id": "AUxOJWlCue-jeR3jNcHn", "_score": 0.02250402, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Doe", "info": { "bio": "Like rock music", "age": 35, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } } ] } }
  12. {…, "hits": { "total": 1, "max_score": 0.30685282, "hits": [ {

    "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_score": 0.30685282, "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Kennedy", "info": { "bio": "I love rock climbing!", "age": 29, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" } } ] } }
  13. $ curl -XPOST localhost:9200/my-app/users/_search -d ‘{ "query": { "match_phrase": {

    "info.bio": "rock climbing" } }, "highlight": { "fields": { "info.bio": {} } } }’
  14. ... { "_index": "my-app", "_type": "users", "_id": "AUxOJe-4ue-jeR3jNcHo", "_score": 0.30685282,

    "_source": { "email": "[email protected]", "first_name": "John", "last_name": "Kennedy", "info": { "bio": "I love rock climbing!", "age": 29, "interests": ["dolphins","whales"] }, "join_date": "2014/05/01" }, "highlight": { "info.bio": ["I love <em>rock</em> <em>climbing</em>!"] } } ] } }
  15. { ..., "hits": {"total": 4,"max_score": 0,"hits": []}, "aggregations": { "all_interests":

    { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "whales", "doc_count": 4 }, { "key": "dolphins", "doc_count": 3 } ] } }}
  16. $ curl -XPOST localhost:9200/my-app/users/_search?search_type=count -d'{ "aggs": { "all_interests": { "terms":

    { "field": "info.interests" }, "aggs": { "avg_age": { "avg": { "field": "info.age" } } } } } }'
  17. { …, "aggregations": { "all_interests": { …, "buckets": [ {

    "key": "whales", "doc_count": 4, "avg_age": { "value": 31.5 } }, { "key": "dolphins", "doc_count": 3, "avg_age": { "value": 32.333333333333336 } } ] }}}
  18. $ curl -XPOST localhost:9200/my-app/users/_search?search_type=count -d'{ "query": { "match_phrase": { "info.bio":

    "rock climbing" } }, "aggs": { "all_interests": { "terms": { "field": "info.interests" }, "aggs": { "avg_age": { "avg": { "field": "info.age" } } } } }}'
  19. { …, "aggregations": { "all_interests": { …, "buckets": [ {

    "key": "whales", "doc_count": 1, "avg_age": { "value": 29 } } ] }}}