How to query using Elastica
This should do:
// Create a "global" query$query = new Elastica_Query;// Create the term query$term = new Elastica_Query_Term;$term->setTerm('click', 'true');// Add term query to "global" query$query->setQuery($term);// Create the facet$facet = new Elastica_Facet_Terms('matches');$facet->setField('pubid') ->setAllTerms(true) ->setSize(200);// Add facet to "global" query$query->addFacet($facet);// Output queryecho json_encode($query->toArray());
To run the query, you need to conntect to your ES servers
// Connect to your ES servers$client = new Elastica_Client(array( 'servers' => array( array('host' => 'localhost', 'port' => 9200), array('host' => 'localhost', 'port' => 9201), array('host' => 'localhost', 'port' => 9202), array('host' => 'localhost', 'port' => 9203), array('host' => 'localhost', 'port' => 9204), ),));
And specify which index and type you want to run your query against
// Get index$index = $client->getIndex('myindex');$type = $index->getType('typename');
Now you can run your query
$type->search($query);
Edit:If you are using a namespaced enviroment and a current version of Elastica, change all the lines where new objects are created accordingly to
$query = new \Elastica\Query;$facet = new \Elastica\Facet\Terms
and so on
You can also query like this:(Thanks to http://tech.vg.no/2012/07/03/using-elastica-to-query-elasticsearch/ for the excellent article)
<?php$query = new Elastica_Query_Builder('{ "query" : { "term": { "click": "true" } }, "facets" : { "matches" : { "terms" : { "field" : "pubid", "all_terms" : true, "size": 200 } } } }');// Create a raw query since the query above can't be passed directly to the search method used below$query = new Elastica_Query($query->toArray());// Create the search object and inject the client$search = new Elastica_Search(new Elastica_Client());// Configure and execute the search$resultSet = $search->addIndex('blog') ->addType('posts') ->search($query);// Loop through the resultsforeach ($resultSet as $hit) { // ...}