当前位置:   article > 正文

DSL的诞生 | 复杂sql转成Elasticsearch DSL深入详解_dsl sql对照

dsl sql对照

问题如下:

where (position=ES or work=ES or content=ES) and academic=本科 and (city=北京 or city=深圳)
 
 
  • 1

怎么构建ES的查询条件?

我的问题拆解与实现如下:

1、sql语句转成DSL有哪些方法?

方案一:借助工具 NLP团体开发的Elasticsearch-sql; 
2.X安装过,5.X没有再安装。 
方案二:借助工具ElasticHQ的自动转换模块: 
这里写图片描述 
方案一、方案二和Github上其他语言开发的sql转DSL工具对简单的sql生成的DSL相对准确,但对于复杂的sql生成的不一定精确。(如上所示)

方案三:徒手生成。

2、如何根据复杂的sql语句生成ES的DSL查询语句呢?

步骤1:拆解

where (position=ES or work=ES or content=ES) and academic=本科 and (city=北京 or city=深圳)

这个sql语句由几部分组成呢? 
以and作为拆分,共分为3部分: 
三个部分sql用and衔接,转换为DSL对应最外层must;

第一部分: position=ES or work=ES or content=ES 
三个子条件sql用or衔接,转换DSL对应should;

第二部分: academic=本科 
单一条件转换为DSL对应term精确匹配;

第三部分: city=北京 or city=深圳 
city的两个or语句转换为DSL对应terms多词精确匹配。

上面的sql都用的=号,假定不需要分词,我们统一采用termquery和termsquery实现。 
引申: 
如果需要分词,更换为matchquery或者match_parsequery即可。 
如果需要模糊匹配,更换为wildcardquery接口。

步骤2:套bool多条件检索DSL模板

复杂bool多条件检索DSL模板: 
包含了:查询/检索、聚合、排序、指定字段输出、输出起点、输出多少等信息。

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [],
  5. "must_not": [],
  6. "should": []
  7. }
  8. },
  9. "aggs": {
  10. "my_agg": {
  11. "terms": {
  12. "field": "user",
  13. "size": 10
  14. }
  15. }
  16. },
  17. "highlight": {
  18. "pre_tags": [
  19. "<em>"
  20. ],
  21. "post_tags": [
  22. "</em>"
  23. ],
  24. "fields": {
  25. "body": {
  26. "number_of_fragments": 1,
  27. "fragment_size": 20
  28. },
  29. "title": {}
  30. }
  31. },
  32. "size": 20,
  33. "from": 100,
  34. "_source": [
  35. "title",
  36. "id"
  37. ],
  38. "sort": [
  39. {
  40. "_id": {
  41. "order": "desc"
  42. }
  43. }
  44. ]
  45. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

简单bool多条件查询DSL模板: 
只包含查询。

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [],
  5. "must_not": [],
  6. "should": []
  7. }
  8. }
  9. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

以上根据我们的sql特点,简单模板即能满足要求。

步骤3:构造生成DSL

根据,步骤1、步骤2,可以构思出根据sql转换后的DSL应该: 
1)最外层bool 
2)第二层:must 三个并行条件 
3)第三层:各自的匹配条件。(存在bool嵌套bool的情况)

3、动动手,验证下。

3.1 创建索引(自动生成mapping)

put test_index_01
 
 
  • 1

3.2 提交数据

  1. post test_index_01/test_type_01/1
  2. {
  3. "no":"1",
  4. "city":"北京",
  5. "academic":"专科",
  6. "content":"ES",
  7. "position":"ES",
  8. "work":"ES"
  9. }
  10. post test_index_01/test_type_01/2
  11. {
  12. "no":"2",
  13. "city":"天津",
  14. "academic":"本科",
  15. "content":"ES",
  16. "position":"ES",
  17. "work":"ES"
  18. }
  19. post test_index_01/test_type_01/3
  20. {
  21. "no":"3",
  22. "city":"深圳",
  23. "academic":"本科",
  24. "content":"ES",
  25. "position":"ES2",
  26. "work":"ES3"
  27. }
  28. post test_index_01/test_type_01/4
  29. {
  30. "no":"4",
  31. "city":"北京",
  32. "academic":"本科",
  33. "content":"ES1",
  34. "position":"ES2",
  35. "work":"ES"
  36. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

插入后ES-head插件控制台查询结果: 
这里写图片描述

3.3 完成检索

  1. post test_index_01/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "terms": {
  8. "city.keyword": [
  9. "北京",
  10. "深圳"
  11. ]
  12. }
  13. },
  14. {
  15. "term": {
  16. "academic.keyword": "本科"
  17. }
  18. },
  19. {
  20. "bool": {
  21. "should": [
  22. {
  23. "term": {
  24. "content.keyword": "ES"
  25. }
  26. },
  27. {
  28. "term": {
  29. "position.keyword": "ES"
  30. }
  31. },
  32. {
  33. "term": {
  34. "work.keyword": "ES"
  35. }
  36. }
  37. ]
  38. }
  39. }
  40. ]
  41. }
  42. },
  43. "size": 10,
  44. "from": 0
  45. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

注意: 
没有做分词,做的精确匹配,所以加了”.keyword”。

3.4 返回结果

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "total": 2,
  11. "max_score": 1.0577903,
  12. "hits": [
  13. {
  14. "_index": "test_index_01",
  15. "_type": "test_type_01",
  16. "_id": "4",
  17. "_score": 1.0577903,
  18. "_source": {
  19. "no": "4",
  20. "city": "北京",
  21. "academic": "本科",
  22. "content": "ES1",
  23. "position": "ES2",
  24. "work": "ES"
  25. }
  26. },
  27. {
  28. "_index": "test_index_01",
  29. "_type": "test_type_01",
  30. "_id": "3",
  31. "_score": 0.8630463,
  32. "_source": {
  33. "no": "3",
  34. "city": "深圳",
  35. "academic": "本科",
  36. "content": "ES",
  37. "position": "ES2",
  38. "work": "ES3"
  39. }
  40. }
  41. ]
  42. }
  43. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

4、小结

实践是检验真理的唯一标准! 
如有不同意见,欢迎拍砖探讨!

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

闽ICP备14008679号