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 小时前
logstash中的input插件(http插件,graphite插件)
网络·网络协议·http·elasticsearch·云原生
silianpan4 小时前
文档检索服务平台
elasticsearch·搜索引擎·开源
(; ̄ェ ̄)。4 小时前
在nodejs中使用ElasticSearch(二)核心概念,应用
大数据·elasticsearch·搜索引擎
boy快快长大5 小时前
【Elasticsearch】同一台服务器部署集群
服务器·elasticsearch·jenkins
一个儒雅随和的男子5 小时前
Elasticsearch除了用作查找以外,还能可以做什么?
大数据·elasticsearch·搜索引擎
跳跳的向阳花6 小时前
06、ElasticStack系列,第六章:elasticsearch设置密码
大数据·elasticsearch·jenkins
Elastic 中国社区官方博客16 小时前
Elasticsearch Open Inference API 增加了对 Jina AI 嵌入和 Rerank 模型的支持
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·jina
隔壁老王15616 小时前
mysql实时同步到es
数据库·mysql·elasticsearch
SunnyRivers18 小时前
关于ES中text类型时间字段范围查询的结构化解决方案
elasticsearch·时间·text·范围查询
API_technology18 小时前
电商搜索API的Elasticsearch优化策略
大数据·elasticsearch·搜索引擎