49、初识搜素引擎_动手实战如何定制搜索结果的排序规则

2020-01-08  本文已影响0人  拉提娜的爸爸

1、默认排序规则

默认情况下,是按照_score降序排序的
然而,某些情况下,可能没有有用的_score,比如说filter,它查询出来的结果_score的结果都是一样的,所以这种排序在一些情况下就没有意义,如下_score都等于0:

GET /_search
{
    "query" : {
      "bool" : {
        "filter" : {
          "range": {
            "age": {
              "gte": 30
            }
          }
        }
      }
    }
}

当然,也可以是constant_score,_score都等于1:

GET /_search
{
    "query" : {
      "constant_score" : {
        "filter" : {
          "range": {
            "age": {
              "gte": 30
            }
          }
        }
      }
    }
}

2、定制排序规则

根据入职时间升序排序

GET /company/employee/_search
{
    "query" : {
      "constant_score" : {
        "filter" : {
          "range": {
            "age": {
              "gte": 30
            }
          }
        }
      }
    },
    "sort": [
      {
        "join_date": {
          "order": "asc"
        }
      }
    ]
}
--------------------------------结果---------------------------
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "company",
        "_type": "employee",
        "_id": "3",
        "_score": null,
        "_source": {
          "address": {
            "country": "china",
            "province": "shanxi",
            "city": "xian"
          },
          "name": "marry",
          "age": 35,
          "join_date": "2015-01-01"
        },
        "sort": [
          1420070400000
        ]
      },
      {
        "_index": "company",
        "_type": "employee",
        "_id": "2",
        "_score": null,
        "_source": {
          "address": {
            "country": "china",
            "province": "jiangsu",
            "city": "nanjing"
          },
          "name": "tom",
          "age": 30,
          "join_date": "2016-01-01"
        },
        "sort": [
          1451606400000
        ]
      }
    ]
  }
}
上一篇 下一篇

猜你喜欢

热点阅读