Elasticsearch提供了一个REST API,通过HTTP通过JSON访问。 Elasticsearch使用以下约定 -
多索引
API中的大多数操作(主要是搜索和其他操作)用于一个或多个索引。 这有助于用户通过只执行一次查询来搜索多个位置或所有可用数据。 许多不同的符号用于在多个索引中执行操作。 我们将在本节讨论其中的一些。
逗号分隔符号
举例如下:
POST http://localhost:9200/index1,index2,index3/_search
{
"query":{
"query_string":{
"query":"any_string"
}
}
}
响应结果
来自index1
,index2
,index3
的JSON对象,这些JSON对象都包含any_string
字符串。
所有索引的_all关键字
POST http://localhost:9200/_all/_search //_all表示查询所有索引
{
"query":{
"query_string":{
"query":"any_string"
}
}
}
响应结果
得到来自所有索引的包含“any_string”字符串的JSON对象,
通配符(*,+, - )
POST http://localhost:9200/school*/_search
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
响应结果:
从以school
开头的索引库中查询到包含字符串“CBSE
”的JSON对象
扩展:
POST http://localhost:9200/school*,-schools_gov /_search
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
响应结果:
对所有以“school
”开头,但不是schools_gov
的索引库进行搜索,然后得到包含字符串“CBSE”的JSON对象
其他参数
ignore_unavailable参数的讲解
举例说明如下:
如果URL中存在的一个或多个索引不存在,则不会发生错误或操作不会停止。 例如,schools
索引存在,但book_shops
不存在
POST http://localhost:9200/school*,book_shops/_search
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
响应结果报错如下: (原因就是因为book_shops这个索引库不存在)
{
"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
}
所以我们需要修改一下: 在URL后面加上?ignore_unavailable = true
POST http://localhost:9200/school*,book_shops/_search?ignore_unavailable = true
{
"query":{
"query_string":{
"query":"CBSE"
}
}
}
响应(无错误)
在以school开头的索引库中进行搜索,得到包含字符串CBSE
的JSON对象
allow_no_indices参数讲解
如果从带有通配符的网址中没有找到索引,这个参数是true
值时将防止错误,
举例说明: 如果在elasticSearch里面不存在以schools_pri
开头的索引,我们却用下面的代码去搜索,那么就会报错
POST http://localhost:9200/schools_pri*/_search
{
"query":{
"match_all":{}
}
}
做一下修改来解决这个报错 (在URL后面加上?allow_no_indices = true
即可)
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":[]}
}
expand_wildcards
设置是否扩展通配符到closed的index中,open表示只在open的index中查询,closed表示在匹配的所有的index中查询, 默认为closed, 例如查询已经关闭的index
举2个例子来说明该参数的作用:
①第一个例子:
查询已经关闭的名为test1
索引
输入:
GEt /test*/_search?expand_wildcards=closed
输出:
{ //这个test1就是我们关闭的索引
"error": "IndexClosedException[[test1] closed]",
"status": 403
}
②第二个例子
我们先执行下面的代码把schools这个索引给关闭
POST http://localhost:9200/schools/_close
然后我们再去用如下通配符查找这个索引库的话,就会找不到了
POST http://localhost:9200/school*/_search
但是我们如果加上?expand_wildcards = closed
,见如下代码
POST http://localhost:9200/school*/_search?expand_wildcards = closed
{
"query":{
"match_all":{}
}
}
就可以找到关闭了的索引库
{
"error":{
"root_cause":[{ //下面的schools就是我们关闭的索引
"type":"index_closed_exception", "reason":"closed", "index":"schools"
}],
//下面的schools就是我们关闭的索引
"type":"index_closed_exception", "reason":"closed", "index":"schools"
}, "status":403
}
日期索引名称中的数学支持
Elasticsearch提供了根据日期和时间搜索索引的功能。我们需要以特定格式指定日期和时间。 例如,accountdetail-2015.12.30
,索引将存储2015年12月30日的银行帐户详细信息。可以执行数学操作以获取特定日期或日期和时间范围的详细信息。
日期数字索引名称的格式:
<static_name{date_math_expr{date_format|time_zone}}>
和下面的代码做对比(<accountdetail-{now-2d{YYYY.MM.dd|utc}}>
和<static_name{date_math_expr{date_format|time_zone}}>
对应,其中accountdetail
和static_name
对应,date_math_expr
和now-2d
对应,date_format
和YYYY.MM.dd
对应)
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
美化结果
可以通过附加一个网址查询参数(即pretty = true
),获得格式正确的JSON对象的响应。
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"
}
}
………………….
响应过滤
可以通过将其添加到field_path
参数中来过滤对较少字段的响应。 例如,
POST http://localhost:9200/schools/_search?filter_path = hits.total
{
"query":{
"match_all":{}
}
}
响应的结果
{"hits":{"total":3}}
本文转载自:https://www.yiibai.com/elasticsearch/elasticsearch_api_conventions.html