上面我们已经介绍了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来操作)的更多相关文章

  1. 百度搜索常用api

    http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词:http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜索 ...

  2. 利用kibana学习 elasticsearch restful api (DSL)

    利用kibana学习 elasticsearch restful api (DSL) 1.了解elasticsearch基本概念Index: databaseType: tableDocument: ...

  3. windows系统中 利用kibana创建elasticsearch索引等操作

    elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...

  4. Elasticsearch索引的操作,利用kibana 创建/删除一个es的索引及mapping映射

    索引的创建及删除 1. 通过索引一篇文档创建了一个新的索引 .这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射. 利用Kibana提供的DevTools来执行命令,要创建一个索引 ...

  5. (转)通过HTTP RESTful API 操作elasticsearch搜索数据

    样例数据集 这是编造的JSON格式银行客户账号信息文档,文档schema如下: { “account_number”: 0, “balance”: 16623, “firstname”: “Brads ...

  6. Elasticsearch索引的操作,利用kibana(如何创建/删除一个es的索引?)

    我们已经通过索引一篇文档创建了一个新的索引 .这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射.现在我们需要对这个建立索引的过程做更多的控制:我们想要确保这个索引有数量适中的主分 ...

  7. Elasticsearch 常用API

    1.   Elasticsearch 常用API 1.1.数据输入与输出 1.1.1.Elasticsearch 文档   #在 Elasticsearch 中,术语 文档 有着特定的含义.它是指最顶 ...

  8. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

  9. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

随机推荐

  1. centos下kong源码安装

    参考资料: https://docs.konghq.com/install/source/ 环境准备:操作系统 centeros7.3 1 :openssl和pcre一般系统自带,如果没有可自己安装  ...

  2. Linux学习笔记(二) 文件管理

    了解 Linux 系统基本的文件管理命令可以帮助我们更好的使用 Linux 系统,以下介绍几个常用的文件管理命令 1.pwd pwd 是 Print Working Directory 的简写,用于显 ...

  3. linux find-在指定目录下查找文件

    推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find ...

  4. 67.基于nested object实现博客与评论嵌套关系

    1.做一个实验,引出来为什么需要nested object 冗余数据方式的来建模,其实用的就是object类型,我们这里又要引入一种新的object类型,nested object类型 博客,评论,做 ...

  5. 负载均衡之nginx+consul(自动更新路由)

    前几篇先是记载了如何通过nginx配置服务负载均衡,后面记载了如何通过 ocelot 配置 服务负载均衡,分别介绍了用webapi注册服务以及配置文件注册服务,通过ocelot webapi + co ...

  6. hdu 5040bfs+优先队列 需要存状态

    /* 剪枝:四秒后状态会变得和原来一样,所以四秒后如果再经过这个点肯定不是最优的舍去 易错点:在一个是从.到.这两个点都没有被照到并且不是摄像机,也可能需要等3秒,因为后面的结果可能再这三秒中发生改变 ...

  7. NOIP2010 提高组合集

    NOIP 2010 提高组合集 T1 机器翻译 模拟题,用一个栈模拟,桶记录即可. #include <iostream> #include <cstdio> #include ...

  8. Hive之内置函数

    函数分类 UDF(User Defined Function):数据一对一 UDAF(User Defined Aggreation Function):数据多对一 UDTF(User Defined ...

  9. 【CV论文阅读】 Fast RCNN + SGD笔记

    Fast RCNN的结构: 先从这幅图解释FAST RCNN的结构.首先,FAST RCNN的输入是包含两部分,image以及region proposal(在论文中叫做region of inter ...

  10. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers         本章中,你讲学到: 了解远程evernt ...