基本的增删查改
新建一个索引 PUT 方法需要指定id
PUT /library/books/3
{
"title":"天下无敌",
"name":{
"first":"Zachary",
"last":"Tong"
},
"publish_date":"2015-08-25",
"price":"500"
}
return
{
"_index": "library",
"_type": "books",
"_id": "3",
"_version": 2,
"created": false // 如果这个id的数据已经存在就返回false
}
{
"_index": "library",
"_type": "books",
"_id": "4",
"_version": 1,
"created": true
}
POST 方法可以不指定id id为自动生成
POST /library/books/
{
"title": "Elasticsearch Blueprints",
"name" : {
"first" : "Vineeth",
"last" : "Mohan"
},
"publish_date":"2015-06-06",
"price":"35.99"
}
>
return
{
"_index": "library",
"_type": "books",
"_id": "AVSd0qjGioND9IccWuDh", // 每个id都不会重复 切不一样
"_version": 1,
"created": true
}
GET 方法是通过指定的id获取对应数据
GET /library/books/AVSd1ETGioND9IccWuDi
GET /library/books/1
GET /library/books/2
获取某个字段的值
GET /library/books/1?_source=title //获取title
GET /library/books/1?_source=title,price // 获取title以及价格 如果写的某个字段没有值将不会返回某个字段
GET /library/books/1?_source // 获取全部字段
PUT 更新 (直接覆盖) 这个方法不推荐
GET /library/books/AVSd1ETGioND9IccWuDi
GET /library/books/1
GET /library/books/2
//获取某个字段的值
GET /library/books/1?_source=title //获取title
GET /library/books/1?_source=title,price // 获取title以及价格 如果写的某个字段没有值将不会返回某个字段
GET /library/books/1?_source // 获取全部字段
PUT 更新 (直接覆盖) 这个方法不推荐
PUT /library/books/1
{
"title": "Elasticsearch: The Definitive Guide",
"name" : {
"first" : "Zachary",
"last" : "Tong"
},
"publish_date":"2015-02-06",
"price":"59.99"
}
return
{
"_index": "library",
"_type": "books",
"_id": "4",
"_version": 4,
"created": false
}
或者通过POST _update API的方式单独更新你想要更新的
POST /library/books/4/_update
{
"doc": {
"price":"1000"
}
}
删除一个文档的方法
DELETE /library/books/1
DELETE /library
GET /library/books/1
高级
同时获取多个文档信息
例子:获取index:bank, type:bank_account下面
ID为1,2,3,4,15,6,28 的文档信息
#先建立index
PUT /bank/bank_account/4
{
"title":"工商银行",
"area":"总部就在北京",
"people":"200",
"content":{
"fuwu":{
"one":"可以办理信用卡",
"two":"可以直接汇款"
}
},
"number":"20"
}
PUT /shakespeare/line/3
{
"title":"工商银行",
"area":"总部就在北京",
"people":"200"
}
PUT /shakespeare/line/5
{
"title":"工123商银行",
"area":"总部就在北京",
"people":"200"
}
# 获取值数组[]
GET /_mget
{
"docs" : [
{
"_index" : "bank",
"_type" : "bank_account",
"_id" : 1
},
{
"_index" : "bank",
"_type" : "bank_account",
"_id" : 2
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 3
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 4
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 15
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 6
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 28
}
]
}
# 也可以指定_source字段,获取你想要的
GET /_mget
{
"docs" : [
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 6,
"_source": "play_name"
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 28,
"_source": "play_name"
}
]
}
# 指定多个_source字段,数组的形式[]
GET /_mget
{
"docs" : [
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 6
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 28,
"_source": ["play_name","speaker","text_entry"]
}
]
}
# 获取相同index相同type下不同ID的文档
GET /shakespeare/line/_mget
{
"docs" : [
{ "_id" : 6 },
{ "_type" : "line", "_id" : 28 }
]
}
# 可以这样简便的写
GET /shakespeare/line/_mget
{
"ids" : [ "6", "28" ]
}
GET /shakespeare/line/_mget
{
"ids" : [ "1", "2", "3", "4", "5", "6", "7" ]
}
# 多重模式
# 批量操作bulk
POST /library/books/_bulk
{ "index": { "_id": 1}}
{ "title":"Elasticsearch: The Definitive Guide","price":5 }
{ "index": { "_id": 2}}
{ "title":"The Elasticsearch cookbook","price":15 }
{ "index": { "_id": 3}}
{ "title":"Elasticsearch Blueprints","price":9 }
{ "index": { "_id": 4}}
{ "title":"Thinking in Python","price":22 }
{ "index": { "_id": 5}}
{ "title":"Thinking in Java","price":7 }
GET /library/
GET /library/books/_mget
{
"ids" : [ "1", "2" , "3", "4", "5"]
}
# 当然还可以有delete ,update等操作
# 注意delete下面没有具体的request body
POST /library/books/_bulk
{ "delete": { "_index": "library", "_type": "books", "_id": "1" }}
{ "create": { "_index": "music", "_type": "classical", "_id": "1" }}
{ "title": "Ave Verum Corpus" }
{ "index": { "_index": "music", "_type": "classical" }}
{ "title": "Litaniac de Venerabili Altaris Sacromento" }
{ "update": { "_index": "library", "_type": "books", "_id": "2"} }
{ "doc" : {"price" : "18"} }
GET /library/books/1
GET /library/books/_mget
{
"ids" : [ "1", "2" , "3", "4", "5"]
}
Date 字段接受日期范围的形式查询.
一下格式被支持:
关键词 / 关键短语
now
today
tomorrow
yesterday
last / this / next + week / month / year
搜索关键字匹配的日期. last year 将搜索过去全年.
范围
1000 secs
5mins
1day
2days
80d
9 months
2yrs (空格可选, 同等于多个范围修饰词)
创建一个指定时间范围的搜索, 将围绕现在 并延伸至过去与未来时间段.
DateTime 与 DateTime局部
2011
2011-01
2011-01-18
2011-01-18 12
2011-01-18 12:32
2011-01-18 12:32:45
指定一个特定的日期范围. 2011会搜索整个 2011年, 而 2011-01-18 12:32:45 将只搜索1秒范围内
Time 与 Time局部
12
12:32
12:32:45
这些格式只搜索当天的特定时间. 12:32 将搜索当天的那一分钟
日期范围
2010 -> 2011
last week -> next week
2011-05 ->
< now
日期范围是将两个日期格式串 (日期关键字 / DateTime / Time) 用 < 或 -> (效果相同) 分隔. 如果缺少任意一端,那么在这个方向上时间将没有限制.
偏移日期范围
2010 -> 1yr
3mins < now 搜索包括指定方向上偏移的日期.
锚定范围
2010-05-13 05:13 <> 10m
now <> 1yr
lastweek <> 1month
类似于上面的便宜日期,在两个方向上将锚定的日期延长在linux中成的操作
获取索引初始化设置
- CURL -XGET ‘http://180.76.188.294:9200/library/_settings’
- CURL -XGET ‘http://180.76.188.294:9200/library,library2/_settings’
- CURL -XGET ‘http://180.76.188.294:9200/_all/_settings’
API创建,更新,删除索引
//如何用API创建索引
例: curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{ "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }'
//如何用API更新索引
例: curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : "ctx._source.counter += count", "params" : { "count" : 4 } }'
//如何用API删除索引
例: curl -XDELETE "http://localhost:9200/twitter/tweet/1"
Elasticsearch的内置字段以及类型
内置字段
_uid
_id
_type
_source
_all
_analyzer
_boost
_parent
_routing
_index
_size
_timestamp
_ttl
字段类型
String
Integer/long
Float/double
Boolean
Null
Date
手册
http://88250.b3log.org/full-text-search-elasticsearch
http://es.xiaoleilu.com/010_Intro/00_README.html