004 API约定
在具体的学习前,我还是决定学一下,REST风格中在ES中的约定。
一:索引
1.多重索引
先准备数据:
如果不小心,json里的值写错了,修改过来,重新执行即可。
PUT index1/_doc/1
{
"name":"tom1"
}
PUT index2/_doc/1
{
"name":"tom1",
"age":20
}
PUT index3/_doc/1
{
"name":"tom3",
"address":"tom3 is good ,in beijing"
}
多重索引:
POST /index1,index2,index3/_search
{
"query":{
"query_string": {
"query":"tom1"
}
}
}
结果:
返回index1,index2,index3中包含tom1的Json对象,如果只输入tom,则是查询不到值的。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "index2",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.6931472,
"_source" : {
"name" : "tom1",
"age" : 20
}
},
{
"_index" : "index1",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "tom1"
}
}
]
}
}
2._all关键字
所有的索引
POST _all/_search
{
"query":{
"query_string": {
"query":"tom3"
}
}
}
效果:
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 6,
"successful" : 6,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "index3",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "tom3",
"address" : "tom3 is good ,in beijing"
}
}
]
}
}
3.通配符的使用
通配符主要有:* ,+ ,-
*:
index开头的索引中查找有tom3的JSON对象。
POST /index*/_search
{
"query":{
"query_string": {
"query":"tom3"
}
}
}
-:
不包含index2的索引中查找
POST /index*,-index2/_search
{
"query":{
"query_string": {
"query": "tom1"
}
}
}
4.URL查询字符串参数
①ignore_unavailable:如果不存在索引,一个或者多个,都不会停止,继续运行
POST /index1,index4/_search
{
"query":{
"query_string": {
"query": "tom1"
}
}
}
则有问题:
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [index4]",
"resource.type" : "index_or_alias",
"resource.id" : "index4",
"index_uuid" : "_na_",
"index" : "index4"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [index4]",
"resource.type" : "index_or_alias",
"resource.id" : "index4",
"index_uuid" : "_na_",
"index" : "index4"
},
"status" : 404
}
加上参数:
POST /index1,index4/_search?ignore_unavailable=true
{
"query":{
"query_string": {
"query": "tom1"
}
}
}
效果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "index1",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "tom1"
}
}
]
}
}
②allow_no_indices:如果没有通配符指定的索引,true可以防止报错
POST /mmm*/_search?allow_no_indices=true
{
"query":{
"query_string": {
"query": "tom1"
}
}
}
效果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 0,
"successful" : 0,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [ ]
}
}
二:响应
1.响应信息过滤
数据:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "index1",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "tom1"
}
},
{
"_index" : "index2",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "tom1",
"age" : 20
}
}
]
}
}
过滤:
POST /index1,index2/_search?filter_path=hits.hits._source.age
{
"query":{
"query_string": {
"query": "tom1"
}
}
}
效果:
{
"hits" : {
"hits" : [
{
"_source" : {
"age" : 20
}
}
]
}
}
2.漂亮的结果
Post /index1/_search?pretty=true
{
"query":{
"match_all": {}
}
}
004 API约定的更多相关文章
- ECharts之force力导向布局图——数据源说明及后端API约定
Echarts ? 关于 Echarts 请移步这里 force 力导向图 实现方式,如: function require_EC () { require( [ 'echarts', //载入for ...
- elasticsearch api约定
elasticsearch REST API 使用JSON通过HTTP协议传输. 本约定贯穿整个REST API,除非有特别的说明. 一.多重索引 大多数APIs引用到一个index参数来在多个索引中 ...
- elasticsearch 第四篇(API约定)
对多个indices进行操作 es中大多resetapi支持请求多个index, 例如”test1,test2,test3”,index也可以使用通配符, 例如”test*“, 还可以使用+,-来包含 ...
- EF Code First教程-02.1 Fluent API约定配置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Entity Framework 5.0系列之约定配置
Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来复杂,大家也都试图将软件设计的越来越灵活,很多内容我们都希望是可配置的, ...
- 【转】Entity Framework 5.0系列之约定配置
Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来复杂,大家也都试图将软件设计的越来越灵活,很多内容我们都希望是可配置的, ...
- ASP.NET Web API queryString访问的一点总结
自从开始使用ASP.NET Web API,各种路由的蛋疼问题一直困扰着我,相信大家也都一样. Web API的路由配置与ASP.MVC类似,在App_Start文件夹下,有一个WebApiConfi ...
- 使用Etherscan API通过区块号获取块及叔块奖励
本文原文链接 点击这里获取Etherscan API 中文文档(完整版) 完整内容排版更好,推荐读者前往阅读. 区块(Blocks) 区块相关的 API,接口的参数说明请参考Etherscan API ...
- Etherscan API 中文文档-交易以及检查交易收据状态
本文原文链接 点击这里获取Etherscan API 中文文档(完整版) 完整内容排版更好,推荐读者前往阅读. 交易(Transaction) 交易相关的 API,接口的参数说明请参考Ethersca ...
随机推荐
- python算法与数据结构-快速排序算法(36)
一.快速排序的介绍 快速排序(英语:Quicksort),又称划分交换排序(partition-exchange sort),通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外 ...
- LG4720 【模板】扩展卢卡斯定理
扩展卢卡斯定理 求 \(C_n^m \bmod{p}\),其中 \(C\) 为组合数. \(1≤m≤n≤10^{18},2≤p≤1000000\) ,不保证 \(p\) 是质数. Fading的题解 ...
- spring-boot maven插件
Spring Boot Maven Plugin提供了Spring Boot的Maven支持,允许你打包可执行文件和war文件,并且就地运行. 1.Spring Boot Maven plugin的5 ...
- Spring源码窥探之:ImportSelector
1. 编写实现ImportSelector的类 /** * @author 70KG * @Title: SelectImportBean * @Description: * @date 2018/7 ...
- LightOJ - 1354 - IP Checking(进制)
链接: https://vjudge.net/problem/LightOJ-1354 题意: An IP address is a 32 bit address formatted in the f ...
- Backpack II
Description There are n items and a backpack with size m. Given array A representing the size of eac ...
- shiro授权+注解式开发
shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...
- 深入了解js函数
<script> //深入了解函数1.函数参数:2.函数的返回值:3. 变量的作用域:4.自调用函数 //函数的定义 function test(){ console.log(" ...
- (尚003).Vue_模板语法
1.双大括号表达式 2.指令一:强制数据绑定 3.指令二;绑定事件监听 test003.html<!DOCTYPE html><html lang="en"> ...
- 【EF】vs2017中没有EF模型
在添加->新建项目 中找不到实体模型? 或者 在vs中打开edmx文件时,显示的只有文本,没有图形模式 原因:是因为没有安装实体模型插件 解决方法: 1.打开网址 https://marketp ...