HM-SpringCloud微服务系列6.2【搜索结果处理】

1 排序

搜索的结果可以按照用户指定的方式去处理或展示。
elasticsearch默认是根据相关度算分(_score)来排序,但是也支持自定义方式对搜索结果排序。可以排序字段类型有:keyword类型、数值类型、地理坐标类型、日期类型等。

1.1 普通字段排序

  1. keyword、数值、日期类型排序的语法基本一致。
  2. 语法
    GET /indexName/_search
    {
      "query": {
    	"match_all": {}
      },
      "sort": [
    	{
    	  "FIELD": "desc"  // 排序字段、排序方式ASC、DESC
    	}
      ]
    }
    
  3. 排序条件是一个数组,也就是可以写多个排序条件。按照声明的顺序,当第一个条件相等时,再按照第二个条件排序,以此类推
  4. 需求描述:酒店数据按照用户评价(score)降序排序,评价相同的按照价格(price)升序排序
    image
    image
点击查看代码
# sort排序
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "score": {
        "order": "desc"
      },
      "price": {
        "order": "asc"
      }
    }
  ]
}

# 效果同上(推荐这种order简化写法)
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "score": "desc",
      "price": "asc"
    }
  ]
}
点击查看代码
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 201,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2060618247",
        "_score" : null,
        "_source" : {
          "address" : "粤海街道后海社区后海第二统建楼商业裙楼第二层B",
          "brand" : "汉庭",
          "business" : "海岸城/后海",
          "city" : "深圳",
          "id" : 2060618247,
          "location" : "22.507276, 113.931251",
          "name" : "汉庭酒店(深圳海岸城店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/TBoXdgEx5Yjc2HobeC3fPWWnSJi_w200_h200_c1_t0.jpg",
          "price" : 562,
          "score" : 49,
          "starName" : "二钻"
        },
        "sort" : [
          49,
          562
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1951709780",
        "_score" : null,
        "_source" : {
          "address" : "福海街道宝安大道 6259号",
          "brand" : "万怡",
          "business" : "深圳国际会展中心商圈",
          "city" : "深圳",
          "id" : 1951709780,
          "location" : "22.678611, 113.805695",
          "name" : "深圳同泰万怡酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3oUfktphxMAWq9hUxD9uqdjRdZGB_w200_h200_c1_t0.jpg",
          "price" : 617,
          "score" : 48,
          "starName" : "五钻"
        },
        "sort" : [
          48,
          617
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2056105938",
        "_score" : null,
        "_source" : {
          "address" : "新华东街289号2号楼",
          "brand" : "希尔顿",
          "business" : "果园环岛/通州区",
          "city" : "北京",
          "id" : 2056105938,
          "location" : "39.908805, 116.659748",
          "name" : "北京通州北投希尔顿酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/NGKdpec3tZJNUUNWJ5pd67Cp5AY_w200_h200_c1_t0.png",
          "price" : 1068,
          "score" : 48,
          "starName" : "五钻"
        },
        "sort" : [
          48,
          1068
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2048042240",
        "_score" : null,
        "_source" : {
          "address" : "高米店南里18号楼",
          "brand" : "希尔顿",
          "business" : "大兴北京新机场地区",
          "city" : "北京",
          "id" : 2048042240,
          "location" : "39.76875, 116.339199",
          "name" : "北京大兴希尔顿酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3B32F8zSU2CJCWzs1hoH2o4WcquR_w200_h200_c1_t0.jpg",
          "price" : 1283,
          "score" : 48,
          "starName" : "五钻"
        },
        "sort" : [
          48,
          1283
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1989806195",
        "_score" : null,
        "_source" : {
          "address" : "留仙大道4088号",
          "brand" : "喜来登",
          "business" : "大学城/西丽动物园",
          "city" : "深圳",
          "id" : 1989806195,
          "location" : "22.582918, 113.97219",
          "name" : "深圳博林天瑞喜来登酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/4Rx55fZoneUeKbE3TCRSPB6WQ6bw_w200_h200_c1_t0.jpg",
          "price" : 1369,
          "score" : 48,
          "starName" : "五钻"
        },
        "sort" : [
          48,
          1369
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2056126831",
        "_score" : null,
        "_source" : {
          "address" : "申长路630弄1-3 号",
          "brand" : "皇冠假日",
          "business" : "虹桥机场/国家会展中心",
          "city" : "上海",
          "id" : 2056126831,
          "location" : "31.19036, 121.31535",
          "name" : "上海虹桥金臣皇冠假日酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/PvFh4Vzc84xXhm5N41F6AqdAqyJ_w200_h200_c1_t0.jpg",
          "price" : 2488,
          "score" : 48,
          "starName" : "五钻"
        },
        "sort" : [
          48,
          2488
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1393017952",
        "_score" : null,
        "_source" : {
          "address" : "松岗镇河滨北路12号盛华大厦",
          "brand" : "汉庭",
          "business" : "松岗商业中心区",
          "city" : "深圳",
          "id" : 1393017952,
          "location" : "22.768912, 113.83325",
          "name" : "汉庭酒店(深圳宝安松岗地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/4NehRjdHyZDKxTjAxTYv27FHq8LJ_w200_h200_c1_t0.jpg",
          "price" : 166,
          "score" : 47,
          "starName" : "二钻"
        },
        "sort" : [
          47,
          166
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1406627919",
        "_score" : null,
        "_source" : {
          "address" : "海德一道88号中洲控股中心A座",
          "brand" : "万豪",
          "business" : "海岸城/后海",
          "city" : "深圳",
          "id" : 1406627919,
          "location" : "22.517293, 113.933785",
          "name" : "深圳中洲万豪酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3wsinQAcuWtCdmv1yxauVG2PSYpC_w200_h200_c1_t0.jpg",
          "price" : 204,
          "score" : 47,
          "starName" : "五钻"
        },
        "sort" : [
          47,
          204
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "684720",
        "_score" : null,
        "_source" : {
          "address" : "沿河南路1064号",
          "brand" : "和颐",
          "business" : "罗湖口岸/火车站",
          "city" : "深圳",
          "id" : 684720,
          "location" : "22.533753, 114.122491",
          "name" : "和颐酒店(深圳罗湖口岸火车地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2LFgB2iFawKKoGADwzhW6jpCSaJT_w200_h200_c1_t0.jpg",
          "price" : 208,
          "score" : 47,
          "starName" : "四钻"
        },
        "sort" : [
          47,
          208
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "343341695",
        "_score" : null,
        "_source" : {
          "address" : "东北旺西路8号中关村软件园一期9号楼",
          "brand" : "和颐",
          "business" : "上地产业园/西三旗",
          "city" : "北京",
          "id" : 343341695,
          "location" : "40.044663, 116.29607",
          "name" : "和颐酒店(北京中关村软件园店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3hSkPeWRQ3VK1heRQpHzJNMTanQz_w200_h200_c1_t0.jpg",
          "price" : 245,
          "score" : 47,
          "starName" : "四钻"
        },
        "sort" : [
          47,
          245
        ]
      }
    ]
  }
}

1.2 地理坐标排序

  1. 地理坐标排序略有不同。
  2. 语法
    GET /indexName/_search
    {
      "query": {
    	"match_all": {}
      },
      "sort": [
    	{
    	  "_geo_distance" : {
    		  "FIELD" : "纬度,经度", // 文档中geo_point类型的字段名、目标坐标点
    		  "order" : "asc", // 排序方式
    		  "unit" : "km" // 排序的距离单位
    	  }
    	}
      ]
    }
    
  3. 这个查询的含义是:
    • 指定一个坐标,作为目标点
    • 计算每一个文档中,指定字段(必须是geo_point类型)的坐标 到目标点的距离是多少
    • 根据距离排序
  4. 需求描述:实现对酒店数据按照到你的位置坐标的距离升序排序

image
image
image

点击查看代码
# 找到120.426976,36.069965周围的酒店,距离升序排序(数据库里只有上海的数据QAQ)
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 36.069965,
          "lon": 120.426976
        },
        "order": "asc",
        "unit": "km"
      }
    }
  ]
}
点击查看代码
{
  "took" : 673,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 201,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "339777429",
        "_score" : null,
        "_source" : {
          "address" : "菊园新区嘉唐公路66号",
          "brand" : "喜来登",
          "business" : "嘉定新城",
          "city" : "上海",
          "id" : 339777429,
          "location" : "31.394595, 121.245773",
          "name" : "上海嘉定喜来登酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2v2fKuo5bzhunSBC1n1E42cLTkZV_w200_h200_c1_t0.jpg",
          "price" : 1286,
          "score" : 44,
          "starName" : "五钻"
        },
        "sort" : [
          525.3560665791878
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "5872067",
        "_score" : null,
        "_source" : {
          "address" : "陈家镇揽海路799弄",
          "brand" : "凯悦",
          "business" : "崇明岛/长兴岛/横沙岛",
          "city" : "上海",
          "id" : 5872067,
          "location" : "31.466563, 121.799671",
          "name" : "崇明金茂凯悦酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/fsKrbnNsmSsYnNLmhh3ZvVjZ5cA_w200_h200_c1_t0.jpg",
          "price" : 1024,
          "score" : 46,
          "starName" : "五钻"
        },
        "sort" : [
          527.3482755519434
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1880614409",
        "_score" : null,
        "_source" : {
          "address" : "揽海路2888号",
          "brand" : "喜来登",
          "business" : "崇明岛/长兴岛/横沙岛",
          "city" : "上海",
          "id" : 1880614409,
          "location" : "31.462167, 121.823103",
          "name" : "上海崇明由由喜来登酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/21gDCGgRT3xFqCd3FxBh633j6Qsu_w200_h200_c1_t0.jpg",
          "price" : 2198,
          "score" : 45,
          "starName" : "五钻"
        },
        "sort" : [
          528.3477196486933
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "60916",
        "_score" : null,
        "_source" : {
          "address" : "沪宜公路3101号",
          "brand" : "万怡",
          "business" : "嘉定新城",
          "city" : "上海",
          "id" : 60916,
          "location" : "31.368523, 121.258567",
          "name" : "上海绿地万怡酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3VLwG9tTQQnp3M3MTeMTdx9nas9B_w200_h200_c1_t0.jpg",
          "price" : 328,
          "score" : 45,
          "starName" : "四钻"
        },
        "sort" : [
          528.3973669641412
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1942992995",
        "_score" : null,
        "_source" : {
          "address" : "裕民南路1366号",
          "brand" : "凯悦",
          "business" : "嘉定新城",
          "city" : "上海",
          "id" : 1942992995,
          "location" : "31.352298, 121.263314",
          "name" : "上海嘉定凯悦酒店",
          "pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/53/2D/Cii-U13edkqIfZhLAAJEW25WIF4AAGVxQIg38sAAkRz517_w200_h200_c1_t0.jpg",
          "price" : 758,
          "score" : 46,
          "starName" : "五钻"
        },
        "sort" : [
          530.2471858008814
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "609023",
        "_score" : null,
        "_source" : {
          "address" : "花山路1209号",
          "brand" : "如家",
          "business" : "浦东外高桥地区",
          "city" : "上海",
          "id" : 609023,
          "location" : "31.351148, 121.585606",
          "name" : "如家酒店·neo(上海外高桥保税区北地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3cJ6KTfms9cfEnME8WRkQQBXBkYm_w200_h200_c1_t0.jpg",
          "price" : 266,
          "score" : 45,
          "starName" : "二钻"
        },
        "sort" : [
          535.5278390326132
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "644417",
        "_score" : null,
        "_source" : {
          "address" : "自由贸易试验区基隆路28号(二号门内)",
          "brand" : "喜来登",
          "business" : "浦东外高桥地区",
          "city" : "上海",
          "id" : 644417,
          "location" : "31.350989, 121.588751",
          "name" : "上海外高桥喜来登酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/1Rrtg9n7PdMEivVDhsehbJBrEre_w200_h200_c1_t0.jpg",
          "price" : 2419,
          "score" : 46,
          "starName" : "五钻"
        },
        "sort" : [
          535.6033948065311
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "751035",
        "_score" : null,
        "_source" : {
          "address" : "花山路706号",
          "brand" : "7天酒店",
          "business" : "浦东外高桥地区",
          "city" : "上海",
          "id" : 751035,
          "location" : "31.348029, 121.576896",
          "name" : "7天连锁酒店(上海自贸区北门地铁站店)(原外高桥地铁北站店)",
          "pic" : "https://m.tuniucdn.com/fb2/t1/G1/M00/3A/21/Cii-U1kwxUCINXaHAAGmh7z6qRAAALUdwMKGREAAaaf928_w200_h200_c1_t0.jpg",
          "price" : 328,
          "score" : 39,
          "starName" : "二钻"
        },
        "sort" : [
          535.7076895060407
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "60359",
        "_score" : null,
        "_source" : {
          "address" : "杨高北路1000号",
          "brand" : "皇冠假日",
          "business" : "浦东外高桥地区",
          "city" : "上海",
          "id" : 60359,
          "location" : "31.338944, 121.590611",
          "name" : "上海外高桥皇冠假日酒店",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/VcKUM9zUSiVgDhFioc6mWQoX9ES_w200_h200_c1_t0.jpg",
          "price" : 3299,
          "score" : 46,
          "starName" : "五星级"
        },
        "sort" : [
          536.9514646752967
        ]
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "489756",
        "_score" : null,
        "_source" : {
          "address" : "文化北街4-16号",
          "brand" : "7天酒店",
          "business" : "平谷城区",
          "city" : "北京",
          "id" : 489756,
          "location" : "40.14308, 117.111554",
          "name" : "7天连锁酒店(北京平谷店)",
          "pic" : "https://m2.tuniucdn.com/filebroker/cdn/res/2e/b4/2eb4edb22ddb981307d8570beb1d746d_w200_h200_c1_t0.jpg",
          "price" : 544,
          "score" : 40,
          "starName" : "二钻"
        },
        "sort" : [
          537.7424802389243
        ]
      }
    ]
  }
}

2 分页

  1. elasticsearch 默认情况下只返回top10的数据。而如果要查询更多数据就需要修改分页参数了。elasticsearch中通过修改from、size参数来控制要返回的分页结果:
    • from:从第几个文档开始
    • size:总共查询几个文档
  2. 类似于mysql中的limit ?, ?

2.1 基本分页

分页的基本语法如下:

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0, // 分页开始的位置,默认为0
  "size": 5, // 期望获取的文档总数,默认为10
  "sort": [
    {"price": "asc"}
  ]
}

image

2.2 深度分页

2.2.1

  1. 现在,我要查询990~1000的数据,查询逻辑要这么写:
    GET /hotel/_search
    {
      "query": {
    	"match_all": {}
      },
      "from": 990, // 分页开始的位置,默认为0
      "size": 10, // 期望获取的文档总数
      "sort": [
    	{"price": "asc"}
      ]
    }
    
  2. 这里是查询990开始的数据,也就是 第990~第1000条 数据。
    不过,elasticsearch内部分页时,必须先查询 0~1000条,然后截取其中的990 ~ 1000的这10条:
    image
    查询TOP1000,如果es是单点模式,这并无太大影响。
  3. 但是elasticsearch将来一定是集群,例如我集群有5个节点,我要查询TOP1000的数据,并不是每个节点查询200条就可以了。
    因为节点A的TOP200,在另一个节点可能排到10000名以外了。
    因此要想获取整个集群的TOP1000,必须先查询出每个节点的TOP1000,汇总结果后,重新排名,重新截取TOP1000。
    image
    那如果我要查询9900~10000的数据呢?是不是要先查询TOP10000呢?那每个节点都要查询10000条?汇总到内存中?
    当查询分页深度较大时,汇总数据过多,对内存和CPU会产生非常大的压力,因此elasticsearch会禁止from+ size 超过10000的请求。

2.2.2 深度分页问题

image

2.2.3 深度分页解决方案

针对深度分页,ES提供了两种解决方案,官方文档

  • search after:分页时需要排序,原理是从上一次的排序值开始,查询下一页数据。官方推荐使用的方式。
  • scroll:原理将排序后的文档id形成快照,保存在内存。官方已经不推荐使用。

2.3 小结

分页查询的常见实现方案以及优缺点:

  • from + size
    • 优点:支持随机翻页
    • 缺点:深度分页问题,默认查询上限(from + size)是10000
    • 场景:百度、京东、谷歌、淘宝这样的随机翻页搜索
  • after search
    • 优点:没有查询上限(单次查询的size不超过10000)
    • 缺点:只能向后逐页查询,不支持随机翻页
    • 场景:没有随机翻页需求的搜索,例如手机向下滚动翻页
  • scroll
    • 优点:没有查询上限(单次查询的size不超过10000)
    • 缺点:会有额外内存消耗,并且搜索结果是非实时的
    • 场景:海量数据的获取和迁移。从ES7.1开始不推荐,建议用 after search方案。

3 高亮

3.1 高亮原理

  1. 什么是高亮显示呢?
    搜索结果中吧搜索关键词突出显示
    我们在百度,京东搜索时,关键字会变成红色,比较醒目,这叫高亮显示:
    image
  2. 高亮显示的实现分为两步:
    • 1)给文档中的所有关键字都添加一个标签,例如<em>标签
    • 2)页面给<em>标签编写CSS样式

3.2 实现高亮

  1. 语法
    GET /hotel/_search
    {
      "query": {
    	"match": {
    	  "FIELD": "TEXT" // 查询条件,高亮一定要使用全文检索查询
    	}
      },
      "highlight": {
    	"fields": { // 指定要高亮的字段
    	  "FIELD": {
    		"pre_tags": "<em>",  // 用来标记高亮字段的前置标签,默认就是em标签,可以不写
    		"post_tags": "</em>" // 用来标记高亮字段的后置标签
    	  }
    	}
      }
    }
    
  2. 注意:
    • 高亮是对关键字高亮,因此搜索条件必须带有关键字,而不能是范围这样的查询。
    • 默认情况下,高亮的字段,必须与搜索指定的字段一致,否则无法高亮
    • 如果要对非搜索字段高亮,则需要添加一个属性:required_field_match=false
  3. 示例
    image
点击查看代码
# 高亮查询(默认情况下,ES搜索字段必须与高亮字段一致,不然高亮不生效,可以通过require_field_match配置)
GET /hotel/_search
{
  "query": {
    "match": {
      "all": "如家"
    }
  },
  "highlight": {
    "fields": {
      "name": {
        "require_field_match": "false"
      }
    }
  }
}
点击查看代码
{
  "took" : 489,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 30,
      "relation" : "eq"
    },
    "max_score" : 2.7994788,
    "hits" : [
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "339952837",
        "_score" : 2.7994788,
        "_source" : {
          "address" : "良乡西路7号",
          "brand" : "如家",
          "business" : "房山风景区",
          "city" : "北京",
          "id" : 339952837,
          "location" : "39.73167, 116.132482",
          "name" : "如家酒店(北京良乡西路店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3Dpgf5RTTzrxpeN5y3RLnRVtxMEA_w200_h200_c1_t0.jpg",
          "price" : 159,
          "score" : 46,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(北京良乡西路店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2359697",
        "_score" : 2.7299232,
        "_source" : {
          "address" : "清河小营安宁庄东路18号20号楼",
          "brand" : "如家",
          "business" : "上地产业园/西三旗",
          "city" : "北京",
          "id" : 2359697,
          "location" : "40.041322, 116.333316",
          "name" : "如家酒店(北京上地安宁庄东路店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2wj2f8mo9WZQCmzm51cwkZ9zvyp8_w200_h200_c1_t0.jpg",
          "price" : 420,
          "score" : 46,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(北京上地安宁庄东路店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1455383931",
        "_score" : 2.7299232,
        "_source" : {
          "address" : "西乡河西金雅新苑34栋",
          "brand" : "如家",
          "business" : "宝安商业区",
          "city" : "深圳",
          "id" : 1455383931,
          "location" : "22.590272, 113.881933",
          "name" : "如家酒店(深圳宝安客运中心站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2w9cbbpzjjsyd2wRhFrnUpBMT8b4_w200_h200_c1_t0.jpg",
          "price" : 169,
          "score" : 45,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(深圳宝安客运中心站店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "728180",
        "_score" : 2.6637402,
        "_source" : {
          "address" : "西乡大道298-7号(富通城二期公交站旁)",
          "brand" : "如家",
          "business" : "宝安体育中心商圈",
          "city" : "深圳",
          "id" : 728180,
          "location" : "22.569693, 113.860186",
          "name" : "如家酒店(深圳宝安西乡地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/FHdugqgUgYLPMoC4u4rdTbAPrVF_w200_h200_c1_t0.jpg",
          "price" : 184,
          "score" : 43,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(深圳宝安西乡地铁站店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "2316304",
        "_score" : 2.6637402,
        "_source" : {
          "address" : "龙岗街道龙岗墟社区龙平东路62号",
          "brand" : "如家",
          "business" : "龙岗中心区/大运新城",
          "city" : "深圳",
          "id" : 2316304,
          "location" : "22.730828, 114.278337",
          "name" : "如家酒店(深圳双龙地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/4AzEoQ44awd1D2g95a6XDtJf3dkw_w200_h200_c1_t0.jpg",
          "price" : 135,
          "score" : 45,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(深圳双龙地铁站店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "1765008760",
        "_score" : 2.6637402,
        "_source" : {
          "address" : "西直门北大街49号",
          "brand" : "如家",
          "business" : "西直门/北京展览馆地区",
          "city" : "北京",
          "id" : 1765008760,
          "location" : "39.945106, 116.353827",
          "name" : "如家酒店(北京西直门北京北站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/4CLwbCE9346jYn7nFsJTQXuBExTJ_w200_h200_c1_t0.jpg",
          "price" : 356,
          "score" : 44,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(北京西直门北京北站店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "416121",
        "_score" : 2.6006904,
        "_source" : {
          "address" : "莲花池东路120-2号6层",
          "brand" : "如家",
          "business" : "北京西站/丽泽商务区",
          "city" : "北京",
          "id" : 416121,
          "location" : "39.896449, 116.317382",
          "name" : "如家酒店(北京西客站北广场店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/42DTRnKbiYoiGFVzrV9ZJUxNbvRo_w200_h200_c1_t0.jpg",
          "price" : 275,
          "score" : 43,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(北京西客站北广场店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "441836",
        "_score" : 2.6006904,
        "_source" : {
          "address" : "西坝河东里36号",
          "brand" : "如家",
          "business" : "国展中心地区",
          "city" : "北京",
          "id" : 441836,
          "location" : "39.966238, 116.450142",
          "name" : "如家酒店(北京国展三元桥店)",
          "pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/39/Cii-TF3eRTGITp1UAAYIilRD7skAAGLngIuAnQABgii479_w200_h200_c1_t0.png",
          "price" : 458,
          "score" : 47,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(北京国展三元桥店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "517915",
        "_score" : 2.6006904,
        "_source" : {
          "address" : "布吉路1036号",
          "brand" : "如家",
          "business" : "田贝/水贝珠宝城",
          "city" : "深圳",
          "id" : 517915,
          "location" : "22.583191, 114.118499",
          "name" : "如家酒店·neo(深圳草埔地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/228vhBCQmFRFWQBYX1cgoFQb6x58_w200_h200_c1_t0.jpg",
          "price" : 159,
          "score" : 44,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店·neo(深圳草埔地铁站店)"
          ]
        }
      },
      {
        "_index" : "hotel",
        "_type" : "_doc",
        "_id" : "197492479",
        "_score" : 2.6006904,
        "_source" : {
          "address" : "光明南大街14号",
          "brand" : "如家",
          "business" : "顺义温泉休闲区",
          "city" : "北京",
          "id" : 197492479,
          "location" : "40.124783, 116.65751",
          "name" : "如家酒店(北京顺义中心地铁站店)",
          "pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2hNBSjmMTk6JQ2o8ixr5s3ioevhB_w200_h200_c1_t0.jpg",
          "price" : 306,
          "score" : 45,
          "starName" : "二钻"
        },
        "highlight" : {
          "name" : [
            "<em>如家</em>酒店(北京顺义中心地铁站店)"
          ]
        }
      }
    ]
  }
}

image

4 总结

  1. 查询的DSL是一个大的JSON对象,包含下列属性:
    • query:查询条件
    • from和size:分页条件
    • sort:排序条件
    • highlight:高亮条件
  2. 搜索结果处理整体语法示例:
    image
posted @   yub4by  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示