Class: ICFS::CacheElastic
- Includes:
- Elastic
- Defined in:
- lib/icfs/cache_elastic.rb
Overview
Implements Cache using Elasticsearch
Constant Summary collapse
- ResultsCase =
the Case results fields
{ caseid: 'caseid', template: 'template', status: 'status', title: 'title', tags: 'tags', }.freeze
- DefaultSize =
default page size
25- ResultsEntry =
Entry search results fields
{ caseid: 'caseid', entry: 'entry', time: 'time', title: 'title', tags: 'tags', perms: ['perms', :empty], action: ['action', :zero], index: ['index', :size], files: ['files', :size], stats: ['stats', :size], }.freeze
- ResultsIndex =
Index search results fields
{ caseid: 'caseid', index: 'index', title: 'title', tags: 'tags', }.freeze
- ResultsLog =
Log search results fields
{ caseid: 'caseid', log: 'log', time: 'time', user: 'user', entry: ['entry', :sub, 'num'].freeze, index: ['index', :sub, 'num'].freeze, action: ['action', :sub, 'num'].freeze, files: ['files_hash', :size].freeze, }.freeze
Instance Method Summary collapse
-
#_agg_filter(name, qu, sub) ⇒ Object
filter bucket aggregation.
-
#_agg_nested(name, field, sub) ⇒ Object
nested bucket aggregation.
-
#_agg_stats(name, field) ⇒ Object
stats metric aggregation.
-
#_agg_terms(name, field, sub) ⇒ Object
terms bucket aggregation.
-
#_query_all ⇒ Object
match all query.
-
#_query_bool(must, filter, should, must_not) ⇒ Object
bool query.
-
#_query_constant(filter) ⇒ Object
constant score.
-
#_query_exists(field, val) ⇒ Object
Exists query.
-
#_query_keyw(field, val) ⇒ Object
keyword query.
-
#_query_match(field, val) ⇒ Object
match query.
-
#_query_nested(field, query) ⇒ Object
Nested query.
-
#_query_prefix(field, val) ⇒ Object
prefix string query.
-
#_query_term(field, val) ⇒ Object
Term query.
-
#_query_times(field, val_gt, val_lt) ⇒ Object
times query.
-
#action_read(cid, anum) ⇒ String
Read an action.
-
#action_search(query) ⇒ Object
Search for actions.
-
#action_tags(query) ⇒ Object
List tags used on action tasks.
-
#action_write(cid, anum, item) ⇒ Object
Write an action.
-
#case_read(cid) ⇒ String
Read a case.
-
#case_search(query) ⇒ Object
Search for cases.
-
#case_tags(query) ⇒ Object
Get list of tags for cases.
-
#case_write(cid, item) ⇒ Object
Write a case.
-
#current_read(cid) ⇒ String
Read current.
-
#current_write(cid, item) ⇒ Object
Write current.
-
#entry_read(cid, enum) ⇒ String
Read an entry.
-
#entry_search(query) ⇒ Object
Search for entries.
-
#entry_tags(query) ⇒ Object
List tags used on Entries.
-
#entry_write(cid, enum, item) ⇒ Object
Write an entry.
-
#index_read(cid, xnum) ⇒ String
Read an Index.
-
#index_search(query) ⇒ Object
Search for Indexes.
-
#index_tags(query) ⇒ Object
List tags used in indexes.
-
#index_write(cid, xnum, item) ⇒ Object
Write an Index.
-
#initialize(map, es) ⇒ CacheElastic
constructor
New instance.
-
#lock_release(cid) ⇒ Object
Release a case lock.
-
#lock_take(cid) ⇒ Object
Take a case lock.
-
#log_read(cid, lnum) ⇒ String
Read a log.
-
#log_search(query) ⇒ Object
Search for a log.
-
#log_write(cid, lnum, item) ⇒ Object
Write a log.
-
#stats(query) ⇒ Object
Analyze stats.
Methods included from Elastic
Methods inherited from Cache
Constructor Details
#initialize(map, es) ⇒ CacheElastic
New instance
157 158 159 160 161 162 |
# File 'lib/icfs/cache_elastic.rb', line 157 def initialize(map, es) @map = map @es = es @name = '%s:%d' % [Socket.gethostname, Process.pid] @name.freeze end |
Instance Method Details
#_agg_filter(name, qu, sub) ⇒ Object
filter bucket aggregation
864 865 866 867 868 |
# File 'lib/icfs/cache_elastic.rb', line 864 def _agg_filter(name, qu, sub) ag = { name => { 'filter' => qu } } ag[name]['aggs'] = sub if sub return ag end |
#_agg_nested(name, field, sub) ⇒ Object
nested bucket aggregation
874 875 876 877 878 |
# File 'lib/icfs/cache_elastic.rb', line 874 def _agg_nested(name, field, sub) ag = { name => { 'nested' => { 'path' => field } } } ag[name]['aggs'] = sub if sub return ag end |
#_agg_stats(name, field) ⇒ Object
stats metric aggregation
846 847 848 |
# File 'lib/icfs/cache_elastic.rb', line 846 def _agg_stats(name, field) { name => { 'stats' => { 'field' => field } } } end |
#_agg_terms(name, field, sub) ⇒ Object
terms bucket aggregation
854 855 856 857 858 |
# File 'lib/icfs/cache_elastic.rb', line 854 def _agg_terms(name, field, sub) ag = { name => { 'terms' => { 'field' => field } } } ag[name]['aggs'] = sub if sub return ag end |
#_query_all ⇒ Object
match all query
250 251 252 |
# File 'lib/icfs/cache_elastic.rb', line 250 def _query_all() { 'match_all' => {} } end |
#_query_bool(must, filter, should, must_not) ⇒ Object
bool query
933 934 935 936 937 938 939 940 941 942 943 944 |
# File 'lib/icfs/cache_elastic.rb', line 933 def _query_bool(must, filter, should, must_not) qu = {} qu['must'] = must if(must && !must.empty?) qu['filter'] = filter if(filter && !filter.empty?) qu['should'] = should if(should && !should.empty?) qu['must_not'] = must_not if(must_not && !must_not.empty?) if qu.empty? return { 'match_all' => {} } else return { 'bool' => qu } end end |
#_query_constant(filter) ⇒ Object
constant score
1012 1013 1014 |
# File 'lib/icfs/cache_elastic.rb', line 1012 def _query_constant(filter) {'constant_score' => { 'filter' => filter } } end |
#_query_exists(field, val) ⇒ Object
Exists query
893 894 895 896 |
# File 'lib/icfs/cache_elastic.rb', line 893 def _query_exists(field, val) return nil if val.nil? { 'exists' => { 'field' => field } } end |
#_query_keyw(field, val) ⇒ Object
keyword query
901 902 903 904 905 906 907 908 909 |
# File 'lib/icfs/cache_elastic.rb', line 901 def _query_keyw(field, val) return nil if val.nil? if val.is_a?(Array) qu = { 'terms' => { field => val } } else qu = {'term' => { field => val } } end return qu end |
#_query_match(field, val) ⇒ Object
match query
241 242 243 244 |
# File 'lib/icfs/cache_elastic.rb', line 241 def _query_match(field, val) return nil if !val { 'match' => { field => { 'query' => val } } } end |
#_query_nested(field, query) ⇒ Object
Nested query
448 449 450 451 452 453 454 455 |
# File 'lib/icfs/cache_elastic.rb', line 448 def _query_nested(field, query) { 'nested' => { 'path' => field, 'query' => query } } end |
#_query_prefix(field, val) ⇒ Object
prefix string query
925 926 927 928 |
# File 'lib/icfs/cache_elastic.rb', line 925 def _query_prefix(field, val) return nil if val.nil? return { 'prefix' => { field => val } } end |
#_query_term(field, val) ⇒ Object
Term query
884 885 886 887 |
# File 'lib/icfs/cache_elastic.rb', line 884 def _query_term(field, val) return nil if val.nil? { 'term' => { field => val } } end |
#_query_times(field, val_gt, val_lt) ⇒ Object
times query
914 915 916 917 918 919 920 |
# File 'lib/icfs/cache_elastic.rb', line 914 def _query_times(field, val_gt, val_lt) return nil if( val_gt.nil? && val_lt.nil? ) tq = {} tq['gt'] = val_gt if val_gt tq['lt'] = val_lt if val_lt return {'range' => { field => tq } } end |
#action_read(cid, anum) ⇒ String
Read an action
545 546 547 |
# File 'lib/icfs/cache_elastic.rb', line 545 def action_read(cid, anum) _read(:action, '%s.%d' % [cid, anum]) end |
#action_search(query) ⇒ Object
Search for actions
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/icfs/cache_elastic.rb', line 561 def action_search(query) # build the query task_must = [ _query_match('tasks.title', query[:title]) ].compact task_filter = [ _query_term('tasks.assigned', query[:assigned]), _query_term('tasks.status', query[:status]), _query_term('tasks.flag', query[:flag]), _query_times('tasks.time', query[:after], query[:before]), _query_term('tasks.tags', query[:tags]), ].compact must = [ _query_nested( 'tasks', _query_bool(task_must, task_filter, nil, nil) ) ] filter = [ _query_term('caseid', query[:caseid]) ].compact req = { 'query' => _query_bool(must, filter, nil, nil) } # sort case query[:sort] when 'time_desc' srt = 'desc' when 'time_asc' srt = 'asc' else srt = query[:title] ? nil : 'desc' end if srt req['sort'] = [ { 'tasks.time' => { 'order' => srt, 'nested' => { 'path' => 'tasks', 'filter' => _query_term( 'tasks.assigned', query[:assigned]) } } }, { '_id' => { 'order' => 'desc' } } ] end # paging _page(query, req) # run the search url = @map[:action] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? return _results(resp, query) do |src| tsk = src['tasks'].select{|tk| tk['assigned'] == query[:assigned]}.first { caseid: src['caseid'], action: src['action'], status: tsk['status'], flag: tsk['flag'], title: tsk['title'], time: tsk['time'], tags: tsk['tags'], } end end |
#action_tags(query) ⇒ Object
List tags used on action tasks
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 |
# File 'lib/icfs/cache_elastic.rb', line 1114 def (query) # build the query task_filter = [ _query_term('tasks.assigned', query[:assigned]), _query_term('tasks.status', query[:status]), _query_term('tasks.flag', query[:flag]), _query_times('tasks.time', query[:after], query[:before]), ].compact qu_filt = _query_bool(nil, task_filter, nil, nil) ag = _agg_terms('tags', 'tasks.tags', nil) ag = _agg_filter('filt', qu_filt, ag) ag = _agg_nested('nest', 'tasks', ag) if query[:caseid] qu = _query_term('caseid', query[:caseid]) else qu = _query_all() end req = { 'query' => qu, 'aggs' => ag, 'size' => 0 } # run the search url = @map[:action] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? # extract tags rh = JSON.parse(resp.body) rh = rh['aggregations']['nest']['filt']['tags']['buckets'] list = rh.map do |hh| { object: { tag: hh['key'], count: hh['doc_count'], } } end return { query: query, list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag]} } end |
#action_write(cid, anum, item) ⇒ Object
Write an action
553 554 555 |
# File 'lib/icfs/cache_elastic.rb', line 553 def action_write(cid, anum, item) _write(:action, '%s.%d' % [cid, anum], item) end |
#case_read(cid) ⇒ String
Read a case
225 226 227 |
# File 'lib/icfs/cache_elastic.rb', line 225 def case_read(cid) _read(:case, cid) end |
#case_search(query) ⇒ Object
Search for cases
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/icfs/cache_elastic.rb', line 258 def case_search(query) # build the query must = [ _query_match('title', query[:title]), ].compact filter = [ _query_term('tags', query[:tags]), _query_term('status', query[:status]), _query_term('template', query[:template]), ].compact access = [ _query_term('access.grant', query[:grantee]), _query_term('access.perm', query[:perm]), ].compact unless access.empty? qu = (access.size == 1) ? access[0] : _query_bool(nil, access, nil, nil) filter << _query_nested('access', qu) end req = { 'query' => _query_bool(must, filter, nil, nil) } # highlight hl = {} hl['title'] = {} if query[:title] req['highlight'] = { 'fields' => hl } unless hl.empty? # sort unless query[:title] req['sort'] = { 'caseid' => 'asc' } end # paging _page(query, req) # run the search url = @map[:case] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? return _results(resp, query, ResultsCase) end |
#case_tags(query) ⇒ Object
Get list of tags for cases
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 |
# File 'lib/icfs/cache_elastic.rb', line 1062 def (query) # build the query filter = [ _query_term('status', query[:status]), _query_term('template', query[:template]), ].compact access = [ _query_term('access.grant', query[:grantee]), _query_term('access.perm', query[:perm]), ].compact unless access.empty? qu = (access.size == 1) ? access[0] : _query_bool(nil, access, nil, nil) filter << _query_nested('access', qu) end qu = _query_bool(nil, filter, nil, nil) ag = _agg_terms('tags', 'tags', nil) req = { 'query' => qu, 'aggs' => ag, 'size' => 0 } # run the search url = @map[:case] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? # extract tags rh = JSON.parse(resp.body) rh = rh['aggregations']['tags']['buckets'] list = rh.map do |hh| { object: { tag: hh['key'], count: hh['doc_count'], } } end return { query: query, list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag] } } end |
#case_write(cid, item) ⇒ Object
Write a case
233 234 235 |
# File 'lib/icfs/cache_elastic.rb', line 233 def case_write(cid, item) _write(:case, cid, item) end |
#current_read(cid) ⇒ String
Read current
209 210 211 |
# File 'lib/icfs/cache_elastic.rb', line 209 def current_read(cid) _read(:current, cid) end |
#current_write(cid, item) ⇒ Object
Write current
217 218 219 |
# File 'lib/icfs/cache_elastic.rb', line 217 def current_write(cid, item) _write(:current, cid, item) end |
#entry_read(cid, enum) ⇒ String
Read an entry
432 433 434 |
# File 'lib/icfs/cache_elastic.rb', line 432 def entry_read(cid, enum) _read(:entry, '%s.%d' % [cid, enum]) end |
#entry_search(query) ⇒ Object
Search for entries
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/icfs/cache_elastic.rb', line 461 def entry_search(query) # build the query must = [ _query_match('title', query[:title]), _query_match('content', query[:content]), ].compact filter = [ _query_term('tags', query[:tags]), _query_term('caseid', query[:caseid]), _query_times('time', query[:after], query[:before]), _query_term('action', query[:action]), _query_term('index', query[:index]), ].compact stats = [ _query_term('stats.name', query[:stat]), _query_term('stats.credit', query[:credit]), ].compact unless stats.empty? qu = (stats.size == 1) ? stats[0] : _query_bool(nil, stats, nil, nil) filter << _query_nested('stats', qu) end req = { 'query' => _query_bool(must, filter, nil, nil) } # highlight hl = {} hl['title'] = {} if query[:title] hl['content'] = {} if query[:content] req['highlight'] = { 'fields' => hl } unless hl.empty? # sort case query[:sort] when 'time_desc' req['sort'] = [ { 'time' => 'desc' }, { '_id' => 'desc' }, ] when 'time_asc' req['sort'] = [ { 'time' => 'asc' }, { '_id' => 'desc' }, ] when nil if !query[:title] && !query[:content] req['sort'] = [ { 'time' => 'desc' }, { '_id' => 'desc' }, ] end end # paging _page(query, req) # run the search url = @map[:entry] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? return _results(resp, query, ResultsEntry) end |
#entry_tags(query) ⇒ Object
List tags used on Entries
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/icfs/cache_elastic.rb', line 1020 def (query) # build the query ag = _agg_terms('tags', 'tags', nil) qu = _query_term('caseid', query[:caseid]) qu = _query_constant(qu) req = { 'query' => qu, 'aggs' => ag, 'size' => 0 } # run the search url = @map[:entry] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? # extract tags rh = JSON.parse(resp.body) rh = rh['aggregations']['tags']['buckets'] list = rh.map do |hh| { object: { caseid: query[:caseid], tag: hh['key'], count: hh['doc_count'], } } end return { query: query, list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag]} } end |
#entry_write(cid, enum, item) ⇒ Object
Write an entry
440 441 442 |
# File 'lib/icfs/cache_elastic.rb', line 440 def entry_write(cid, enum, item) _write(:entry, '%s.%d' % [cid, enum], item) end |
#index_read(cid, xnum) ⇒ String
Read an Index
646 647 648 |
# File 'lib/icfs/cache_elastic.rb', line 646 def index_read(cid, xnum) _read(:index, '%s.%d' % [cid, xnum]) end |
#index_search(query) ⇒ Object
Search for Indexes
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/icfs/cache_elastic.rb', line 653 def index_search(query) # build the query must = [ _query_match('title', query[:title]), _query_match('content', query[:content]), ].compact filter = [ _query_term('caseid', query[:caseid]), _query_term('tags', query[:tags]), _query_prefix('title.raw', query[:prefix]), ].compact req = { 'query' => _query_bool(must, filter, nil, nil) } # highlight hl = {} hl['title'] = {} if query[:title] hl['content'] = {} if query[:content] req['highlight'] = { 'fields' => hl } unless hl.empty? # sort case query[:sort] when 'index_asc' req['sort'] = [ { 'index' => 'asc' }, { '_id' => 'desc' }, ] when 'index_desc' req['sort'] = [ { 'index' => 'desc' }, { '_id' => 'desc' }, ] when 'title_desc' req['sort'] = [ { 'title.raw' => 'desc' }, { '_id' => 'desc' }, ] when 'title_asc' req['sort'] = [ { 'title.raw' => 'asc' }, { '_id' => 'desc' }, ] else # default if not a title/content query if must.empty? req['sort'] = [ { 'title.raw' => 'asc' }, { '_id' => 'desc' }, ] end end # paging _page(query, req) # run the search url = @map[:index] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? return _results(resp, query, ResultsIndex) end |
#index_tags(query) ⇒ Object
List tags used in indexes
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/icfs/cache_elastic.rb', line 731 def (query) # build the query ag = _agg_terms('tags', 'tags', nil) qu = _query_term('caseid', query[:caseid]) qu = _query_constant(qu) req = { 'query' => qu, 'aggs' => ag, 'size' => 0 } # run the search url = @map[:index] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? # extract tags rh = JSON.parse(resp.body) rh = rh['aggregations']['tags']['buckets'] list = rh.map do |hh| { object: { caseid: query[:caseid], tag: hh['key'], count: hh['doc_count'], } } end return { query: query, list: list.sort{|aa, bb| aa[:object][:tag] <=> bb[:object][:tag]} } end |
#index_write(cid, xnum, item) ⇒ Object
Write an Index
638 639 640 |
# File 'lib/icfs/cache_elastic.rb', line 638 def index_write(cid, xnum, item) _write(:index, '%s.%d' % [cid, xnum], item) end |
#lock_release(cid) ⇒ Object
Release a case lock
197 198 199 200 201 202 203 |
# File 'lib/icfs/cache_elastic.rb', line 197 def lock_release(cid) url = '%s/_doc/%s' % [@map[:lock], CGI.escape(cid)] resp = @es.run_request(:delete, url, '', {}) if !resp.success? raise('Elasticsearch lock release failed: %s' % cid) end end |
#lock_take(cid) ⇒ Object
Take a case lock
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/icfs/cache_elastic.rb', line 174 def lock_take(cid) json = '{"client":"%s"}' % @name url = '%s/_doc/%s/_create' % [@map[:lock], CGI.escape(cid)] head = {'Content-Type' => 'application/json'}.freeze # try to take tries = 5 while tries > 0 resp = @es.run_request(:put, url, json, head) return true if resp.success? tries = tries - 1 sleep(0.1) end # failed to take lock raise('Elasticsearch lock take failed: %s' % cid) end |
#log_read(cid, lnum) ⇒ String
Read a log
773 774 775 |
# File 'lib/icfs/cache_elastic.rb', line 773 def log_read(cid, lnum) _read(:log, '%s.%d' % [cid, lnum]) end |
#log_search(query) ⇒ Object
Search for a log
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
# File 'lib/icfs/cache_elastic.rb', line 802 def log_search(query) # build the query filter = [ _query_term('caseid', query[:caseid]), _query_times('times', query[:after], query[:before]), _query_term('user', query[:user]), _query_term('entry.num', query[:entry]), _query_term('index.num', query[:index]), _query_term('action.num', query[:action]), ].compact req = { 'query' => _query_bool(nil, filter, nil, nil) } # sort case query[:sort] when 'time_desc', nil req['sort'] = [ { 'time' => 'desc' }, { '_id' => 'desc' }, ] when 'time_asc' req['sort'] = [ { 'time' => 'asc' }, { '_id' => 'desc' }, ] end # paging _page(query, req) # run the search url = @map[:log] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? return _results(resp, query, ResultsLog) end |
#log_write(cid, lnum, item) ⇒ Object
Write a log
781 782 783 |
# File 'lib/icfs/cache_elastic.rb', line 781 def log_write(cid, lnum, item) _write(:log, '%s.%d' % [cid, lnum], item) end |
#stats(query) ⇒ Object
Analyze stats
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'lib/icfs/cache_elastic.rb', line 950 def stats(query) # aggs ag = _agg_stats('vals', 'stats.value') ag = _agg_terms('stats', 'stats.name', ag) if query[:credit] cd = _query_term('stats.credit', query[:credit]) ag = _agg_filter('credit', cd, ag) end ag = _agg_nested('nested', 'stats', ag) # build the query filt = [ _query_term('caseid', query[:caseid]), _query_times('time', query[:after], query[:before]), ].compact qu = _query_bool(nil, filt, nil, nil) # the request req = { 'query' => qu, 'aggs' => ag, 'size' => 0, } # run the search url = @map[:entry] + '/_search' body = JSON.generate(req) head = { 'Content-Type' => 'application/json' } resp = @es.run_request(:get, url, body, head) raise 'search failed' if !resp.success? # extract stats rh = JSON.parse(resp.body) if query[:credit] rh = rh['aggregations']['nested']['credit']['stats']['buckets'] else rh = rh['aggregations']['nested']['stats']['buckets'] end list = rh.map do |hh| { object: { stat: hh['key'], sum: hh['vals']['sum'], count: hh['vals']['count'], min: hh['vals']['min'], max: hh['vals']['max'], } } end # return the results return { query: query, list: list } end |