Elasticsearch提供了一个REST API,通过HTTP通过JSON访问。 Elasticsearch使用以下约定 -
多索引
API中的大多数操作(主要是搜索和其他操作)用于一个或多个索引。 这有助于用户通过只执行一次查询来搜索多个位置或所有可用数据。 许多不同的符号用于在多个索引中执行操作。 我们将在本节讨论其中的一些。
- 逗号分隔
GET users,accounts/person/_search
{
"query" : { "match" : { "desc": "运" }},
"size": 10
}
返回
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "users",
"_type" : "person",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"user" : "王五",
"title" : "运营",
"desc" : "运营人员",
"age" : 25
}
},
{
"_index" : "accounts",
"_type" : "person",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"user" : "李四",
"title" : "运营",
"desc" : "运营人员",
"age" : 25
}
}
]
}
}
- 所有索引的_all关键字
GET _all/_search
{
"query" : { "match" : { "desc": "运" }},
"size": 10
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 16,
"successful" : 16,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "users",
"_type" : "person",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"user" : "王五",
"title" : "运营",
"desc" : "运营人员",
"age" : 25
}
},
{
"_index" : "accounts",
"_type" : "person",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"user" : "李四",
"title" : "运营",
"desc" : "运营人员",
"age" : 25
}
}
]
}
}
- 通配符*、+、- 的应用
GET users*/_search
{
"query" : { "match" : { "title": "师" }},
"size": 10
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "users",
"_type" : "person",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库库存"
}
},
{
"_index" : "users-profile",
"_type" : "person",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user" : "李四",
"title" : "工程师",
"desc" : "数据库库存"
}
}
]
}
}
*和- 的应用,这里去除掉了users-profile索引的数据
GET users*,-users-profile/_search
{
"query":{
"query_string":{
"query":"师"
}
}
}
GET users*,-users-pro*/_search
{
"query":{
"query_string":{
"query":"师"
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "users",
"_type" : "person",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库库存"
}
}
]
}
}
- 忽略不存在的索引
ignore_unavailable=true 不能有空格
说明:users-pro索引不存在,如果不加ignore_unavailable=true会报错
GET users*,users-pro/_search?ignore_unavailable=true
{
"query":{
"query_string":{
"query":"师"
}
}
}
- filter_path 简化返回值 【多个参数&拼接】
filter_path = hits.total
filter_path=hits.hits
GET users*,users-pr/_search?ignore_unavailable=true&filter_path=hits.hits
{
"query":{
"query_string":{
"query":"师"
}
}
}
{
"hits" : {
"hits" : [
{
"_index" : "users",
"_type" : "person",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库库存"
}
},
{
"_index" : "users-profile",
"_type" : "person",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user" : "李四",
"title" : "工程师",
"desc" : "数据库库存"
}
}
]
}
}
下一篇文章:Elasticsearch DSL查询
上一篇文章:Elasticsearch - Kibana安装教程