The Search API
来源:互联网 发布:淘宝模板软件破解版 编辑:程序博客网 时间:2024/05/17 08:49
现在 我们开始使用一些简单的搜索, 有两种基本的方式来运行搜索。一个是通过发送搜索参数通过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()."\n"; #获取的是响应正文 {"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 是完成了请求 不在维护任何类型的服务端资源
阅读全文
0 0
- The Search API
- Marklogic search development -2.1Understanding the Search API
- .NET Framework Solutions: In Search of the Lost Win32 API
- Google Search API Worms
- 苹果Search API
- ios9:Search API
- Elastic Search RESTful API
- iTunes Search API 翻译
- elasticsearch之Search API
- Drupal7 API Search interface
- The Semantic Search Engine
- The Google Search toolbar
- Moving the Search Box
- the summary of search
- elasticsearch Java API [2.3] » Search API
- Elasticsearch java API (11)Search API scrolls
- Elasticsearch JAVA API 之 Search API
- GitHub API 学习笔记 Search API
- STM32常见问题整理
- java jdk配置
- 66_常用类_DateFormat和SimpleDateFormat_时间和字符串的互相转换
- iOS在image上画文字-画图片
- 学习笔记——JAVA设计模式<14>中介者模式
- The Search API
- 升级 phpStudy 中 MySQL 版本至 5.7.17
- [bzoj2763]2763: [JLOI2011]飞行路线 分层图最短路
- SVN 错误:Unable to create pristine install stream
- 5-18 银行业务队列简单模拟 (25分)
- Linux之死锁产生条件&解决方法
- 十七、IDEA 常用快捷键整理
- Apache Zeppelin配置
- CentOS7.0下mysql服务启动失败的快速解决办法