Elasticsearch API约定

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

多索引

API中的大多数操做(主要是搜索和其余操做)用于一个或多个索引。 这有助于用户经过只执行一次查询来搜索多个位置或全部可用数据。 许多不一样的符号用于在多个索引中执行操做。 咱们将在本节讨论其中的一些。json

逗号分隔符号

举例以下:api

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

响应结果
来自index1index2index3的JSON对象,这些JSON对象都包含any_string字符串。markdown

全部索引的_all关键字

POST http://localhost:9200/_all/_search    //_all表示查询全部索引
{
   "query":{
      "query_string":{
         "query":"any_string"
      }
   }
}

响应结果
获得来自全部索引的包含“any_string”字符串的JSON对象,yii

通配符(*,+, - )

POST http://localhost:9200/school*/_search
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

响应结果:
从以school开头的索引库中查询到包含字符串“CBSE”的JSON对象elasticsearch

扩展:

POST http://localhost:9200/school*,-schools_gov /_search
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

响应结果:
对全部以“school”开头,但不是schools_gov的索引库进行搜索,而后获得包含字符串“CBSE”的JSON对象ide

其余参数

ignore_unavailable参数的讲解

举例说明以下:
若是URL中存在的一个或多个索引不存在,则不会发生错误或操做不会中止。 例如,schools 索引存在,但book_shops不存在spa

POST http://localhost:9200/school*,book_shops/_search
{
   "query":{
      "query_string":{
         "query":"CBSE"
      }
   }
}

响应结果报错以下: (缘由就是由于book_shops这个索引库不存在)code

{
   "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 = trueorm

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

相关文章
相关标签/搜索