1.创建索引库
curl -XPUT 'http://localhost:9200/index' //索引名称
访问http://192.168.x.x:9100/查看新建索引
这种方式创建的索引库默认创建5个分片,每个分片有一个备份,也可以自己指定分片和备份的数量,命令如下
curl -H 'Content-Type:application/json' -XPUT 'http://localhost:9200/index2?pretty' -d' { "settings":{ "index":{ "number_of_shards":4 //分片数量 "number_of_replicas":2 //备份分片数量 } } }'
如下图
2.查看索引库
curl -XGET 'http://localhost:9200/indextest/_cat/indices?v'
3.插入数据
curl -H 'Content-Type:application/json' -XPUT 'http://localhost/9200/index/ fulltext/1' -d' { "id":1, "content":"美国留给伊拉克的是个烂摊子吗" }'
如果插入两条相同的数据,那么则会对原有的文档进行修改
3.1.如果不想让ES默认第二个已经文档编号的添加形成称为修改行为,我们可以有如下两种方案。
方法一
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/index/fulltext/p1?op_type=create -d '{ "id":"2", "content":"苹果笔记本" }'
方法二
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/index/fulltext/p1_create -d '{ "id":"3", "content":"雷神笔记本" }'
3.2自动创建索引可以通过配置文件设置action.auto_create_index为false在所有节点的配置文件中禁用
在ES目录下的config文件夹中的elasticSearch.yml文件中配置
4.查询数据
4.1包含列name和price列
curl -XGET 'http://localhost:9200/index/product/p2/?_source=name,price&pretty'
4.2不包含列name和price列
curl -XGET 'http://localhost:9200/index/product/p1?_source_exclude=name,price,&pretty'
4.3不显示_search中的文本
curl -XGET 'http://localhost:9200/index/product/p1?_source=false&pretty'
5.修改数据
curl -H "Content-Type: application/json" -XPOST http://localhost:9200/index/product/p2/_update?pretty -d '
{
"doc":{
"id":"1", "content":"小米笔记本" } }'
5.1.版本号问题
使用version可以指定版本号,但是不能乱写,只能写当前的版本号,如果更新成功则会更新版本号,失败则不会,例如添加的数据一致,ES就会就会把它当成同一个版本,不会更新。
6.删除数据
curl -XDELETE http://localhost:9200/index/product/p1?pretty
删除一个文档也不会立即从磁盘上移除,它只是被标记成已删除。ES将会在你之后添加更多索引的时候才会在后台进行删除内容的清理。
删除索引库和这个一样
7.简单查询
curl -XGET 'http://localhost:9200/index2/product/_search?q=name:小李&q=price:30&pretty'
8.分页查询
curl -XGET 'http://localhost:9200/index2/product/_search?size=2&from=0&pretty'
size:每页记录数
from:起始位置