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": []
}
}