-
Simonas Šerlinskas authoredSimonas Šerlinskas authored
How to search with Elasticsearch DSL
In this chapter we will take a look how to perform a search via objective way with Elasticsearch DSL. Well, the good news is that is very simple. That's why we created this library ;).
To start a search you have to create a Search
object.
$search = new Search();
We won't include namespaces in any examples. Don't worry all class's names are unique, so any IDE editor should autocomplete and include it for you ;).
So, when we have a Search
object we can start add something to it. Usually you will add Query
, Filter
and Aggregation
.
More info how create queries, filters and aggregations objects.
Form a Query
Lets take a simple query example with MatchAllQuery
.
$matchAllQuery = new MatchAllQuery();
To add query to the Search
simply call addQuery
function.
$search->addQuery($matchAllQuery);
At the end it will form this query:
{
"query": {
"match_all": {}
}
}
There is no limits to add queries or filters or anything.
Form a Filter
To add a filter is the same way like a query. First, lets create some Filter
object.
$matchAllFilter = new MatchAllFilter();
And simply add to the Search
:
$search->addFilter($matchAllFilter);