准备工作
删除已有的nba索引
curl -X DELETE "localhost:9200/nba"
新增一个索引,并且指定mapping
curl -X PUT "localhost:9200/nba" -H 'Content-Type:application/json' -d '
{
"mappings":{
"properties":{
"name":{
"type":"text"
},
"team_name":{
"type":"text"
},
"position":{
"type":"text"
},
"play_year":{
"type":"long"
},
"jerse_no":{
"type":"keyword"
}
}
}
}
'
新增文档
curl -X PUT "localhost:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
"name":"哈登",
"team_name":"火箭",
"position":"得分后卫",
"play_year":10,
"jerse_no":"13"
}
'
curl -X PUT "localhost:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
"name":"库里",
"team_name":"勇士",
"position":"控球后卫",
"play_year":10,
"jerse_no":"30"
}
'
curl -X PUT "localhost:9200/nba/_doc/3" -H 'Content-Type:application/json' -d '
{
"name":"詹姆斯",
"team_name":"湖人",
"position":"小前锋",
"play_year":15,
"jerse_no":"23"
}
'
搜索
term(词条)查询
词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索
单条term查询 POST
_search
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"term":{
"jerse_no":"23"
}
}
}
'
took
表示查询的时间
_shards
分片的信息
hits
搜索结果的关键
多条terms查询 POST
_search
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"terms":{
"jerse_no":[
"23",
"13"
]
}
}
}
'
full text(全文)查询
全文查询:ES引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字符中包含词条的任意一个,或全部包含、就匹配查询条件,返回文档;如果不包含任意一个分词,表示没有任何文档匹配查询条件
match 针对 text 类型
match_all
查询全部文档
默认显示10条记录
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match_all":{}
}
}
'
返回一个json
{
"took": 23,
"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": 10,
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "库里",
"team_name": "勇士",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "詹姆斯",
"team_name": "湖人",
"position": "小前锋",
"play_year": 15,
"jerse_no": "23"
}
}
]
}
}
指定分页 查询所有
从0开始,数量为100
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match_all":{}
},
"from":0,
"size":100
}
'
match
查询字符中包含词条的任意一个 数据
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match":{
"name":"库小里"
}
},
"from":0,
"size":100
}
'
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match":{
"position":"后卫"
}
}
}
'
返回一个json
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.90630186,
"hits": [
{
"_index": "nba",
"_type": "_doc",
"_id": "1",
"_score": 0.90630186,
"_source": {
"name": "哈登",
"team_name": "火箭",
"position": "得分后卫",
"play_year": 10,
"jerse_no": "13"
}
},
{
"_index": "nba",
"_type": "_doc",
"_id": "2",
"_score": 0.90630186,
"_source": {
"name": "库里",
"team_name": "勇士",
"position": "控球后卫",
"play_year": 10,
"jerse_no": "30"
}
}
]
}
}
multi_match
多个查询
更新一下id=2的数据
curl -X POST "localhost:9200/nba/_update/2" -H 'Content-Type:application/json' -d '
{
"doc":{
"name":"库里",
"team_name":"勇士",
"position":"控球后卫",
"play_year":10,
"jerse_no":"30",
"title":"the best shooter"
}
}
'
title 和 name 字段里包含 shooter
的数据
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"multi_match":{
"query":"shooter",
"fields":["title","name"]
}
}
}
'
match_phrase
类似与词条查询(精确查询)
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match_phrase":{
"position":"得分后卫"
}
}
}
'
match_phrase_prefix
满足单词前缀的数据(模糊查询)
更新一下id=3数据
curl -X POST "localhost:9200/nba/_update/3" -H 'Content-Type:application/json' -d '
{
"doc":{
"name":"詹姆斯",
"team_name":"湖人",
"position":"小前锋",
"play_year":15,
"jerse_no":"23",
"title":"the best small forward"
}
}
'
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"multi_match":{
"query":"shooter",
"fields":["title","name"]
}
}
}
'