关于用Elasticsearch进行文档管理的的补充
ES数据模型
文档(Document)管理
文件是ES最小数据单元
原始数据:_source:原始JSON格式文档
文档元数据:
_index:索引名
_type 索引类型
_id 文档编号
_version 文档版本号
索引的创建
PUT demo.12 //索引名
{
"settings" : {
"index" : {
"number_of_shards" : 2, //主分片数量,默认5
"number_of_replicas" : 2 //副本数量,默认1
}
}
}
PUT 更新 或创建
批量操作bulk文件:
PUT _bulk
{"index/create/update/delete":{"_index":"...","_type":"...","_id":"..."}}
{json串,如}
例子,不能换行
{"create":{"_index":"stu","_type":"doc","_id":"1"}}
{"id": 1, "studentNo": "TH-CHEM-2016-C001", "name": "Jonh Smith", "major":"Chemistry", "gpa": 4.8, "yearOfBorn": 2000, "classOf": 2016, "interest": "soccer, basketball, badminton, chess"}
POST 创建
GET 查看
GET demo.12/_mapping //查看索引映射
批量读取文档
GET stu/doc/_mget
{"docs":[{"_id":"1"},{"_id":"2"}]}
GET _mget
{"docs":[{"_index":"stu","_type":"doc","_id":"1"},{"_index":"stu","_type":"doc","_id":"2"}]} //把要查询的条件放入【】中
match_all :返回所有文档
GET stu/_search
{
"query": {
"match_all": {}
}
}
match:对查询的字符进行分词查询,包含就返回
GET stu/_search
{
"query": {
"match": {
"name":"John Tim"
}
}
}
match_phrase :短语匹配查询,结果必须包含所有分词
GET stu/_search
{
"query": {
"match_phrase": {
"name":"John Kerry"
}
}
}
range:范围查询
GET stu/_search
{"query": {
"range" : {
"yearOfBorn" : {
"gte" : 1995, //大于等于
"lte" : 2000 //小于等于
}
}
}
}
bool 布尔查询(多条件查询)
GET stu/_search
{
"query": {
"bool": {
"must": {
"match": { "interest": "cooking"} ,
...,
"range": { "yearOfBorn": { "gte": 1995, "lte": 2000 }}}
"must_not": {
"range": { "yearOfBorn": { "gte": 1995, "lte": 2000 }}}
}
}
}
DELETE删除
DELETE 索引名 //删除索引