Elasticsearch的学习(-)
1.Elasticsearch的基本概念:
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的
es | 数据库 |
---|---|
index | database |
type | table |
document | row |
column | field |
POST /uri 创建
DELETE /uri/xxx 删除
PUT /uri/xxx 更新或创建
GET /uri/xxx 查看
添加
#添加指定id的文档
PUT /test/user/1
{
"id":1,
"name":"张三",
"age":22
}
POST /test/user
{
"id":2,
"name":"赵四",
"age":52
}
PUT和POST的区别
POST不用加具体的id,它是作用在一个集合资源之上的(/uri),而PUT操作是作用在一个具体资源之上的(/uri/xxx)。
在ES中,如果不确定document的ID(documents具体含义见下),那么直接POST对应uri( “POST /website/blog” ),ES可以自己生成不会发生碰撞的UUID;
如果确定document的ID,比如 “PUT /website/blog/123”,那么执行创建或修改(修改时_version版本号提高1)
查询
#根据id查询
GET test/user/1
#查询所有的
GET _search
{
"query": {
"match_all": {}
}
}
#只查询文档的name字段
GET test/user/1?_source=name
#只获取文档
GET test/user/1/_source
#查询test的所有
GET test/user/_search
#获取部分的文档
GET test/user/_mget
{
"ids":["1","3"]
}
修改
#部分修改
POST test/user/1/_update
{
"doc":
{
"id":1,
"name":"名字被修改了..."
}
}
#更新整个文档(即先删除后再添加)
PUT test/user/1
{
"id":1,
"name":"王五"
}
删除
#根据id删除
DELETE test/user/1
#删除所有的
DELETE _all
高级查询
#查询age在10-30的文档
GET test/user/_search?q=age:[10 TO 30]
#排序【asc:升序 desc:降序】
GET test/user/_search?q=age:[10 TO 20]&sort=age:asc
#排序+分页 【from:从第几页开始 size:每页数据条数】
GET test/user/_search?q=age:[10 TO 20]&sort=age:asc&from=0&size=1
#排序+分页+截取字段 【_source:截取name和age字段】
GET test/user/_search?q=age:[10 TO 20]&sort=age:asc&from=0&size=2&_source=name,age
es的mapping
es不能删除mapping,官方也没有提供
可以先删除索引
GET item #获取索引
DELETE item 删除索引
然后在建立
PUT /item
{
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "long"
},
"itemName": {
"type": "text"
},
"sellPoint": {
"type": "text"
},
"price": {
"type": "text"
},
"num": {
"type": "integer"
},
"brand": {
"type": "text"
},
"image": {
"type": "text"
},
"cid": {
"type": "long"
},
"status": {
"type": "short"
},
"createTime": {
"type": "date"
},
"modifyTime": {
"type": "date"
}
}
}
}
}