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>"
          ]
        }
      }
    ]
  }
}
相关推荐
it噩梦9 小时前
elasticsearch中使用fuzzy查询
elasticsearch
喝醉酒的小白11 小时前
Elasticsearch相关知识@1
大数据·elasticsearch·搜索引擎
小小工匠13 小时前
ElasticSearch - 深入解析 Elasticsearch Composite Aggregation 的分页与去重机制
elasticsearch·composite·after_key·桶聚合分页
风_流沙13 小时前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
TGB-Earnest14 小时前
【py脚本+logstash+es实现自动化检测工具】
大数据·elasticsearch·自动化
woshiabc1111 天前
windows安装Elasticsearch及增删改查操作
大数据·elasticsearch·搜索引擎
arnold661 天前
探索 ElasticSearch:性能优化之道
大数据·elasticsearch·性能优化
成长的小牛2331 天前
es使用knn向量检索中numCandidates和k应该如何配比更合适
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客1 天前
Elasticsearch:什么是查询语言?
大数据·数据库·elasticsearch·搜索引擎·oracle
启明真纳1 天前
elasticache备份
运维·elasticsearch·云原生·kubernetes