微服务框架 SpringCloud微服务架构 22 DSL 查询语法 22.2 全文检索查询
微服务框架
【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】
SpringCloud微服务架构
文章目录
- 微服务框架
- SpringCloud微服务架构
- 22 DSL 查询语法
- 22.2 全文检索查询
- 22.2.1 全文检索查询
- 22.2.2 总结
22 DSL 查询语法
22.2 全文检索查询
22.2.1 全文检索查询
全文检索查询,会对用户输入内容分词,常用于搜索框搜索:
【分类】
match查询:全文检索查询的一种,会对用户输入内容分词,然后去倒排索引库检索,语法:
GET /indexName/_search
{
"query": {
"match": {
"FIELD": "TEXT"
}
}
}
试试
【老师这儿,用到了all,但是,笔者创建的时候已经把它删了…】
那能咋办,重新建一个索引库吧
package cn.itcast.hotel.constants;
/**
* ClassName: HotelConstants
* date: 2022/11/1 10:16
*
* @author DingJiaxiong
*/
public class HotelConstants {
public static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"score\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\":\"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"location\":{\n" +
" \"type\": \"geo_point\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
先把已有的索引库删掉
重新创建索引库
OK,再次执行批量插入操作
OK,这次就有all
了
# match 查询
GET /hotel/_search
{
"query": {
"match": {
"all": "外滩"
}
}
}
直接运行
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 6.0247297,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "60487",
"_score" : 6.0247297,
"_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" : 4.85054,
"_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" : "434082",
"_score" : 3.8000445,
"_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" : "二钻"
}
}
]
}
}
OK, 一共命中了三条
加上点儿
这样就变多 了,因为分词也变多了【而且可以看到两个词,如果都满足,得分就较高,就会排在前面】
multi_match:与match查询类似,只不过允许同时查询多个字段,语法:
GET /indexName/_search
{
"query": {
"multi_match": {
"query": "TEXT",
"fields": ["FIELD1", " FIELD12"]
}
}
}
试试
# multi_match 查询
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "外滩如家",
"fields": ["brand","name","business"]
}
}
}
直接运行
可以看到,居然和上面查询的一样
【为什么?】
原因很简单,因为我们的结构中,all 就是这三个合起来的
如果不一样,反而出错了
但是还是建议用all 的方式,就是不要用多个字段,会降低性能,都copy_to 到all 中,成为一个字段,那样可以加快速度
22.2.2 总结
match和multi_match的区别是什么?
- match:根据一个字段查询
- multi_match:根据多个字段查询,参与查询字段越多,查询性能越差