dongyouzhui1969
2015-06-23 10:28
浏览 552
已采纳

Elasticsearch实现按字段和方向排序

I've been reading the documentation and other example posts since yesterday to work out how to add sort by field and the direction to my current query array here but so far no luck. I've seen examples like {"sort": {"_score": "desc"}, "query": { .... but couldn't quiet get my head around it to modify my array since this is the very first time I'm dealing with ES.

All I need is: I want to be able to sort by price or stock in asc or desc order.

Everytime I try to update my array to implement sorting, I get {"error":{"code":0,"message":"Invalid option sort"}} error.

NOTE: The query array is being passed to Pagerfanta to get results.

    $paginator = $this->productFinder->findPaginated($myArray)
        ->setMaxPerPage($limit)
        ->setCurrentPage($page);
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpl22899 2015-07-11 08:53
    已采纳

    For the specific field and direction:

    {
       "sort": {
          "price": "asc"
       }
       ... rest of the code
    }
    

    For no particular sorting (this is set by default):

    {
       "sort": {
          "_score": "desc"
       }
       ... rest of the code
    }
    
    已采纳该答案
    打赏 评论
  • douhua1760 2015-06-23 11:36

    Here's an example:

    {
      "_source":true,
      "query":{
        "simple_query_string":{
          "query":"1*"
        }
      },
      "sort":[
        {
          "price":{
            "order":"desc",
            "missing":"_last"
          }
        },
        {
          "_score":{
            "order":"desc",
            "missing":"_last"
          }
        }
      ]
    }
    
    打赏 评论

相关推荐 更多相似问题