With some documents in the index we can start searching. A basic search is executing a query against elasticsearch. The result of a query then can be filtered by defining some filters. Additionally you can aggregate Documents to create metadata or graphs based on filters. For further information check out the Query DSL and the Aggregations reference. In this example we don’t use all possible options to keep it simple.
Search
The Search class is one of most flexible entry points in Elastica to query elasticsearch form your PHP application. Other classes which can execute queries are: Index, Type, Bulk and even Client for raw queries.
Search class provides useful methods and constants to configure your search query.
You may have noticed that the most important part of a search query is actually missing: the query. Because this is the most complicated (and fun) part of elasticsearch, Elastica provides a bunch of classes to help you creating that part of your search query.
Query is dedicated to the “body” of request body search. It provides a method for every configuration field:
to create more complex queries use QueryBuilder to construct all of the different parts of your query. QueryBuilder is going to check, whether the method you want to use is available in your current elasticsearch version or not.
1234567
// version checks against latest DSL version$qb=new\Elastica\QueryBuilder();// version checks against 1.3 DSL version$qb=new\Elastica\QueryBuilder(new\Elastica\QueryBuilder\Version\Version130());
Now just start to write your query as you would write the raw JSON. If your IDE is configured correctly you should experience a handy way to access all available methods in elasticsearch.
// Get aggregations from the result of the search query$aggregations=$resultSet->getAggregations();// Notice, that "range" is the same name you chose// when the aggregation was defined.foreach($aggregations['range']['buckets']as$bucket){// ... handle array $bucket }