【Elasticsearch学习之二】Elasticsearch Rest风格操作

环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  elasticsearch-2.2.0

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

REST的操作分为以下几种
  GET:获取对象的当前状态;
  PUT:改变对象的状态;
  POST:创建对象;
  DELETE:删除对象;
  HEAD:获取头信息。


ES内置的REST接口


二、curl-rest命令
curl属于linux命令,可以在命令行下访问url的一个工具,利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求
curl
  -X 指定http请求的方法
HEAD GET POST PUT DELETE
  -d 指定要传输的数据

1、创建索引库:wjy

[cluster@PCS101 bin]$ curl -XPUT http://196.168.123.101:9200/wjy/
{"acknowledged":true}
[cluster@PCS101 bin]$ 

2、创建类型(表):employee,插入一行记录,即文档document

#POST不指定ID 会自动生成ID;指定ID,会根据ID,如果存在该ID会更新,不存在就创建

[cluster@PCS101 bin]$ curl -XPOST http://196.168.123.101:9200/wjy/employee -d '
> {
>  "first_name" : "bin",
>  "age" : 33,
>  "about" : "I love to go rock climbing",
>  "interests": [ "sports", "music" ]
> }'
{"_index":"wjy","_type":"employee","_id":"AWlv8cAl4hD4em0kzO4U","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created":true}
  索引库          类型              编号                         版本         分片                                            是否创建成功

#PUT方式 后面必须指定ID编码,如果重复添加相同编码数据 会失败

[cluster@PCS101 bin]$ curl -XPUT http://196.168.123.101:9200/wjy/employee/1 -d '
> {
>  "first_name" : "god bin",
>  "last_name" : "pang",
>  "age" : 42,
>  "about" : "I love to go rock climbing",
>  "interests": [ "sports", "music" ]
> }'
{"_index":"wjy","_type":"employee","_id":"1","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created":true} 

3、增加field:sex

[cluster@PCS101 bin]$ curl -XPOST http://196.168.123.101:9200/wjy/employee -d '
> {
>  "first_name" : "pablo2",
>  "age" : 33,
>  "about" : "I love to go rock climbing",
>  "interests": [ "sports", "music" ],
>  "sex": "man"
> }'
{"_index":"wjy","_type":"employee","_id":"AWlv-tng4hD4em0kzO4W","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created":true}

 

4、获取数据 GET
(1)根据document的id来获取数据:pretty参数就是用json格式化的方式展示

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/1?pretty
{
  "_index" : "wjy",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "god bin",
    "last_name" : "pang",
    "age" : 42,
    "about" : "I love to go rock climbing",
    "interests" : [ "sports", "music" ]
  }
}

对比:

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/1
{"_index":"wjy","_type":"employee","_id":"1","_version":1,"found":true,"_source":
{
 "first_name" : "god bin",
 "last_name" : "pang",
 "age" : 42,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}}

(2)根据field来查询数据:_search根据索引查找

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?q=first_name="bin"
{"took":18,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.014065012,"hits":[{"_index":"wjy","_type":"employee","_id":"AWlv8cAl4hD4em0kzO4U","_score":0.014065012,"_source":
{
 "first_name" : "bin",
 "age" : 33,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}},{"_index":"wjy","_type":"employee","_id":"AWlv91mF4hD4em0kzO4V","_score":0.01125201,"_source":
{
 "first_name" : "gob bin",
 "age" : 43,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}},{"_index":"wjy","_type":"employee","_id":"1","_score":0.01125201,"_source":
{
 "first_name" : "god bin",
 "last_name" : "pang",
 "age" : 42,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}}]}}

(3)根据field来查询数据,参数封装起来传进去:match 匹配

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
>  "query":
>   {"match":
>    {"first_name":"bin"}
>   }
> }'
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv8cAl4hD4em0kzO4U",
      "_score" : 0.30685282,
      "_source" : {
        "first_name" : "bin",
        "age" : 33,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    }, {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv91mF4hD4em0kzO4V",
      "_score" : 0.19178301,
      "_source" : {
        "first_name" : "gob bin",
        "age" : 43,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    }, {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 0.19178301,
      "_source" : {
        "first_name" : "god bin",
        "last_name" : "pang",
        "age" : 42,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    } ]
  }
}

#对多个field发起查询:multi_match

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
>  "query":
>   {"multi_match":
>    {
>     "query":"bin",
>     "fields":["last_name","first_name"],
>     "operator":"and"
>    }
>   }
> }'
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.09415865,
    "hits" : [ {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv8cAl4hD4em0kzO4U",
      "_score" : 0.09415865,
      "_source" : {
        "first_name" : "bin",
        "age" : 33,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    }, {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv91mF4hD4em0kzO4V",
      "_score" : 0.058849156,
      "_source" : {
        "first_name" : "gob bin",
        "age" : 43,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    }, {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 0.058849156,
      "_source" : {
        "first_name" : "god bin",
        "last_name" : "pang",
        "age" : 42,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    } ]
  }
}

(4)多个term对多个field发起查询:bool(boolean),组合查询,must,must_not,should
#must + must : 交集

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
>  "query":
>   {"bool" :
>    {
>     "must" : 
>      {"match":
>       {"first_name":"bin"}
>      },
>     "must" : 
>      {"match":
>       {"age":33}
>      }
>    }
>   }
> }'
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.4339554,
    "hits" : [ {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv8cAl4hD4em0kzO4U",
      "_score" : 0.4339554,
      "_source" : {
        "first_name" : "bin",
        "age" : 33,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    } ]
  }
}

#must +must_not :差集

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
>  "query":
>   {"bool" :
>    {
>     "must" : 
>      {"match":
>       {"first_name":"bin"}
>      },
>     "must_not" : 
>      {"match":
>       {"age":33}
>      }
>    }
>   }
> }'
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.19178301,
    "hits" : [ {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv91mF4hD4em0kzO4V",
      "_score" : 0.19178301,
      "_source" : {
        "first_name" : "gob bin",
        "age" : 43,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    }, {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 0.19178301,
      "_source" : {
        "first_name" : "god bin",
        "last_name" : "pang",
        "age" : 42,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    } ]
  }
}

#must+must_not 查询first_name=bin的,或者年龄在20岁到33岁之间的

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
>  "query":
>   {"bool" :
>    {
>    "must" :
>     {"term" : 
>      { "first_name" : "bin" }
>     }
>    ,
>    "must_not" : 
>     {"range":
>      {"age" : { "from" : 20, "to" : 33 }
>     }
>    }
>    }
>   }
> }'
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.19178301,
    "hits" : [ {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "AWlv91mF4hD4em0kzO4V",
      "_score" : 0.19178301,
      "_source" : {
        "first_name" : "gob bin",
        "age" : 43,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    }, {
      "_index" : "wjy",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 0.19178301,
      "_source" : {
        "first_name" : "god bin",
        "last_name" : "pang",
        "age" : 42,
        "about" : "I love to go rock climbing",
        "interests" : [ "sports", "music" ]
      }
    } ]
  }
}

#must_not+must_not  : 并集

[cluster@PCS101 bin]$ curl -XGET http://196.168.123.101:9200/wjy/employee/_search?pretty -d '
> {
>  "query":
>   {"bool" :
>    {
>     "must_not" : 
>      {"match":
>       {"first_name":"bin"}
>      },
>     "must_not" : 
>      {"match":
>       {"age":33}
>      }
>    }
>   }
> }'
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

 

(5)删除

curl -XDELETE http://196.168.123.101:9200/test2/

 

(6)设置

#设置备份数
curl -XPUT 'http://196.168.123.101:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'

#设置分片数 备份数
curl -XPUT 'http://196.168.123.101:9200/test3/' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'

 

posted @ 2019-03-12 11:13  cac2020  阅读(2552)  评论(0编辑  收藏  举报