写入数据
- 查询索引状态
- 写入一条数据
- 查询数据
- 按id查询一条 类比 getById
- 不按id查
 
- 写入官方测试数据
 
查询索引状态
GET _cat/indices

写入一条数据
PUT/POST my_index/_doc/1
{
  "k": "test key"
}
my_index:索引名
 _doc:文档类型,现在只有一个类型,都是_doc
 1:文档id(PUT时,必须,POST时不是必须)
PUT 为修改操作;必须指定文档id
 POST 为保存操作;
 指定id时,以该id保存;id存在则修改,不存在则新增。
 未指定id时,直接新增,并随机生成一个id。
查询数据
按id查询一条 类比 getById
GET my_index/_doc/1
my_index:索引名
 _doc:文档类型,现在只有一个类型,都是_doc
 1:文档id
不按id查
GET my_index/_doc/_search
{
  "query": {
    "match_all": {}
  }
}
my_index:索引名。
 _doc:文档类型,非id查询时,可以不加。
 _search:不按id查询时,需加上。
query:定义查询条件;“match_all”: {} 表示查询该索引下所有数据。
写入官方测试数据
- 数据文件
- 将文件内容写入es
curl -u elastic:elastic -H "Content-Type: application/json" -XPOST "192.168.0.101:9200/bank/_bulk?pretty&refresh" --data-binary "@/opt/accounts.json"
有可能会报错
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "The bulk request must be terminated by a newline [\\n]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "The bulk request must be terminated by a newline [\\n]"
  },
  "status" : 400
}
如果报类似错误,需要在json文件最后回车另起一行,即可。
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 1000
}
可以看到写入es的数据。



















