This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
For lazy people (get started in 5 minutes)
scharrier edited this page Jul 19, 2012
·
8 revisions
// Just include this file : every needed class will be loaded automatically
require_once(SIMPLE_ES_DIR . '/lib/Autoload.php') ;
// Get a client. Note that you can use Simples::current() to get your current connection later.
$client = Simples::connect(array(
'host' => 'your.host.net', // Default : localhost
'index' => 'your.index', // Optionnal : it can be specified in each request
'type' => 'your.type' // Optionnal : it can be specified in each request
));
// Some data you want to index
$data = array(
'firstname' => 'Jim',
'lastname' => 'Morrison',
'categories' => array('Poet','Composer','Singer')
);
try {
// Tell your current connection to index this data.
Simples::current()->index($data)->execute() ; // Don't forget the "execute()" on each request !
} catch (Exception $e) {
die('Error indexing data : ' . $e->getMessage()) ; // ES errors are intercepted and generates same exceptions as Simpl-ES
}
A search request is composed by 4 distinct elements :
- the global request options (size, sort, ...)
- the query builder
- the filters builder
- the facets builder
$client = Simples::current() ;
try {
// Really simple search : execute a query string
$count = $client->search('jim')->hits->total ; // Hey what ? Where's the execute() call ? No need if
// you ask for response parameters : Simpl-ES is smart
// enough to execute your request.
// Ok, now a real case
$request = $client->search()
->should() // You can define explicitly bool criteria on first level only (this is not recursive)
->match('Morrison')->in('lastname') // Subquery : term request in the lastname field
->match('Jim') // Subquery : query_string (in all fields)
->not()
->match('poet')->in(array('lastname','categories'))
->limit(5) // Global request option
->sort('lastname') ;
$results = $request->execute() ; // Yes, yes, you HAVE to call execute() there.
} catch (Exception $e) {
die('Error during search : ' . $e->getMessage()) ;
}
$client = Simples::current() ;
try {
// Single filter
$results = $client->search()
->filter()
->field('categories')->match('Poet')
->execute() ;
// Multiple filters
$results = $client->search()
->filters(array(
'categories' => array('Poet','Composer'),
'status' => 'active'
))
->execute() ;
} catch (Exception $e) {
die('Error during search : ' . $e->getMessage()) ;
}
$client = Simples::current() ;
try {
// Single facet
$results = $client->search()
->facet('categories') // Simpl-ES will create a "term" facet called "categories", on the field "categories"
->execute() ; // If no query specified, Simpl-ES will do a match_all query.
// Multiple facets
$results = $client->search()
->facets(array(
'categories',
'statuses' => array('field' => 'status') // Advanced description (you can give any key supported by ES here)
))
->execute() ;
} catch (Exception $e) {
die('Error during search : ' . $e->getMessage()) ;
}
$client = Simples::current() ;
try {
$results = $client->search()
->match('jim')
->filter()->match('active')->in('status')
->facet('categories')
->execute() ;
} catch (Exception $e) {
die('Error during search : ' . $e->getMessage()) ;
}
Each request you call generates a Simples_Request object. When you call the execute() method, you get a Simples_Response object. Theses response objects have some magic : you can call any of their data properties as a simple php object property.
$response = $resquest->execute() ;
echo 'Request took ' . $response->took . ' ms : ' . $response->hits->total . ' results for your request' ;
Response can be transformed in JSON or array calling the to() method :
$results = $response->to('array') ; // Extract a search request response into an array
$esults = $response->to('json') ; // Extract a search request response into an JSON response
You can get the response hits directly, using the hits() method, whitch returns an iterable object :
foreach($response->hits() as $hit) {
echo $hit->username . ' : ' . $hit->age ;
}