Easticsearch概述(API使用)二

Rest简介

  一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务端互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制

 

 

 

Rest的操作分为以下几种:

1、GET:获取对象的当前状态

2、PUT:改变对象的状态

3、POST:创建对象

4、DELETE:删除对象

5、HEAD:获取头信息

 

ES内置的API:

 

 

 

 

索引文档的语法curl用法:

CURL:简单认为可以在命令行下访问url的一个工具

curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求

curl

  -X 指定http请求的方法    

      GET   POST   PUT     DELETE           restful

  -d  指定要传输的数据

 

索引库的创建和删除:

创建索引库blog,默认分片5个,每个分片一个副本分片

curl -XPOST node3:9200/blog

 

 

POST创建type以及新建或修改文档  :创建表(类似数据库)

curl -XPOST http://node2:9200/blog/employee/_mapping -d '{
    "employee":{
        "properties":{
            "id":{
                "type":"long",
                "store":"yes",
                "index":"not_analyzed"
            },
            "first_name":{
                "type":"String",
                "store":"yes",
                "analyzer":"ik_max_word"
            },
            "about":{
                "type":"String",
                "store":"yes",
                "analyzer":"ik_max_word"
            },
            "iterests":{
                "type":"String",
                "store":"yes",
                "analyzer":"ik_max_word"
            }
        }
    }
}'

 

 

POST添加和删除:

#添加
curl -XPOST http://node2:9200/blog/employee -d '{
    "first_name":"bin",
    "age":33,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'
#修改
curl -XPOST http://node2:9200/blog/employee -d '{
    "first_name":"god bin",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'
#添加字段
curl -XPOST http://node2:9200/blog/employee -d '{
    "first_name":"god bin",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"],
    "sex":"man"
}'

#没有id为1的 添加
curl -XPOST http://node2:9200/blog/employee/1 -d '{
    "first_name":"tom",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'
#如果有id为1的修改
curl -XPOST http://node2:9200/blog/employee/1 -d '{
    "first_name":"tom",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'

 

 

PUT创建和修改文档:

#PUT创建文档
curl -XPUT http://node2:9200/blog/employee -d '{
    "first_name":"tom",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'
#PUT创建文档
curl -XPUT http://node2:9200/blog/employee/2 -d '{
    "first_name":"test bin",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'
#已存在修改 包括POST创建的文档也可以修改
curl -XPUT http://node2:9200/blog/employee/2 -d '{
    "first_name":"111",
    "age":43,
    "about":"I love es and redis",
    "iterests":["sports","muisc"]
}'

 

 

GET查询

#单字段查询
curl -XGET http://node2:9200/blog/employee/_search?pretty -d '{
    "query":{
        "match":{
            "first_name":"bin"
        }
    }
}'

#多字段查询
curl -XGET http://node2:9200/blog/employee/_search?pretty -d '{
    "query":{
        "multi_match":{
            "query":"bin",
            "fields":["first_name","laset_name"],
            "operator":"and"
        }
    }
}'

 

posted @ 2020-10-13 17:13  随心的风  阅读(211)  评论(0编辑  收藏  举报