经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Elasticsearch » 查看文章
[Elasticsearch] ES 的Mapping 设计在实际场景中应用
来源:cnblogs  作者:chaplinthink  时间:2021/12/31 8:42:13  对本文有异议

背景

项目中有个需求是需要几个字段作为标签,统计各个标签的文档数量,同时支持分词后的全文检索功能。

所使用的ES版本: elasticsearch-5.6.16

原有的mapping设计:

  1. curl -XPUT http://ip:9200/meta_es_metric_data -d'
  2. {
  3. "settings": {
  4. "number_of_shards": 5,
  5. "number_of_replicas": 0
  6. },
  7. "mappings": {
  8. "meta_metric": {
  9. "properties": {
  10. "metricCode": {
  11. "type": "text",
  12. "analyzer" : "ik_max_word"
  13. },
  14. "metricTechType": {
  15. "type": "keyword"
  16. },
  17. "dataDomainName": {
  18. "type": "keyword"
  19. },
  20. "sceneClassify": {
  21. "type": "keyword"
  22. },
  23. "metricClassify": {
  24. "type": "keyword"
  25. }
  26. }
  27. }
  28. }
  29. }'

其中keyword类型就是作为标签统计字段,因为其类型不支持分词检索,检索时必须精确查找,我们尝试把其类型修改成text,text本身就是支持分词索引,但是修改后就报错了:

  1. Fielddata is disabled on text fields by default

经过查询了解es一个字段类型被设置为text,再进行聚合统计,就会报上面的问题.

那么ES有没有办法对一个字段支持分词检索同时可以进行统计的特性呢?其实就是ES是否可以一个字段定义两种类型: keyword 和 text?

答案是可以的.

ES字段的fields属性

通过fields属性来让当前字段同时具备keyword和text类型

由于我们本身的字段类型是keyword,那我在field 属性中添加一个text,是否就满足需求呢?如:

  1. curl -XPUT http://ip:9200/meta_es_metric_data -d'
  2. {
  3. "settings": {
  4. "number_of_shards": 5,
  5. "number_of_replicas": 0
  6. },
  7. "mappings": {
  8. "meta_metric": {
  9. "properties": {
  10. "metricCode": {
  11. "type": "text",
  12. "analyzer" : "ik_max_word"
  13. },
  14. "metricTechType": {
  15. "type": "keyword"
  16. "fields": {
  17. "raw": {
  18. "type": "text"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }'

当用match 搜索metricTechType.raw, 分词搜索是不行的。

之所以想这样做是因为ES支持新增字段、更新字段,但是不支持字段类型的修改

这条方法走不通,就比较复杂了,因为考虑修改字段类型,我们只能重建mapping, 同时涉及历史数据的加载处理。

具体步骤

1.重建索引,因es不支持修改字段类型

  1. curl -XPUT http://ip:9200/meta_es_metric_data_new -d'
  2. {
  3. "settings": {
  4. "number_of_shards": 5,
  5. "number_of_replicas": 0
  6. },
  7. "mappings": {
  8. "meta_metric": {
  9. "properties": {
  10. "metricCode": {
  11. "type": "text",
  12. "analyzer" : "ik_max_word"
  13. },
  14. "metricTechType": {
  15. "type": "text",
  16. "fields": {
  17. "raw": {
  18. "type": "keyword"
  19. }
  20. }
  21. },
  22. "dataDomainName": {
  23. "type": "text",
  24. "fields": {
  25. "raw": {
  26. "type": "keyword"
  27. }
  28. }
  29. },
  30. "sceneClassify": {
  31. "type": "text",
  32. "fields": {
  33. "raw": {
  34. "type": "keyword"
  35. }
  36. }
  37. },
  38. "metricClassify": {
  39. "type": "text",
  40. "fields": {
  41. "raw": {
  42. "type": "keyword"
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }'

2.查看索引映射

  1. curl -XGET 'http://ip:9200/meta_es_metric_data_new/_mapping'

3.将数据加载到新的索引上(老索引的数据还是在的)

  1. curl -XPOST http://ip:9200/_reindex -d'
  2. {
  3. "source":{
  4. "index": "meta_es_metric_data"
  5. },
  6. "dest": {
  7. "index": "meta_es_metric_data_new"
  8. }
  9. }'

4.查看老索引数据:

  1. curl -XGET 'http://ip:9200/meta_es_metric_data/_search?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "query": {
  4. "match": {
  5. "dataDomainName": "用户"
  6. }
  7. }
  8. }
  9. '

5.删除原索引,给新索引创建别名(为了代码不动)

  1. curl -XDELETE http://ip:9200/meta_es_metric_data
  2. curl -XPOST http://ip:9200/_aliases -d'
  3. {
  4. "actions":[
  5. {
  6. "add": {
  7. "index": "meta_es_metric_data_new",
  8. "alias": "meta_es_metric_data"
  9. }
  10. }
  11. ]
  12. }'

6.测试字段是否支持全文检索及聚合

  1. curl -XGET 'http://ip:9200/meta_es_metric_data_new/_search?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "query": {
  4. "match": {
  5. "dataDomainName": "用户"
  6. }
  7. },
  8. "sort": {
  9. "dataDomainName.raw": "asc"
  10. },
  11. "aggs": {
  12. "Cities": {
  13. "terms": {
  14. "field": "dataDomainName.raw"
  15. }
  16. }
  17. }
  18. }
  19. '

总结

本文主要讲解如何让一个字段支持不同方式索引,利用Fields属性. 同时如何对历史存量数据进行处理. keyword类型支持es精确查找以及聚合排序,text支持全文检索,但是不能进行聚合、排序.

参考

  1. https://doc.codingdict.com/elasticsearch/330/

  2. https://cloud.tencent.com/developer/article/1555004

本文作者: chaplinthink, 关注领域:大数据、基础架构、系统设计, 一个热爱学习、分享的大数据工程师

原文链接:http://www.cnblogs.com/bigdata1024/p/15727435.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号