目录
1 批量导入数据
1.1 目的
1.2 bulk指令
1.3 实操
2 term多种查询
2.1 exists 查询
2.2 prefix 查询
2.3 wildcard查询
3 范围查询
3.1 基本内容
3.2 简单查询
3.3 时间查询
在进行数据搜索之前,需要进行数据的创建,本文以多名歌手的基本信息作为搜索测试数据,采用bulk指令进行数据的批量导入,方便后续的数据搜索。
bulk为批量导入数据指令,采用PUT请求,该方式导入数据十分方便,无需建立索引、映射等复杂操作,将数据按照特定格式(指定索引名称、映射名称等)导入后,会自动创建索引、映射等。
根据上述内容,导入数名歌手基本信息。发送PUT请求,_bulk指令进行批量数据导入。
PUT _bulk
{"index":{"_index":"singers","_type":"_doc","_id":"1"}}
{"name":"许嵩","neck":"Vae","No":"1","age":"35","sex":"男","brith":"1986/5/14","address":"安徽省合肥市"}
{"index":{"_index":"singers","_type":"_doc","_id":"2"}}
{"name":"周杰伦","neck":"Jay","No":"2","age":"42","sex":"男","brith":"1979/1/18","address":"台湾省新北市"}
{"index":{"_index":"singers","_type":"_doc","_id":"3"}}
{"name":"徐良","neck":"","No":"3","age":"34","sex":"男","brith":"1987/2/8","address":"山东省青岛市"}
{"index":{"_index":"singers","_type":"_doc","_id":"4"}}
{"name":"汪苏泷","neck":"Silence","No":"4","age":"32","sex":"男","brith":"1989/9/17","address":"辽宁省沈阳市"}
{"index":{"_index":"singers","_type":"_doc","_id":"5"}}
{"name":"韩红","No":"5","age":"50","sex":"女","brith":"1971/8/26","address":"西藏自治区昌都市"}
{"index":{"_index":"singers","_type":"_doc","_id":"6"}}
{"name":"汪峰","No":"6","age":"50","sex":"男","brith":"1971/6/29","address":"北京市"}
}
之前提到过term词条查询并进行了简单的学习和理解,本文对term的一些其他用法进行讨论。
exists查询是查询非空值文档的一中查询方式,当字段不为空时被查询到。
POST singers/_search
{
"query": {
"exists": {
"field": "neck"
}
}
}
得到结果:
#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See to enable security.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "许嵩",
"neck" : "Vae",
"No" : "1",
"age" : "35",
"sex" : "男",
"address" : "安徽省合肥市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "周杰伦",
"neck" : "Jay",
"No" : "2",
"age" : "42",
"sex" : "男",
"address" : "台湾省新北市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "徐良",
"neck" : "",
"No" : "3",
"age" : "34",
"sex" : "男",
"address" : "山东省青岛市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "汪苏泷",
"neck" : "Silence",
"No" : "4",
"age" : "32",
"sex" : "男",
"address" : "辽宁省沈阳市"
}
}
]
}
}
上述实例可知,本文在导入数据时,“韩红”“汪峰”两个文档并不包括neck字段,故使用exists查询时查询不到这两条文档。
该查询方式与之前提到的match_phrase_prefix类似,但由于属于term查询方式,无法进行全文查询(不能查询text类型的字段)。
查询neck字段中带“V”的文档。
POST singers/_search
{
"query": {
"prefix": {
"neck": "V"
}
}
}
得到结果:
#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See to enable security.
{
"took" : 54,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "许嵩",
"neck" : "Vae",
"No" : "1",
"age" : "35",
"sex" : "男",
"address" : "安徽省合肥市"
}
}
]
}
}
通配符查询
查询某一字段的范围,符合查询条件则返回结果
关键字:range
gt 区间最小值(不包括最小值)
gte 区间最小值(包括最小值)
lt:区间最大值(不包括最大值)
lte:区间最大值(包括最大值)
可查询number类型的字段,例如:查询年龄在20到30岁的歌手。
请求
GET singers/_search
{
"query": {
"range": {
"brith": {
"gte": 20,
"lte": 35,
}
}
}
}
返回结果:
#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See to enable security.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "许嵩",
"neck" : "Vae",
"No" : "1",
"age" : "35",
"sex" : "男",
"brith" : "1986/5/14",
"address" : "安徽省合肥市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "徐良",
"neck" : "",
"No" : "3",
"age" : "34",
"sex" : "男",
"brith" : "1987/2/8",
"address" : "山东省青岛市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "汪苏泷",
"neck" : "Silence",
"No" : "4",
"age" : "32",
"sex" : "男",
"brith" : "1989/9/17",
"address" : "辽宁省沈阳市"
}
}
]
}
}
可查询时间字段,使用format设定时间的格式,例如查找1980年到1990年出生的歌手
GET singers/_search
{
"query": {
"range": {
"brith": {
"gte": 1970,
"lte": 1990,
"format": "yyyy/MM/dd||yyyy"
}
}
}
}
返回结果:
#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See to enable security.
{
"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" : "singers",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "许嵩",
"neck" : "Vae",
"No" : "1",
"age" : "35",
"sex" : "男",
"brith" : "1986/5/14",
"address" : "安徽省合肥市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "徐良",
"neck" : "",
"No" : "3",
"age" : "34",
"sex" : "男",
"brith" : "1987/2/8",
"address" : "山东省青岛市"
}
},
{
"_index" : "singers",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "汪苏泷",
"neck" : "Silence",
"No" : "4",
"age" : "32",
"sex" : "男",
"brith" : "1989/9/17",
"address" : "辽宁省沈阳市"
}
}
]
}
}
更新中...