zoukankan      html  css  js  c++  java
  • The Search API

    现在 我们开始使用一些简单的搜索, 有两种基本的方式来运行搜索。
    
    一个是通过发送搜索参数通过RSET请求URI, 另外的通过RESET 请求体发送。
    
    请求body 方法允许你更加有表现力的,定义你的搜索是更加可读的JSON格式
    
    我们尝试一个请求URI 方法的例子但是这个章节的剩余部分,我们会专门使用请求body 方法。
    
    
    The REST API  用于搜索是可访问的从_search endpoint. 这个例子返回所有的文档在bank index.
    
    
    让我们首先分析搜索调用,我们是搜索(_search endpoint) 在bankindex,
    
    q=* parameter 通知Elasticsearch 来匹配所有的文档在索引里
    
    sort=account_number:asc 参数表明 排序 结果集使用account_number  字段
    
    
    pretty  参数 告诉Elasticsearch  返回漂亮的JSON结果
    
    
    
    
    
    
    GET /test/_search?q=*&sort=account_number:asc&pretty
    
    [elk@node01 api]$ curl -H "Content-Type: application/json" -XPOST 'http://192.168.137.2:9200/test/account/_bulk?pretty&refresh' --data-binary "@bb.json"(_search endpoint)
    
    {
      "took": 15,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 5,
        "max_score": null,
        "hits": [
          {
            "_index": "test",
            "_type": "account",
            "_id": "1",
            "_score": null,
            "_source": {
              "account_number": 1,
              "balance": 39225,
              "firstname": "Amber",
              "lastname": "Duke",
              "age": 32,
              "gender": "M",
              "address": "880 Holmes Lane",
              "employer": "Pyrami",
              "email": "amberduke@pyrami.com",
              "city": "Brogan",
              "state": "IL"
            },
            "sort": [
              1
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "6",
            "_score": null,
            "_source": {
              "account_number": 6,
              "balance": 5686,
              "firstname": "Hattie",
              "lastname": "Bond",
              "age": 36,
              "gender": "M",
              "address": "671 Bristol Street",
              "employer": "Netagy",
              "email": "hattiebond@netagy.com",
              "city": "Dante",
              "state": "TN"
            },
            "sort": [
              6
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "13",
            "_score": null,
            "_source": {
              "account_number": 13,
              "balance": 32838,
              "firstname": "Nanette",
              "lastname": "Bates",
              "age": 28,
              "gender": "F",
              "address": "789 Madison Street",
              "employer": "Quility",
              "email": "nanettebates@quility.com",
              "city": "Nogal",
              "state": "VA"
            },
            "sort": [
              13
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "18",
            "_score": null,
            "_source": {
              "account_number": 18,
              "balance": 4180,
              "firstname": "Dale",
              "lastname": "Adams",
              "age": 33,
              "gender": "M",
              "address": "467 Hutchinson Court",
              "employer": "Boink",
              "email": "daleadams@boink.com",
              "city": "Orick",
              "state": "MD"
            },
            "sort": [
              18
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "20",
            "_score": null,
            "_source": {
              "account_number": 20,
              "balance": 16418,
              "firstname": "Elinor",
              "lastname": "Ratliff",
              "age": 36,
              "gender": "M",
              "address": "282 Kings Place",
              "employer": "Scentric",
              "email": "elinorratliff@scentric.com",
              "city": "Ribera",
              "state": "WA"
            },
            "sort": [
              20
            ]
          }
        ]
      }
    }
    
    
    GET /test/_search?q=Amber&sort=account_number:asc&pretty
    
    
    
    至于响应,我们看几个部分:
    
    {
      "took": 20,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": null,
        "hits": [
          {
            "_index": "test",
            "_type": "account",
            "_id": "1",
            "_score": null,
            "_source": {
              "account_number": 1,
              "balance": 39225,
              "firstname": "Amber",
              "lastname": "Duke",
              "age": 32,
              "gender": "M",
              "address": "880 Holmes Lane",
              "employer": "Pyrami",
              "email": "amberduke@pyrami.com",
              "city": "Brogan",
              "state": "IL"
            },
            "sort": [
              1
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "99",
            "_score": null,
            "_source": {
              "account_number": 99,
              "balance": 39225,
              "firstname": "Amber",
              "lastname": "Duke",
              "age": 32,
              "gender": "M",
              "address": "880 Holmes Lane",
              "employer": "Pyrami",
              "email": "amberduke@pyrami.com",
              "city": "Brogan",
              "state": "IL"
            },
            "sort": [
              99
            ]
          }
        ]
      }
    }
    
    至于响应,我们看下面几个部分:
    
    1.took: Elasticsearch  执行搜索单位毫秒
    
    2.timed_out  告诉我们如果搜索是否超时
    
    3._shards 告诉我们搜索分片的数量
    
    
    下面是使用使用请求body 方法:
    
    GET /test/_search
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
         
      ],
      "fields":  [ "account_number", "firstname", "lastname" ]
    }
    
    
    
    
    
    
    {
      "took": 4,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 6,
        "max_score": null,
        "hits": [
          {
            "_index": "test",
            "_type": "account",
            "_id": "1",
            "_score": null,
            "fields": {
              "account_number": [
                1
              ],
              "firstname": [
                "Amber"
              ],
              "lastname": [
                "Duke"
              ]
            },
            "sort": [
              1
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "6",
            "_score": null,
            "fields": {
              "account_number": [
                6
              ],
              "firstname": [
                "Hattie"
              ],
              "lastname": [
                "Bond"
              ]
            },
            "sort": [
              6
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "13",
            "_score": null,
            "fields": {
              "account_number": [
                13
              ],
              "firstname": [
                "Nanette"
              ],
              "lastname": [
                "Bates"
              ]
            },
            "sort": [
              13
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "18",
            "_score": null,
            "fields": {
              "account_number": [
                18
              ],
              "firstname": [
                "Dale"
              ],
              "lastname": [
                "Adams"
              ]
            },
            "sort": [
              18
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "20",
            "_score": null,
            "fields": {
              "account_number": [
                20
              ],
              "firstname": [
                "Elinor"
              ],
              "lastname": [
                "Ratliff"
              ]
            },
            "sort": [
              20
            ]
          },
          {
            "_index": "test",
            "_type": "account",
            "_id": "99",
            "_score": null,
            "fields": {
              "account_number": [
                99
              ],
              "firstname": [
                "Amber"
              ],
              "lastname": [
                "Duke"
              ]
            },
            "sort": [
              99
            ]
          }
        ]
      }
    }
    
    利用perl 实现:
    
    [elk@node01 api]$ cat a10.pl 
    ##发送消息
    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/test/_search";  
       my $post ={
      "query" => { "match_all" => {} },
      "sort" => [
        { "account_number" => "asc" }
         
      ],
      "fields" =>  [ "account_number", "firstname", "lastname" ]
    };
    
            
        
        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()."
    ";            #获取的是响应正文  
    
    	
    	{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":6,"max_score":null,
    	"hits":[{"_index":"test","_type":"account","_id":"1","_score":null,
    	"fields":{"account_number":[1],"firstname":["Amber"],"lastname":["Duke"]},"sort":[1]},
    	{"_index":"test","_type":"account","_id":"6","_score":null,
    	"fields":{"account_number":[6],"firstname":["Hattie"],"lastname":["Bond"]},"sort":[6]},
    	{"_index":"test","_type":"account","_id":"13","_score":null,
    	"fields":{"account_number":[13],"firstname":["Nanette"],"lastname":["Bates"]},"sort":[13]},
    	{"_index":"test","_type":"account","_id":"18","_score":null,"fields":{"account_number":[18],
    	"firstname":["Dale"],"lastname":["Adams"]},"sort":[18]},{"_index":"test","_type":"account","_id":"20","_score":null,
    	"fields":{"account_number":[20],"firstname":["Elinor"],"lastname":["Ratliff"]},"sort":[20]},
    	{"_index":"test","_type":"account","_id":"99","_score":null,"fields":{"account_number":[99],
    	"firstname":["Amber"],"lastname":["Duke"]},"sort":[99]}]}}
    
    不同的是 代替了q=* in the URI, 我们POST 一个JSON累心的查询请求体到_search API.
    
    我们会讨论JSON 查询在下面一个章节。
    
    
    重要的是了解一旦你得到搜索结果返回,
    
    Elasticsearch  是完成了请求 不在维护任何类型的服务端资源

  • 相关阅读:
    Android Studio 修改Logcat的颜色
    Android Studio 视图解析
    Android应用Design Support Library完全使用实例
    Android5.x新特性之 Toolbar和Theme的使用
    常见Android Native崩溃及错误原因
    判断App整体处于前台还是后台
    ubuntu学习: apt-get命令
    docker 学习笔记20:docker守护进程的配置与启动
    docker学习笔记18:Dockerfile 指令 VOLUME 介绍
    docker学习笔记17:Dockerfile 指令 ONBUILD介绍
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349688.html
Copyright © 2011-2022 走看看