fields.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. define([
  2. 'angular',
  3. 'underscore',
  4. 'config'
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('kibana.services');
  9. module.service('fields', function(dashboard, $rootScope, $http, alertSrv) {
  10. // Save a reference to this
  11. var self = this;
  12. this.list = ['_type'];
  13. this.indices = [];
  14. // Stop tracking the full mapping, too expensive, instead we only remember the index names
  15. // we've already seen.
  16. //
  17. $rootScope.$watch(function(){return dashboard.indices;},function(n) {
  18. if(!_.isUndefined(n) && n.length && dashboard.current.index.warm_fields) {
  19. // Only get the mapping for indices we don't know it for
  20. var indices = _.difference(n,_.keys(self.indices));
  21. // Only get the mapping if there are new indices
  22. if(indices.length > 0) {
  23. self.map(indices).then(function(result) {
  24. self.indices = _.union(self.indices,_.keys(result));
  25. self.list = mapFields(result);
  26. });
  27. }
  28. }
  29. });
  30. var mapFields = function (m) {
  31. var fields = [];
  32. _.each(m, function(types) {
  33. _.each(types, function(type) {
  34. fields = _.without(_.union(fields,_.keys(type)),'_all','_source');
  35. });
  36. });
  37. return fields;
  38. };
  39. this.map = function(indices) {
  40. var request = $http({
  41. url: config.elasticsearch + "/" + indices.join(',') + "/_mapping",
  42. method: "GET"
  43. }).error(function(data, status) {
  44. if(status === 0) {
  45. alertSrv.set('Error',"Could not contact Elasticsearch at "+config.elasticsearch+
  46. ". Please ensure that Elasticsearch is reachable from your system." ,'error');
  47. } else {
  48. alertSrv.set('Error',"No index found at "+config.elasticsearch+"/" +
  49. indices.join(',')+"/_mapping. Please create at least one index." +
  50. "If you're using a proxy ensure it is configured correctly.",'error');
  51. }
  52. });
  53. // Flatten the mapping of each index into dot notated keys.
  54. return request.then(function(p) {
  55. var mapping = {};
  56. _.each(p.data, function(type,index) {
  57. mapping[index] = {};
  58. _.each(type, function (fields,typename) {
  59. mapping[index][typename] = flatten(fields);
  60. });
  61. });
  62. return mapping;
  63. });
  64. };
  65. var flatten = function(obj,prefix) {
  66. var propName = (prefix) ? prefix : '',
  67. dot = (prefix) ? '.':'',
  68. ret = {};
  69. for(var attr in obj){
  70. if(attr === 'dynamic_templates' || attr === '_default_') {
  71. continue;
  72. }
  73. // For now only support multi field on the top level
  74. // and if there is a default field set.
  75. if(obj[attr]['type'] === 'multi_field') {
  76. ret[attr] = obj[attr]['fields'][attr] || obj[attr];
  77. var keys = _.without(_.keys(obj[attr]['fields']),attr);
  78. for(var key in keys) {
  79. ret[attr+'.'+keys[key]] = obj[attr]['fields'][keys[key]];
  80. }
  81. } else if (attr === 'properties') {
  82. _.extend(ret,flatten(obj[attr], propName));
  83. } else if(typeof obj[attr] === 'object'){
  84. _.extend(ret,flatten(obj[attr], propName + dot + attr));
  85. } else {
  86. ret[propName] = obj;
  87. }
  88. }
  89. return ret;
  90. };
  91. });
  92. });