【问题标题】:How to make Elastica Query sorting work?如何使 Elastica Query 排序工作?
【发布时间】:2013-07-30 16:12:45
【问题描述】:

我正在尝试对搜索结果进行排序,但我只是使用以下代码获得未排序的结果(在此示例中,我查询所有结果):

$query = new \Elastica\Query();
$query->setSort(array('id' => array('order' => 'desc')));
$resultSet = $type->search($query); // where $type is an elastica type object
$results = $resultSet->getResults();

作为参考,我将 FOSElasticaBundle 与 Symfony2 项目一起使用(使用 $type = $this->container->get('fos_elastica.index.website.myType');),但这应该没什么区别。

如果我直接在浏览器上使用http://localhost:9200/website/myType/_search?sort=id:desc&pretty=true 进行查询,我会得到正确排序的结果。

【问题讨论】:

    标签: elastica


    【解决方案1】:

    其实问题中给出的代码是正确的,它确实返回了正确排序的结果。

    我遇到的问题与 Elastica 和问题中给出的代码无关。

    原来我在 HTML 中重新调整的结果是未排序的,因为我没有返回我的 Elastica 查询结果,而是从我的 SQL 数据库中从这样的查询中获得的相应对象:

    SELECT * FROM ...
    WHERE id IN([ids from Elastica results in order])
    

    上面的 SQL 查询没有按照“IN”子句中给定的 id 顺序返回结果,因此有必要重新排序 SQL 结果以匹配我的 ElasticSearch 结果的顺序(我使用 foreach声明)。如果我一直使用 MySQL,我可以使用 ORDER BY FIELD(id, \[ids in order\]) 以与 IN 子句中提供的 id 相同的顺序获取 SQL 结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多