当前位置:   article > 正文

easy-es使用详解与源码解析_easy-es 源码解读

easy-es 源码解读

1.git clone后,easy-es-core中的pom中需要引入:

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpcore</artifactId>
  4. <version>4.4.12</version>
  5. </dependency>

2.easy-es-sample 中提供了基本案例,可以用来解析源码。

3.easy-es-common中的pom里可以看到,它是基于elasticsearch-rest-high-level-client的。

如果不熟悉elasticsearch-rest-high-level-client,建议先熟悉一下。

1. DSL语句

1.1 DSL常见的查询分类

查询所有:match_all (一般也就是测试用用)

全文检索:利用分词器对用户输入的内容进行分词后进行匹配查询

match_query,multi_match_query

精确查询:根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等
ids,range,term

地理(geo)查询:根据经纬度查询。
geo_distance,geo_bounding_box

复合查询:将上述查询条件组合起来,合并查询条件

bool,function_score

1.2 全文检索

对用户输入的内容进行分词后进行匹配查询

  1. GET /easyes_document/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }
  7. GET /easyes_document/_search
  8. {
  9. "query": {
  10. "match": {
  11. "title": "老王"
  12. }
  13. }
  14. }
  15. GET /easyes_document/_search
  16. {
  17. "query": {
  18. "multi_match": {
  19. "query": "老王",
  20. "fields": ["content","creator"]
  21. }
  22. }
  23. }

1.3 精确检索

根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等,不会对用户输入的内容进行分词

term:根据词条精确值查询
range:根据值的范围查询

  1. #term 的使用
  2. GET /easyes_document/_search
  3. {
  4. "query": {
  5. "term": {
  6. "_id": {
  7. "value": "3"
  8. }
  9. }
  10. }
  11. }
  12. GET /easyes_document/_search
  13. {
  14. "query": {
  15. "range": {
  16. "star_num": {
  17. "gte": 20
  18. }
  19. }
  20. }
  21. }
  22. GET /easyes_document/_search
  23. {
  24. "query": {
  25. "range": {
  26. "star_num": {
  27. "lte": 5
  28. }
  29. }
  30. }
  31. }
  32. GET /easyes_document/_search
  33. {
  34. "query": {
  35. "range": {
  36. "gmt_create": {
  37. "lte": "2023-06-18 07:38:43"
  38. }
  39. }
  40. }
  41. }

1.4地理坐标查询

如果没有提示需要硬写:

  1. GET /easyes_document/_search
  2. {
  3.   "query": {
  4.     "geo_distance": {
  5.       "location": "40.13,116.63",
  6.       "distance": "2km"
  7.     }
  8.   }
  9. }

1.5 function score query改变权重影响得分

在这里插入图片描述

 term默认对keyword中文无效,需要field.keyword才可以。

  1. #function score query
  2. GET /easyes_document/_search
  3. {
  4. "query": {
  5. "function_score": {
  6. "query": {
  7. "match": {
  8. "title": "老王"
  9. }
  10. },
  11. "functions": [
  12. {
  13. "filter": {
  14. "term": {
  15. "sub_title.keyword": "小毛子"
  16. }
  17. },
  18. "weight":100
  19. }
  20. ]
  21. }
  22. }
  23. }

1.6 复合查询booleanQuery

布尔查询是一个或多个查询子句的组合。子查询的组合方式有:

  • must:必须匹配每个子查询,类似“与”
  • should:选择性匹配子查询,类似“或”【应该】
  • must_not:必须不匹配,不参与算分,类似“非”
  • filter:必须匹配,不参与算分

找汉子,把地理位置写在must中和放在filter中得分是不一样的

  1. #bool query
  2. GET /easyes_document/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "match": {"title" : "测试"}
  9. },
  10. {
  11. "match": {"creator" : "老汉"}
  12. },
  13. {
  14. "geo_distance": {
  15. "geo_location": "POINT (34.400544 73.53028599999999)",
  16. "distance": "500km"
  17. }
  18. }
  19. ],
  20. "must_not": [
  21. {
  22. "range": {
  23. "star_num": {
  24. "gte": 10,
  25. "lte": 20
  26. }
  27. }
  28. }
  29. ]
  30. }
  31. }
  32. }
  1. #bool query
  2. GET /easyes_document/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "match": {"title" : "测试"}
  9. },
  10. {
  11. "match": {"creator" : "老汉"}
  12. }
  13. ],
  14. "must_not": [
  15. {
  16. "range": {
  17. "star_num": {
  18. "gte": 10,
  19. "lte": 20
  20. }
  21. }
  22. }
  23. ],
  24. "filter": [
  25. {
  26. "geo_distance": {
  27. "geo_location": "POINT (34.400544 73.53028599999999)",
  28. "distance": "500km"
  29. }
  30. }
  31. ]
  32. }
  33. }
  34. }

1.7 sort排序

sort集合,写在前面的起主要作用

  1. #sort
  2. GET /easyes_document/_search
  3. {
  4. "query": {
  5. "function_score": {
  6. "query": {
  7. "match": {
  8. "title": "老王"
  9. }
  10. },
  11. "functions": [
  12. {
  13. "filter": {
  14. "term": {
  15. "sub_title.keyword": "小毛子"
  16. }
  17. },
  18. "weight":100
  19. }
  20. ]
  21. }
  22. },
  23. "sort": [
  24. {"star_num": "asc"},
  25. {"_score" : "desc"}
  26. ]
  27. }

1.8 分页

  1. GET /easyes_document/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "测试"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "star_num": {
  11. "order": "desc"
  12. }
  13. }
  14. ],
  15. "from": 10,
  16. "size": 10
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/252129
推荐阅读
相关标签
  

闽ICP备14008679号