Search code examples
elasticsearchelastica

Elastica : IN equivalent operator in Elastica


Following this question and it's answers, I'm trying to do the same but with the PHP Elastica and I wasn't successful doing it.

I am trying to give my new \Elastica\Query\Terms an array and I can't find the right way to do so.

I've tried doing it this way :

new \Elastica\Query\Terms(array($grp_field_p => array('value' => $array_pids)))

Where $array_pids is an array containing multiple ids :

array(
    1,
    2,
    3,
    ...
    23015
);

The term aggregation is expecting a $key => $value and the $value can't be an array, if it's not a number he gives me an error.

[terms] query does not support [null]]

The question is, how to correctly pass to the terms aggregation an array instead of a number to simulate an SQL : IN ?


Solution

  • Based on its source code :

    public function __construct($key = '', array $terms = [])
    {
        $this->setTerms($key, $terms);
    }
    

    it should work like this:

    new Elastica\Query\Terms($grp_field_p, $array_pids);