在具体的学习前,我还是决定学一下,REST风格中在ES中的约定。
一:索引
1.多重索引
先准备数据:
如果不小心,json里的值写错了,修改过来,重新执行即可。
1 PUT index1/_doc/1 2 { 3 "name":"tom1" 4 } 5 PUT index2/_doc/1 6 { 7 "name":"tom1", 8 "age":20 9 } 10 PUT index3/_doc/1 11 { 12 "name":"tom3", 13 "address":"tom3 is good ,in beijing" 14 }
多重索引:
1 POST /index1,index2,index3/_search 2 { 3 "query":{ 4 "query_string": { 5 "query":"tom1" 6 } 7 } 8 }
结果:
返回index1,index2,index3中包含tom1的Json对象,如果只输入tom,则是查询不到值的。
1 { 2 "took" : 2, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 3, 6 "successful" : 3, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 2, 13 "relation" : "eq" 14 }, 15 "max_score" : 0.6931472, 16 "hits" : [ 17 { 18 "_index" : "index2", 19 "_type" : "_doc", 20 "_id" : "1", 21 "_score" : 0.6931472, 22 "_source" : { 23 "name" : "tom1", 24 "age" : 20 25 } 26 }, 27 { 28 "_index" : "index1", 29 "_type" : "_doc", 30 "_id" : "1", 31 "_score" : 0.2876821, 32 "_source" : { 33 "name" : "tom1" 34 } 35 } 36 ] 37 } 38 }
2._all关键字
所有的索引
1 POST _all/_search 2 { 3 "query":{ 4 "query_string": { 5 "query":"tom3" 6 } 7 } 8 }
效果:
1 { 2 "took" : 19, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 6, 6 "successful" : 6, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 1, 13 "relation" : "eq" 14 }, 15 "max_score" : 0.2876821, 16 "hits" : [ 17 { 18 "_index" : "index3", 19 "_type" : "_doc", 20 "_id" : "1", 21 "_score" : 0.2876821, 22 "_source" : { 23 "name" : "tom3", 24 "address" : "tom3 is good ,in beijing" 25 } 26 } 27 ] 28 } 29 }
3.通配符的使用
通配符主要有:* ,+ ,-
*:
index开头的索引中查找有tom3的JSON对象。
1 POST /index*/_search 2 { 3 "query":{ 4 "query_string": { 5 "query":"tom3" 6 } 7 } 8 }
-:
不包含index2的索引中查找
1 POST /index*,-index2/_search 2 { 3 "query":{ 4 "query_string": { 5 "query": "tom1" 6 } 7 } 8 }
4.URL查询字符串参数
①ignore_unavailable:如果不存在索引,一个或者多个,都不会停止,继续运行
1 POST /index1,index4/_search 2 { 3 "query":{ 4 "query_string": { 5 "query": "tom1" 6 } 7 } 8 }
则有问题:
1 { 2 "error" : { 3 "root_cause" : [ 4 { 5 "type" : "index_not_found_exception", 6 "reason" : "no such index [index4]", 7 "resource.type" : "index_or_alias", 8 "resource.id" : "index4", 9 "index_uuid" : "_na_", 10 "index" : "index4" 11 } 12 ], 13 "type" : "index_not_found_exception", 14 "reason" : "no such index [index4]", 15 "resource.type" : "index_or_alias", 16 "resource.id" : "index4", 17 "index_uuid" : "_na_", 18 "index" : "index4" 19 }, 20 "status" : 404 21 }
加上参数:
1 POST /index1,index4/_search?ignore_unavailable=true 2 { 3 "query":{ 4 "query_string": { 5 "query": "tom1" 6 } 7 } 8 }
效果:
1 { 2 "took" : 1, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 1, 6 "successful" : 1, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 1, 13 "relation" : "eq" 14 }, 15 "max_score" : 0.2876821, 16 "hits" : [ 17 { 18 "_index" : "index1", 19 "_type" : "_doc", 20 "_id" : "1", 21 "_score" : 0.2876821, 22 "_source" : { 23 "name" : "tom1" 24 } 25 } 26 ] 27 } 28 }
②allow_no_indices:如果没有通配符指定的索引,true可以防止报错
1 POST /mmm*/_search?allow_no_indices=true 2 { 3 "query":{ 4 "query_string": { 5 "query": "tom1" 6 } 7 } 8 }
效果:
1 { 2 "took" : 0, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 0, 6 "successful" : 0, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 0, 13 "relation" : "eq" 14 }, 15 "max_score" : 0.0, 16 "hits" : [ ] 17 } 18 }
二:响应
1.响应信息过滤
数据:
1 { 2 "took" : 2, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 3, 6 "successful" : 3, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 2, 13 "relation" : "eq" 14 }, 15 "max_score" : 0.2876821, 16 "hits" : [ 17 { 18 "_index" : "index1", 19 "_type" : "_doc", 20 "_id" : "1", 21 "_score" : 0.2876821, 22 "_source" : { 23 "name" : "tom1" 24 } 25 }, 26 { 27 "_index" : "index2", 28 "_type" : "_doc", 29 "_id" : "1", 30 "_score" : 0.2876821, 31 "_source" : { 32 "name" : "tom1", 33 "age" : 20 34 } 35 } 36 ] 37 } 38 }
过滤:
1 POST /index1,index2/_search?filter_path=hits.hits._source.age 2 { 3 "query":{ 4 "query_string": { 5 "query": "tom1" 6 } 7 } 8 }
效果:
1 { 2 "hits" : { 3 "hits" : [ 4 { 5 "_source" : { 6 "age" : 20 7 } 8 } 9 ] 10 } 11 }
2.漂亮的结果
1 Post /index1/_search?pretty=true 2 { 3 "query":{ 4 "match_all": {} 5 } 6 }