介紹
- 單詞級別查詢
- 這些查詢通常用於結構化的數據,比如:number, date, keyword 等,而不是對 text
- 也就是說,全文本查詢之前要先對文本內容進行分詞、而單詞級別的查詢直接在相應字段的反向索引中精確查找,單詞級別的查詢一般用於數值、日期等類型的字段上
準備工作
使用 kibana 可視化工具
刪除 nba 索引
創建 nba 索引 和 映射
PUT nba
{
"mappings": {
"properties": {
"birthDay":{
"type":"long"
},
"code":{
"type":"text"
},
"country":{
"type":"text"
},
"countryEn":{
"type":"text"
},
"displayAffiliation":{
"type":"text"
},
"displayName":{
"type":"text"
},
"displayNameEn":{
"type":"text"
},
"draft":{
"type":"long"
},
"heightValue":{
"type":"float"
},
"jerseyNo":{
"type":"text"
},
"playYear":{
"type":"long"
},
"playerId":{
"type":"text"
},
"position":{
"type":"text"
},
"schoolType":{
"type":"text"
},
"teamCity":{
"type":"text"
},
"teamCityEn":{
"type":"text"
},
"teamConference":{
"type":"text"
},
"teamConferenceEn":{
"type":"text"
},
"teamName":{
"type":"keyword"
},
"teamNameEn":{
"type":"keyword"
},
"weight":{
"type":"text"
}
}
}
}
導入數據,參考:數據導入參考
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @player
Term query 精確匹配查詢
查找球衣號碼爲23的球員
POST nba/_search
{
"query":{
"term":{
"jerseyNo": "23"
}
}
}
Exsit query 在特定的字段中查找非空值的文檔
查出 teamNameEn
字段不爲空的文檔
POST nba/_search
{
"query":{
"exists":{
"field": "teamNameEn"
}
}
}
Prefix query 查找包含帶有指定前綴term的文檔
適合 字段類型 爲 keyword
, 非 text
類型
POST nba/_search
{
"query":{
"prefix": {
"teamNameEn": "Rock"
}
}
}
wildcard query 支持通配符查詢
*
表示任意字符 ?
表示任意單個字符
POST nba/_search
{
"query":{
"wildcard": {
"teamNameEn": "Ro*s"
}
}
}
Regexp query 正則表達式查詢
POST nba/_search
{
"query":{
"regexp": {
"teamNameEn": "Ro.*s"
}
}
}
ids query 通過ID查詢
POST nba/_search
{
"query":{
"ids":{
"values": [1,2]
}
}
}