zoukankan      html  css  js  c++  java
  • 5. Elastisearch API CURD操作

    5. Elastisearch API CURD操作
    
    shards: 碎片
    
    replicas 备份片数量
    
    CURL -XPUT 'http://192.168.1.10:9200/library/' -d '{
        "settings": {
            "index": {
                    "number_of_shards": 5,
                    "number_of_replicas": 1
            }
        }
    }'
    
    
    library:索引名称
    
    
    PUT http://192.168.137.2:9200/library/
    {
     "settings": {
         "index": {
             "number_of_shards": 5,
             "number_of_replicas": 1
           }
    }
    }
    
    
    返回:
    {
      "acknowledged": true
    }
    
    索引信息:
    {
    
        "state": "open",
        "settings": {
            "index": {
                "creation_date": "1495520852452",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "QP0uOey4RBKSnqwwZFgkSA",
                "version": {
                    "created": "2030499"
                }
            }
        },
        "mappings": { },
        "aliases": [ ]
    
    }
    
    
    利用perl api创建:
    
    ##发送消息
    use  LWP::UserAgent; 
    use LWP;
    use Encode;
    use LWP::Simple;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use URI::Escape;
    use URI::URL;
    use JSON;
    use Data::Dumper;
      my $ua = LWP::UserAgent->new;
         $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
      my $cookie_jar = HTTP::Cookies->new(
         file=>'lwp_cookies.txt',
         autosave=>1,
         ignore_discard=>1);
         $ua->cookie_jar($cookie_jar);  
       my     $login_url ="http://192.168.137.2:9200/czcb";  
       my $post = {  
            settings => { 
              index=>{		
              "number_of_shards"=> 5,
              "number_of_replicas"=>1
              }			
            }  
        };  
        use JSON qw(encode_json);  
        $json_string = encode_json($post);  
      
        my $req = HTTP::Request->new(  
            'POST' => $login_url
        );  
        $req->content_type('application/json; charset=UTF-8')  
          ;    #post请求,如果有发送参数,必须要有这句  
        $req->content("$json_string");    #发送post的参数  
        my $res = $ua->request($req);  
        print $res->content();            #获取的是响应正文  
    	
    	
    	
    	获取索引信息:
    	GET http://192.168.137.2:9200/library/
    	
    	{
      "library": {
        "aliases": {},
        "mappings": {},
        "settings": {
          "index": {
            "creation_date": "1495520852452",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "QP0uOey4RBKSnqwwZFgkSA",
            "version": {
              "created": "2030499"
            }
          }
        },
        "warmers": {}
      }
    }
    
    
    
    perl 通过api操作:
    use  LWP::UserAgent;   
    my $ua = LWP::UserAgent->new;  
    $ua->timeout(10);  
    $ua->env_proxy;  
    $ua->agent("Mozilla/8.0");  
    my $host = "http://192.168.137.2:9200/library/";  
    my $response = $ua->get($host);  
       $ua->default_headers;  
      if ($response->is_success) {  
          print $response->decoded_content;  # or whatever  
       }  
        else {  
        die $response->status_line;  
    } 
    
    
    C:UsersTLCBDesktopelkElasticsearch Api>perl get1.pl
    {"library":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"149
    5520852452","number_of_shards":"5","number_of_replicas":"1","uuid":"QP0uOey4RBKS
    nqwwZFgkSA","version":{"created":"2030499"}}},"warmers":{}}}
    C:UsersTLCBDesktopelkElasticsearch Api>
    
    
    
    #----------------------------------------
    # 创建一个索引
    #       ---- 索引名称
    #       |
    #       |     Type 名称
    #       |     |
    #       |     |    文档ID
    #       |     |    |
    #       V     V    V
    PUT /library/books/2
    {
      "title": "Elasticsearch: The Definitive Guide007",
      "name" : {
        "first" : "Zachary",
        "last" : "Tong"
      },
      "publish_date":"2015-02-06",
      "price":"49.99"
    }
    
    /********************************************************
    PUT /library/books/2
    {
      "title": "Elasticsearch: The Definitive Guide007",
      "name" : {
        "first" : "Zachary",
        "last" : "Tong"
      },
      "publish_date":"2015-02-06",
      "price":"49.99"
    }
    
    {
      "_index": "library",
      "_type": "books",
      "_id": "2",
      "_version": 1,
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": true
    }
    
    
    使用perl api创建
    
    use  LWP::UserAgent;   
    my $ua = LWP::UserAgent->new;  
    $ua->timeout(10);  
    $ua->env_proxy;  
    $ua->agent("Mozilla/8.0");  
    my $host = "http://192.168.137.2:9200/library/books/2";  
    my $response = $ua->get($host);  
       $ua->default_headers;  
      if ($response->is_success) {  
          print $response->decoded_content;  # or whatever  
       }  
        else {  
        die $response->status_line;  
    } 
    
    {"_index":"library","_type":"books","_id":"2","_version":1,"found":true,"_source
    ":{
      "title": "Elasticsearch: The Definitive Guide007",
      "name" : {
        "first" : "Zachary",
        "last" : "Tong"
      },
      "publish_date":"2015-02-06",
      "price":"49.99"
    }
    }
    
    ##发送消息
    use  LWP::UserAgent; 
    use LWP;
    use Encode;
    use LWP::Simple;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use URI::Escape;
    use URI::URL;
    use JSON;
    use Data::Dumper;
      my $ua = LWP::UserAgent->new;
         $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
      my $cookie_jar = HTTP::Cookies->new(
         file=>'lwp_cookies.txt',
         autosave=>1,
         ignore_discard=>1);
         $ua->cookie_jar($cookie_jar);  
       my $login_url ="http://192.168.137.2:9200/zhou/books/2";  
       my $post = {
               "title"=>"testtesttest",   
               "user" => "kimchy",
        "post_date" => "2009-11-15T14:12:12",
        "message" => "trying out Elasticsearch999999"
            }  ;
        
        use JSON qw(encode_json);  
        $json_string = encode_json($post);  
      
        my $req = HTTP::Request->new(  
            'PUT' => $login_url
        );  
        $req->content_type('application/json; charset=UTF-8')  
          ;    #post请求,如果有发送参数,必须要有这句  
        $req->content("$json_string");    #发送post的参数  
        my $res = $ua->request($req);  
        print $res->content();            #获取的是响应正文  
    	
    	
    
    

  • 相关阅读:
    JS 数组
    JS 模拟彩票
    C++ 动态内存
    计算机网络--OSI七层模型
    C++ 异常处理
    C++ 文件和流
    数据库学习教程网站
    数据结构--哈夫曼树
    数据结构--红黑树
    数据结构--伸展树
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349705.html
Copyright © 2011-2022 走看看