上面我们已经介绍了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" : [ ]
}
}
}

文章到此就结束了,有问题可以在下方评论,技术问题可以私聊我

最新文章

  1. Android开发之Activity的创建跳转及传值
  2. cookie入门
  3. 使用BCP导出导入数据
  4. mysql主从同步mysql slave_io_running:no的解决方案
  5. 【Apache运维基础(2)】主配置文件说明
  6. Spring JTA应用JOTM &amp; Atomikos I Application
  7. mysql 触发器,insert,auto字段竟然一样....
  8. codeforces #305 div1 done
  9. Qt 经典出错信息之”Basic XLib functionality test failed!”(Z..z..)
  10. querystring,parse和stringify相互转换
  11. poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
  12. 一个简单的Java死锁示例(转)
  13. Flink源码分析 - 源码构建
  14. linux中vi编辑
  15. Linux------使用Xfpt6连接阿里云ECS服务器
  16. js 整数型数组和字符型数组相互转换
  17. centos添加额外测源,解决:No package openvpn available.
  18. 阿里云:游戏行业DDoS攻击解决方案
  19. C++ 原来 const 中所使用的函数 必须 全都具有 const 才行
  20. mac上为nodejs设置环境变量

热门文章

  1. Linux下查看Tomcat运行日志
  2. CentOS7.x的DNS服务的基础配置
  3. php利用32进制实现对id加密解密
  4. 线程 synchronized锁机制
  5. 【13】AngularJS&#160;模块
  6. 【Codeforces 1096D】Easy Problem
  7. python 线程进程
  8. noip模拟赛 列车调度
  9. Luca Canali
  10. keil编译后生成的M51文件解析