• Save
Elastic Searching With PHP
Upcoming SlideShare
Loading in...5
×
 

Elastic Searching With PHP

on

  • 9,455 views

 

Statistics

Views

Total Views
9,455
Views on SlideShare
9,375
Embed Views
80

Actions

Likes
9
Downloads
0
Comments
1

2 Embeds 80

https://twitter.com 79
http://darya-ld1.linkedin.biz 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • quite good!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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

Elastic Searching With PHP Elastic Searching With PHP Presentation Transcript

  • Elastic Searching WithPHPLea HänsenbergerLiip AG – Agile Web Development – Fribourg, Zürich, Bern
  • About mesmartvote.chDeveloper at Liip for 3 years
  • ElasticaFOSElasticaBundle
  • elasticsearchLuceneFull textsearchDocument orientedRESTfulAPISchema freehttp://elasticsearch.org
  • elasticsearch – full textsearchBased on LuceneMulti-language supportGeolocationDid-you-mean suggestionsand a lot more...
  • elasticsearch – documentorientedEntities as structured JSON documentsAll fields indexed by default{"field" : "value"}
  • elasticsearch – schema freeAll fields indexed by defaultDetecting data structure{"name" : "Peter","age" : 22}
  • elasticsearch – RESTful APIJSON over HTTP
  • 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
  • elasticsearch – RESTful API–Delete document$ curl -XDELETE http://localhost:9200/phpday/speaker/1{"ok" : true,"_index" : "phpday","_type" : "speaker","_id" : "1","found" : true}RequestResponse
  • 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" }}}}
  • elasticsearch – RESTful API–Search$ curl -XGET http://localhost:9200/phpday/speaker/_search-d {"query":{"bool":{"should":[{"query_string":{"default_field":"_all","query":"John"}}]}}}Request
  • elasticsearch – RESTful API–Search{..."hits":{..."hits":[{..."_source":{"firstname":"John","lastname":"Doe","speeches":2}}]}}Response
  • Elasticsearch - ToolsBrowser/Queryinghttp://mobz.github.io/elasticsearch-head/
  • Elasticahttp://elastica.ioPHP client
  • elastica - Connect$elasticaClient = new ElasticaClient();$elasticaClient = new ElasticaClient(array(host => otherhost.org,port => 12345,));
  • elastica - Prepare// Load index$elasticaIndex = $elasticaClient->getIndex(phpday);// Create a type$elasticaType = $elasticaIndex->getType(speaker);
  • elastica – Add/updatedocument$doc = new ElasticaDocument(1, array(firstname => John,(...)));$elasticaType->addDocument($doc);$elasticaIndex->refresh();
  • elastica – Bulk indexing/* @var array ElasticaDocument */$docs = array();$docs[] = ...$elasticaType->addDocuments($docs);$elasticaIndex->refresh();
  • elastica – Add object$elasticaType->setSerializer($callable);$elasticaType->addObject($object, $elasticaDoc);Configure serializer Object -> ArrayAdd objects directly
  • elastica – Delete document(s)$elasticaType->deleteDocument($elasticaDoc);$elasticaType->deleteById($id);$elasticaType->deleteIds($ids);$elasticaType->deleteByQuery($elasticaQuery);
  • elastica – Field mapping$mapping = new ElasticaTypeMapping();$mapping->setType($elasticaType);$mapping->setProperties(array(firstname => array(type => string),(...)));$mapping->send();Index must exist!
  • elastica – Search$elasticaQueryString = new ElasticaQueryQueryString();$elasticaQueryString->setQuery(John);Define the query
  • elastica – Search$elasticaQuery = new ElasticaQuery();$elasticaQuery->setQuery($elasticaQueryString);$resultSet = $elasticaType->search($elasticaQuery);Create the actual search objectSearch on type or index
  • elastica – Search$elasticaQuery->setFilter($elasticaFilter);$elasticaQuery->addFacet($elasticaFacet);...Filters, facets, etc. are added to the search object
  • FOSElasticaBundleSymfony 2 Bundlehttp://git.io/foselasticabundleelastica
  • FOSElasticaBundleMoved to FOS namespace recentlyNot compatible with latest elastica version, i.e. withnamespaces=> no serializer support yetbut: theres a pull request ;-)
  • FOSElasticaBundle –Configurationfos_elastica:clients:default: { host: localhost, port: 9200 }indexes:phpday:client: defaulttypes:speaker: ~
  • FOSElasticaBundle –Add/update documentsAutomatic provider for Doctrine, MongoDB, PropelManual provideror: just use elastica
  • FOSElasticaBundle –Add/update – AutomaticConfig:! Mapping requiredfos_elastica:...speaker:mappings:firstname: {type: string}persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~
  • FOSElasticaBundle –Add/update – AutomaticRun command:$ app/console fos:elastica:populate
  • FOSElasticaBundle –Add/update – AutomaticSpeakerRepository::getSpeakers() returnsQueryBuilderNo propel support yet!fos_elastica:...speaker:...persistence:...provider:query_builder_method: getSpeakers
  • 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 ]
  • FOSElasticaBundle –Add/update – ManualProviderManually create and add elastica documentsclass SpeakerProvider implements ProviderInterface{function populate(Closure $loggerClosure = null){...}}
  • FOSElasticaBundle –Delete DocumentUse elastica directlyor: use populate command to completely reindex
  • FOSElasticaBundle – SearchUsing elastica/** var Elastica_Type */$speakerType = $this->container->get(fos_elastica.index.phpday.speaker);/** var Elastica_ResultSet */$results = $speakerType->search(MariaSymfony);
  • FOSElasticaBundle – SearchGetting back Doctrine/Propel Entitiesfos_elastica:...mappings: ...persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~finder: ~
  • 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);
  • FOSElasticaBundle – SearchCustom queriesfos_elastica:...mappings: ...persistence:driver: ormmodel: LiipPhpdayBundleEntitySpeakerprovider: ~finder: ~repository: ...SearchSpeakerRepository
  • FOSElasticaBundle – SearchCustom queriesclass SpeakerRepository extends FOSElasticaBundleRepository{public function findWithCustomQuery($searchTerm){// build query with Elastica objectsreturn $this->find($searchTerm);}}
  • 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);
  • FOSElasticaBundle – Andmore...Live index updatesAdvanced elasticsearch configurationPaged search...=> http://git.io/foselasticabundle
  • ElasticaFOQElasticaBundle?