1
votes

I just don't figure out how to do my custom query with Elastica. I have a search form to find products, where the user can enter a name, select a category, a brand etc.

In my product mapping there is a field called "category_id" which contains the id of the category as you can guess and there is also another field called "category" which contains an object.

Here is an example of this field when I display all products without filters :

[category] => Array
                (
                    [id] => 2
                    [name] => Laptop
                    [slug] => laptop
                    [created_at] => 2012-03-12T17:11:42+0000
                    [updated_at] => 2013-04-25T00:01:04+0000
                )

I added manually the previous "category_id" field but I didn't notice there was the "category" field. In order to not overload my product mapping I would like to know how to build a query by specifing the id of the "category" field.

This is how I do actually :

$elasticaFilterCategory = new \Elastica\Filter\Term();
$elasticaFilterCategory->setTerm('category_id', $category_id]);

I checked the doc of Elastica and we can see the setTerm method accepts array() : http://elastica.io/api/classes/Elastica.Filter.Term.html#method_setTerm

So I tried without success :

$elasticaFilterCategory = new \Elastica\Filter\Term();
$elasticaFilterCategory->setTerm('category', array( "id" =>  $category_id ));
1

1 Answers

0
votes

you can use something like this

$elasticaFilterCategory = new \Elastica\Filter\Term();
$elasticaFilterCategory->setTerm('category.id', $category_id);