More Related Content Similar to Elastic Searching With PHP Similar to Elastic Searching With PHP (20) Elastic Searching With PHP2. About me – work life
smartvote.ch
Web Developer at Liip AG for 3 years
3. About me – the other life
Aerobatics Paragliding Pilot
6. elasticsearch – full text
search
Based on Lucene
„Apache LuceneTM is a high-performance, full-featured
text search engine library written entirely in Java. It is
a technology suitable for nearly any application that
requires full-text search, especially cross-platform.“
7. elasticsearch – full text
search
Multi-language support
Geolocation
Did-you-mean suggestions
Autocomplete
and a lot more...
9. elasticsearch – schema free
All fields indexed by default
Detecting data structure
{
"name" : "Peter",
"age" : 22
}
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
}
Request
Response
12. elasticsearch – RESTful API
–
Delete document
$ curl -XDELETE 'http://localhost:9200/phpday/speaker/1'
{
"ok" : true,
"_index" : "phpday",
"_type" : "speaker",
"_id" : "1",
"found" : true
}
Request
Response
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
19. elastica - Prepare
// Load index
$elasticaIndex = $elasticaClient->getIndex('phpday');
// Create a type
$elasticaType = $elasticaIndex->getType('speaker');
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 -> Array
Add 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!
26. elastica – Search
$elasticaQuery = new ElasticaQuery();
$elasticaQuery->setQuery($elasticaQueryString);
$resultSet = $elasticaType->search($elasticaQuery);
Create the actual search object
Search on type or index
32. FOSElasticaBundle –
Add/update – Automatic
Config:
! Mapping required
fos_elastica:
...
speaker:
mappings:
firstname: {type: string}
persistence:
driver: orm
model: LiipPhpdayBundleEntitySpeaker
provider: ~
34. FOSElasticaBundle –
Add/update – Automatic
SpeakerRepository::getSpeakers() returns
QueryBuilder
No propel support yet!
fos_elastica:
...
speaker:
...
persistence:
...
provider:
query_builder_method: getSpeakers
35. FOSElasticaBundle –
Add/update – Manual
Provider
Remove persistence configuration
Define service for provider class
services:
liip_phpday.search_provider.speaker:
class: LiipPhpdayBundleProviderSpeakerProvider
tags:
- {name: fos_elastica.provider,index: phpday,type: speaker}
arguments: [ @fos_elastica.index.phpday.speaker ]
36. FOSElasticaBundle –
Add/update – Manual
Provider
Manually create and add elastica documents
class SpeakerProvider implements ProviderInterface
{
function populate(Closure $loggerClosure = null)
{
...
}
}
38. FOSElasticaBundle – Search
Using elastica
/** var ElasticaType */
$speakerType = $this->container
->get('fos_elastica.index.phpday.speaker');
/** var ElasticaResultSet */
$results = $speakerType->search('MariaSymfony');
39. FOSElasticaBundle – Search
Getting back Doctrine/Propel Entities
fos_elastica:
...
mappings: ...
persistence:
driver: orm
model: LiipPhpdayBundleEntitySpeaker
provider: ~
finder: ~
40. FOSElasticaBundle – Search
Getting 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 – Search
Custom queries
fos_elastica:
...
mappings: ...
persistence:
driver: orm
model: LiipPhpdayBundleEntitySpeaker
provider: ~
finder: ~
repository: ...SearchSpeakerRepository
42. FOSElasticaBundle – Search
Custom queries
class SpeakerRepository extends FOSElasticaBundleRepository
{
public function findWithCustomQuery($searchTerm)
{
// build query with Elastica objects
return $this->find($searchTerm);
}
}
43. FOSElasticaBundle – Search
Custom 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');
Editor's Notes 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