在上一章介绍了常见的数据类型,有兴趣的可以参考一下
elasticSearch核心概念的介绍(七):常见的数据类型
这里我们来介绍一下ES的批量导入数据
Bulk
ES提供了一个叫bulk的API来进行批量导入操作
批量导入
数据
{“index”:{"_index":“book”,"_type":"_doc","_id":1}}
{“name”:“权力的游戏”}
{“index”:{"_index":“book”,"_type":"_doc","_id":2}}
{“name”:“疯狂的游戏”}
请求
curl -X POST "http://172.25.45.150:9200/_bulk" -H 'Content-Type:application/json' --data-binary @test
> @test :这里为相对路径,因为这里不区分文件后缀。所以test或者是text.txt也就无所谓了
查看导入的数据
curl -X POST "http://172.25.45.150:9200/book/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"match":{
"name":"疯狂"
}
},
"from":0,
"size":100
}
'
响应
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3862944,
"hits": [
{
"_index": "book",
"_type": "_doc",
"_id": "2",
"_score": 1.3862944,
"_source": {
"name": "疯狂的游戏"
}
}
]
}
}
term的多种查询
介绍
单词级别查询这些查询通常用于结构化数据,比如number,data,keyword等,而不是对text。也就是说,全文本查询之前要先对文本内容进行分词,而单词级别的查询直接在相应字段的反向索引中精确查找,单词级别的查询一般用于数值、日期等类型的字段上。
term query 精确匹配查询
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"term":{
"jerse_no":"23"
}
},
"from":0,
"size":100
}
'
Exsit Query : 在特定的字段中查找非空的字段
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"exists":{
"field":"jerse_no"
}
},
"from":0,
"size":100
}
'
Prefix Query :查找包含带指定前缀term的文档
type需要是keyword(非text)类型的数据
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"prefix":{
"jerse_no":"2"
}
},
"from":0,
"size":100
}
'
Wildcard Query 支持通配符查询, *表示任意字符,?表示任意单个字符
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"wildcard":{
"name":"库*"
}
},
"from":0,
"size":100
}
'
Regexp Query:正则表达式查询
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"regexp":{
"name":"库.*"
}
},
"from":0,
"size":100
}
'
Ids Query:通过id去批量获取
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
"query":{
"ids":{
"valuse":[1,2,3,4,5]
}
},
"from":0,
"size":100
}
'



