Elasticsearch-排序

上一篇博客中,我们记录了如何使用Elasticsearch的查询表达式进行一些简单的搜索。接下来将记录如何对查询的数据进行排序。

默认排序

在之前的博客中有提到,Elasticsearch默认是按照_score的值来进行倒叙排序的:

curl -XGET 'http://localhost:9200/pycharm/python/_search' -d '
{
    "query":{
        "match":{
            "name":"老王"
        }
    }    
}
'

将获得如下数据:

{
  "hits": {
    "total": 2,
    "max_score": 0.51623213,
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "AV91thbjDeJV7vg4lrC0",
        "_score": 0.51623213,
        "_source": {
          "name": "老王",
          "age": 20,
          "single": true,
          "sinature": "一切皆有可能",
          "addr": "广西"
        }
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "AV91tbWcDeJV7vg4lrCz",
        "_score": 0.25811607,
        "_source": {
          "name": "老张",
          "age": 18,
          "single": false,
          "sinature": "一切皆不可能",
          "addr": "江西"
        }
      }
    ]
  }
}

从查询到的数据结果可以看出,数据是按照_score的大小进行排序的,_score值越大,越靠前,而且匹配程度越高,_score值越大。

按照指定的字段值排序

在日常的开发中,我们不可能每次都是按照默认的排序进行获取数据,有时,需要数据按照我们指定的字段的值来进行排序,此时,我们可以使用sort参数来进行实现:

curl -XGET 'http://localhost:9200/pycharm/python/_search' -d '
{
    "query":{
        "match":{
            "name":"老王"
        }
    },
    "sort":{
        "age":{
            "order":"asc"
        }
    }
}
'

将获得如下结果:

"hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "AV91tbWcDeJV7vg4lrCz",
        "_score": null,
        "_source": {
          "name": "老张",
          "age": 18,
          "single": false,
          "sinature": "一切皆不可能",
          "addr": "江西"
        },
        "sort": [
          18
        ]
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "AV91thbjDeJV7vg4lrC0",
        "_score": null,
        "_source": {
          "name": "老王",
          "age": 20,
          "single": true,
          "sinature": "一切皆有可能",
          "addr": "广西"
        },
        "sort": [
          20
        ]
      }
    ]
}

从结果数据可以看出,数据真的按照指定的字段值进行排序。因为修改了默认排序的缘故,数据将不再返回_score值。

值得注意的是,修改了默认排序之后,在每个的结果中都会有一个新的名为sort的元素,它包含了我们用于排序的值。倘若数据是按照日期进行排序,即假设数据中包含了一个日期字段,并且按照日期进行排序,那么sort将会包含这个日期的long类型,即将日期转换成毫秒数的值。

多级排序

在日常开发中,只对一个字段进行排序往往不能满足我们的需求,此时,我们需要对多个字段进行排序,我们可以这样:

curl -XGET 'http://localhost:9200/pycharm/python/_search' -d '
{
    "query":{
        "match":{
            "name":"老王"
        }
    },
    "sort":{
        "age":{
            "order":"asc"
        },
        "_score":{
            "order":"desc"
        }
    }
}
'

将会获得如下结果:

"hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "AV91tbWcDeJV7vg4lrCz",
        "_score": 0.25811607,
        "_source": {
          "name": "老张",
          "age": 18,
          "single": false,
          "sinature": "一切皆不可能",
          "addr": "江西"
        },
        "sort": [
          18,
          0.25811607
        ]
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "AV91thbjDeJV7vg4lrC0",
        "_score": 0.51623213,
        "_source": {
          "name": "老王",
          "age": 20,
          "single": true,
          "sinature": "一切皆有可能",
          "addr": "广西"
        },
        "sort": [
          20,
          0.51623213
        ]
      }
    ]
}

此时我们就能够根据多个条件进行排序了。如果我们将排序中age_score的顺序调换一下,你会发现有这截然不同的结果(这里我就补贴代码进行举证了,有兴趣的同学可以自己进行验证一下),此时我们可以判断sort排序是根据字段的前后顺序来进行优先级排序的。

字段多值排序

有的字段是列表类型的,那我们该如何根据这个类型的字段进行排序呢?此时,我们可以将此字段减为单值,这样就可以通过使用minmaxsum或者是avg模式进行排序,假设有这样一组数据:


"hits": {
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "2",
        "_source": {
          "name": "laozhang",
          "info": [
            80,
            95,
            85
          ]
        }
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "1",
        "_source": {
          "name": "laowang",
          "info": [
            80,
            95,
            77
          ]
        }
      }
    ]
}

从数据可以看出,字段info是数组类型,接下来我们通过info字段进行排序,info列表中的最小值进行升序排序:

curl -XGET 'http://localhost:9200/pycharm/python/_search' -d '
{
    "sort":{
        "info":{
            "order":"asc",
            "mode":"min"
        }
    }    
}
'

将会获得如下结果:

"hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "laowang",
          "info": [
            80,
            95,
            77
          ]
        },
        "sort": [
          77
        ]
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "laozhang",
          "info": [
            80,
            95,
            85
          ]
        },
        "sort": [
          80
        ]
      }
    ]
}

从结果数据中可以看出,在每个的结果中都会有一个新的名为sort的元素,它包含了我们用于排序的值,第一组数组中是77,即info列表中的最小值。可以看出数据正是按照我们想要的排序方式进行获取数据。

字段多值排序(二)

上面的栗子是针对于字段为列表类型,然后对该字段的最小值,最大值,平均值或者和来进行排序,如果字段是字典类型,那么该怎么对其进行排序呢?下面有这样一组数据:

"hits": {
    "total": 2,
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "laozhang",
          "userInfo": {
            "addr":"深圳",
            "age":18
          }
        }
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "laowang",
          "userInfo": {
            "addr":"广州",
            "age":20
          }
        }
      }
    ]
}

我们需要对userInfo中的age值进行倒序排序,如下:

curl -XGET 'http://localhost:9200/pycharm/python/_search' -d '
{
    "sort":{
        "userInfo.age":{
            "order":"desc"
        }
    }    
}
'

将会获得如下数据:

"hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "laowang",
          "userInfo": {
            "addr": "广州",
            "age": 20
          }
        },
        "sort": [
          20
        ]
      },
      {
        "_index": "pycharm",
        "_type": "python",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "laozhang",
          "userInfo": {
            "addr": "深圳",
            "age": 18
          }
        },
        "sort": [
          18
        ]
      }
    ]
}

从结果数据可以看出,数据的排列正是按照我们想要的排序方式排列的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值