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>"
          ]
        }
      }
    ]
  }
}
相关推荐
筱源源3 小时前
Elasticsearch-linux环境部署
linux·elasticsearch
Elastic 中国社区官方博客13 小时前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理
Shenqi Lotus20 小时前
ELK-ELK基本概念_ElasticSearch的配置
elk·elasticsearch
yeye1989122421 小时前
10-Query & Filtering 与多字符串多字段查询
elasticsearch
Narutolxy1 天前
精准优化Elasticsearch:磁盘空间管理与性能提升技巧20241106
大数据·elasticsearch·jenkins
谢小涛2 天前
ES管理工具Cerebro 0.8.5 Windows版本安装及启动
elasticsearch·es·cerebro
LKID体2 天前
Elasticsearch核心概念
大数据·elasticsearch·搜索引擎
晨欣2 天前
Elasticsearch里的索引index是什么概念?(ChatGPT回答)
大数据·elasticsearch·jenkins
许苑向上2 天前
最详细【Elasticsearch】Elasticsearch Java API + Spring Boot集成 实战入门(基础篇)
java·数据库·spring boot·elasticsearch
笔墨登场说说2 天前
git sonar maven 配置
大数据·elasticsearch·搜索引擎