Elasticsearch API约定

Elasticsearch提供了一个REST API,通过HTTP通过JSON访问。 Elasticsearch使用以下约定 -

多索引

API中的大多数操作(主要是搜索和其他操作)用于一个或多个索引。 这有助于用户通过只执行一次查询来搜索多个位置或所有可用数据。 许多不同的符号用于在多个索引中执行操作。 我们将在本节讨论其中的一些。

逗号分隔符号

举例如下:

POST http://localhost:9200/index1,index2,index3/_search
{
   "query":{
      "query_string":{
         "query":"any_string"
      }
   }
}

响应结果
来自index1index2index3的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}}>对应,其中accountdetailstatic_name对应,date_math_exprnow-2d对应,date_formatYYYY.MM.dd对应)

http://localhost:9200/<accountdetail-{now-2d{YYYY.MM.dd|utc}}>/_search

在这里插入图片描述
讲解:static_name是表达式的一部分,在每个日期数学索引(如帐户详细信息)中保持相同。 date_math_expr包含动态确定日期和时间的数学表达式,如now-2ddate_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值