I'm using the ongr/elasticsearch-dsl bundle to build my queries and I need to compose the search-object on different places. When I do it in the same place, it works as expected.
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery;
$search = new Search();
$bool = new BoolQuery();
$search->addQuery($bool);
$bool->add(new TermsQuery('id', [1]));
$bool->add(new TermsQuery('id2', [2]));
echo json_encode($search->toArray());
The result look like this:
{"query":{"bool":{"must":[{"terms":{"id":[1]}},{"terms":{"id2":[2]}}]}}}
Now I pass the search-object to another method and try do add my TermQuery-Objects.
$search = new Search();
$bool = new BoolQuery();
$search->addQuery($bool);
$mapper->map($search);
//---
public function map(search) {
$bool = $search->getQueries();
$bool->add(new TermsQuery('id', [1]));
$bool->add(new TermsQuery('id2', [2]));
echo json_encode($search->toArray());
}
The result is almost the same, but with an extra and empty BoolQuery.
{"query":{"bool":{"must":[{"bool":[]},{"terms":{"id":[1]}},{"terms":{"id2":[2]}}]}}}
What I'm doing wrong? Or how can I extend my search and avoid that empty query?