簡體   English   中英

在彈性中,如何將排序添加到我的查詢中,以便它以排序順序返回結果

[英]In elastic how can I add sort to my query so that it returns the results in a sorted order

"query": {

        "bool": {

            "must":[

                {

                    "range": {

                        "Date": {

                            "gte": firstDate,

                            "lte": lastDate

                        }

                    }

                },

               

               
            ]

        }

    }

以上是我的查詢如何添加排序功能,以便我得到返回的值,因為它們現在沒有被返回排序

您可以使用排序搜索按一個或多個字段對結果進行排序。

例子:

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "Date": {
              "gte": "firstDate",
              "lte": "lastDate"
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "firstDate": {
        "order": "desc"
      }
    }
  ]
}

你可以插入一個排序子句

{
    "sort": [
        {
            "Date": {
                "format": "strict_date_optional_time_nanos",// From https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
                "order" : "desc"                
            }
        }
    ],
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "Date": {
                            "gte": firstDate,
                            "lte": lastDate
                        }
                    }
                }
            ]
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM