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

Elasticsearch for PHP - Lone Star PHP 2012

Elasticsearch for PHP - Lone Star PHP 2012

Elasticsearch for PHP - Lone Star PHP in Dallas, Texas
June 29 & 30, 2012

Shaun Farrell

June 30, 2012
Tweet

More Decks by Shaun Farrell

Other Decks in Technology

Transcript

  1. What Is Elasticsearch? • Storage Engine • Schema Free •

    Document Oriented • Built on top of Lucene • Opensource • RESTFul (JSON over HTTP) • Multi-tenancy Saturday, June 30, 12
  2. What We will Cover • Indexes and Types • Mappings

    • Search • Elastica - PHP Library • Examples • Resources Saturday, June 30, 12
  3. Quick & Easy Installation • Download Elasticsearch • http://elasticsearch.org/ •

    Extract • Run • Service, Background, Foreground Saturday, June 30, 12
  4. Indexes & Types • Index: Group of Items (Types) •

    Types: Relevant Data in a Group • Amazon: Books, Movies, Clothes, Etc. • Airlines: American, Delta, KLM, etc. • Each Type & Index can have different data elements Saturday, June 30, 12
  5. Create Type & Add Data curl -XPOST 'http://localhost:9200/dfw/beer/1' -d '

    { "name": "Deep Ellum IPA" } ' Saturday, June 30, 12
  6. Create Type & Add Data curl -XPOST 'http://localhost:9200/dfw/beer/1' -d '

    { "name": "Deep Ellum IPA" } ' curl -XPOST 'http://localhost:9200/dfw/beer/2' -d ' { "name": "Double Brown Stout" } ' Saturday, June 30, 12
  7. curl -XPOST 'http://localhost:9200/dfw/brewery/1' - d ' { "name": "Deep Ellum

    Brewing Company", "beers": [ "Deep Ellum IPA", "Double Brown Stout" ] } ' Saturday, June 30, 12
  8. PUT vs POST • You define ID - POST or

    PUT • ElasticSearch Define Id - POST • Using PUT with no ID will throw error Saturday, June 30, 12
  9. PUT vs POST POST {"ok":true,"_index":"dfw","_type":"brewery ","_id":"Iw9kfa3vSx2FyFen- uK26Q","_version":1} POST OR PUT

    {"ok":true,"_index":"dfw","_type":"brewery ","_id":"1","_version":1} PUT No handler found for uri [/dfw/brewery/] and method [PUT] Saturday, June 30, 12
  10. Update Data curl -XPUT 'http://localhost:9200/dfw/beer/1' -d ' { "name": "Deep

    Ellum IPA", "style": "American-Style India Pale Ale" } ' {"ok":true,"_index":"dfw","_type":"beer","_id":"1","_version":2} Saturday, June 30, 12
  11. { "_index": "dfw", "_type": "brewery", "_id": "1", "_version": 1, "exists":

    true, "_source": { "name": "Deep Ellum Brewing Company", "beers": [ "Deep Ellum IPA", "Double Brown Stout" ] } } Saturday, June 30, 12
  12. Elasticsearch API’s • Allow you to perform operations • Search,

    Add Mappings, Status, Refresh & Optimization • It’s just another endpoint • Checkout Elasticsearch.org for lots more Saturday, June 30, 12
  13. Status • Displays Comprehensive Status Information on an indices. •

    Can be done at all levels (es, index, type) • Endpoint: _status Saturday, June 30, 12
  14. Refresh & Optimize • Refresh: refresh data for near real-time

    search. • All Levels • Endpoint: _refresh • Optimize: Optimizes Lucene segments for faster searching. • All Levels • Endpoint: _optimize Saturday, June 30, 12
  15. Mapping • Defines how the document is mapped to search

    engine • You don’t have to define this. It’s dynamic. But you can.... • You can define them at the Index and Type level • Endpoint: _mapping Saturday, June 30, 12
  16. What you can Define • What fields are searchable •

    Fields dataType • How they are stored • How they are tokenized (index, analyzed) • etc... Saturday, June 30, 12
  17. Mapping Types • Core Data Types • string, integer/long, float/double,

    boolean, and null • Arrays • IP Addresses • Geo Point • Attachment Saturday, June 30, 12
  18. curl -XPOST 'http://localhost:9200/dfw/brewery/ _mapping' -d ' { "brewery" : {

    "properties" : { "name" : { "type" : "string", "store" : "yes", "index" : "not_analyzed" }, "established" : { "type" : "date", "format" : "YYYY" } } } } Saturday, June 30, 12
  19. Dynamic Mapping { "brewery" : { "properties" : { "name"

    : { "type" : "string" }, "established" : { "type" : "string" } } } } Saturday, June 30, 12
  20. Delete Mapping curl -XDELETE 'http://localhost:9200/dfw/brewery/ _mapping' Important to note that

    you are just deleting the mapping not the data. Saturday, June 30, 12
  21. Two Types of Search • URI Request • Limited Searching

    • Request Body • Full functionality • JSON requests Saturday, June 30, 12
  22. URI Request • Performed through a web request or curl

    request. • Simple & Limited • No Filter, Facet, etc. http://localhost:9200/dfw/brewery/ _search/?name:Deep Ellum Brewing Company Saturday, June 30, 12
  23. Request Body • Uses the Query DSL • Allows for

    Filters, Facets, Boosting, More Like this, Fuzzy, etc. • Is a JSON Request Saturday, June 30, 12
  24. Faceted Searching • Facets are “Logical Groupings” that allow easier

    search navigation. • Drill down searching • Think Amazon or NewEgg.com • Type of Facets in Elasticsearch • Terms, Range, Histogram, Date Histogram, Statistical, & Geo Saturday, June 30, 12
  25. Other Features • Highlighting • TTL • Routing - Tell

    were to look at node/shard, etc. • Scripting • Scrolling - Pagination of results • Plugins - Rivers & Attachments Saturday, June 30, 12
  26. Elastica • PHP Library • Opensource Project • GitHub -

    https://github.com/ruflin/Elastica • Follows ZF Standards • Alternative to cUrl • Not a lot of documentation but there are TESTS! Saturday, June 30, 12
  27. Elastica • Everything is an Object • Inject objects to

    create Queries. • Under the hood - Array Based converted to JSON. • Debug • echo json_encode($query->toArray()) Saturday, June 30, 12
  28. Examples • ElasticSearch Query DSL vs. Elastica • Preloaded Index

    (dfw) with two types (brewery, beer). • Uses BreweryDB data through the API • Examples available on Github Saturday, June 30, 12
  29. Resources • Elastica - Github (http://ruflin.github.com/Elastica/) • Elasticsearch - http://www.elasticsearch.org/

    • Elasticsearch GitHub - https://github.com/elasticsearch/ elasticsearch • Google Groups (ES and Elastica) • Slideshare • Slides: http://farrelley.github.com/ElasticSearch-For-PHP/ • Examples - https://github.com/farrelley/ElasticSearch- For-PHP Saturday, June 30, 12
  30. Thank You! • farrelley - Twitter, Github • Follow me

    on Mojo Live • Joind.in - http://joind.in/6341 Saturday, June 30, 12