FOSElasticaBundle CustomScore query pagination

≯℡__Kan透↙ 提交于 2019-12-10 00:21:35

问题


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:


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.



来源:https://stackoverflow.com/questions/18718211/foselasticabundle-customscore-query-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!