直播系统搭建,Elasticsearch通过Http请求实现查询操作
直播系统搭建,Elasticsearch通过Http请求实现查询操作
数据准备
为了方便查询操作,创建索引叫做:nba,并且添加数据,http请求如下:
# 创建索引及映射
# PUT http://192.168.80.121:9200/nba
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"team_name": {
"type": "keyword"
},
"position": {
"type": "text",
"analyzer": "ik_max_word"
},
"play_year": {
"type": "integer",
"index": "false"
},
"jerse_no": {
"type": "integer"
}
}
}
}
# 依次创建如下文档
# POST http://192.168.80.121:9200/nba/_doc/1
{
"name": "韦德",
"team_name": "热火",
"position": "得分后卫",
"play_year": 16,
"jerse_no": 3
}
# POST http://192.168.80.121:9200/nba/_doc/2
{
"name": "波什",
"team_name": "热火",
"position": "大前锋",
"play_year": 16,
"jerse_no": 1
}
# POST http://192.168.80.121:9200/nba/_doc/3
{
"name": "詹姆斯",
"team_name": "热火",
"position": "小前锋",
"play_year": 16,
"jerse_no": 6
}
1.基本查询:
基本语法
# GET /索引库名/_search
{
"query":{
"查询类型":{
"字段":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
查询类型:
例如:match_all, match,term , range 等等
查询条件:查询条件会根据类型的不同,写法也有差异,后面详细讲解
1.1 查询所有(match_all)
请求方式:GET
请求路径:ip+port/索引库名/_search
请求参数:json
# GET http://192.168.80.121:9200/nba/_search
# 请求参数
{
"query":{
"match_all": {}
}
}
query:代表查询对象
match_all:代表查询所有
结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "韦德",
"team_name": "热火",
"position": "得分后卫",
"play_year": 16,
"jerse_no": "3"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "波斯",
"team_name": "热火",
"position": "大前锋",
"play_year": 16,
"jerse_no": "1"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "詹姆斯",
"team_name": "热火",
"position": "小前锋",
"play_year": 16,
"jerse_no": "6"
}
}
]
}
}
以上就是 直播系统搭建,Elasticsearch通过Http请求实现查询操作,更多内容欢迎关注之后的文章