您当前的位置: 首页 >  搜索

Dongguo丶

暂无认证

  • 2浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

44实战如何定制搜索结果的排序规则

Dongguo丶 发布时间:2021-11-08 22:36:04 ,浏览量:2

1、默认排序规则

默认情况下,是按照_score降序排序的

然而,某些情况下,可能没有用到_score,比如说filter

GET /_search
{
    "query" : {
        "bool" : {
            "filter" : {
                "term" : {
                    "author_id" : 1
                }
            }
        }
    }
}

当然,也可以是constant_score

GET /_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "term" : {
                    "author_id" : 1
                }
            }
        }
    }
}
2、定制排序规则

按照员工入职时间升序

GET /company/employee/_search 
{
    "query":{
        "range":{
            "age":{
                "gte": 30
            }
        }
    },
    "sort": [{
        "join_date":{
            "order": "asc"
        }
    }]
}

响应结果

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "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
        ]
      }
    ]
  }
}
关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.0374s