0
votes

I have just started using the FOSElasticaBundle and have so far found it really good to use. I am not sure if it is my ignorance of the bundle/Elastica or my lack of knowledge about ElasticSearch in general, but is it possible to have a CustomScore query with pagination?

If I try calling setFrom/setSize on the CustomQuery object I am told that the methods do not exist. If I create a Query object and setFrom and size there and pass this query into the CustomScore query object then the pagination parameters are ignored. I have included a copy of my code for what it is worth...

        $queryString = new QueryString();
        $queryString->setFields(array('_all'))
            ->setDefaultOperator('OR')
            ->setQuery($terms);

        $query = new \Elastica\Query();
        $query->setQuery($queryString);
        $query->setSize($maxItems);
        $query->setFrom(($page - 1) * $maxItems);

        $custScoreQuery = new CustomScore();
        $custScoreQuery->setQuery($query);
        $custScoreQuery->setScript("_score * (doc['section.id'] == 7) ? 0.5 : 1");
        $index   = $this->get('fos_elastica.index.search_en_gb');
        $results = $index->search($custScoreQuery);

Any help gladly accepted :o)

1

1 Answers

6
votes

Size and From must be applied to the top level query, you are losing them here.

Try this:

$queryString = new QueryString();
$queryString->setFields(array('_all'))
        ->setDefaultOperator('OR')
        ->setQuery($terms);

$custScoreQuery = new CustomScore();
$custScoreQuery->setQuery($queryString);
$custScoreQuery->setScript("_score * (doc['section.id'] == 7) ? 0.5 : 1");

$query = new \Elastica\Query();
$query->setQuery($custScoreQuery);
$query->setSize($maxItems);
$query->setFrom(($page - 1) * $maxItems);

$index   = $this->get('fos_elastica.index.search_en_gb');
$results = $index->search($query);

Also, turn on the logs in Elastica, and you will be able to see if there is a "size" and "offset" in the Json query. That being said, of course pagination works with custom score.