2

I am faily new to elasticsearch and try to get along with elastica. I try to find out what Items are often togehter in a set of items when one of them is 2 and/or 7. So the index contains a lot of nested sets of items. The mutual items will be extracted with facets. But I don't seem to get the following query converted to elastica:

curl -X POST "http://localhost:9200/ratings/rating/_search?pretty=true" -d '
{
  "query": {
    "terms": {
      "bookid": [2, 7],
      "minimum_match" : 1
    }
  },
  "size": 0,
  "facets": {
    "bookid": {
      "terms": {
        "field": "bookid"
      }
    }
  }
}'

I was trying this:

        // Load index
        $index = $client->getIndex('ratings');
        $type = $index->getType('rating');


        // We want a Terms query.
        $query = new Elastica_Query_Terms();


        // Setting Terms
        $query->setTerms('bookid', $bookids);

        // Facets
        $facet = new Elastica_Facet_Query('matches');
        $facet->setField( 'bookid' )
            ->setSize(100);
        $facet->setQuery($query);

        $resultSet = $type->search($query);
        return $resultSet;

but no luck so far. How do I add the facet properties?

t j
  • 7,026
  • 12
  • 46
  • 66
fisch
  • 683
  • 1
  • 6
  • 17

1 Answers1

1

Elastica query classes have an addFacet method, so rather than set the query on the facet, it makes more sense to add the facet to the query.

$facet = new Elastica_Facet_Query('matches');
$facet->setField('bookid')
      ->setSize(100);

$query = new Elastica_Query_Terms();
$query->setTerms('bookid', $bookids);
$query->addFacet($facet);
t j
  • 7,026
  • 12
  • 46
  • 66