I have this running example with 2 aggregations:
<?php
// Create the actual search query
$elasticaQuery = new Elastica\Query();
// create the multimatch
$query = new Elastica\Query\MultiMatch();
$query->setQuery($queryParam);
$query->setFields(array('title^12', 'description', 'company_name'));
$query->setType("cross_fields");
$query->setOperator("and");
// create bool query object
$elasticaFilterBool = new Elastica\Query\BoolQuery();
if(!is_null($type_id)){
$filter1 = new Elastica\Query\Terms();
$filter1->setTerms('job_type_id', $type_id_array);
$elasticaFilterBool->addMust($filter1);
}
if(!is_null($location_id)){
$filter2 = new Elastica\Query\Terms();
$filter2->setTerms('location_id', $location_id_array);
$elasticaFilterBool->addMust($filter2);
}
// add the filter to the query Object
$elasticaFilterBool->addMust($query);
// set up the aggregations
$termsAgg = new Elastica\Aggregation\Terms("location_id");
$termsAgg->setField("location_id");
$termsAgg->setSize(10);
$elasticaQuery->addAggregation($termsAgg);
$termsAgg1 = new Elastica\Aggregation\Terms("job_type_id");
$termsAgg1->setField("job_type_id");
$termsAgg1->setSize(10);
$elasticaQuery->addAggregation($termsAgg1);
$elasticaQuery
->setFrom(0)
->setSize(10)
->setSort(['find_date' => 'desc']);
$elasticaQuery->setQuery($elasticaFilterBool);
How to concat the aggregations, so they are chained. If I select one of the filter, the aggregations of this filter should show all num_documents for each value and the other aggregation should change.
Thanks
Nik
curl -XGET 'http://localhost:9200/twitter/tweet/_search?pretty=true' -d '
{
"filter": { "and": [ { "terms": { "user": [ "gansbrest", "max" ] } } ] },
"aggs": {
"user": {
"filter": { "match_all": {} },
"aggs": {
"user": {
"terms": {
"field": "user"
}
}
}
},
"tag": {
"filter": { "and": [ { "terms": { "user": [ "gansbrest", "max" ] } } ] },
"aggs": {
"tag": {
"terms": {
"field": "tag"
}
}
}
}
}
}'
<?php
// Create the actual search query
$elasticaQuery = new Elastica\Query();
// create the multimatch
$query = new Elastica\Query\MultiMatch();
$query->setQuery($queryParam);
$query->setFields(array('title^12', 'description', 'company_name'));
$query->setType("cross_fields");
$query->setOperator("and");
//create bool query object
$elasticaFilterBool = new Elastica\Query\BoolQuery();
// check for filter $type_id
if(!is_null($type_id)){
$filter1 = new Elastica\Query\Terms();
$filter1->setTerms('job_type_id', $type_id);
$elasticaFilterBool->addMust($filter1);
}
// check for filter $location_id
if(!is_null($location_id)){
$filter2 = new Elastica\Query\Terms();
$filter2->setTerms('location_id', array($location_id)); //$location_id is array
$elasticaFilterBool->addMust($filter2);
}
// add the filter to the queryObject
$elasticaFilterBool->addMust($query);
// set up the aggregation
$termsAgg = new Elastica\Aggregation\Terms("location_id");
$termsAgg->setField("location_id");
$termsAgg->setSize(10);
$elasticaQuery->addAggregation($termsAgg);
$termsAgg1 = new Elastica\Aggregation\Terms("job_type_id");
$termsAgg1->setField("job_type_id");
$termsAgg1->setSize(10);
$elasticaQuery->addAggregation($termsAgg1);
$elasticaQuery
->setFrom(0)
->setSize(10);
$elasticaQuery->setQuery($elasticaFilterBool);