Elasticsearch提供了一个REST API,经过HTTP经过JSON访问。 Elasticsearch使用如下约定 -html
API中的大多数操做(主要是搜索和其余操做)用于一个或多个索引。 这有助于用户经过只执行一次查询来搜索多个位置或全部可用数据。 许多不一样的符号用于在多个索引中执行操做。 咱们将在本节讨论其中的一些。json
举例以下:api
POST http://localhost:9200/index1,index2,index3/_search { "query":{ "query_string":{ "query":"any_string" } } }
响应结果
来自index1
,index2
,index3
的JSON对象,这些JSON对象都包含any_string
字符串。markdown
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
举例说明以下:
若是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 = true
orm
POST http://localhost:9200/school*,book_shops/_search?ignore_unavailable = true { "query":{ "query_string":{ "query":"CBSE" } } }
响应(无错误)
在以school开头的索引库中进行搜索,获得包含字符串CBSE
的JSON对象
若是从带有通配符的网址中没有找到索引,这个参数是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":[]} }
设置是否扩展通配符到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