------------------------------------------------------
1、查看索引信息
请求命令:
GET /library/_settings
GET /library1,library2/_settings
GET /_all/_settings
------------------------------------------------------
2、创建一个 _id 为1的文档
PUT /索引名/类型名/文档id
请求命令:
PUT /library/books/1
{
“title”:“Black Horse”,
“name”:{
“first”:“shengqi”,
“last”:“liang”
},
"publish_date","1900/01/01",
"price":"35.99"
}
POST /library/books/
{
“title”:“Black Horse”,
“name”:{
“first”:“shengqi”,
“last”:“liang”
},
"publish_date","1900/01/01",
"price":"55.99"
}
------------------------------------------------------
3、查看 _id 为1的书信息
请求命令:
GET /library/books/1
GET /library/books/1?_source=title,price ## 返回title和price
响应结果:
{
"_index":"library",
"_type":"books",
"_id":"1",
"_version":"4",
"_found":"true",
"_source":{
"title":"black fly",
"name":{
"first":,"xxx",
"last":,"yyy"
}
"publish_date":"2015-12-06",
"price":"100"
}
}
------------------------------------------------------
4、更新docment
方式1,重新put同一个id的新内容
方式2,POST更新方式(_update,doc)
方式3:脚本更新 http://www.cnblogs.com/xing901022/p/5330778.html
请求命令:
POST /library/books/1/_update
{
“doc”:{
“price”:“100.99”
}
}
响应结果:
------------------------------------------------------
5、删除一个 _id=1 的文档
请求命令:
DELETE /library/books/1
响应结果:
{
“_found”:true,
"_index":"library",
"_type":"books",
"_id":"1",
"_version":"5"
}
删除一个 type和索引
请求命令:
DELETE /library/books
DELETE /library
响应结果1:
{
“acknowledge”:true
}
响应结果2:
{
“error”:"xxx",
“status”:404
}
6、追加字段定义
https://www.elastic.co/guide/en/elasticsearch/guide/current/_controlling_analysis.html
For instance, let’s add a new field to my_index:
PUT /my_index/_mapping/my_type
{
"my_type": {
"properties": {
"english_title": {
"type": "string",
"analyzer": "english"
}
}
}
}
elasticsearch 添加字段
Elasticsearch中的mapping一旦创建,就不能再修改。但是添加字段是可以的。
其实很简单,只需在原来的mapping上面直接新增加一个field,
然后重新创建一下mapping就可以了。
原mapping的定义如下:
Java代码 收藏代码
private static XContentBuilder getMapping() throws Exception{
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject("test")
.startObject("properties")
.startObject("id")
.field("type", "long")
.field("store", "yes")
.endObject()
.startObject("type")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.startObject("catIds")
.field("type", "integer")
.endObject()
.endObject()
.endObject()
.endObject();
return mapping;
}
现在加入要增加一个field,名称叫title。新增后的代码如下:
Java代码 收藏代码
private static XContentBuilder getMapping() throws Exception{
XContentBuilder mapping = jsonBuilder()
.startObject()
.startObject("test")
.startObject("properties")
.startObject("id")
.field("type", "long")
.field("store", "yes")
.endObject()
.startObject("type")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
//新增字段
.startObject("title")
.field("type", "string")
.field("index", "analyzed")
.endObject()
.startObject("catIds")
.field("type", "integer")
.endObject()
.endObject()
.endObject()
.endObject();
return mapping;
}
然后重新putMapping即可。
还不知道如何创建mapping?请看这里。
对这类话题感兴趣?欢迎发送邮件至donlianli@126.com