DSL搜索(一)

1. 数据准备

1.1扩展自定义词库

1.2 创建索引shop

1.3 为shop索引建立mappings

入参json:

{
"properties": {
"id": {
"type": "long"
},
"age": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word"
},
"money": {
"type": "float"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word"
},
"sex": {
"type": "byte"
},
"birthday": {
"type": "date"
},
"face": {
"type": "text",
"index": false
}
}
}

 1.4 录入数据

列举其一,其余类似:

{
    "id": 1001,
    "age": 18,
    "username": "imoocAmazing",
    "nickname":"慕课网",
    "money": 88.8,
    "desc": "我在慕课网学习java和前端, 学习到了很多知识",
    "sex": 0,
    "birthday": "1922-12-24",
    "face": "https://www.imooc.com/static/img/index/logo.png"
}

 一共录入了12条数据。

2.入门语法

2.1 QueryString查询方式

2.1.1查询所有desc字段里包含“慕课网”的数据

 返回数据:

{
	"took": 36,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 6.927379,
		"hits": [
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1001",
				"_score": 6.927379,
				"_source": {
					"id": 1001,
					"age": 18,
					"username": "imoocAmazing",
					"nickname": "慕课网",
					"money": 88.8,
					"desc": "我在慕课网学习java和前端, 学习到了很多知识",
					"sex": 0,
					"birthday": "1922-12-24",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1003",
				"_score": 6.846202,
				"_source": {
					"id": 1003,
					"age": 20,
					"username": "bigFace",
					"nickname": "飞翔的巨鹰",
					"money": 66.8,
					"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲",
					"sex": 1,
					"birthday": "1996-01-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1004",
				"_score": 6.054982,
				"_source": {
					"id": 1004,
					"age": 22,
					"username": "flyfish",
					"nickname": "水中鱼",
					"money": 55.8,
					"desc": "昨天在学校的池塘里,看到有很多鱼在游泳,然后就去慕课网上课了",
					"sex": 0,
					"birthday": "1988-02-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			}
		]
	}
}

 2.1.2查询desc="慕课网"并且age=20的所有数据

 返回数据:

{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1003",
				"_score": 1,
				"_source": {
					"id": 1003,
					"age": 20,
					"username": "bigFace",
					"nickname": "飞翔的巨鹰",
					"money": 66.8,
					"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲",
					"sex": 1,
					"birthday": "1996-01-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			}
		]
	}
}

2.1.3查询nickname="super"

 返回数据:

{
	"took": 3,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.966516,
		"hits": [
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1012",
				"_score": 1.966516,
				"_source": {
					"id": 1012,
					"age": 31,
					"username": "super hero",
					"nickname": "super hero",
					"money": 188.8,
					"desc": "BatMan, GreenArrow, SpiderMan, IronMan... are all Super Hero",
					"sex": 1,
					"birthday": "1980-08-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			}
		]
	}
}

由于nickname字段是text类型,支持倒排索引,所以会搜索nickname里包含super这个词的数据。

2.1.4 查询username="super"

 返回数据:

{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 0,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	}
}

可以看到,返回的数据为空。虽然我们数据中插入了username=super hero这条数据,但是由于username类型是keyword类型,只能进行精确匹配,所以在查找时,用username=super查找就查不到它,username=hero也一样查找不到。

查询username=super hero,就能查到此条数据:

以上QueryString查找方式使用的少,一旦参数复杂了,就难以构建,在企业中,会采取结构化搜索方式,叫做DSL,全称:Domain Specific Language,数据以json方式请求查询。

2.2 DSL搜索

2.2.1查询desc="慕课网"

请求:

 返回:

{
	"took": 5,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 6.927379,
		"hits": [
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1001",
				"_score": 6.927379,
				"_source": {
					"id": 1001,
					"age": 18,
					"username": "imoocAmazing",
					"nickname": "慕课网",
					"money": 88.8,
					"desc": "我在慕课网学习java和前端, 学习到了很多知识",
					"sex": 0,
					"birthday": "1922-12-24",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1003",
				"_score": 6.846202,
				"_source": {
					"id": 1003,
					"age": 20,
					"username": "bigFace",
					"nickname": "飞翔的巨鹰",
					"money": 66.8,
					"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲",
					"sex": 1,
					"birthday": "1996-01-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1004",
				"_score": 6.054982,
				"_source": {
					"id": 1004,
					"age": 22,
					"username": "flyfish",
					"nickname": "水中鱼",
					"money": 55.8,
					"desc": "昨天在学校的池塘里,看到有很多鱼在游泳,然后就去慕课网上课了",
					"sex": 0,
					"birthday": "1988-02-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			}
		]
	}
}

2.2.2 查询属性是否存在

演示1:查询属性username是否存在

请求:

返回数据如下,es会把所有存在username属性的数据返回

{
	"took": 4,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 12,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1002",
				"_score": 1,
				"_source": {
					"id": 1002,
					"age": 19,
					"username": "justbuy",
					"nickname": "周杰棍",
					"money": 77.8,
					"desc": "今天上班都很堵,车流量很大",
					"sex": 1,
					"birthday": "1993-01-24",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1003",
				"_score": 1,
				"_source": {
					"id": 1003,
					"age": 20,
					"username": "bigFace",
					"nickname": "飞翔的巨鹰",
					"money": 66.8,
					"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲",
					"sex": 1,
					"birthday": "1996-01-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1008",
				"_score": 1,
				"_source": {
					"id": 1008,
					"age": 19,
					"username": "muke",
					"nickname": "慕学习",
					"money": 1056.8,
					"desc": "大学毕业后,可以到imooc.com进修",
					"sex": 1,
					"birthday": "1995-06-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1011",
				"_score": 1,
				"_source": {
					"id": 1011,
					"age": 31,
					"username": "sprder",
					"nickname": "皮特帕克",
					"money": 180.8,
					"desc": "它是一个超级英雄",
					"sex": 1,
					"birthday": "1989-08-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1007",
				"_score": 1,
				"_source": {
					"id": 1007,
					"age": 19,
					"username": "msgame",
					"nickname": "gamexbox",
					"money": 1056.8,
					"desc": "明天去进货,最近微软处理很多游戏机,还要买xbox游戏卡带",
					"sex": 1,
					"birthday": "1985-05-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"id": 1001,
					"age": 18,
					"username": "imoocAmazing",
					"nickname": "慕课网",
					"money": 88.8,
					"desc": "我在慕课网学习java和前端, 学习到了很多知识",
					"sex": 0,
					"birthday": "1922-12-24",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"id": 1004,
					"age": 22,
					"username": "flyfish",
					"nickname": "水中鱼",
					"money": 55.8,
					"desc": "昨天在学校的池塘里,看到有很多鱼在游泳,然后就去慕课网上课了",
					"sex": 0,
					"birthday": "1988-02-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1,
				"_source": {
					"id": 1005,
					"age": 25,
					"username": "gotoplay",
					"nickname": "ps游戏机",
					"money": 155.8,
					"desc": "今天生日,女友送了我一台play station游戏机,非常好玩,非常不错",
					"sex": 1,
					"birthday": "1989-03-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1006",
				"_score": 1,
				"_source": {
					"id": 1006,
					"age": 19,
					"username": "missimooc",
					"nickname": "我叫小慕",
					"money": 156.8,
					"desc": "我叫凌云彻,今年20岁,是一名律师,我在奇䯲星球做演讲",
					"sex": 1,
					"birthday": "1993-04-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			},
			{
				"_index": "shop",
				"_type": "_doc",
				"_id": "1009",
				"_score": 1,
				"_source": {
					"id": 1009,
					"age": 22,
					"username": "shaonian",
					"nickname": "骚年轮",
					"money": 96.8,
					"desc": "骚年在大学毕业后,考研究生去了",
					"sex": 1,
					"birthday": "1998-07-14",
					"face": "https://www.imooc.com/static/img/index/logo.png"
				}
			}
		]
	}
}

 演示2:查询属性username1是否存在

请求:

返回:

{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 0,
			"relation": "eq"
		},
		"max_score": null,
		"hits": []
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@所谓伊人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值