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

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

44实战如何定制搜索结果的排序规则 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
        ]
      }
    ]
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5350538.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-09
下一篇 2022-12-09

发表评论

登录后才能评论

评论列表(0条)

保存