elasticsearch 命令操作

1.创建索引

     curl -XPUT http://localhost:9200/index

     

  pretty:格式化

     curl -XPUT http://localhost:9200/myindex?pretty

     

  其他方式创建索引库

     curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/index1?pretty' -d '{
     "settings":{
        "index":{
              "number_of_shards":2, 
              "number_of_replicas":5
                     }
                      }
        }'

         number_of_shards:表示分多少片,默认为5个.

          number_of_replicas :表示每片有多少备份,默认为1个

2.查看索引

   curl -XGET http://localhost:9200/_cat/indices?v

   v:显示表头

   

3.插入数据

curl -H "Content-Type: application/json" -XPUT http://localhost:9200/index/product/p1 -d '{
          "name":"mac",
          "price":20000,
          "description":"苹果笔记本",
          "attr":["computer","高端"]
}'

如果不能预判数据库中是否有该文档编号,那么添加已经存在的文档编号,会变成对原有文档的修改。

小Tip:一旦文档被保存到索引库。和Java字符串相似。它就是不可变的。我们所谓的修改,其实是使用乐观锁机制
对我们的记录做了版本的标注。如果不想让ES默认第二个已经文档编号的添加形成称为修改行为,我们可以有如下两种方案。

curl -H "Content-Type: application/json" -XPUT http://localhost:9200/index/product/p1?op_type=create&pretty -d '{
"name":"mac",
"price":20000,
"description":"苹果笔记本",
"attr":["computer","高端"]
}'

curl -H "Content-Type: application/json" -XPUT http://localhost:9200/index/product/p1/_create -d '{
"name":"mac",
"price":20000,
"description":"苹果笔记本",
"attr":["computer","高端"]
}'

使用后明确表示为添加,如果id和type相同,则不会修改且报错

4.查询数据

    curl -XGET http://localhost:9200/index/product/p1?pretty

curl -XGET http://localhost:9200/index/product/p1?_source=name,price&pretty

你也可以禁掉source,只要设置_source=false即可

curl -XGET http://localhost:9200/index/product/p1?_source=false&pretty

如果你只想获取source中的一部分内容,还可以用_source_include或者_source_exclude来包含或者过滤其中的某些字段

curl -XGET http://localhost:9200/index/product/p1?_source_exclude=name&pretty

如果只想获取文档的内容,可以直接指定_source

curl -XGET http://localhost:9200/index/product/p1/_source?pretty

5.修改数据

curl -H "Content-Type: application/json" -XPOST http://localhost:9200/index/product/p1/_update?pretty -d '{

"doc":{
         "name":"Newmac",
          "price":2000,
          "description":"苹果笔记本",
           "attr":["computer","高端"]

         }
}'

 现

 

ES可以在操作中指定version参数,但必须为最新版本,不能多也不能少,否则报错

curl -H "Content-Type: application/json" -XPOST http://localhost:9200/index/product/p1/_update?version=4 -d '{

"doc":{

      "name":"新版Mac222",

        "price":15200

      }

  }'

6.删除数据

curl -XDELETE http://localhost:9200/index/product/p1?pretty

7.删除索引

curl -XDELETE http://localhost:9200/index1?pretty

8.简单查询

curl -XGET 'http://localhost:9200/myindex/product/_search?q=name:mac&q=price:20000&pretty'

分页

curl -XGET 'http://localhost:9200/blog/blog/_search?size=2&from=0&pretty'

原文地址:https://www.cnblogs.com/1822195505asd/p/9356107.html