Kibana测试开发工具【本地】

1、基本环境准备

1.1、增加索引

增加一个测试索引,索引为 text 类型 name 、keyword 类型 desc,integer 类型 age

  1. PUT /test
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name":{
  6. "type": "text"
  7. },
  8. "desc":{
  9. "type": "keyword"
  10. },
  11. "age":{
  12. "type": "integer"
  13. }
  14. }
  15. }
  16. }

1.2、增加数据

增加3条测试数据

  1. POST test/_doc
  2. {
  3. "name":"This is name",
  4. "age":30,
  5. "desc":"This is desc"
  6. }
  7. POST test/_doc
  8. {
  9. "name":"This is name1",
  10. "age":20,
  11. "desc":"This is desc1"
  12. }
  13. POST test/_doc
  14. {
  15. "name":"This is name2",
  16. "age":15,
  17. "desc":"This is desc2"
  18. }

2、数据查询

2.1、基础查询 - _search

查询索引的结构信息,包括成功,失败数据统计,文档统计,文档内容

  1. GET test/_search

image.png

2.2、限定返回字段 - _source

和 SQL 对比就是 从 select * 变成 select xxx,就是限制返回指定字段,而不需要全部字段都返回

  1. # 筛选字段,只返回 name 字段
  2. GET test/_search
  3. {
  4. "_source": ["name"]
  5. }

image.png

2.3、排序 - sort

和 SQL 中 order 一样,针对字段进行排序,支持正序,倒序

  1. # 排序,根据 age 进行排序
  2. GET test/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "is"
  7. }
  8. },
  9. "sort": [
  10. {
  11. "age": {
  12. "order": "desc"
  13. }
  14. }
  15. ]
  16. }

image.png

2.4、分页 - from、size

和 MySQL limit 功能一致,对结果数据进行分页返回,from 起始页数(从 0 开始),size 每页大小

  1. # 0开始,每页 2个数据
  2. GET test/_search
  3. {
  4. "from": 0,
  5. "size": 2
  6. }

image.png

2.5、都 - must、都不是 - must_not、或者 - should

must 表示 筛选条件都要满足;must_not 表示筛选条件都不能满足;should 功能和 MySQL or 一样,”或者”的功能

  1. # 或者 should
  2. GET test/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "should": [
  7. {
  8. "match": {
  9. "name": "name"
  10. }
  11. },
  12. {
  13. "match": {
  14. "name": "name2"
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }
  21. # 必须不 must not
  22. GET test/_search
  23. {
  24. "query": {
  25. "bool": {
  26. "must_not": [
  27. {
  28. "match": {
  29. "name": "name"
  30. }
  31. }
  32. ]
  33. }
  34. }
  35. }
  36. # 必须 must
  37. GET test/_search
  38. {
  39. "query": {
  40. "bool": {
  41. "must": [
  42. {
  43. "match": {
  44. "name": "name"
  45. }
  46. }
  47. ]
  48. }
  49. }
  50. }

2.6、结果筛选 - filter

针对结果,进行再筛选,比如范围筛选

  1. # 筛选 年龄范围再 [20,30] 之间
  2. GET test/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "filter": [
  7. {
  8. "range": {
  9. "age": {
  10. "gte": 20,
  11. "lte": 30
  12. }
  13. }
  14. }
  15. ]
  16. }
  17. }
  18. }

image.png

2.7、多搜索词 - match

match 支持多个搜索词,搜索词之间用空格 隔开

  1. # 多值筛选,中间用 空格隔开
  2. GET test/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "is name"
  7. }
  8. }
  9. }

3、查询方式 term 和 match

查询方式主要分为 term 和 match 两种,两种查询方式针对搜索词的分词效果也是不一样的。

项目 查询方式
term 精确查询,只能查询单个单词,通过 “倒排索引”进行检索,不会调用分词器。如果使用多个单词精确查询,也查无数据。
可以通过 terms 进行多个单词检索
match 会使用分词器。先分析文档,然后再通过分析的文档进行查询
  1. # terms 查询关键字数组
  2. GET test/_search
  3. {
  4. "query": {
  5. "terms": {
  6. "name":["This","is","name"]
  7. }
  8. }
  9. }
  10. # term 多个单词查无数据
  11. GET test/_search
  12. {
  13. "query": {
  14. "term": {
  15. "name":"This is name"
  16. }
  17. }
  18. }
  19. # match 能分词,查到数据
  20. GET test/_search
  21. {
  22. "query": {
  23. "match": {
  24. "name": "This is name"
  25. }
  26. }
  27. }

4、字段类型 text 和 keyword

同样用 term 情况下,根据字段类型不同,是否分词的效果也不同。
在以上基础上,分别查询 “text 类型的 name” 和“keyword 类型的 desc”( name 和 desc 都是 This is xxx)

  • text

    1. GET test/_search
    2. {
    3. "query": {
    4. "match": {
    5. "name": "is"
    6. }
    7. }
    8. }
    9. # 根据请求结果可知,text 会调用分词器对文档进行分词

    image.png

  • keyword

    1. GET test/_search
    2. {
    3. "query": {
    4. "match": {
    5. "desc": "is"
    6. }
    7. }
    8. }
    9. # 根据运行结果可知,keyword 不会调用分词器对文档进行分词操作

    image.png
    总结:
    text 类型搜索时会调用分词器,而 keyword 不会调用

    5、高亮

    在诸如搜索网站 或者电商页面搜索结果针对 搜索词会有高亮操作,这其实也是 ES 功能。以百度搜索为例
    image.png

5.1 默认样式高亮

ES 默认是不会开启高亮的,在搜索时,需要声明高亮的字段。默认的 ES 高亮样式是用 把搜索词包裹起来。高亮后的结果会显示在 搜索结果中每一项中的 highlight 中

  1. # 高亮字段 name
  2. GET test/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "is"
  7. }
  8. },
  9. "highlight": {
  10. "fields": {
  11. "name": {}
  12. }
  13. }
  14. }

image.png

5.2 自定义高亮样式

如果想自定义 高亮的样式,则需要通过pre_tags(标签之前)和 post_tags(标签之后)自定义样式
比如想用 包括高亮,自定义样式后,就会覆盖取代默认样式

  1. GET test/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "is"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": "<span style='color:red'>",
  10. "post_tags": "</span>",
  11. "fields": {
  12. "name": {}
  13. }
  14. }
  15. }

image.png