【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}}'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM