前面簡單的使用過term查詢過,這里進行加深的學習。
1.說明
單詞級別查詢 這些查詢通常⽤於結構化的數據,⽐如:number, date, keyword等,⽽不是對text。
也就是說,全⽂本查詢之前要先對⽂本內容進⾏分詞,⽽單詞級別的查詢直接在相應字段的
反向索引中精確查找,單詞級別的查詢⼀般⽤於數值、⽇期等類型的字段上
2.准備數據
刪除索引
添加索引
批量導入數據
DELETE /nba
PUT /nba
{
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"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": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
3.Term query 精准匹配查詢(查找號碼為23的球員)
POST nba/_search
{
"query": {
"term": {
"jerseyNo": "23"
}
}
}
4.Exsit Query 在特定的字段中查找⾮空值的⽂檔(查找隊名⾮空的球員)
POST nba/_search
{
"query": {
"exists": {
"field": "teamNameEn"
}
}
}
5.Prefix Query 查找包含帶有指定前綴term的⽂檔(查找隊名以Rock開頭的球員)
POST nba/_search
{
"query": {
"prefix": {
"teamNameEn": "Rock"
}
}
}
6.Wildcard Query ⽀持通配符查詢,*表示任意字符,?表示任意單個字符(查找⽕箭隊的球員)
POST nba/_search
{
"query": {
"wildcard": {
"teamNameEn": "Ro*s"
}
}
}
7.Regexp Query 正則表達式查詢(查找⽕箭隊的球員)
POST nba/_search
{
"query": {
"regexp": {
"teamNameEn": "Ro.*s"
}
}
}
8.Ids Query(查找id為1和2的球員)
POST nba/_search
{
"query": {
"ids": {
"values": [1, 2]
}
}
}
結果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"countryEn" : "United States",
"teamName" : "老鷹",
"birthDay" : 831182400000,
"country" : "美國",
"teamCityEn" : "Atlanta",
"code" : "jaylen_adams",
"displayAffiliation" : "United States",
"displayName" : "傑倫 亞當斯",
"schoolType" : "College",
"teamConference" : "東部",
"teamConferenceEn" : "Eastern",
"weight" : "86.2 公斤",
"teamCity" : "亞特蘭大",
"playYear" : 1,
"jerseyNo" : "10",
"teamNameEn" : "Hawks",
"draft" : 2018,
"displayNameEn" : "Jaylen Adams",
"heightValue" : 1.88,
"birthDayStr" : "1996-05-04",
"position" : "后衛",
"age" : 23,
"playerId" : "1629121"
}
},
{
"_index" : "nba",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"countryEn" : "New Zealand",
"teamName" : "雷霆",
"birthDay" : 743140800000,
"country" : "新西蘭",
"teamCityEn" : "Oklahoma City",
"code" : "steven_adams",
"displayAffiliation" : "Pittsburgh/New Zealand",
"displayName" : "斯蒂文 亞當斯",
"schoolType" : "College",
"teamConference" : "西部",
"teamConferenceEn" : "Western",
"weight" : "120.2 公斤",
"teamCity" : "俄克拉荷馬城",
"playYear" : 6,
"jerseyNo" : "12",
"teamNameEn" : "Thunder",
"draft" : 2013,
"displayNameEn" : "Steven Adams",
"heightValue" : 2.13,
"birthDayStr" : "1993-07-20",
"position" : "中鋒",
"age" : 26,
"playerId" : "203500"
}
}
]
}
}
