IK分词器的分词

ik_max_word

有IK分词器提供,分化的程度按照插件的自行判断,分得更加细而已。
缺点:过于分化,多余条件检索

  1. #方式一ik_max_word 粗密度
  2. GET /_analyze
  3. {
  4. "analyzer": "ik_max_word",
  5. "text": "LOL全球总决赛No.1"
  6. }

image.png

ik_smart

少量关键字,推荐使用这个。

  1. #方式二ik_smart 细密度
  2. GET /_analyze
  3. {
  4. "analyzer": "ik_smart",
  5. "text": "LOL全球总决赛No.1"
  6. }

image.png

准备测试数据

  1. # 添加索引
  2. PUT person2
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name": {
  7. "type": "keyword"
  8. },
  9. "address": {
  10. "type": "text",
  11. "analyzer": "ik_max_word"
  12. }
  13. }
  14. }
  15. }
  16. # 添加文档
  17. POST /person2/_doc/1
  18. {
  19. "name":"张三",
  20. "age":18,
  21. "address":"重庆市江北区"
  22. }
  23. POST /person2/_doc/2
  24. {
  25. "name":"李四",
  26. "age":19,
  27. "address":"重庆市渝中区"
  28. }
  29. POST /person2/_doc/3
  30. {
  31. "name":"王五",
  32. "age":20,
  33. "address":"重庆市南岸区"
  34. }
  35. POST /person2/_doc/4
  36. {
  37. "name":"何六",
  38. "age":21,
  39. "address":"四川省高新区"
  40. }
  41. POST /person2/_doc/5
  42. {
  43. "name":"田七",
  44. "age":22,
  45. "address":"四川省双流区"
  46. }
  47. POST /person2/_doc/7
  48. {
  49. "name":"黄黄",
  50. "age":25,
  51. "address":"上海市长宁区"
  52. }

单个词条检索 [ term ]

  1. # 单个词条检索
  2. # ik分词器分后的词条进行检索
  3. # keyword类型必须完全一致,才能查询出
  4. # text类型会进行单个相匹配的词条查询
  5. GET /person2/_search
  6. {
  7. "query": {
  8. "term": {
  9. "address": {
  10. "value": "南岸"
  11. }
  12. }
  13. }
  14. }

image.png

多词条检索 [ terms ]

  1. # 多词条检索
  2. GET /person2/_search
  3. {
  4. "query": {
  5. "terms": {
  6. "address": ["重庆","区"]
  7. }
  8. }
  9. }

image.png

模糊查询 [ fuzzy ]

  1. # 模糊查询
  2. # 允许关键字出现两个字符的偏差,默认偏差为1
  3. # fuzziness:设置偏差
  4. GET /person2/_search
  5. {
  6. "query": {
  7. "fuzzy": {
  8. "address": {
  9. "value": "南岸12",
  10. "fuzziness":"2"
  11. }
  12. }
  13. }
  14. }

image.png

全文查询 [ match ]

  1. # 条件检索 [重庆][南岸]
  2. # 对关键字进行分词
  3. # 默认取并集(or)
  4. GET /person2/_search
  5. {
  6. "query": {
  7. "match": {
  8. "address":"重庆南岸"
  9. }
  10. }
  11. }
  12. # 交集(and)
  13. GET /person2/_search
  14. {
  15. "query": {
  16. "match": {
  17. "address": {
  18. "query": "重庆南岸",
  19. "operator": "and"
  20. }
  21. }
  22. }
  23. }

并集(or)

image.png

交集(and)

image.png

全部( match_all)

  1. # 全文检索
  2. GET /person2/_search
  3. {
  4. "query": { "match_all": {} }
  5. }

image.png

多字段检索 [ multi_match ]

  1. # 多字段检索 [address] [name] 只要含有“重庆”两字关键字,查询出相应文档
  2. GET /person2/_search
  3. {
  4. "query": {
  5. "multi_match": {
  6. "query": "重庆",
  7. "fields": ["address","name"]
  8. }
  9. }
  10. }

image.png

通配符检索 [ wildcard ]

  1. # 模糊查询
  2. # "重*"以“重”开头所有关键字
  3. GET /person2/_search
  4. {
  5. "query": {
  6. "wildcard": {
  7. "address":"重*"
  8. }
  9. }
  10. }

image.png

  1. # 模糊查询
  2. # "岸?"以“岸”开头所有关键字
  3. GET /person2/_search
  4. {
  5. "query": {
  6. "wildcard": {
  7. "address":"岸?"
  8. }
  9. }
  10. }

image.png

正则查询 [ regexp ]

正则查询取决于正则表达式的效率

  1. # 正则查询
  2. # (.)*为任意字符
  3. GET /person2/_search
  4. {
  5. "query": {
  6. "regexp": {
  7. "address":"(.)*"
  8. }
  9. }
  10. }

image.png

前缀查询 [ prefix ]

  1. # 前缀查询
  2. # 末尾字查询结果为无
  3. GET /person2/_search
  4. {
  5. "query": {
  6. "prefix": {
  7. "address":"上"
  8. }
  9. }
  10. }

image.png

范围查询 [ range ]

  1. # 范围查询
  2. # gt大于 gte大于等于 lt小于 lte小于等于
  3. GET /person2/_search
  4. {
  5. "query": {
  6. "range": {
  7. "age":{
  8. "gte":18,
  9. "lte":20
  10. }
  11. }
  12. }
  13. }

image.png

多条件查询 [ query_string ]

注意:看着下面两个查询,除了“重庆”和“上海”关键字不同外,再无区别。而添加的文档里面,两种类型的数据都是存在,并且分词器,也成功将“重庆市”和“上海市”分词,结果却大出意外。

  1. # 多条件查询
  2. # 识别单个词条 and 两个词条都要查询,得到交集
  3. # or 查询出并集
  4. GET /person2/_search
  5. {
  6. "query": {
  7. "query_string": {
  8. "default_field": "address",
  9. "query": "重庆 AND 市 OR 区"
  10. }
  11. }
  12. }
  13. GET /person2/_search
  14. {
  15. "query": {
  16. "query_string": {
  17. "default_field": "address",
  18. "query": "上海 AND 市 OR 区"
  19. }
  20. }
  21. }

image.png
上海的数据没有查询出来!!!
image.png
结论:query_string查询数据有遗漏问题,选择使用下面的方式补救。

自动分词的连接符 [ default_operator ]

  1. # 自动分词的连接符
  2. GET /person2/_search
  3. {
  4. "query": {
  5. "query_string": {
  6. "default_field": "address",
  7. "default_operator": "AND",
  8. "query": "上海市"
  9. }
  10. }
  11. }

image.png

多条件多词条查询 (or交集)

  1. GET /person2/_search
  2. {
  3. "query": {
  4. "query_string": {
  5. "fields": ["address"],
  6. "query": "上海 OR 市"
  7. }
  8. }
  9. }

image.png

单例多条件查询 [ simple_query_string ]

  1. # 不识别连接符(and、or),分别将[重庆][市][区]词条以and查询
  2. GET /person2/_search
  3. {
  4. "query": {
  5. "simple_query_string": {
  6. "query": "重庆 AND 市 OR 区",
  7. "fields": ["address"]
  8. }
  9. }
  10. }

image.png

布尔查询 [ bool ]

must

  1. # 布尔查询
  2. # must:条件必须成立
  3. # must_not:条件必须不成立
  4. # _score(得分):每条数据匹配度,匹配度越高,得分越高
  5. GET /person2/_search
  6. {
  7. "query": {
  8. "bool": {
  9. "must": [
  10. {
  11. "term": {
  12. "address": {
  13. "value": "重庆"
  14. }
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }

image.png

filter

  1. # filter:过滤
  2. # _score(得分)不显示,性能更优
  3. GET /person2/_search
  4. {
  5. "query": {
  6. "bool": {
  7. "filter": [
  8. {
  9. "term": {
  10. "address": {
  11. "value": "重庆"
  12. }
  13. }
  14. }
  15. ]
  16. }
  17. }
  18. }

image.png

should

  1. # should:或许
  2. GET /person2/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "should": [
  7. {
  8. "terms": {
  9. "address": [
  10. "河南",
  11. "河北",
  12. "重庆"
  13. ]
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

image.png

组合使用

  1. # 组合
  2. GET /person2/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "term": {
  9. "address": {
  10. "value": "重庆"
  11. }
  12. }
  13. }
  14. ],
  15. "filter": [
  16. {
  17. "term": {
  18. "address": {
  19. "value": "南岸"
  20. }
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. }

image.png

倒序 [ sort ]

  1. # id 倒序 asc / desc
  2. GET /person2/_search
  3. {
  4. "query": { "match_all": {} },
  5. "sort": [
  6. { "_id": "desc" }
  7. ]
  8. }

image.png

关键字查询

  1. # 关键字查询
  2. # 查询name和张三关键字,
  3. # 张三不存在,默认忽视
  4. GET /person2/_search
  5. {
  6. "query": { "match_all": {} },
  7. "_source": ["name","张三"]
  8. }

image.png

排除关键字查询

  1. # 排除关键字查询
  2. GET /person2/_search
  3. {
  4. "_source": {
  5. "excludes":"age"
  6. },
  7. "query": { "match_all": {} }
  8. }

image.png

分页查询

  1. # 分页查询
  2. # from:当前页,size:总页数
  3. GET /person2/_search
  4. {
  5. "query": { "match_all": {} },
  6. "from": 3,
  7. "size": 3,
  8. "sort": { "age": "asc" }
  9. }

image.png

桶聚合 [ aggs ]

  1. # 桶聚合 分组
  2. # 格式
  3. # "aggs": {
  4. # "自定义属性名": {
  5. # "terms": {
  6. # "field": "属性",
  7. # "size": 10
  8. # }
  9. # }
  10. # }
  11. GET person2/_search
  12. {
  13. "size": 0,
  14. "query": {
  15. "match": {
  16. "address": "重庆"
  17. }
  18. },
  19. "aggs": {
  20. "address_name": {
  21. "terms": {
  22. "field": "name",
  23. "size": 10
  24. }
  25. }
  26. }
  27. }

image.png

指标聚合 [ aggs ]

max,avg,min,sun….

  1. # 指标聚合 聚合函数
  2. # 四川省内最大年纪的人
  3. GET person2/_search
  4. {
  5. "query": {
  6. "match": {
  7. "address": "四川"
  8. }
  9. },
  10. "aggs": {
  11. "max_age": {
  12. "max": {
  13. "field": "age"
  14. }
  15. }
  16. }
  17. }

image.png

嵌套查询

  1. GET person2/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "my_name": {
  6. "terms": {
  7. "field": "name"
  8. },
  9. "aggs": {
  10. "age_max":{
  11. "max": {
  12. "field": "age"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

image.png

  1. # 嵌套不能查询keyword类型
  2. GET person2/_search
  3. {
  4. "size": 0,
  5. "aggs": {
  6. "age_max":{
  7. "max": {
  8. "field": "age"
  9. }
  10. },
  11. "my_name":{
  12. "terms": {
  13. "field": "name"
  14. }
  15. }
  16. }
  17. }

image.png

阶梯分桶 [ histogram ]

  1. # 阶梯分桶
  2. # interval:阶梯值
  3. # min_doc_count:最小文档数
  4. GET /person2/_search
  5. {
  6. "size": 0,
  7. "aggs": {
  8. "age": {
  9. "histogram": {
  10. "field": "age",
  11. "interval": 2,
  12. "min_doc_count":1
  13. }
  14. }
  15. }
  16. }

image.png

高亮查询 [ highlight ]

  1. # 高亮查询
  2. GET person2/_search
  3. {
  4. "query": {
  5. "match": {
  6. "address": "四川"
  7. }
  8. },
  9. "highlight": {
  10. "fields": {
  11. "address": {
  12. "pre_tags": "<font color='red'>",
  13. "post_tags": "</font>"
  14. }
  15. }
  16. }
  17. }

image.png

批量操作 [ _bulk ]

  1. # 批量操作
  2. # 删除索引为person2,id为5
  3. # 新增索引为person2,id为6
  4. # 内容:名字=徐八,年龄=18,住址=上海市
  5. # 修改索引为person2,id为2
  6. # 内容:名字=刘九
  7. POST _bulk
  8. {"delete":{"_index":"person2","_id":"5"}}
  9. {"create":{"_index":"person2","_id":"6"}}
  10. {"name":"徐八","age":18,"address":"上海市黄浦区"}
  11. {"update":{"_index":"person2","_id":"2"}}
  12. {"doc":{"name":"刘九"}}

image.png

查询别名 [ _alias ]

  1. # 查询别名
  2. GET person2/_alias/

image.png

起别名

  1. # 起别名
  2. POST person2/_alias/person

image.png
image.png

复制索引 [ _reindex ]

  1. # 创建索引
  2. PUT person1
  3. {
  4. "mappings": {
  5. "properties": {
  6. "name":{
  7. "type": "text"
  8. },
  9. "age":{
  10. "type": "integer"
  11. },
  12. "address":{
  13. "type": "keyword"
  14. }
  15. }
  16. }
  17. }
  18. PUT person1/_doc/1
  19. {
  20. "name":"唐一",
  21. "age":52,
  22. "address":"斗罗大陆"
  23. }
  24. GET person1/_search
  25. # person1 拷贝到 person2 中
  26. POST _reindex
  27. {
  28. "source": {"index": "person1"},
  29. "dest": {"index": "person2"}
  30. }
  31. # 使用别名查询
  32. GET person/_search

ElasticSearch Kibana 高级操作 - 图38
结果
image.png

删除别名和索引 [ actions ]

  1. # 删除别名和索引
  2. POST /_aliases
  3. {
  4. "actions": [
  5. {
  6. "remove": {
  7. "index": "person2",
  8. "alias": "person"
  9. }
  10. }
  11. ]
  12. }