1. 程式人生 > 其它 >HM-SpringCloud微服務系列6.2【搜尋結果處理】

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)升序排序

點選檢視程式碼
# 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. 需求描述:實現對酒店資料按照到你的位置座標的距離升序排序



點選檢視程式碼
# 找到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"}
  ]
}

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條:

    查詢TOP1000,如果es是單點模式,這並無太大影響。
  3. 但是elasticsearch將來一定是叢集,例如我叢集有5個節點,我要查詢TOP1000的資料,並不是每個節點查詢200條就可以了。
    因為節點A的TOP200,在另一個節點可能排到10000名以外了。
    因此要想獲取整個叢集的TOP1000,必須先查詢出每個節點的TOP1000,彙總結果後,重新排名,重新擷取TOP1000。

    那如果我要查詢9900~10000的資料呢?是不是要先查詢TOP10000呢?那每個節點都要查詢10000條?彙總到記憶體中?
    當查詢分頁深度較大時,彙總資料過多,對記憶體和CPU會產生非常大的壓力,因此elasticsearch會禁止from+ size 超過10000的請求。

2.2.2 深度分頁問題

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. 什麼是高亮顯示呢?
    搜尋結果中吧搜尋關鍵詞突出顯示
    我們在百度,京東搜尋時,關鍵字會變成紅色,比較醒目,這叫高亮顯示:
  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. 示例
點選檢視程式碼
# 高亮查詢(預設情況下,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>酒店(北京順義中心地鐵站店)"
          ]
        }
      }
    ]
  }
}

4 總結

  1. 查詢的DSL是一個大的JSON物件,包含下列屬性:
    • query:查詢條件
    • from和size:分頁條件
    • sort:排序條件
    • highlight:高亮條件
  2. 搜尋結果處理整體語法示例: