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>"
          ]
        }
      }
    ]
  }
}
相关推荐
hengzhepa2 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
bubble小拾10 小时前
ElasticSearch高级功能详解与读写性能调优
大数据·elasticsearch·搜索引擎
不能放弃治疗11 小时前
重生之我们在ES顶端相遇第 18 章 - Script 使用(进阶)
elasticsearch
hengzhepa11 小时前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
Elastic 中国社区官方博客13 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
慕雪华年14 小时前
【WSL】wsl中ubuntu无法通过useradd添加用户
linux·ubuntu·elasticsearch
Elastic 中国社区官方博客16 小时前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
alfiy17 小时前
Elasticsearch学习笔记(四) Elasticsearch集群安全配置一
笔记·学习·elasticsearch
alfiy18 小时前
Elasticsearch学习笔记(五)Elastic stack安全配置二
笔记·学习·elasticsearch
丶21361 天前
【大数据】Elasticsearch 实战应用总结
大数据·elasticsearch·搜索引擎