FOSElasticaBundle CustomScore查询分页 [英] FOSElasticaBundle CustomScore query pagination
问题描述
我刚刚开始使用FOSElasticaBundle,到目前为止,发现它真的很好用。我不知道我是否对于Elastica或者Elastica的缺乏知识,或者我对于ElasticSearch的知识缺乏一般性,但是是否可以使用分页式的CustomScore查询?
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?
如果我尝试在CustomQuery对象上调用setFrom / setSize,我被告知该方法不存在。如果我创建一个Query对象并将其设置为size,并将此查询传递给CustomScore查询对象,那么分页参数将被忽略。我已经包含了我的代码副本,这是值得的...
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);
任何帮助都乐意接受:o)
Any help gladly accepted :o)
推荐答案
Size和From必须应用于顶级查询,您将在这里丢失。
Size and From must be applied to the top level query, you are losing them here.
尝试这样:
$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);
此外,打开Elastica中的日志,您将可以看到是否有大小和偏移量。话虽如此,当然分页与定制分数一致。
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.
这篇关于FOSElasticaBundle CustomScore查询分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!