Elasticsearch搜索常用API(利用Kibana来操作)
上面我们已经介绍了Elasticsearch的一些基本操作,这篇文章属于进阶篇,我们一起来学习。
前面我们创建了sdb和user文档,现在我们来看如何查询user中所有的文档呢?
GET /sdb/user/_search
此时输出入下:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"username" : "小丽丽",
"age" : 16,
"gender" : "女",
"about" : "this is my info",
"addrs" : [
"北京",
"江西",
"香港"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
接下来我们来查询姓名为秦雪的人
GET /sdb/user/_search?q=username:%e7%a7%a6%e9%9b%aa (这里需要注意,username:是urlencoding过后的字符串,如果是中文,kibana dev tools会报错)
执行结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
可以看到,我们检索出了名字为秦雪的人。
下面我们介绍如何使用Query String的形式来查询
GET /sdb/user/_search
{
"query" : {
"match" : {
"username" : "秦雪"
}
}
}
我们可以看到,检索结果如下:

下面我们来看看更为复杂的检索
例如要查询addr在甘肃的同学
GET /sdb/user/_search
{
"query": {
"bool": {
"must": [
{"match": {
"addrs": "甘肃"
}}
]
}
}
}
结果如下:
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
检索包含甘肃但是不在包含天津的同学
GET /sdb/user/_search
{
"query": {
"bool": {
"must": [
{"match": {
"addrs": "甘肃"
}}
],
"must_not": [
{"match": {
"addrs": "天津"
}}
]
}
}
}
结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
}
]
}
}
接下来为大家介绍es里边的短语搜索
首先使用match_all来显示所有文档
GET /sdb/user/_search
{
"query": {
"match_all": {}
}
}
结果如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"username" : "小丽丽",
"age" : 16,
"gender" : "女",
"about" : "this is my info",
"addrs" : [
"北京",
"江西",
"香港"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
接着我们查询
GET /sdb/user/_search
{
"query": {
"match_phrase": {
"about": "my student"
}
}
}
结果如下:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
}
}
我们发现,查询到的带有my student的记录只有一个,说明查询是正确的。
下面我们说一下关键词高亮,这个在搜索显示的地方用的比较多
GET /sdb/user/_search
{
"query": {
"match_phrase": {
"about": "my student"
}
},
"highlight": {
"fields": {
"about": {}
}
}
}
输出结果如下:
{
"took" : 233,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
},
"highlight" : {
"about" : [
"this is <em>my</em> <em>student</em>"
]
}
}
]
}
}
可能有很多同学会问,我不想用em标签,我想用其他的,那怎么办?不用着急,elasticsearch已经为我们想到了,请看下面
GET /sdb/user/_search
{
"query": {
"match_phrase": {
"about": "my student"
}
},
"highlight": {
"fields": {
"about": {}
},
"pre_tags" : ["<color>"],
"post_tags" : ["</color>"]
}
}
结果如下:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
},
"highlight" : {
"about" : [
"this is <color>my</color> <color>student</color>"
]
}
}
]
}
}
接下来我们来看看分析
GET /sdb/user/_search
{
"aggs": {
"all_interests": {
"terms": {"field": "interests"}
}
}
}
以上写法是死的,结果如下:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "sdb",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"username" : "小丽丽",
"age" : 16,
"gender" : "女",
"about" : "this is my info",
"addrs" : [
"北京",
"江西",
"香港"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"username" : "张三",
"age" : 16,
"gender" : "男",
"about" : "this is my info",
"addrs" : [
"甘肃",
"陕西",
"兰州"
]
}
},
{
"_index" : "sdb",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"username" : "秦雪",
"age" : 16,
"gender" : "女",
"about" : "this is my student",
"addrs" : [
"甘肃",
"陕西",
"天津"
]
}
}
]
},
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
}
文章到此就结束了,有问题可以在下方评论,技术问题可以私聊我
Elasticsearch搜索常用API(利用Kibana来操作)的更多相关文章
- 百度搜索常用api
http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词:http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜索 ...
- 利用kibana学习 elasticsearch restful api (DSL)
利用kibana学习 elasticsearch restful api (DSL) 1.了解elasticsearch基本概念Index: databaseType: tableDocument: ...
- windows系统中 利用kibana创建elasticsearch索引等操作
elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...
- Elasticsearch索引的操作,利用kibana 创建/删除一个es的索引及mapping映射
索引的创建及删除 1. 通过索引一篇文档创建了一个新的索引 .这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射. 利用Kibana提供的DevTools来执行命令,要创建一个索引 ...
- (转)通过HTTP RESTful API 操作elasticsearch搜索数据
样例数据集 这是编造的JSON格式银行客户账号信息文档,文档schema如下: { “account_number”: 0, “balance”: 16623, “firstname”: “Brads ...
- Elasticsearch索引的操作,利用kibana(如何创建/删除一个es的索引?)
我们已经通过索引一篇文档创建了一个新的索引 .这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射.现在我们需要对这个建立索引的过程做更多的控制:我们想要确保这个索引有数量适中的主分 ...
- Elasticsearch 常用API
1. Elasticsearch 常用API 1.1.数据输入与输出 1.1.1.Elasticsearch 文档 #在 Elasticsearch 中,术语 文档 有着特定的含义.它是指最顶 ...
- elasticsearch中常用的API
elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...
- ElasticSearch+Kibana 索引操作
ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...
随机推荐
- 常用Linux命令_20190211
1.创建文件夹:mkdir 文件夹名称 2.查看IP地址信息:ipconfig -a 3.查看内存使用情况:free -m 4.查看CPU使用情况:top 5.查看磁盘使用情况:df -a/-h 6. ...
- Linux:DHCP服务配置
DHCP服务程序能够使局域网内的主机自动且动态的获取IP地址.子网掩码.网关地址以及DNS服务器地址等信息. 说明:先安装DHCP服务 yum install dhcp -y ...
- Python反射、异常处理
反射 :字符串到对象属性的映射 hasattr(obj,string), 判断对象obj里面是否有叫string的字段或方法 getattr(obj,string) 获取obj对象里名叫string的 ...
- 抓包工具的感触(charles and fiddler)
最近测mobile,一直徘徊在fiddler 和 charles之间: charles 的证书装了 ,才能正常抓包: 后来因为重定向,分享到扣扣,微信的跳转功能,跳转到wap 或者跳转到PC 或者跳 ...
- django中配置允许跨域请求
对于django 安装django-cors-headers,详情请看官方文档 pip install django-cors-headers 配置settings.py文件 a.在INSTALLED ...
- 3.2.2.5 BRE运算符优先级
在数学表达式里,正则表达式的运算符具有某种已定义的优先级,指的是某个运算符(优先级较高)将比其他运算符先被处理. BRE运算符优先级,由高至低 运算符 表示含义 [..] [= ...
- Leetcode 95.不同的二叉搜索树II
不同的二叉搜索树2 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null ...
- RabbitMQ-基本概念(一)
整体架构模型 Producer 消息生产者,生产者创建消息然后发布到RabbitM中,消息一般包含2个部分 消息体(payload)和标签 消息体就是带有业务逻辑结构的数据,消息标签用来表述这条消息, ...
- 20180725利用pmm监控管理mysql
转自:https://www.percona.com/doc/percona-monitoring-and-management/architecture.html 报警机制https://www.p ...
- 洛谷—— P2047 社交网络
P2047 社交网络 题目描述 在社交网络(social network)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题.在一个社交圈子里有n个人,人与人之间有不同程度的关系. ...