Created
December 11, 2013 16:24
-
-
Save felipelavinz/7913569 to your computer and use it in GitHub Desktop.
ejemplo de consulta utilizando Elastica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// indicar los namespace que se utilizarán | |
use Elastica; | |
use Elastica\Query; | |
use Elastica\Exception; | |
$results = null; | |
try { | |
$elastica = new Elastica\Client(array( | |
'host' => ES_HOST, | |
'port' => ES_PORT | |
)); | |
// construir objeto de consulta | |
$query = new Query; | |
// indicar el orden de los resultados | |
$query->addSort( array('dtstart' => 'asc') ); | |
// indicar condiciones booleanas | |
$bool = new Query\Bool; | |
// consultar por fechas | |
$bool->addMust( new Query\Range('dtend', array('from' => date('Y-m-d 00:00:00'))) ); | |
// limitar resultados al sitio de UDD corporativo | |
$filterbysite = new Query\Field(); | |
$filterbysite->setQueryString('site.url:"http://www.udd.cl"'); | |
$bool->addMust( $filterbysite ); | |
// agregar las condiciones de búsqueda a la consulta | |
$query->setQuery( $bool ); | |
// construir objeto de búsqueda | |
$search = new Elastica\Search( $elastica ); | |
// obtener resultados | |
$results = $search->addIndex('udd')->addType('event')->search( $query, $limit ); | |
} catch ( Exception\InvalidException $e ) { | |
// atrapar error | |
// en este ejemplo, sólo indicamos que no existen resultados | |
$results = array(); | |
} | |
return $results; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment