8 排序&分页&高亮

1. 排序

(1) ES支持对搜索结果排序,默认是根据相关度算分(_score)来排序。可以排序字段类型有: keyword,数值类型,地理坐标,日期等.
java 复制代码
get /hotel/_search
{
  "query":{
    "match_all": {}
  },
  "sort":[
    {
      "score":"desc"
    },
    {
      "price":"asc"
    }
  ]
}

结果:

java 复制代码
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "address" : "是单独发2号",
          "brand" : "四季如春",
          "business" : "新街口商圈",
          "city" : "南京市鼓楼区",
          "id" : 4,
          "location" : "66.66,133.36",
          "name" : "四季如春",
          "pic" : "http://www.qiniu.com/images/xxx.png",
          "price" : 999,
          "score" : 9,
          "starName" : "五星"
        },
        "sort" : [
          9,
          999
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "address" : "洪武北路1号",
          "brand" : "四季",
          "business" : "新街口商圈",
          "city" : "南京市玄武区",
          "id" : 3,
          "location" : "33.35,131.36",
          "name" : "四季",
          "pic" : "http://www.bai.com/images/7.png",
          "price" : 489,
          "score" : 8,
          "starName" : "3星"
        },
        "sort" : [
          8,
          489
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "address" : "月华西路2号",
          "brand" : "7天",
          "business" : "江宁商圈",
          "city" : "南京市江宁区",
          "id" : 2,
          "location" : "33.33,131.35",
          "name" : "7天",
          "pic" : "http://www.bai.com/images/7.png",
          "price" : 188,
          "score" : 7,
          "starName" : "二星"
        },
        "sort" : [
          7,
          188
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "address" : "柳州东路1号",
          "brand" : "如家",
          "business" : "弘阳商圈",
          "city" : "南京市浦口区",
          "id" : 1,
          "location" : "33.33,131.33",
          "name" : "如家",
          "pic" : "http://www.bai.com/images/rujia.png",
          "price" : 189,
          "score" : 7,
          "starName" : "二星"
        },
        "sort" : [
          7,
          189
        ]
      }
    ]
  }
}

2. 分页

es默认情况下,只返回top10条数据,而如果需要查询更多的数据就需要设置分页参数,from,size

java 复制代码
get /hotel/_search
{
  "query":{
    "match_all": {}
  },
  "sort":[
    {
      "score":"desc"
    },
    {
      "price":"asc"
    }
  ],
  "from":0,
  "size":2
}
java 复制代码
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "address" : "是单独发2号",
          "brand" : "四季如春",
          "business" : "新街口商圈",
          "city" : "南京市鼓楼区",
          "id" : 4,
          "location" : "66.66,133.36",
          "name" : "四季如春",
          "pic" : "http://www.qiniu.com/images/xxx.png",
          "price" : 999,
          "score" : 9,
          "starName" : "五星"
        },
        "sort" : [
          9,
          999
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "address" : "洪武北路1号",
          "brand" : "四季",
          "business" : "新街口商圈",
          "city" : "南京市玄武区",
          "id" : 3,
          "location" : "33.35,131.36",
          "name" : "四季",
          "pic" : "http://www.bai.com/images/7.png",
          "price" : 489,
          "score" : 8,
          "starName" : "3星"
        },
        "sort" : [
          8,
          489
        ]
      }
    ]
  }
}

es的查询限制,是10000条,如果需要大于10000,实际生产环境会从业务上限制在10000条数据.

注意: 分页并不是es所擅长的

search after方式: 分页时候需要排序,原理是从上一次的排序值开始。查询下一页的数据。是官方推荐的方式。

scroll: 滚动读取,读的数据是快照方式,意味着读取的数据不是最新的,官方不推荐

3. 高亮显示

高亮: 就是在搜索结果中把搜索关键字突出显示。
原理: 将搜索结果中的关键字用<em>标签标记
		在页面中使用css样式实现高亮、

语法:

高亮查询的时候,肯定不是match_all

java 复制代码
get /hotel/_search
{
  "query":{
    "match": {
      "FIELD": "TEXT"
    }
  },
  "highlight":{
    "fields": {//指定要高亮的字段
      , "fields": {
        "pre_tags": "<em>",//前置标签
        "post_tags": "<em>"//后置标签
      }
    }
  }
}
注意: 搜索字段需要和高亮字段保持一致,否则不会高亮
java 复制代码
get /hotel/_search
{
  "query":{
    "match": {
      "brand": "如家"
    }
  },
  "highlight":{
    "fields": {
       "brand": {
        "pre_tags": "<em>",
        "post_tags": "<em>"
      }
    }
  }
}

此时,标签已经添加

java 复制代码
{
  "took" : 55,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.2039728,
        "_source" : {
          "address" : "柳州东路1号",
          "brand" : "如家",
          "business" : "弘阳商圈",
          "city" : "南京市浦口区",
          "id" : 1,
          "location" : "33.33,131.33",
          "name" : "如家",
          "pic" : "http://www.bai.com/images/rujia.png",
          "price" : 189,
          "score" : 7,
          "starName" : "二星"
        },
        "highlight" : {
          "brand" : [
            "<em>如家<em>"
          ]
        }
      }
    ]
  }
}
如果搜索词和高亮词需要不匹配,则需要进行配置,如下:
高亮显示不改变_source中的数据,是另外显示的
java 复制代码
get /hotel/_search
{
  "query":{
    "match": {
      "brand": "如家"
    }
  },
  "highlight":{
    "fields": {
       "name": {
        "require_field_match": "false"
      }
    }
  }
}
java 复制代码
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.2039728,
        "_source" : {
          "address" : "柳州东路1号",
          "brand" : "如家",
          "business" : "弘阳商圈",
          "city" : "南京市浦口区",
          "id" : 1,
          "location" : "33.33,131.33",
          "name" : "如家",
          "pic" : "http://www.bai.com/images/rujia.png",
          "price" : 189,
          "score" : 7,
          "starName" : "二星"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>"
          ]
        }
      }
    ]
  }
}
相关推荐
梦幻通灵2 小时前
ES分词环境实战
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客2 小时前
Elasticsearch 中的热点以及如何使用 AutoOps 解决它们
大数据·运维·elasticsearch·搜索引擎·全文检索
小黑屋说YYDS7 小时前
ElasticSearch7.x入门教程之索引概念和基础操作(三)
elasticsearch
Java 第一深情10 小时前
Linux上安装单机版ElasticSearch6.8.1
linux·elasticsearch·全文检索
KevinAha1 天前
Elasticsearch 6.8 分析器
elasticsearch
wuxingge1 天前
elasticsearch7.10.2集群部署带认证
运维·elasticsearch
Elastic 中国社区官方博客1 天前
Elasticsearch:如何部署文本嵌入模型并将其用于语义搜索
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
Dreams°1231 天前
【大数据测试 Elasticsearch 的标准--超详细篇】
大数据·elasticsearch·jenkins
鸠摩智首席音效师1 天前
如何在 Elasticsearch 中配置 SSL / TLS ?
elasticsearch·ssl
fishjam2 天前
[开源重构]Search(Elasticsearch/OpenSearch) Sync Tool
elasticsearch·重构·开源