HM-SpringCloud微服务系列6.1【DSL查询文档】
1 DSL查询分类
1.1 DSL Query的分类
Elasticsearch提供了基于JSON的DSL(Domain Specific Language)来定义查询。常见的查询类型包括:
- 查询所有:查询出所有数据,一般测试用。例如:match_all
- 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:
- match_query
- multi_match_query
- 精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。例如:
- ids
- range
- term
- 地理(geo)查询:根据经纬度查询。例如:
- geo_distance
- geo_bounding_box
- 复合(compound)查询:复合查询可以将上述各种查询条件组合起来,合并查询条件。例如:
- bool
- function_score
1.2 DSL Query基本语法
我们以查询所有为例,其中:
- 查询类型为match_all
- 没有查询条件
查询的语法基本一致,其它查询无非就是查询类型、查询条件的变化。
2 全文检索查询
2.1 使用场景
- 全文检索查询的基本流程如下:
- 对用户搜索的内容做分词,得到词条
- 根据词条去倒排索引库中匹配,得到文档id
- 根据文档id找到文档,返回给用户
- 比较常用的场景包括:
- 商城的输入框搜索
- 百度输入框搜索
- 例如
- 因为是拿着词条去匹配,因此参与搜索的字段也必须是可分词的text类型的字段。
2.2 基本语法
- 常见的全文检索查询包括:
- match查询:单字段查询
- multi_match查询:多字段查询,任意一个字段符合条件就算符合查询条件
- 语法
2.3 示例
- match查询示例
点击查看代码
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 4.4094586,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "60487",
"_score" : 4.4094586,
"_source" : {
"address" : "黄浦路199号",
"brand" : "君悦",
"business" : "外滩地区",
"city" : "上海",
"id" : 60487,
"location" : "31.245409, 121.492969",
"name" : "上海外滩茂悦大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2Swp2h1fdj9zCUKsk63BQvVgKLTo_w200_h200_c1_t0.jpg",
"price" : 689,
"score" : 44,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "434082",
"_score" : 3.5042057,
"_source" : {
"address" : "复兴东路260号",
"brand" : "如家",
"business" : "豫园地区",
"city" : "上海",
"id" : 434082,
"location" : "31.220706, 121.498769",
"name" : "如家酒店·neo(上海外滩城隍庙小南门地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/B6/Cii-U13eXLGIdHFzAAIG-5cEwDEAAGRfQNNIV0AAgcT627_w200_h200_c1_t0.jpg",
"price" : 392,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "432335",
"_score" : 3.1362786,
"_source" : {
"address" : "唐山路145号",
"brand" : "7天酒店",
"business" : "北外滩地区",
"city" : "上海",
"id" : 432335,
"location" : "31.252585, 121.498753",
"name" : "7天连锁酒店(上海北外滩国际客运中心地铁站店)",
"pic" : "https://m2.tuniucdn.com/filebroker/cdn/res/c1/ba/c1baf64418437c56617f89840c6411ef_w200_h200_c1_t0.jpg",
"price" : 249,
"score" : 35,
"starName" : "二钻"
}
}
]
}
}
点击查看代码
{
"took" : 248,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 32,
"relation" : "eq"
},
"max_score" : 5.82968,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "434082",
"_score" : 5.82968,
"_source" : {
"address" : "复兴东路260号",
"brand" : "如家",
"business" : "豫园地区",
"city" : "上海",
"id" : 434082,
"location" : "31.220706, 121.498769",
"name" : "如家酒店·neo(上海外滩城隍庙小南门地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/B6/Cii-U13eXLGIdHFzAAIG-5cEwDEAAGRfQNNIV0AAgcT627_w200_h200_c1_t0.jpg",
"price" : 392,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "60487",
"_score" : 4.4094586,
"_source" : {
"address" : "黄浦路199号",
"brand" : "君悦",
"business" : "外滩地区",
"city" : "上海",
"id" : 60487,
"location" : "31.245409, 121.492969",
"name" : "上海外滩茂悦大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2Swp2h1fdj9zCUKsk63BQvVgKLTo_w200_h200_c1_t0.jpg",
"price" : 689,
"score" : 44,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "432335",
"_score" : 3.1362786,
"_source" : {
"address" : "唐山路145号",
"brand" : "7天酒店",
"business" : "北外滩地区",
"city" : "上海",
"id" : 432335,
"location" : "31.252585, 121.498753",
"name" : "7天连锁酒店(上海北外滩国际客运中心地铁站店)",
"pic" : "https://m2.tuniucdn.com/filebroker/cdn/res/c1/ba/c1baf64418437c56617f89840c6411ef_w200_h200_c1_t0.jpg",
"price" : 249,
"score" : 35,
"starName" : "二钻"
}
},
{
"_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" : "二钻"
}
},
{
"_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" : "二钻"
}
},
{
"_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" : "二钻"
}
},
{
"_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" : "二钻"
}
},
{
"_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" : "二钻"
}
},
{
"_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" : "二钻"
}
},
{
"_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" : "二钻"
}
}
]
}
}
- multi_match查询示例
点击查看代码
{
"took" : 163,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 32,
"relation" : "eq"
},
"max_score" : 4.6828966,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "434082",
"_score" : 4.6828966,
"_source" : {
"address" : "复兴东路260号",
"brand" : "如家",
"business" : "豫园地区",
"city" : "上海",
"id" : 434082,
"location" : "31.220706, 121.498769",
"name" : "如家酒店·neo(上海外滩城隍庙小南门地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/B6/Cii-U13eXLGIdHFzAAIG-5cEwDEAAGRfQNNIV0AAgcT627_w200_h200_c1_t0.jpg",
"price" : 392,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "60487",
"_score" : 4.5087996,
"_source" : {
"address" : "黄浦路199号",
"brand" : "君悦",
"business" : "外滩地区",
"city" : "上海",
"id" : 60487,
"location" : "31.245409, 121.492969",
"name" : "上海外滩茂悦大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2Swp2h1fdj9zCUKsk63BQvVgKLTo_w200_h200_c1_t0.jpg",
"price" : 689,
"score" : 44,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "432335",
"_score" : 2.9920945,
"_source" : {
"address" : "唐山路145号",
"brand" : "7天酒店",
"business" : "北外滩地区",
"city" : "上海",
"id" : 432335,
"location" : "31.252585, 121.498753",
"name" : "7天连锁酒店(上海北外滩国际客运中心地铁站店)",
"pic" : "https://m2.tuniucdn.com/filebroker/cdn/res/c1/ba/c1baf64418437c56617f89840c6411ef_w200_h200_c1_t0.jpg",
"price" : 249,
"score" : 35,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "339952837",
"_score" : 2.0731194,
"_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" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "2359697",
"_score" : 1.9731021,
"_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" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "1455383931",
"_score" : 1.9731021,
"_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" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "728180",
"_score" : 1.8822913,
"_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" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "2316304",
"_score" : 1.8822913,
"_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" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "1765008760",
"_score" : 1.8822913,
"_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" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "416121",
"_score" : 1.7994716,
"_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" : "二钻"
}
}
]
}
}
- 可以看到,两种查询结果是一样的,为什么?
- 因为我们将brand、name、business值都利用copy_to复制到了all字段中。因此你根据三个字段搜索,和根据all字段搜索效果当然一样了。
- 但是,搜索字段越多,对查询性能影响越大,因此建议采用copy_to,然后单字段查询的方式。
2.4 小结
match和multi_match的区别是什么?
- match:根据一个字段查询
- multi_match:根据多个字段查询,参与查询字段越多,查询性能越差
3 精准查询
精确查询一般是查找keyword、数值、日期、boolean等类型字段。所以不会对搜索条件分词。常见的有:
- term:根据词条精确值查询
- range:根据值的范围查询
3.1 term查询
- 因为精确查询的字段搜是不分词的字段,因此查询的条件也必须是不分词的词条。查询时,用户输入的内容跟自动值完全匹配时才认为符合条件。如果用户输入的内容过多,反而搜索不到数据。
- 语法
// term查询
GET /indexName/_search
{
"query": {
"term": {
"FIELD": {
"value": "VALUE"
}
}
}
}
- 示例:当我搜索的是精确词条时,能正确查询出结果:
点击查看代码
{
"took" : 544,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 83,
"relation" : "eq"
},
"max_score" : 0.88043153,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "36934",
"_score" : 0.88043153,
"_source" : {
"address" : "静安交通路40号",
"brand" : "7天酒店",
"business" : "四川北路商业区",
"city" : "上海",
"id" : 36934,
"location" : "31.251433, 121.47522",
"name" : "7天连锁酒店(上海宝山路地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G1/M00/3E/40/Cii9EVkyLrKIXo1vAAHgrxo_pUcAALcKQLD688AAeDH564_w200_h200_c1_t0.jpg",
"price" : 336,
"score" : 37,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "38609",
"_score" : 0.88043153,
"_source" : {
"address" : "广灵二路126号",
"brand" : "速8",
"business" : "四川北路商业区",
"city" : "上海",
"id" : 38609,
"location" : "31.282444, 121.479385",
"name" : "速8酒店(上海赤峰路店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G2/M00/DF/96/Cii-TFkx0ImIQZeiAAITil0LM7cAALCYwKXHQ4AAhOi377_w200_h200_c1_t0.jpg",
"price" : 249,
"score" : 35,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "38665",
"_score" : 0.88043153,
"_source" : {
"address" : "兰田路38号",
"brand" : "速8",
"business" : "长风公园地区",
"city" : "上海",
"id" : 38665,
"location" : "31.244288, 121.422419",
"name" : "速8酒店上海中山北路兰田路店",
"pic" : "https://m.tuniucdn.com/fb2/t1/G2/M00/EF/86/Cii-Tlk2mV2IMZ-_AAEucgG3dx4AALaawEjiycAAS6K083_w200_h200_c1_t0.jpg",
"price" : 226,
"score" : 35,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "38812",
"_score" : 0.88043153,
"_source" : {
"address" : "徐汇龙华西路315弄58号",
"brand" : "7天酒店",
"business" : "八万人体育场地区",
"city" : "上海",
"id" : 38812,
"location" : "31.174377, 121.442875",
"name" : "7天连锁酒店(上海漕溪路地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G2/M00/E0/0E/Cii-TlkyIr2IEWNoAAHQYv7i5CkAALD-QP2iJwAAdB6245_w200_h200_c1_t0.jpg",
"price" : 298,
"score" : 37,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "39106",
"_score" : 0.88043153,
"_source" : {
"address" : "闵行莘庄镇七莘路299号",
"brand" : "7天酒店",
"business" : "莘庄工业区",
"city" : "上海",
"id" : 39106,
"location" : "31.113812, 121.375869",
"name" : "7天连锁酒店(上海莘庄地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G2/M00/D8/11/Cii-T1ku2zGIGR7uAAF1NYY9clwAAKxZAHO8HgAAXVN368_w200_h200_c1_t0.jpg",
"price" : 348,
"score" : 41,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "39141",
"_score" : 0.88043153,
"_source" : {
"address" : "杨浦国权路315号",
"brand" : "7天酒店",
"business" : "江湾、五角场商业区",
"city" : "上海",
"id" : 39141,
"location" : "31.290057, 121.508804",
"name" : "7天连锁酒店(上海五角场复旦同济大学店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G2/M00/C7/E3/Cii-T1knFXCIJzNYAAFB8-uFNAEAAKYkQPcw1IAAUIL012_w200_h200_c1_t0.jpg",
"price" : 349,
"score" : 38,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "45845",
"_score" : 0.88043153,
"_source" : {
"address" : "虹桥路100号",
"brand" : "万怡",
"business" : "徐家汇地区",
"city" : "上海",
"id" : 45845,
"location" : "31.192714, 121.434717",
"name" : "上海西藏大厦万怡酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/48GNb9GZpJDCejVAcQHYWwYyU8T_w200_h200_c1_t0.jpg",
"price" : 589,
"score" : 45,
"starName" : "四钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "45870",
"_score" : 0.88043153,
"_source" : {
"address" : "新元南路555号",
"brand" : "豪生",
"business" : "滴水湖临港地区",
"city" : "上海",
"id" : 45870,
"location" : "30.871729, 121.81959",
"name" : "上海临港豪生大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2F5HoQvBgypoDUE46752ppnQaTqs_w200_h200_c1_t0.jpg",
"price" : 896,
"score" : 45,
"starName" : "四星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "46829",
"_score" : 0.88043153,
"_source" : {
"address" : "恒丰路338号",
"brand" : "万怡",
"business" : "上海火车站地区",
"city" : "上海",
"id" : 46829,
"location" : "31.242977, 121.455864",
"name" : "上海浦西万怡酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/x87VCoyaR8cTuYFZmKHe8VC6Wk1_w200_h200_c1_t0.jpg",
"price" : 726,
"score" : 46,
"starName" : "四钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "47066",
"_score" : 0.88043153,
"_source" : {
"address" : "施新路958号",
"brand" : "华美达",
"business" : "浦东机场核心区",
"city" : "上海",
"id" : 47066,
"location" : "31.147989, 121.759199",
"name" : "上海浦东东站华美达酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2pNujAVaQbXACzkHp8bQMm6zqwhp_w200_h200_c1_t0.jpg",
"price" : 408,
"score" : 46,
"starName" : "四钻"
}
}
]
}
}
点击查看代码
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 62,
"relation" : "eq"
},
"max_score" : 1.1771091,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "309208",
"_score" : 1.1771091,
"_source" : {
"address" : "王府井东街8号",
"brand" : "希尔顿",
"business" : "天安门/王府井地区",
"city" : "北京",
"id" : 309208,
"location" : "39.914539, 116.413392",
"name" : "北京王府井希尔顿酒店",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/10/Cii-TF3ePt2IX9UEAALb6VYBSmoAAGKMgGsuW8AAtwB147_w200_h200_c1_t0.jpg",
"price" : 1679,
"score" : 46,
"starName" : "五钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "394559",
"_score" : 1.1771091,
"_source" : {
"address" : "长安街1号东方广场",
"brand" : "君悦",
"business" : "天安门/王府井地区",
"city" : "北京",
"id" : 394559,
"location" : "39.909635, 116.414621",
"name" : "北京东方君悦大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3mFqcNSh7eEo9yc3Rw2P5HDNTdDe_w200_h200_c1_t0.jpg",
"price" : 686,
"score" : 45,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "394617",
"_score" : 1.1771091,
"_source" : {
"address" : "建国门内大街甲18号",
"brand" : "豪生",
"business" : "北京站/建国门地区",
"city" : "北京",
"id" : 394617,
"location" : "39.905768, 116.428153",
"name" : "北京宝辰饭店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/NEYa6EfDHuhhb19Ct85WBbkKHZU_w200_h200_c1_t0.jpg",
"price" : 418,
"score" : 44,
"starName" : "四星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "394796",
"_score" : 1.1771091,
"_source" : {
"address" : "东北三环西坝河太阳宫桥东北角云南大厦",
"brand" : "皇冠假日",
"business" : "国展中心地区",
"city" : "北京",
"id" : 394796,
"location" : "39.972409, 116.434698",
"name" : "北京新云南皇冠假日酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/dfP8K782eTsohQWSRdkd7St9LA2_w200_h200_c1_t0.jpg",
"price" : 485,
"score" : 46,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "395434",
"_score" : 1.1771091,
"_source" : {
"address" : "东三环北路东方路1号",
"brand" : "希尔顿",
"business" : "燕莎/朝阳公园商业区",
"city" : "北京",
"id" : 395434,
"location" : "39.952703, 116.462387",
"name" : "北京希尔顿酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3fwNbKGhk6XCrkdVyxwhC5uGpLVy_w200_h200_c1_t0.jpg",
"price" : 350,
"score" : 45,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "395702",
"_score" : 1.1771091,
"_source" : {
"address" : "首都机场3号航站楼三经路1号",
"brand" : "希尔顿",
"business" : "首都机场/新国展地区",
"city" : "北京",
"id" : 395702,
"location" : "40.048969, 116.619566",
"name" : "北京首都机场希尔顿酒店",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/10/Cii-U13ePtuIMRSjAAFZ58NGQrMAAGKMgADZ1QAAVn_167_w200_h200_c1_t0.jpg",
"price" : 222,
"score" : 46,
"starName" : "五钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "395787",
"_score" : 1.1771091,
"_source" : {
"address" : "霄云路甲26号",
"brand" : "万豪",
"business" : "燕莎/朝阳公园商业区",
"city" : "北京",
"id" : 395787,
"location" : "39.959861, 116.467363",
"name" : "北京海航大厦万豪酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/3zFiWi2C9SmbcQwCZgJFQC9ahvs5_w200_h200_c1_t0.jpg",
"price" : 1302,
"score" : 46,
"starName" : "五钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "395799",
"_score" : 1.1771091,
"_source" : {
"address" : "王府井大街48号",
"brand" : "皇冠假日",
"business" : "天安门/王府井地区",
"city" : "北京",
"id" : 395799,
"location" : "39.918994, 116.411277",
"name" : "北京国际艺苑皇冠假日酒店",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/10/Cii-U13ePvyIahjPAAMykV278aEAAGKOQO9e4UAAzKp283_w200_h200_c1_t0.jpg",
"price" : 636,
"score" : 44,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "395815",
"_score" : 1.1771091,
"_source" : {
"address" : "天竺镇府前一街13号",
"brand" : "华美达",
"business" : "首都机场/新国展地区",
"city" : "北京",
"id" : 395815,
"location" : "40.062832, 116.580678",
"name" : "北京明豪华美达酒店",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/13/Cii-U13eP2mIKCwvAAODTZXT-fAAAGKVAA9taIAA4Nl245_w200_h200_c1_t0.jpg",
"price" : 558,
"score" : 46,
"starName" : "四钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "396189",
"_score" : 1.1771091,
"_source" : {
"address" : "三丰北里3号",
"brand" : "皇冠假日",
"business" : "三里屯/工体/东直门地区",
"city" : "北京",
"id" : 396189,
"location" : "39.92129, 116.43847",
"name" : "北京朝阳悠唐皇冠假日酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/tT6ipLain1ZovR5gnQ7tJ4KKym5_w200_h200_c1_t0.jpg",
"price" : 944,
"score" : 46,
"starName" : "五钻"
}
}
]
}
}
- 示例:但是,当我搜索的内容不是词条,而是多个词语形成的短语时,反而搜索不到:
3.2 range查询
- 范围查询,一般应用在对数值类型做范围过滤的时候。比如做价格范围过滤。
- 语法
// range查询
GET /indexName/_search
{
"query": {
"range": {
"FIELD": {
"gte": 10, // 这里的gte代表大于等于,gt则代表大于
"lte": 20 // lte代表小于等于,lt则代表小于
}
}
}
}
- 示例
3.3 小结
精确查询常见的有哪些?
- term查询:根据词条精确匹配,一般搜索keyword类型、数值类型、布尔类型、日期类型字段
- range查询:根据数值范围查询,可以是数值、日期的范围
4 地理坐标查询
4.1 使用场景
- 所谓的地理坐标查询,其实就是根据经纬度查询,官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-queries.html
- 常见的使用场景包括:
- 携程:搜索我附近的酒店
- 滴滴:搜索我附近的出租车
- 微信:搜索我附近的人
4.2 矩形范围查询
查询时,需要指定矩形的左上、右下两个点的坐标,然后画出一个矩形,落在该矩形内的都是符合条件的点。
// geo_bounding_box查询
GET /indexName/_search
{
"query": {
"geo_bounding_box": {
"FIELD": {
"top_left": { // 左上点
"lat": 31.1,
"lon": 121.5
},
"bottom_right": { // 右下点
"lat": 30.9,
"lon": 121.7
}
}
}
}
}
4.3 附近(距离)查询
在地图上找一个点作为圆心,以指定距离为半径,画一个圆,落在圆内的坐标都算符合条件。
// geo_distance 查询
GET /indexName/_search
{
"query": {
"geo_distance": {
"distance": "15km", // 半径
"FIELD": "31.21,121.5" // 圆心
}
}
}
先搜索陆家嘴附近15km的酒店:
发现共有47家酒店。然后把半径缩短到5公里:
可以发现,搜索到的酒店数量减少到了13家。
5 复合查询
5.1 复合查询分类&应用场景
复合(compound)查询:复合查询可以将其它简单查询组合起来,实现更复杂的搜索逻辑。常见的有两种:
- fuction score:算分函数查询,可以控制文档相关性算分,控制文档排名。例如百度竞价
- bool query:布尔查询,利用逻辑关系组合多个其它的查询,实现复杂搜索
5.2 相关性算分
-
当我们利用match查询时,文档结果会根据与搜索词条的关联度打分(_score),返回结果时按照分值降序排列。
例如,我们搜索 "虹桥如家",结果如下:[ { "_score" : 17.850193, "_source" : { "name" : "虹桥如家酒店真不错", } }, { "_score" : 12.259849, "_source" : { "name" : "外滩如家酒店真不错", } }, { "_score" : 11.91091, "_source" : { "name" : "迪士尼如家酒店真不错", } } ]
-
在elasticsearch中,早期使用的打分算法是TF-IDF算法,公式如下:
-
在后来的5.1版本升级中,elasticsearch将算法改进为BM25算法,公式如下:
-
TF-IDF算法有一各缺陷,就是词条频率越高,文档得分也会越高,单个词条对文档影响较大。而BM25则会让单个词条的算分有一个上限,曲线更加平滑:
-
小结
elasticsearch会根据词条和文档的相关度做打分,算法有两种:TF-IDF算法和BM25算法(elasticsearch5.1版本后采用的算法)
5.3 算分函数查询
根据相关度打分是比较合理的需求,但合理的不一定是产品经理需要的。
以百度为例,你搜索的结果中,并不是相关度越高排名越靠前,而是谁掏的钱多排名就越靠前。如图:
要想认为控制相关性算分,就需要利用elasticsearch中的function score 查询了。
5.3.1 语法
- function score 查询中包含四部分内容:
- 原始查询条件:query部分,基于这个条件搜索文档,并且基于BM25算法给文档打分,原始算分(query score)
- 过滤条件:filter部分,符合该条件的文档才会重新算分
- 算分函数:符合filter条件的文档要根据这个函数做运算,得到的函数算分(function score),有四种函数
- weight:函数结果是常量
- field_value_factor:以文档中的某个字段值作为函数结果
- random_score:以随机数作为函数结果
- script_score:自定义算分函数算法
- 运算模式:算分函数的结果、原始查询的相关性算分,两者之间的运算方式,包括:
- multiply:相乘
- replace:用function score替换query score
- 其它,例如:sum、avg、max、min
- function score的运行流程如下:
- 1)根据原始条件查询搜索文档,并且计算相关性算分,称为原始算分(query score)
- 2)根据过滤条件,过滤文档
- 3)符合过滤条件的文档,基于算分函数运算,得到函数算分(function score)
- 4)将原始算分(query score)和函数算分(function score)基于运算模式做运算,得到最终结果,作为相关性算分。
- 因此,其中的关键点是:
- 过滤条件:决定哪些文档的算分被修改
- 算分函数:决定函数算分的算法
- 运算模式:决定最终算分结果
5.3.2 案例
- 翻译一下这个需求,转换为之前说的四个要点:
- 原始条件:不确定,可以任意变化
- 过滤条件:brand = "如家"
- 算分函数:可以简单粗暴,直接给固定的算分结果,weight
- 运算模式:比如求和
- 在未添加算分函数时,如家得分如下:
点击查看代码
{
"took" : 57,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 4.4094586,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "60487",
"_score" : 4.4094586,
"_source" : {
"address" : "黄浦路199号",
"brand" : "君悦",
"business" : "外滩地区",
"city" : "上海",
"id" : 60487,
"location" : "31.245409, 121.492969",
"name" : "上海外滩茂悦大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2Swp2h1fdj9zCUKsk63BQvVgKLTo_w200_h200_c1_t0.jpg",
"price" : 689,
"score" : 44,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "434082",
"_score" : 3.5042057,
"_source" : {
"address" : "复兴东路260号",
"brand" : "如家",
"business" : "豫园地区",
"city" : "上海",
"id" : 434082,
"location" : "31.220706, 121.498769",
"name" : "如家酒店·neo(上海外滩城隍庙小南门地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/B6/Cii-U13eXLGIdHFzAAIG-5cEwDEAAGRfQNNIV0AAgcT627_w200_h200_c1_t0.jpg",
"price" : 392,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "432335",
"_score" : 3.1362786,
"_source" : {
"address" : "唐山路145号",
"brand" : "7天酒店",
"business" : "北外滩地区",
"city" : "上海",
"id" : 432335,
"location" : "31.252585, 121.498753",
"name" : "7天连锁酒店(上海北外滩国际客运中心地铁站店)",
"pic" : "https://m2.tuniucdn.com/filebroker/cdn/res/c1/ba/c1baf64418437c56617f89840c6411ef_w200_h200_c1_t0.jpg",
"price" : 249,
"score" : 35,
"starName" : "二钻"
}
}
]
}
}
- 添加了算分函数后,如家得分就提升了:
点击查看代码
{
"took" : 58,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 35.042057,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "434082",
"_score" : 35.042057,
"_source" : {
"address" : "复兴东路260号",
"brand" : "如家",
"business" : "豫园地区",
"city" : "上海",
"id" : 434082,
"location" : "31.220706, 121.498769",
"name" : "如家酒店·neo(上海外滩城隍庙小南门地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/B6/Cii-U13eXLGIdHFzAAIG-5cEwDEAAGRfQNNIV0AAgcT627_w200_h200_c1_t0.jpg",
"price" : 392,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "60487",
"_score" : 4.4094586,
"_source" : {
"address" : "黄浦路199号",
"brand" : "君悦",
"business" : "外滩地区",
"city" : "上海",
"id" : 60487,
"location" : "31.245409, 121.492969",
"name" : "上海外滩茂悦大酒店",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2Swp2h1fdj9zCUKsk63BQvVgKLTo_w200_h200_c1_t0.jpg",
"price" : 689,
"score" : 44,
"starName" : "五星级"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "432335",
"_score" : 3.1362786,
"_source" : {
"address" : "唐山路145号",
"brand" : "7天酒店",
"business" : "北外滩地区",
"city" : "上海",
"id" : 432335,
"location" : "31.252585, 121.498753",
"name" : "7天连锁酒店(上海北外滩国际客运中心地铁站店)",
"pic" : "https://m2.tuniucdn.com/filebroker/cdn/res/c1/ba/c1baf64418437c56617f89840c6411ef_w200_h200_c1_t0.jpg",
"price" : 249,
"score" : 35,
"starName" : "二钻"
}
}
]
}
}
- 课件
5.3.3 小结
function score query定义的三要素是什么?
- 过滤条件:哪些文档要加分
- 算分函数:如何计算function score
- 加权方式:function score 与 query score如何运算
5.4 布尔查询
- 布尔查询是一个或多个查询子句的组合,每一个子句就是一个子查询。子查询的组合方式有:
- must:必须匹配每个子查询,类似“与”
- should:选择性匹配子查询,类似“或”
- must_not:必须不匹配,不参与算分,类似“非”
- filter:必须匹配,不参与算分
- 比如在搜索酒店时,除了关键字搜索外,我们还可能根据品牌、价格、城市等字段做过滤:
- 每一个不同的字段,其查询的条件、方式都不一样,必须是多个不同的查询,而要组合这些查询,就必须用bool查询了。
- 需要注意的是,搜索时,参与打分的字段越多,查询的性能也越差。因此这种多条件查询时,建议这样做:
- 搜索框的关键字搜索,是全文检索查询,使用must查询,参与算分
- 其它过滤条件,采用filter查询。不参与算分
5.4.1 语法
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{"term": {"city": "上海" }}
],
"should": [
{"term": {"brand": "皇冠假日" }},
{"term": {"brand": "华美达" }}
],
"must_not": [
{ "range": { "price": { "lte": 500 } }}
],
"filter": [
{ "range": {"score": { "gte": 45 } }}
]
}
}
}
5.4.2 案例
分析:
- 名称搜索,属于全文检索查询,应该参与算分。放到must中
- 价格不高于400,用range查询,属于过滤条件,不参与算分。放到must_not中
- 周围10km范围内,用geo_distance查询,属于过滤条件,不参与算分。放到filter中
点击查看代码
# bool 查询
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gt": 400
}
}
}
],
"filter": [
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 31.21,
"lon": 121.5
}
}
}
]
}
}
}
点击查看代码
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.7236328,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "433576",
"_score" : 1.7236328,
"_source" : {
"address" : "南京东路480号保安坊内",
"brand" : "如家",
"business" : "人民广场地区",
"city" : "上海",
"id" : 433576,
"location" : "31.236454, 121.480948",
"name" : "如家酒店(上海南京路步行街店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/BA/Cii-U13eXVaIQmdaAAWxgzdXXxEAAGRrgNIOkoABbGb143_w200_h200_c1_t0.jpg",
"price" : 379,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "434082",
"_score" : 1.4749806,
"_source" : {
"address" : "复兴东路260号",
"brand" : "如家",
"business" : "豫园地区",
"city" : "上海",
"id" : 434082,
"location" : "31.220706, 121.498769",
"name" : "如家酒店·neo(上海外滩城隍庙小南门地铁站店)",
"pic" : "https://m.tuniucdn.com/fb2/t1/G6/M00/52/B6/Cii-U13eXLGIdHFzAAIG-5cEwDEAAGRfQNNIV0AAgcT627_w200_h200_c1_t0.jpg",
"price" : 392,
"score" : 44,
"starName" : "二钻"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "1584362548",
"_score" : 1.4236369,
"_source" : {
"address" : "御青路315-317号",
"brand" : "如家",
"business" : "周浦康桥地区",
"city" : "上海",
"id" : 1584362548,
"location" : "31.15719, 121.572392",
"name" : "如家酒店(上海浦东国际旅游度假区御桥地铁站店)",
"pic" : "https://m.tuniucdn.com/fb3/s1/2n9c/2ybd3wqdoBtBeKcPxmyso9y1hNXa_w200_h200_c1_t0.jpg",
"price" : 339,
"score" : 44,
"starName" : "二钻"
}
}
]
}
}
5.4.3 小结
bool查询有几种逻辑关系?
- must:必须匹配的条件,可以理解为“与”
- should:选择性匹配的条件,可以理解为“或”
- must_not:必须不匹配的条件,不参与打分
- filter:必须匹配的条件,不参与打分