Your SlideShare is downloading. ×
Elastic Searching With PHP
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Elastic Searching With PHP

1,830
views

Published on

Published in: Technology

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,830
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Index automatically created if not existing Id auto-generated if not in URL
  • If index does not exist yet, nothing happened so far But no error either
  • At this point the index is created if it did not exist before
  • Second argument is optional, in case you want to define the id of the document
  • Transcript

    • 1. Elastic Searching WithPHPLea Hänsenberger - @lea_hLiip AG – Agile Web Development – Fribourg, Zürich, Bern
    • 2. About me – work lifesmartvote.chWeb Developer at Liip AG for 3 years
    • 3. About me – the other lifeAerobatics Paragliding Pilot
    • 4. ElasticaFOSElasticaBundle
    • 5. elasticsearchLuceneFull textsearchDocument orientedRESTfulAPISchema freehttp://elasticsearch.org
    • 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. elasticsearch – full textsearchMulti-language supportGeolocationDid-you-mean suggestionsAutocompleteand a lot more...
    • 8. elasticsearch – documentorientedEntities as structured JSON documentsAll fields indexed by default{"field" : "value"}
    • 9. elasticsearch – schema freeAll fields indexed by defaultDetecting data structure{"name" : "Peter","age" : 22}
    • 10. elasticsearch – RESTful APIJSON over HTTP
    • 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. 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. 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. 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. elasticsearch – RESTful API–Search{..."hits":{..."hits":[{..."_source":{"firstname":"John","lastname":"Doe","speeches":2}}]}}Response
    • 16. Elasticsearch - ToolsBrowser/Queryinghttp://mobz.github.io/elasticsearch-head/
    • 17. Elasticahttp://elastica.ioPHP client
    • 18. elastica - Connect$elasticaClient = new ElasticaClient();$elasticaClient = new ElasticaClient(array(host => otherhost.org,port => 12345,));
    • 19. elastica - Prepare// Load index$elasticaIndex = $elasticaClient->getIndex(phpday);// Create a type$elasticaType = $elasticaIndex->getType(speaker);
    • 20. elastica – Add/updatedocument$doc = new ElasticaDocument(1, array(firstname => John,(...)));$elasticaType->addDocument($doc);$elasticaIndex->refresh();
    • 21. elastica – Bulk indexing/* @var array ElasticaDocument */$docs = array();$docs[] = ...$elasticaType->addDocuments($docs);$elasticaIndex->refresh();
    • 22. elastica – Add object$elasticaType->setSerializer($callable);$elasticaType->addObject($object, $elasticaDoc);Configure serializer Object -> ArrayAdd objects directly
    • 23. elastica – Delete document(s)$elasticaType->deleteDocument($elasticaDoc);$elasticaType->deleteById($id);$elasticaType->deleteIds($ids);$elasticaType->deleteByQuery($elasticaQuery);
    • 24. elastica – Field mapping$mapping = new ElasticaTypeMapping();$mapping->setType($elasticaType);$mapping->setProperties(array(firstname => array(type => string),(...)));$mapping->send();Index must exist!
    • 25. elastica – Search$elasticaQueryString = new ElasticaQueryQueryString();$elasticaQueryString->setQuery(John);Define the query
    • 26. elastica – Search$elasticaQuery = new ElasticaQuery();$elasticaQuery->setQuery($elasticaQueryString);$resultSet = $elasticaType->search($elasticaQuery);Create the actual search objectSearch on type or index
    • 27. elastica – Search$elasticaQuery->setFilter($elasticaFilter);$elasticaQuery->addFacet($elasticaFacet);...Filters, facets, etc. are added to the search object
    • 28. FOSElasticaBundleSymfony 2 Bundlehttp://git.io/foselasticabundleelastica
    • 29. FOSElasticaBundleMoved to FOS namespace recentlyFormerly known as FOQElasticaBundle
    • 30. FOSElasticaBundle –Configurationfos_elastica:clients:default: { host: localhost, port: 9200 }indexes:phpday:client: defaulttypes:speaker: ~
    • 31. FOSElasticaBundle –Add/update documentsAutomatic provider for Doctrine, MongoDB, PropelManual provideror: just use elastica
    • 32. FOSElasticaBundle –Add/update – AutomaticConfig:! Mapping requiredfos_elastica:...speaker:mappings:firstname: {type: string}persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~
    • 33. FOSElasticaBundle –Add/update – AutomaticRun command:$ app/console fos:elastica:populate
    • 34. FOSElasticaBundle –Add/update – AutomaticSpeakerRepository::getSpeakers() returnsQueryBuilderNo propel support yet!fos_elastica:...speaker:...persistence:...provider:query_builder_method: getSpeakers
    • 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. FOSElasticaBundle –Add/update – ManualProviderManually create and add elastica documentsclass SpeakerProvider implements ProviderInterface{function populate(Closure $loggerClosure = null){...}}
    • 37. FOSElasticaBundle –Delete DocumentUse elastica directlyor: use populate command to completely reindex
    • 38. FOSElasticaBundle – SearchUsing elastica/** var ElasticaType */$speakerType = $this->container->get(fos_elastica.index.phpday.speaker);/** var ElasticaResultSet */$results = $speakerType->search(MariaSymfony);
    • 39. FOSElasticaBundle – SearchGetting back Doctrine/Propel Entitiesfos_elastica:...mappings: ...persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~finder: ~
    • 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. FOSElasticaBundle – SearchCustom queriesfos_elastica:...mappings: ...persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~finder: ~repository: ...SearchSpeakerRepository
    • 42. FOSElasticaBundle – SearchCustom queriesclass SpeakerRepository extends FOSElasticaBundleRepository{public function findWithCustomQuery($searchTerm){// build query with Elastica objectsreturn $this->find($searchTerm);}}
    • 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. FOSElasticaBundle – Andmore...Live index updatesAdvanced elasticsearch configurationPaged search...=> http://git.io/foselasticabundle
    • 45. ElasticaFOSElasticaBundle?