SimpleDao
elasticsearch常用操作
2019-05-06, 访问数: 1088

使用aggregations,排序必须要开启fielddata

  1. PUT my_index/_mapping/my_type
  2. {
  3. "properties": {
  4. "my_field": {
  5. "type": "text",
  6. "fielddata": true
  7. }
  8. }
  9. }

安装,参数设置

  1. # 内核设置
  2. vm.swappiness=1
  3. vm.max_map_count=262144
  4. # 内存设置
  5. export ES_HEAP_SIZE=8g
  6. # 配置文件
  7. path.data: /home/stat/elasticsearch-6.1.1/data
  8. path.logs: /home/stat/elasticsearch-6.1.1/logs
  9. indices.fielddata.cache.size: 20%
  10. # 外部访问
  11. network.host: 0.0.0.0
  12. # [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
  13. # [2]: max number of threads [1024] for user [stat] is too low, increase to at least [4096]
  14. # [3]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
  15. 修改 /etc/security/limits.conf
  16. * soft nofile 65536
  17. * hard nofile 131072
  18. * soft nproc 4096
  19. * hard nproc 4096
  20. 修改 /etc/security/limits.d/90-nproc.conf
  21. * soft nproc 4096
  22. 要在Memory下面修改增加:
  23. bootstrap.memory_lock: false
  24. bootstrap.system_call_filter: false

开启fielddata

  1. PUT http://127.0.0.1:9200/_template/template_test
  2. {
  3. "index_patterns": ["test*"],
  4. "mappings": {
  5. "default": {
  6. "_source": {
  7. "enabled": true
  8. },
  9. "properties": {
  10. "@timestamp": {
  11. "type": "date",
  12. "format": "epoch_millis"
  13. },
  14. "host": {
  15. "type": "text",
  16. "fielddata": true
  17. },
  18. "server": {
  19. "type": "text",
  20. "fielddata": true
  21. }
  22. }
  23. }
  24. }
  25. }

不分词的string

  1. "domain": {
  2. "type": "keyword",
  3. "doc_values": true
  4. }

允许跨域访问

解决Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header…

  1. http.cors.enabled: true
  2. http.cors.allow-origin: "*"

设置副本,字段默认类型,匹配,模版

  1. POST http://127.0.0.1:9200/_template/template_metrics
  2. {
  3. "index_patterns":[
  4. "metrics-*"
  5. ],
  6. "settings":{
  7. "number_of_shards":3,
  8. "number_of_replicas":"1",
  9. "refresh_interval":"30s"
  10. },
  11. "mappings":{
  12. "_default_":{
  13. "_all":{
  14. "enabled":false
  15. },
  16. "_source":{
  17. "enabled":true
  18. },
  19. "dynamic_templates":[
  20. {
  21. "template_string":{
  22. "match_mapping_type":"string",
  23. "mapping":{
  24. "type":"keyword",
  25. "doc_values":true,
  26. "norms":false
  27. }
  28. }
  29. }
  30. ],
  31. "properties":{
  32. "@timestamp":{
  33. "type":"date",
  34. "format":"epoch_millis"
  35. }
  36. }
  37. }
  38. }
  39. }