Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Elastic Searching With PHP

3,539 views

Published on

Published in: Technology
  • Be the first to comment

Elastic Searching With PHP

  1. 1. Elastic Searching WithPHPLea Hänsenberger - @lea_hLiip AG – Agile Web Development – Fribourg, Zürich, Bern
  2. 2. About me – work lifesmartvote.chWeb Developer at Liip AG for 3 years
  3. 3. About me – the other lifeAerobatics Paragliding Pilot
  4. 4. ElasticaFOSElasticaBundle
  5. 5. elasticsearchLuceneFull textsearchDocument orientedRESTfulAPISchema freehttp://elasticsearch.org
  6. 6. elasticsearch – full textsearchBased on Lucene„Apache LuceneTM is a high-performance, full-featuredtext search engine library written entirely in Java. It isa technology suitable for nearly any application thatrequires full-text search, especially cross-platform.“
  7. 7. elasticsearch – full textsearchMulti-language supportGeolocationDid-you-mean suggestionsAutocompleteand a lot more...
  8. 8. elasticsearch – documentorientedEntities as structured JSON documentsAll fields indexed by default{"field" : "value"}
  9. 9. elasticsearch – schema freeAll fields indexed by defaultDetecting data structure{"name" : "Peter","age" : 22}
  10. 10. elasticsearch – RESTful APIJSON over HTTP
  11. 11. elasticsearch – RESTful API–Add/update document$ curl -XPUT http://localhost:9200/phpday/speaker/1-d {"firstname" : "John","lastname" : "Doe","speeches" : 2}{"ok" : true,"_index" : "phpday","_type" : "speaker","_id" : "1","_version" : 1}RequestResponse
  12. 12. elasticsearch – RESTful API–Delete document$ curl -XDELETE http://localhost:9200/phpday/speaker/1{"ok" : true,"_index" : "phpday","_type" : "speaker","_id" : "1","found" : true}RequestResponse
  13. 13. elasticsearch – RESTful API–Field Mapping$ curl -XPUT http://localhost:9200/phpday/speaker/_mapping-d {"speaker" : {"properties" : {"firstname" : { "type" : "string" },"lastname" : { "type" : "string" },"speeches" : { "type" : "integer" }}}}
  14. 14. elasticsearch – RESTful API–Search$ curl -XGET http://localhost:9200/phpday/speaker/_search-d {"query":{"bool":{"should":[{"query_string":{"default_field":"_all","query":"John"}}]}}}Request
  15. 15. elasticsearch – RESTful API–Search{..."hits":{..."hits":[{..."_source":{"firstname":"John","lastname":"Doe","speeches":2}}]}}Response
  16. 16. Elasticsearch - ToolsBrowser/Queryinghttp://mobz.github.io/elasticsearch-head/
  17. 17. Elasticahttp://elastica.ioPHP client
  18. 18. elastica - Connect$elasticaClient = new ElasticaClient();$elasticaClient = new ElasticaClient(array(host => otherhost.org,port => 12345,));
  19. 19. elastica - Prepare// Load index$elasticaIndex = $elasticaClient->getIndex(phpday);// Create a type$elasticaType = $elasticaIndex->getType(speaker);
  20. 20. elastica – Add/updatedocument$doc = new ElasticaDocument(1, array(firstname => John,(...)));$elasticaType->addDocument($doc);$elasticaIndex->refresh();
  21. 21. elastica – Bulk indexing/* @var array ElasticaDocument */$docs = array();$docs[] = ...$elasticaType->addDocuments($docs);$elasticaIndex->refresh();
  22. 22. elastica – Add object$elasticaType->setSerializer($callable);$elasticaType->addObject($object, $elasticaDoc);Configure serializer Object -> ArrayAdd objects directly
  23. 23. elastica – Delete document(s)$elasticaType->deleteDocument($elasticaDoc);$elasticaType->deleteById($id);$elasticaType->deleteIds($ids);$elasticaType->deleteByQuery($elasticaQuery);
  24. 24. elastica – Field mapping$mapping = new ElasticaTypeMapping();$mapping->setType($elasticaType);$mapping->setProperties(array(firstname => array(type => string),(...)));$mapping->send();Index must exist!
  25. 25. elastica – Search$elasticaQueryString = new ElasticaQueryQueryString();$elasticaQueryString->setQuery(John);Define the query
  26. 26. elastica – Search$elasticaQuery = new ElasticaQuery();$elasticaQuery->setQuery($elasticaQueryString);$resultSet = $elasticaType->search($elasticaQuery);Create the actual search objectSearch on type or index
  27. 27. elastica – Search$elasticaQuery->setFilter($elasticaFilter);$elasticaQuery->addFacet($elasticaFacet);...Filters, facets, etc. are added to the search object
  28. 28. FOSElasticaBundleSymfony 2 Bundlehttp://git.io/foselasticabundleelastica
  29. 29. FOSElasticaBundleMoved to FOS namespace recentlyFormerly known as FOQElasticaBundle
  30. 30. FOSElasticaBundle –Configurationfos_elastica:clients:default: { host: localhost, port: 9200 }indexes:phpday:client: defaulttypes:speaker: ~
  31. 31. FOSElasticaBundle –Add/update documentsAutomatic provider for Doctrine, MongoDB, PropelManual provideror: just use elastica
  32. 32. FOSElasticaBundle –Add/update – AutomaticConfig:! Mapping requiredfos_elastica:...speaker:mappings:firstname: {type: string}persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~
  33. 33. FOSElasticaBundle –Add/update – AutomaticRun command:$ app/console fos:elastica:populate
  34. 34. FOSElasticaBundle –Add/update – AutomaticSpeakerRepository::getSpeakers() returnsQueryBuilderNo propel support yet!fos_elastica:...speaker:...persistence:...provider:query_builder_method: getSpeakers
  35. 35. FOSElasticaBundle –Add/update – ManualProviderRemove persistence configurationDefine service for provider classservices:liip_phpday.search_provider.speaker:class: LiipPhpdayBundleProviderSpeakerProvidertags:- {name: fos_elastica.provider,index: phpday,type: speaker}arguments: [ @fos_elastica.index.phpday.speaker ]
  36. 36. FOSElasticaBundle –Add/update – ManualProviderManually create and add elastica documentsclass SpeakerProvider implements ProviderInterface{function populate(Closure $loggerClosure = null){...}}
  37. 37. FOSElasticaBundle –Delete DocumentUse elastica directlyor: use populate command to completely reindex
  38. 38. FOSElasticaBundle – SearchUsing elastica/** var ElasticaType */$speakerType = $this->container->get(fos_elastica.index.phpday.speaker);/** var ElasticaResultSet */$results = $speakerType->search(MariaSymfony);
  39. 39. FOSElasticaBundle – SearchGetting back Doctrine/Propel Entitiesfos_elastica:...mappings: ...persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~finder: ~
  40. 40. FOSElasticaBundle – SearchGetting back Doctrine/Propel Entities/** var FOSElasticaBundleFinderTransformedFinder */$finder = $this->container->get(fos_elastica.finder.phpday.speaker);/** var array of LiipPhpdayBundleEntitySpeaker */$speakers = $finder->find(MariaSymfony);
  41. 41. FOSElasticaBundle – SearchCustom queriesfos_elastica:...mappings: ...persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~finder: ~repository: ...SearchSpeakerRepository
  42. 42. FOSElasticaBundle – SearchCustom queriesclass SpeakerRepository extends FOSElasticaBundleRepository{public function findWithCustomQuery($searchTerm){// build query with Elastica objectsreturn $this->find($searchTerm);}}
  43. 43. FOSElasticaBundle – SearchCustom queries/** var FOSElasticaBundleManagerRepositoryManager */$repositoryManager = $this->container->get(fos_elastica.manager);/** var FOSElasticaBundleRepository */$repository = $repositoryManager->getRepository(LiipPhpdayBundle:Speaker);/** var array of LiipPhpdayBundleEntitySpeaker */$results = $repository->findWithCustomQuery(MariaSymfony);
  44. 44. FOSElasticaBundle – Andmore...Live index updatesAdvanced elasticsearch configurationPaged search...=> http://git.io/foselasticabundle
  45. 45. ElasticaFOSElasticaBundle?

×