Web中的应用程序编程接口(API)是一组函数调用或其他编程指令,用于访问该特定Web应用程序中的软件组件.例如,Facebook API帮助开发人员通过访问Facebook的数据或其他功能来创建应用程序;它可以是出生日期或状态更新.
Elasticsearch提供REST API,JSON通过HTTP访问. Elasticsearch使用以下约定 :
API中的大多数操作(主要是搜索和其他操作)都是针对一个或多个一个指数.这有助于用户通过执行一次查询来搜索多个地方或所有可用数据.许多不同的符号用于在多个索引中执行操作.我们将在本节讨论其中的一些.
POST http://localhost:9200/index1,index2,index3/_search
{ "query":{ "query_string":{ "query":"any_string" } } }
来自index1,index2,index3的JSON对象,其中包含any_string.
所有索引的
POST http://localhost:9200/_all/_search
{ "query":{ "query_string":{ "query":"any_string" } } }
来自所有索引的JSON对象并且其中包含any_string.
POST http://localhost:9200/school */_ search
{ "query":{ "query_string":{ "query":"CBSE" } } }
所有从学校开始有CBSE的索引的JSON对象.
或者,您也可以使用以下代码和减号;
POST http://localhost:9200/school*,-schools_gov /_search
{ "query":{ "query_string":{ "query":"CBSE" } } }
来自所有索引的JSON对象,以"school"开头但不是来自schools_gov并且其中包含CBSE.
还有一些 URL查询字符串参数 :
ignore_unavailable : 如果URL中存在的一个或多个索引不存在,则不会发生错误或操作将不会停止.例如,学校索引存在,但book_shops不存在 :
POST http://localhost:9200/school*,book_shops/_search
{ "query":{ "query_string":{ "query":"CBSE" } } }
{ "error":{ "root_cause":[{ "type":"index_not_found_exception", "reason":"no such index", "resource.type":"index_or_alias", "resource.id":"book_shops", "index":"book_shops" }], "type":"index_not_found_exception", "reason":"no such index", "resource.type":"index_or_alias", "resource.id":"book_shops", "index":"book_shops" },"status":404 }
看一下以下代码 :
POST http://localhost:9200/school*,book_shops/_search?ignore_unavailable = true
{ "query":{ "query_string":{ "query":"CBSE" } } }
所有索引的JSON对象,从学校开始有CBSE.
true 此参数的值将防止错误,如果带有通配符的URL不会导致索引.
例如,没有以schools_pri : 开头的索引;
POST http://localhost:9200/schools_pri*/_search?allow_no_indices = true
{ "query":{ "match_all":{} } }
{ "took":1,"timed_out": false, "_shards":{"total":0, "successful":0, "failed":0}, "hits":{"total":0, "max_score":0.0, "hits":[]} }
此参数决定是否需要将通配符扩展为打开索引或关闭索引或两者.此参数的值可以是打开和关闭,也可以是全部.
例如,关闭索引学校 :
POST http://localhost:9200/schools/_close
{"acknowledged":true}
查看以下代码 :
POST http://localhost:9200/school*/_search?expand_wildcards = closed
{ "query":{ "match_all":{} } }
{ "error":{ "root_cause":[{ "type":"index_closed_exception", "reason":"closed", "index":"schools" }], "type":"index_closed_exception", "reason":"closed", "index":"schools" }, "status":403 }
Elasticsearch提供了根据日期和时间搜索索引的功能.我们需要以特定格式指定日期和时间.例如,accountdetail-2015.12.30,index将存储2015年12月30日的银行帐户详细信息.可以执行数学操作以获取特定日期或日期和时间范围的详细信息.
日期数学索引名称的格式 :
<static_name{date_math_expr{date_format|time_zone}}> http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_search
static_name是表达式的一部分,它在每个日期数学索引(如帐户详细信息)中保持相同. date_math_expr包含动态确定日期和时间的数学表达式,如now-2d. date_format包含以YYYY.MM.dd之类的索引编写日期的格式.如果今天的日期是2015年12月30日,那么< accountdetail- {now-2d {YYYY.MM.dd}}>将返回accountdetail-2015.12.28.
表达式 | 解析为 |
---|---|
< accountdetail- {now-d}> | accountdetail-2015.12.29 |
< accountdetail- {now-M}> | accountdetail-2015.11.30 |
< accountdetail- {now {YYYY.MM}}> | accountdetail-2015.12 |
我们现在将看到Elasticsearch中可用的一些常用选项,可用于以指定的格式获取响应.
我们可以通过附加一个URL查询参数来获得格式良好的JSON对象的响应,即,pretty = true.
POST http://localhost:9200/schools/_search?pretty = true
{ "query":{ "match_all":{} } }
…………………….. { "_index" : "schools", "_type" : "school", "_id" : "1", "_score" : 1.0, "_source":{ "name":"Central School", "description":"CBSE Affiliation", "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115", "location": [31.8955385, 76.8380405], "fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5" } } ………………….
此选项可将统计响应更改为人类可读形式(如果human = true)或计算机可读形式(如果人类=假).例如,如果human = true则distance_kilometer = 20KM,如果human = false则distance_meter = 20000,当响应需要由另一个计算机程序使用时.
我们可以通过在field_path参数中添加响应来过滤对较少字段的响应.例如,
POST http://localhost:9200/schools/_search?filter_path = hits.total
{ "query":{ "match_all":{} } }
{"hits":{"total":3}}