1

I am in the way to reqrite my classes to the new logic of Elastica and would like to know if I am on the right way.

My actual solution for a query with an filter is:

<?php
// Create the actual search query
$elasticaQuery = new Elastica\Query();

$query = new Elastica\Query\MultiMatch();
$query->setQuery($queryParam);
$query->setFields(array('title^12', 'description', 'company_name'));
$query->setType("cross_fields");
$query->setOperator("and");

// check for filter
if(!is_null($type_id)){            

    //create bool query object
    $elasticaFilterBool = new Elastica\Filter\BoolFilter();

    // the term
    $filter1 = new Elastica\Filter\Term();
    $filter1->setTerm('job_type_id', (int)$type_id);                
    $elasticaFilterBool->addMust($filter1);

    // add the filter to the queryObject
    $elasticaQuery->setPostFilter($elasticaFilterBool);
}

$elasticaQuery
    ->setFrom(0)
    ->setSize(100)
    ->setSort(['find_date' => 'desc']);

// execute the query
$elasticaQuery->setQuery($query);

Because especially the Filters are depricated in the new verions I have rewrite the logic in this way:

<?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
if(!is_null($type_id)){            
    $filter1 = new Elastica\Query\Term(array('job_type_id' => (int)$type_id));             
    $elasticaFilterBool->addMust($filter1);
}

// add the filter to the queryObject
$elasticaFilterBool->addMust($query);

$elasticaQuery
    ->setFrom(0)
    ->setSize(10)
    ->setSort(['find_date' => 'desc']);

$elasticaQuery->setQuery($elasticaFilterBool);

I am becoming the same results back in both examples.

Is this the right way? This means from this point the BoolQuery() is holding all the filters and the MatchAll() query?

Mutatos
  • 1,675
  • 4
  • 25
  • 55

0 Answers0