前面简单的使用过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" } } ] } }