From 5dbf36f356e67c6eb7df68727417702fa100bec5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 13 Feb 2020 14:06:17 +0800 Subject: [PATCH] Issue search support elasticsearch (#9428) * Issue search support elasticsearch * Fix lint * Add indexer name on app.ini * add a warnning on SearchIssuesByKeyword * improve code --- .drone.yml | 6 + custom/conf/app.ini.sample | 6 +- .../doc/advanced/config-cheat-sheet.en-us.md | 6 +- .../doc/advanced/config-cheat-sheet.zh-cn.md | 4 +- go.mod | 1 + go.sum | 10 + integrations/mysql.ini.tmpl | 2 + modules/indexer/issues/bleve.go | 2 +- modules/indexer/issues/elastic_search.go | 230 ++ modules/indexer/issues/indexer.go | 32 +- modules/setting/indexer.go | 30 +- vendor/github.com/mailru/easyjson/.gitignore | 5 + vendor/github.com/mailru/easyjson/.travis.yml | 12 + vendor/github.com/mailru/easyjson/Makefile | 52 + vendor/github.com/mailru/easyjson/README.md | 336 +++ vendor/github.com/mailru/easyjson/go.mod | 3 + vendor/github.com/mailru/easyjson/helpers.go | 78 + vendor/github.com/mailru/easyjson/raw.go | 45 + .../github.com/olivere/elastic/v7/.fossa.yml | 38 + .../github.com/olivere/elastic/v7/.gitignore | 35 + .../github.com/olivere/elastic/v7/.travis.yml | 32 + .../olivere/elastic/v7/CHANGELOG-3.0.md | 363 +++ .../olivere/elastic/v7/CHANGELOG-5.0.md | 195 ++ .../olivere/elastic/v7/CHANGELOG-6.0.md | 18 + .../olivere/elastic/v7/CHANGELOG-7.0.md | 55 + .../olivere/elastic/v7/CODE_OF_CONDUCT.md | 46 + .../olivere/elastic/v7/CONTRIBUTING.md | 40 + .../olivere/elastic/v7/CONTRIBUTORS | 193 ++ .../olivere/elastic/v7/ISSUE_TEMPLATE.md | 19 + vendor/github.com/olivere/elastic/v7/LICENSE | 20 + .../github.com/olivere/elastic/v7/README.md | 432 ++++ .../elastic/v7/acknowledged_response.go | 13 + .../github.com/olivere/elastic/v7/backoff.go | 148 ++ vendor/github.com/olivere/elastic/v7/bulk.go | 470 ++++ .../olivere/elastic/v7/bulk_delete_request.go | 186 ++ .../v7/bulk_delete_request_easyjson.go | 266 +++ .../olivere/elastic/v7/bulk_index_request.go | 260 ++ .../elastic/v7/bulk_index_request_easyjson.go | 306 +++ .../olivere/elastic/v7/bulk_processor.go | 656 ++++++ .../olivere/elastic/v7/bulk_request.go | 17 + .../olivere/elastic/v7/bulk_update_request.go | 334 +++ .../v7/bulk_update_request_easyjson.go | 493 ++++ .../olivere/elastic/v7/canonicalize.go | 34 + .../olivere/elastic/v7/cat_aliases.go | 222 ++ .../olivere/elastic/v7/cat_allocation.go | 239 ++ .../olivere/elastic/v7/cat_count.go | 215 ++ .../olivere/elastic/v7/cat_health.go | 211 ++ .../olivere/elastic/v7/cat_indices.go | 374 +++ .../olivere/elastic/v7/clear_scroll.go | 162 ++ .../github.com/olivere/elastic/v7/client.go | 2099 +++++++++++++++++ .../olivere/elastic/v7/cluster_health.go | 296 +++ .../olivere/elastic/v7/cluster_reroute.go | 438 ++++ .../olivere/elastic/v7/cluster_state.go | 355 +++ .../olivere/elastic/v7/cluster_stats.go | 409 ++++ .../olivere/elastic/v7/config/config.go | 87 + .../olivere/elastic/v7/config/doc.go | 9 + .../olivere/elastic/v7/connection.go | 90 + vendor/github.com/olivere/elastic/v7/count.go | 381 +++ .../github.com/olivere/elastic/v7/decoder.go | 38 + .../github.com/olivere/elastic/v7/delete.go | 305 +++ .../olivere/elastic/v7/delete_by_query.go | 781 ++++++ vendor/github.com/olivere/elastic/v7/doc.go | 51 + .../olivere/elastic/v7/docker-compose.yml | 59 + .../olivere/elastic/v7/docvalue_field.go | 42 + .../github.com/olivere/elastic/v7/errors.go | 184 ++ .../github.com/olivere/elastic/v7/exists.go | 236 ++ .../github.com/olivere/elastic/v7/explain.go | 390 +++ .../elastic/v7/fetch_source_context.go | 90 + .../olivere/elastic/v7/field_caps.go | 257 ++ .../olivere/elastic/v7/geo_point.go | 54 + vendor/github.com/olivere/elastic/v7/get.go | 317 +++ vendor/github.com/olivere/elastic/v7/go.mod | 18 + .../olivere/elastic/v7/highlight.go | 469 ++++ vendor/github.com/olivere/elastic/v7/index.go | 377 +++ .../olivere/elastic/v7/indices_analyze.go | 320 +++ .../olivere/elastic/v7/indices_close.go | 214 ++ .../olivere/elastic/v7/indices_create.go | 189 ++ .../olivere/elastic/v7/indices_delete.go | 184 ++ .../elastic/v7/indices_delete_template.go | 180 ++ .../olivere/elastic/v7/indices_exists.go | 204 ++ .../elastic/v7/indices_exists_template.go | 169 ++ .../olivere/elastic/v7/indices_flush.go | 224 ++ .../elastic/v7/indices_flush_synced.go | 281 +++ .../olivere/elastic/v7/indices_forcemerge.go | 235 ++ .../olivere/elastic/v7/indices_freeze.go | 229 ++ .../olivere/elastic/v7/indices_get.go | 237 ++ .../olivere/elastic/v7/indices_get_aliases.go | 230 ++ .../elastic/v7/indices_get_field_mapping.go | 238 ++ .../olivere/elastic/v7/indices_get_mapping.go | 225 ++ .../elastic/v7/indices_get_settings.go | 238 ++ .../elastic/v7/indices_get_template.go | 184 ++ .../olivere/elastic/v7/indices_open.go | 227 ++ .../olivere/elastic/v7/indices_put_alias.go | 399 ++++ .../olivere/elastic/v7/indices_put_mapping.go | 261 ++ .../elastic/v7/indices_put_settings.go | 242 ++ .../elastic/v7/indices_put_template.go | 259 ++ .../olivere/elastic/v7/indices_refresh.go | 149 ++ .../olivere/elastic/v7/indices_rollover.go | 324 +++ .../olivere/elastic/v7/indices_segments.go | 278 +++ .../olivere/elastic/v7/indices_shrink.go | 231 ++ .../olivere/elastic/v7/indices_stats.go | 530 +++++ .../olivere/elastic/v7/indices_unfreeze.go | 229 ++ .../elastic/v7/ingest_delete_pipeline.go | 181 ++ .../olivere/elastic/v7/ingest_get_pipeline.go | 177 ++ .../olivere/elastic/v7/ingest_put_pipeline.go | 210 ++ .../elastic/v7/ingest_simulate_pipeline.go | 213 ++ .../olivere/elastic/v7/inner_hit.go | 170 ++ .../github.com/olivere/elastic/v7/logger.go | 10 + vendor/github.com/olivere/elastic/v7/mget.go | 314 +++ .../github.com/olivere/elastic/v7/msearch.go | 169 ++ .../olivere/elastic/v7/mtermvectors.go | 526 +++++ .../olivere/elastic/v7/nodes_info.go | 419 ++++ .../olivere/elastic/v7/nodes_stats.go | 747 ++++++ vendor/github.com/olivere/elastic/v7/ping.go | 197 ++ .../github.com/olivere/elastic/v7/plugins.go | 40 + vendor/github.com/olivere/elastic/v7/query.go | 13 + .../github.com/olivere/elastic/v7/reindex.go | 745 ++++++ .../github.com/olivere/elastic/v7/request.go | 123 + .../github.com/olivere/elastic/v7/rescore.go | 44 + .../github.com/olivere/elastic/v7/rescorer.go | 64 + .../github.com/olivere/elastic/v7/response.go | 62 + .../github.com/olivere/elastic/v7/retrier.go | 68 + vendor/github.com/olivere/elastic/v7/retry.go | 56 + .../github.com/olivere/elastic/v7/run-es.sh | 3 + .../olivere/elastic/v7/run-tests.sh | 2 + .../github.com/olivere/elastic/v7/script.go | 160 ++ .../olivere/elastic/v7/script_delete.go | 186 ++ .../olivere/elastic/v7/script_get.go | 169 ++ .../olivere/elastic/v7/script_put.go | 226 ++ .../github.com/olivere/elastic/v7/scroll.go | 590 +++++ .../github.com/olivere/elastic/v7/search.go | 831 +++++++ .../olivere/elastic/v7/search_aggs.go | 1764 ++++++++++++++ .../v7/search_aggs_bucket_adjacency_matrix.go | 96 + .../search_aggs_bucket_auto_date_histogram.go | 163 ++ .../elastic/v7/search_aggs_bucket_children.go | 76 + .../v7/search_aggs_bucket_composite.go | 577 +++++ .../v7/search_aggs_bucket_count_thresholds.go | 13 + .../v7/search_aggs_bucket_date_histogram.go | 342 +++ .../v7/search_aggs_bucket_date_range.go | 255 ++ .../search_aggs_bucket_diversified_sampler.go | 126 + .../elastic/v7/search_aggs_bucket_filter.go | 77 + .../elastic/v7/search_aggs_bucket_filters.go | 138 ++ .../v7/search_aggs_bucket_geo_distance.go | 198 ++ .../v7/search_aggs_bucket_geohash_grid.go | 104 + .../elastic/v7/search_aggs_bucket_global.go | 71 + .../v7/search_aggs_bucket_histogram.go | 265 +++ .../elastic/v7/search_aggs_bucket_ip_range.go | 195 ++ .../elastic/v7/search_aggs_bucket_missing.go | 81 + .../elastic/v7/search_aggs_bucket_nested.go | 82 + .../elastic/v7/search_aggs_bucket_range.go | 244 ++ .../v7/search_aggs_bucket_reverse_nested.go | 86 + .../elastic/v7/search_aggs_bucket_sampler.go | 111 + .../search_aggs_bucket_significant_terms.go | 450 ++++ .../v7/search_aggs_bucket_significant_text.go | 250 ++ .../elastic/v7/search_aggs_bucket_terms.go | 422 ++++ .../elastic/v7/search_aggs_matrix_stats.go | 126 + .../elastic/v7/search_aggs_metrics_avg.go | 112 + .../v7/search_aggs_metrics_cardinality.go | 127 + .../v7/search_aggs_metrics_extended_stats.go | 117 + .../v7/search_aggs_metrics_geo_bounds.go | 105 + .../v7/search_aggs_metrics_geo_centroid.go | 95 + .../elastic/v7/search_aggs_metrics_max.go | 108 + .../elastic/v7/search_aggs_metrics_min.go | 109 + .../search_aggs_metrics_percentile_ranks.go | 140 ++ .../v7/search_aggs_metrics_percentiles.go | 169 ++ .../v7/search_aggs_metrics_scripted_metric.go | 112 + .../elastic/v7/search_aggs_metrics_stats.go | 108 + .../elastic/v7/search_aggs_metrics_sum.go | 108 + .../v7/search_aggs_metrics_top_hits.go | 161 ++ .../v7/search_aggs_metrics_value_count.go | 102 + .../v7/search_aggs_metrics_weighted_avg.go | 156 ++ .../v7/search_aggs_pipeline_avg_bucket.go | 94 + .../v7/search_aggs_pipeline_bucket_script.go | 113 + .../search_aggs_pipeline_bucket_selector.go | 115 + .../v7/search_aggs_pipeline_bucket_sort.go | 119 + .../v7/search_aggs_pipeline_cumulative_sum.go | 71 + .../v7/search_aggs_pipeline_derivative.go | 105 + ...rch_aggs_pipeline_extended_stats_bucket.go | 107 + .../v7/search_aggs_pipeline_max_bucket.go | 95 + .../v7/search_aggs_pipeline_min_bucket.go | 95 + .../v7/search_aggs_pipeline_mov_avg.go | 378 +++ .../elastic/v7/search_aggs_pipeline_mov_fn.go | 132 ++ ...search_aggs_pipeline_percentiles_bucket.go | 104 + .../v7/search_aggs_pipeline_serial_diff.go | 105 + .../v7/search_aggs_pipeline_stats_bucket.go | 94 + .../v7/search_aggs_pipeline_sum_bucket.go | 94 + .../elastic/v7/search_collapse_builder.go | 68 + .../olivere/elastic/v7/search_queries_bool.go | 203 ++ .../elastic/v7/search_queries_boosting.go | 97 + .../elastic/v7/search_queries_common_terms.go | 141 ++ .../v7/search_queries_constant_score.go | 59 + .../elastic/v7/search_queries_dis_max.go | 104 + .../search_queries_distance_feature_query.go | 119 + .../elastic/v7/search_queries_exists.go | 49 + .../olivere/elastic/v7/search_queries_fsq.go | 159 ++ .../v7/search_queries_fsq_score_funcs.go | 582 +++++ .../elastic/v7/search_queries_fuzzy.go | 120 + .../v7/search_queries_geo_bounding_box.go | 121 + .../elastic/v7/search_queries_geo_distance.go | 107 + .../elastic/v7/search_queries_geo_polygon.go | 72 + .../elastic/v7/search_queries_has_child.go | 131 + .../elastic/v7/search_queries_has_parent.go | 108 + .../olivere/elastic/v7/search_queries_ids.go | 78 + .../elastic/v7/search_queries_match.go | 189 ++ .../elastic/v7/search_queries_match_all.go | 51 + .../elastic/v7/search_queries_match_none.go | 39 + .../elastic/v7/search_queries_match_phrase.go | 79 + .../v7/search_queries_match_phrase_prefix.go | 89 + .../v7/search_queries_more_like_this.go | 414 ++++ .../elastic/v7/search_queries_multi_match.go | 278 +++ .../elastic/v7/search_queries_nested.go | 96 + .../elastic/v7/search_queries_parent_id.go | 99 + .../elastic/v7/search_queries_percolator.go | 131 + .../elastic/v7/search_queries_prefix.go | 67 + .../elastic/v7/search_queries_query_string.go | 350 +++ .../elastic/v7/search_queries_range.go | 155 ++ .../elastic/v7/search_queries_raw_string.go | 26 + .../elastic/v7/search_queries_regexp.go | 82 + .../elastic/v7/search_queries_script.go | 51 + .../elastic/v7/search_queries_script_score.go | 110 + .../v7/search_queries_simple_query_string.go | 246 ++ .../elastic/v7/search_queries_slice.go | 53 + .../olivere/elastic/v7/search_queries_term.go | 58 + .../elastic/v7/search_queries_terms.go | 75 + .../elastic/v7/search_queries_terms_set.go | 97 + .../olivere/elastic/v7/search_queries_type.go | 26 + .../elastic/v7/search_queries_wildcard.go | 81 + .../elastic/v7/search_queries_wrapper.go | 28 + .../olivere/elastic/v7/search_request.go | 586 +++++ .../olivere/elastic/v7/search_shards.go | 258 ++ .../olivere/elastic/v7/search_source.go | 592 +++++ .../olivere/elastic/v7/search_terms_lookup.go | 76 + .../olivere/elastic/v7/snapshot_create.go | 227 ++ .../elastic/v7/snapshot_create_repository.go | 257 ++ .../olivere/elastic/v7/snapshot_delete.go | 154 ++ .../elastic/v7/snapshot_delete_repository.go | 183 ++ .../olivere/elastic/v7/snapshot_get.go | 225 ++ .../elastic/v7/snapshot_get_repository.go | 185 ++ .../olivere/elastic/v7/snapshot_restore.go | 294 +++ .../elastic/v7/snapshot_verify_repository.go | 184 ++ vendor/github.com/olivere/elastic/v7/sort.go | 654 +++++ .../olivere/elastic/v7/suggest_field.go | 90 + .../olivere/elastic/v7/suggester.go | 15 + .../elastic/v7/suggester_completion.go | 358 +++ .../olivere/elastic/v7/suggester_context.go | 124 + .../elastic/v7/suggester_context_category.go | 154 ++ .../elastic/v7/suggester_context_geo.go | 178 ++ .../olivere/elastic/v7/suggester_phrase.go | 550 +++++ .../olivere/elastic/v7/suggester_term.go | 233 ++ .../olivere/elastic/v7/tasks_cancel.go | 207 ++ .../olivere/elastic/v7/tasks_get_task.go | 168 ++ .../olivere/elastic/v7/tasks_list.go | 280 +++ .../olivere/elastic/v7/termvectors.go | 520 ++++ .../github.com/olivere/elastic/v7/update.go | 412 ++++ .../olivere/elastic/v7/update_by_query.go | 773 ++++++ .../olivere/elastic/v7/uritemplates/LICENSE | 18 + .../elastic/v7/uritemplates/uritemplates.go | 359 +++ .../olivere/elastic/v7/uritemplates/utils.go | 13 + .../github.com/olivere/elastic/v7/validate.go | 339 +++ .../elastic/v7/xpack_ilm_delete_lifecycle.go | 194 ++ .../elastic/v7/xpack_ilm_get_lifecycle.go | 193 ++ .../elastic/v7/xpack_ilm_put_lifecycle.go | 214 ++ .../olivere/elastic/v7/xpack_info.go | 225 ++ .../v7/xpack_security_change_password.go | 201 ++ .../elastic/v7/xpack_security_delete_role.go | 159 ++ .../v7/xpack_security_delete_role_mapping.go | 159 ++ .../elastic/v7/xpack_security_delete_user.go | 170 ++ .../elastic/v7/xpack_security_disable_user.go | 168 ++ .../elastic/v7/xpack_security_enable_user.go | 168 ++ .../elastic/v7/xpack_security_get_role.go | 186 ++ .../v7/xpack_security_get_role_mapping.go | 165 ++ .../elastic/v7/xpack_security_get_user.go | 176 ++ .../elastic/v7/xpack_security_put_role.go | 174 ++ .../v7/xpack_security_put_role_mapping.go | 174 ++ .../elastic/v7/xpack_security_put_user.go | 215 ++ .../elastic/v7/xpack_watcher_ack_watch.go | 198 ++ .../v7/xpack_watcher_activate_watch.go | 169 ++ .../v7/xpack_watcher_deactivate_watch.go | 169 ++ .../elastic/v7/xpack_watcher_delete_watch.go | 171 ++ .../elastic/v7/xpack_watcher_execute_watch.go | 214 ++ .../elastic/v7/xpack_watcher_get_watch.go | 214 ++ .../elastic/v7/xpack_watcher_put_watch.go | 212 ++ .../olivere/elastic/v7/xpack_watcher_start.go | 137 ++ .../olivere/elastic/v7/xpack_watcher_stats.go | 165 ++ .../olivere/elastic/v7/xpack_watcher_stop.go | 137 ++ vendor/modules.txt | 5 + 286 files changed, 57032 insertions(+), 25 deletions(-) create mode 100644 modules/indexer/issues/elastic_search.go create mode 100644 vendor/github.com/mailru/easyjson/.gitignore create mode 100644 vendor/github.com/mailru/easyjson/.travis.yml create mode 100644 vendor/github.com/mailru/easyjson/Makefile create mode 100644 vendor/github.com/mailru/easyjson/README.md create mode 100644 vendor/github.com/mailru/easyjson/go.mod create mode 100644 vendor/github.com/mailru/easyjson/helpers.go create mode 100644 vendor/github.com/mailru/easyjson/raw.go create mode 100644 vendor/github.com/olivere/elastic/v7/.fossa.yml create mode 100644 vendor/github.com/olivere/elastic/v7/.gitignore create mode 100644 vendor/github.com/olivere/elastic/v7/.travis.yml create mode 100644 vendor/github.com/olivere/elastic/v7/CHANGELOG-3.0.md create mode 100644 vendor/github.com/olivere/elastic/v7/CHANGELOG-5.0.md create mode 100644 vendor/github.com/olivere/elastic/v7/CHANGELOG-6.0.md create mode 100644 vendor/github.com/olivere/elastic/v7/CHANGELOG-7.0.md create mode 100644 vendor/github.com/olivere/elastic/v7/CODE_OF_CONDUCT.md create mode 100644 vendor/github.com/olivere/elastic/v7/CONTRIBUTING.md create mode 100644 vendor/github.com/olivere/elastic/v7/CONTRIBUTORS create mode 100644 vendor/github.com/olivere/elastic/v7/ISSUE_TEMPLATE.md create mode 100644 vendor/github.com/olivere/elastic/v7/LICENSE create mode 100644 vendor/github.com/olivere/elastic/v7/README.md create mode 100644 vendor/github.com/olivere/elastic/v7/acknowledged_response.go create mode 100644 vendor/github.com/olivere/elastic/v7/backoff.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_delete_request.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_delete_request_easyjson.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_index_request.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_index_request_easyjson.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_processor.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_request.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_update_request.go create mode 100644 vendor/github.com/olivere/elastic/v7/bulk_update_request_easyjson.go create mode 100644 vendor/github.com/olivere/elastic/v7/canonicalize.go create mode 100644 vendor/github.com/olivere/elastic/v7/cat_aliases.go create mode 100644 vendor/github.com/olivere/elastic/v7/cat_allocation.go create mode 100644 vendor/github.com/olivere/elastic/v7/cat_count.go create mode 100644 vendor/github.com/olivere/elastic/v7/cat_health.go create mode 100644 vendor/github.com/olivere/elastic/v7/cat_indices.go create mode 100644 vendor/github.com/olivere/elastic/v7/clear_scroll.go create mode 100644 vendor/github.com/olivere/elastic/v7/client.go create mode 100644 vendor/github.com/olivere/elastic/v7/cluster_health.go create mode 100644 vendor/github.com/olivere/elastic/v7/cluster_reroute.go create mode 100644 vendor/github.com/olivere/elastic/v7/cluster_state.go create mode 100644 vendor/github.com/olivere/elastic/v7/cluster_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/config/config.go create mode 100644 vendor/github.com/olivere/elastic/v7/config/doc.go create mode 100644 vendor/github.com/olivere/elastic/v7/connection.go create mode 100644 vendor/github.com/olivere/elastic/v7/count.go create mode 100644 vendor/github.com/olivere/elastic/v7/decoder.go create mode 100644 vendor/github.com/olivere/elastic/v7/delete.go create mode 100644 vendor/github.com/olivere/elastic/v7/delete_by_query.go create mode 100644 vendor/github.com/olivere/elastic/v7/doc.go create mode 100644 vendor/github.com/olivere/elastic/v7/docker-compose.yml create mode 100644 vendor/github.com/olivere/elastic/v7/docvalue_field.go create mode 100644 vendor/github.com/olivere/elastic/v7/errors.go create mode 100644 vendor/github.com/olivere/elastic/v7/exists.go create mode 100644 vendor/github.com/olivere/elastic/v7/explain.go create mode 100644 vendor/github.com/olivere/elastic/v7/fetch_source_context.go create mode 100644 vendor/github.com/olivere/elastic/v7/field_caps.go create mode 100644 vendor/github.com/olivere/elastic/v7/geo_point.go create mode 100644 vendor/github.com/olivere/elastic/v7/get.go create mode 100644 vendor/github.com/olivere/elastic/v7/go.mod create mode 100644 vendor/github.com/olivere/elastic/v7/highlight.go create mode 100644 vendor/github.com/olivere/elastic/v7/index.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_analyze.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_close.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_create.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_delete.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_delete_template.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_exists.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_exists_template.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_flush.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_flush_synced.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_forcemerge.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_freeze.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_get.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_get_aliases.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_get_field_mapping.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_get_mapping.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_get_settings.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_get_template.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_open.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_put_alias.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_put_mapping.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_put_settings.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_put_template.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_refresh.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_rollover.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_segments.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_shrink.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/indices_unfreeze.go create mode 100644 vendor/github.com/olivere/elastic/v7/ingest_delete_pipeline.go create mode 100644 vendor/github.com/olivere/elastic/v7/ingest_get_pipeline.go create mode 100644 vendor/github.com/olivere/elastic/v7/ingest_put_pipeline.go create mode 100644 vendor/github.com/olivere/elastic/v7/ingest_simulate_pipeline.go create mode 100644 vendor/github.com/olivere/elastic/v7/inner_hit.go create mode 100644 vendor/github.com/olivere/elastic/v7/logger.go create mode 100644 vendor/github.com/olivere/elastic/v7/mget.go create mode 100644 vendor/github.com/olivere/elastic/v7/msearch.go create mode 100644 vendor/github.com/olivere/elastic/v7/mtermvectors.go create mode 100644 vendor/github.com/olivere/elastic/v7/nodes_info.go create mode 100644 vendor/github.com/olivere/elastic/v7/nodes_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/ping.go create mode 100644 vendor/github.com/olivere/elastic/v7/plugins.go create mode 100644 vendor/github.com/olivere/elastic/v7/query.go create mode 100644 vendor/github.com/olivere/elastic/v7/reindex.go create mode 100644 vendor/github.com/olivere/elastic/v7/request.go create mode 100644 vendor/github.com/olivere/elastic/v7/rescore.go create mode 100644 vendor/github.com/olivere/elastic/v7/rescorer.go create mode 100644 vendor/github.com/olivere/elastic/v7/response.go create mode 100644 vendor/github.com/olivere/elastic/v7/retrier.go create mode 100644 vendor/github.com/olivere/elastic/v7/retry.go create mode 100644 vendor/github.com/olivere/elastic/v7/run-es.sh create mode 100644 vendor/github.com/olivere/elastic/v7/run-tests.sh create mode 100644 vendor/github.com/olivere/elastic/v7/script.go create mode 100644 vendor/github.com/olivere/elastic/v7/script_delete.go create mode 100644 vendor/github.com/olivere/elastic/v7/script_get.go create mode 100644 vendor/github.com/olivere/elastic/v7/script_put.go create mode 100644 vendor/github.com/olivere/elastic/v7/scroll.go create mode 100644 vendor/github.com/olivere/elastic/v7/search.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_adjacency_matrix.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_auto_date_histogram.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_children.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_composite.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_count_thresholds.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_histogram.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_range.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_diversified_sampler.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filter.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filters.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geo_distance.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geohash_grid.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_global.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_histogram.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_ip_range.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_missing.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_nested.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_range.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_reverse_nested.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_sampler.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_terms.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_text.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_bucket_terms.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_matrix_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_avg.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_cardinality.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_extended_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_bounds.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_centroid.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_max.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_min.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentile_ranks.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentiles.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_scripted_metric.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_sum.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_top_hits.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_value_count.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_metrics_weighted_avg.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_avg_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_script.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_selector.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_sort.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_cumulative_sum.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_derivative.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_extended_stats_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_max_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_min_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_avg.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_fn.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_percentiles_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_serial_diff.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_stats_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_sum_bucket.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_collapse_builder.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_bool.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_boosting.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_common_terms.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_constant_score.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_dis_max.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_distance_feature_query.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_exists.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_fsq.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_fsq_score_funcs.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_fuzzy.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_geo_bounding_box.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_geo_distance.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_geo_polygon.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_has_child.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_has_parent.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_ids.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_match.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_match_all.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_match_none.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_match_phrase.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_match_phrase_prefix.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_more_like_this.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_multi_match.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_nested.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_parent_id.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_percolator.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_prefix.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_query_string.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_range.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_raw_string.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_regexp.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_script.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_script_score.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_simple_query_string.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_slice.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_term.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_terms.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_terms_set.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_type.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_wildcard.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_queries_wrapper.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_request.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_shards.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_source.go create mode 100644 vendor/github.com/olivere/elastic/v7/search_terms_lookup.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_create.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_create_repository.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_delete.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_delete_repository.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_get.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_get_repository.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_restore.go create mode 100644 vendor/github.com/olivere/elastic/v7/snapshot_verify_repository.go create mode 100644 vendor/github.com/olivere/elastic/v7/sort.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggest_field.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester_completion.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester_context.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester_context_category.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester_context_geo.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester_phrase.go create mode 100644 vendor/github.com/olivere/elastic/v7/suggester_term.go create mode 100644 vendor/github.com/olivere/elastic/v7/tasks_cancel.go create mode 100644 vendor/github.com/olivere/elastic/v7/tasks_get_task.go create mode 100644 vendor/github.com/olivere/elastic/v7/tasks_list.go create mode 100644 vendor/github.com/olivere/elastic/v7/termvectors.go create mode 100644 vendor/github.com/olivere/elastic/v7/update.go create mode 100644 vendor/github.com/olivere/elastic/v7/update_by_query.go create mode 100644 vendor/github.com/olivere/elastic/v7/uritemplates/LICENSE create mode 100644 vendor/github.com/olivere/elastic/v7/uritemplates/uritemplates.go create mode 100644 vendor/github.com/olivere/elastic/v7/uritemplates/utils.go create mode 100644 vendor/github.com/olivere/elastic/v7/validate.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_ilm_delete_lifecycle.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_ilm_get_lifecycle.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_ilm_put_lifecycle.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_info.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_change_password.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_delete_role.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_delete_role_mapping.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_delete_user.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_disable_user.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_enable_user.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_get_role.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_get_role_mapping.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_get_user.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_put_role.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_put_role_mapping.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_security_put_user.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_ack_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_activate_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_deactivate_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_delete_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_execute_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_get_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_put_watch.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_start.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_stats.go create mode 100644 vendor/github.com/olivere/elastic/v7/xpack_watcher_stop.go diff --git a/.drone.yml b/.drone.yml index 5796ad503a..b2d16116e3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -86,6 +86,12 @@ services: pull: default image: gitea/test-openldap:latest + - name: elasticsearch + pull: default + environment: + discovery.type: single-node + image: elasticsearch:7.5.0 + steps: - name: fetch-tags pull: default diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 31e8bf0b09..cf9eda05d4 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -368,8 +368,12 @@ CONN_MAX_LIFETIME = 3s MAX_OPEN_CONNS = 0 [indexer] -; Issue indexer type, currently support: bleve or db, default is bleve +; Issue indexer type, currently support: bleve, db or elasticsearch, default is bleve ISSUE_INDEXER_TYPE = bleve +; Issue indexer connection string, available when ISSUE_INDEXER_TYPE is elasticsearch +ISSUE_INDEXER_CONN_STR = http://elastic:changeme@localhost:9200 +; Issue indexer name, available when ISSUE_INDEXER_TYPE is elasticsearch +ISSUE_INDEXER_NAME = gitea_issues ; Issue indexer storage path, available when ISSUE_INDEXER_TYPE is bleve ISSUE_INDEXER_PATH = indexers/issues.bleve ; Issue indexer queue, currently support: channel, levelqueue or redis, default is levelqueue diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index cbf05b5349..5ce8f8a758 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -228,8 +228,10 @@ relation to port exhaustion. ## Indexer (`indexer`) -- `ISSUE_INDEXER_TYPE`: **bleve**: Issue indexer type, currently support: bleve or db, if it's db, below issue indexer item will be invalid. -- `ISSUE_INDEXER_PATH`: **indexers/issues.bleve**: Index file used for issue search. +- `ISSUE_INDEXER_TYPE`: **bleve**: Issue indexer type, currently supported: `bleve`, `db` or `elasticsearch`. +- `ISSUE_INDEXER_CONN_STR`: ****: Issue indexer connection string, available when ISSUE_INDEXER_TYPE is elasticsearch. i.e. http://elastic:changeme@localhost:9200 +- `ISSUE_INDEXER_NAME`: **gitea_issues**: Issue indexer name, available when ISSUE_INDEXER_TYPE is elasticsearch +- `ISSUE_INDEXER_PATH`: **indexers/issues.bleve**: Index file used for issue search; available when ISSUE_INDEXER_TYPE is bleve and elasticsearch. - The next 4 configuration values are deprecated and should be set in `queue.issue_indexer` however are kept for backwards compatibility: - `ISSUE_INDEXER_QUEUE_TYPE`: **levelqueue**: Issue indexer queue, currently supports:`channel`, `levelqueue`, `redis`. - `ISSUE_INDEXER_QUEUE_DIR`: **indexers/issues.queue**: When `ISSUE_INDEXER_QUEUE_TYPE` is `levelqueue`, this will be the queue will be saved path. diff --git a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md index 80861f457a..acdee1a56a 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md +++ b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md @@ -89,7 +89,9 @@ menu: ## Indexer (`indexer`) -- `ISSUE_INDEXER_TYPE`: **bleve**: 工单索引类型,当前支持 `bleve` 或 `db`,当为 `db` 时其它工单索引项可不用设置。 +- `ISSUE_INDEXER_TYPE`: **bleve**: 工单索引类型,当前支持 `bleve`, `db` 和 `elasticsearch`,当为 `db` 时其它工单索引项可不用设置。 +- `ISSUE_INDEXER_CONN_STR`: ****: 工单索引连接字符串,仅当 ISSUE_INDEXER_TYPE 为 `elasticsearch` 时有效。例如: http://elastic:changeme@localhost:9200 +- `ISSUE_INDEXER_NAME`: **gitea_issues**: 工单索引名称,仅当 ISSUE_INDEXER_TYPE 为 `elasticsearch` 时有效。 - `ISSUE_INDEXER_PATH`: **indexers/issues.bleve**: 工单索引文件存放路径,当索引类型为 `bleve` 时有效。 - `ISSUE_INDEXER_QUEUE_TYPE`: **levelqueue**: 工单索引队列类型,当前支持 `channel`, `levelqueue` 或 `redis`。 - `ISSUE_INDEXER_QUEUE_DIR`: **indexers/issues.queue**: 当 `ISSUE_INDEXER_QUEUE_TYPE` 为 `levelqueue` 时,保存索引队列的磁盘路径。 diff --git a/go.mod b/go.mod index 78b2b96090..cfe392b2c6 100644 --- a/go.mod +++ b/go.mod @@ -74,6 +74,7 @@ require ( github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 github.com/niklasfasching/go-org v0.1.9 github.com/oliamb/cutter v0.2.2 + github.com/olivere/elastic/v7 v7.0.9 github.com/pkg/errors v0.8.1 github.com/pquerna/otp v0.0.0-20160912161815-54653902c20e github.com/prometheus/client_golang v1.1.0 diff --git a/go.sum b/go.sum index fdbc7fd56e..fd95430ad5 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,7 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/aws/aws-sdk-go v1.25.25/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -154,6 +155,8 @@ github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 h1:E2s37DuLxFhQD github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= +github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -248,6 +251,7 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= @@ -322,6 +326,7 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U= github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= @@ -411,6 +416,8 @@ github.com/niklasfasching/go-org v0.1.9/go.mod h1:AsLD6X7djzRIz4/RFZu8vwRL0VGjUv github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k= github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU= +github.com/olivere/elastic/v7 v7.0.9 h1:+bTR1xJbfLYD8WnTBt9672mFlKxjfWRJpEQ1y8BMS3g= +github.com/olivere/elastic/v7 v7.0.9/go.mod h1:2TeRd0vhLRTK9zqm5xP0uLiVeZ5yUoL7kZ+8SZA9r9Y= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w= @@ -418,6 +425,7 @@ github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo= @@ -486,6 +494,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w= github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM= +github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8= @@ -573,6 +582,7 @@ go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qL go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= +go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= diff --git a/integrations/mysql.ini.tmpl b/integrations/mysql.ini.tmpl index 4dde212798..304434dbd6 100644 --- a/integrations/mysql.ini.tmpl +++ b/integrations/mysql.ini.tmpl @@ -10,6 +10,8 @@ PASSWD = {{TEST_MYSQL_PASSWORD}} SSL_MODE = disable [indexer] +ISSUE_INDEXER_TYPE = elasticsearch +ISSUE_INDEXER_CONN_STR = http://elastic:changeme@elasticsearch:9200 ISSUE_INDEXER_PATH = integrations/indexers-mysql/issues.bleve REPO_INDEXER_ENABLED = true REPO_INDEXER_PATH = integrations/indexers-mysql/repos.bleve diff --git a/modules/indexer/issues/bleve.go b/modules/indexer/issues/bleve.go index 787ff0dec5..655bc7dd17 100644 --- a/modules/indexer/issues/bleve.go +++ b/modules/indexer/issues/bleve.go @@ -170,7 +170,7 @@ func NewBleveIndexer(indexDir string) *BleveIndexer { } } -// Init will initial the indexer +// Init will initialize the indexer func (b *BleveIndexer) Init() (bool, error) { var err error b.indexer, err = openIndexer(b.indexDir, issueIndexerLatestVersion) diff --git a/modules/indexer/issues/elastic_search.go b/modules/indexer/issues/elastic_search.go new file mode 100644 index 0000000000..1f9c59965c --- /dev/null +++ b/modules/indexer/issues/elastic_search.go @@ -0,0 +1,230 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package issues + +import ( + "context" + "errors" + "fmt" + "strconv" + "time" + + "code.gitea.io/gitea/modules/log" + + "github.com/olivere/elastic/v7" +) + +var ( + _ Indexer = &ElasticSearchIndexer{} +) + +// ElasticSearchIndexer implements Indexer interface +type ElasticSearchIndexer struct { + client *elastic.Client + indexerName string +} + +type elasticLogger struct { + *log.Logger +} + +func (l elasticLogger) Printf(format string, args ...interface{}) { + _ = l.Logger.Log(2, l.Logger.GetLevel(), format, args...) +} + +// NewElasticSearchIndexer creates a new elasticsearch indexer +func NewElasticSearchIndexer(url, indexerName string) (*ElasticSearchIndexer, error) { + opts := []elastic.ClientOptionFunc{ + elastic.SetURL(url), + elastic.SetSniff(false), + elastic.SetHealthcheckInterval(10 * time.Second), + elastic.SetGzip(false), + } + + logger := elasticLogger{log.GetLogger(log.DEFAULT)} + + if logger.GetLevel() == log.TRACE || logger.GetLevel() == log.DEBUG { + opts = append(opts, elastic.SetTraceLog(logger)) + } else if logger.GetLevel() == log.ERROR || logger.GetLevel() == log.CRITICAL || logger.GetLevel() == log.FATAL { + opts = append(opts, elastic.SetErrorLog(logger)) + } else if logger.GetLevel() == log.INFO || logger.GetLevel() == log.WARN { + opts = append(opts, elastic.SetInfoLog(logger)) + } + + client, err := elastic.NewClient(opts...) + if err != nil { + return nil, err + } + + return &ElasticSearchIndexer{ + client: client, + indexerName: indexerName, + }, nil +} + +const ( + defaultMapping = `{ + "mappings": { + "properties": { + "id": { + "type": "integer", + "index": true + }, + "repo_id": { + "type": "integer", + "index": true + }, + "title": { + "type": "text", + "index": true + }, + "content": { + "type": "text", + "index": true + }, + "comments": { + "type" : "text", + "index": true + } + } + } + }` +) + +// Init will initialize the indexer +func (b *ElasticSearchIndexer) Init() (bool, error) { + ctx := context.Background() + exists, err := b.client.IndexExists(b.indexerName).Do(ctx) + if err != nil { + return false, err + } + + if !exists { + var mapping = defaultMapping + + createIndex, err := b.client.CreateIndex(b.indexerName).BodyString(mapping).Do(ctx) + if err != nil { + return false, err + } + if !createIndex.Acknowledged { + return false, errors.New("init failed") + } + + return false, nil + } + return true, nil +} + +// Index will save the index data +func (b *ElasticSearchIndexer) Index(issues []*IndexerData) error { + if len(issues) == 0 { + return nil + } else if len(issues) == 1 { + issue := issues[0] + _, err := b.client.Index(). + Index(b.indexerName). + Id(fmt.Sprintf("%d", issue.ID)). + BodyJson(map[string]interface{}{ + "id": issue.ID, + "repo_id": issue.RepoID, + "title": issue.Title, + "content": issue.Content, + "comments": issue.Comments, + }). + Do(context.Background()) + return err + } + + reqs := make([]elastic.BulkableRequest, 0) + for _, issue := range issues { + reqs = append(reqs, + elastic.NewBulkIndexRequest(). + Index(b.indexerName). + Id(fmt.Sprintf("%d", issue.ID)). + Doc(map[string]interface{}{ + "id": issue.ID, + "repo_id": issue.RepoID, + "title": issue.Title, + "content": issue.Content, + "comments": issue.Comments, + }), + ) + } + + _, err := b.client.Bulk(). + Index(b.indexerName). + Add(reqs...). + Do(context.Background()) + return err +} + +// Delete deletes indexes by ids +func (b *ElasticSearchIndexer) Delete(ids ...int64) error { + if len(ids) == 0 { + return nil + } else if len(ids) == 1 { + _, err := b.client.Delete(). + Index(b.indexerName). + Id(fmt.Sprintf("%d", ids[0])). + Do(context.Background()) + return err + } + + reqs := make([]elastic.BulkableRequest, 0) + for _, id := range ids { + reqs = append(reqs, + elastic.NewBulkDeleteRequest(). + Index(b.indexerName). + Id(fmt.Sprintf("%d", id)), + ) + } + + _, err := b.client.Bulk(). + Index(b.indexerName). + Add(reqs...). + Do(context.Background()) + return err +} + +// Search searches for issues by given conditions. +// Returns the matching issue IDs +func (b *ElasticSearchIndexer) Search(keyword string, repoIDs []int64, limit, start int) (*SearchResult, error) { + kwQuery := elastic.NewMultiMatchQuery(keyword, "title", "content", "comments") + query := elastic.NewBoolQuery() + query = query.Must(kwQuery) + if len(repoIDs) > 0 { + var repoStrs = make([]interface{}, 0, len(repoIDs)) + for _, repoID := range repoIDs { + repoStrs = append(repoStrs, repoID) + } + repoQuery := elastic.NewTermsQuery("repo_id", repoStrs...) + query = query.Must(repoQuery) + } + searchResult, err := b.client.Search(). + Index(b.indexerName). + Query(query). + Sort("id", true). + From(start).Size(limit). + Do(context.Background()) + if err != nil { + return nil, err + } + + hits := make([]Match, 0, limit) + for _, hit := range searchResult.Hits.Hits { + id, _ := strconv.ParseInt(hit.Id, 10, 64) + hits = append(hits, Match{ + ID: id, + }) + } + + return &SearchResult{ + Total: searchResult.TotalHits(), + Hits: hits, + }, nil +} + +// Close implements indexer +func (b *ElasticSearchIndexer) Close() {} diff --git a/modules/indexer/issues/indexer.go b/modules/indexer/issues/indexer.go index c942013e34..d2233ac6a8 100644 --- a/modules/indexer/issues/indexer.go +++ b/modules/indexer/issues/indexer.go @@ -21,13 +21,13 @@ import ( // IndexerData data stored in the issue indexer type IndexerData struct { - ID int64 - RepoID int64 - Title string - Content string - Comments []string - IsDelete bool - IDs []int64 + ID int64 `json:"id"` + RepoID int64 `json:"repo_id"` + Title string `json:"title"` + Content string `json:"content"` + Comments []string `json:"comments"` + IsDelete bool `json:"is_delete"` + IDs []int64 `json:"ids"` } // Match represents on search result @@ -100,7 +100,7 @@ func InitIssueIndexer(syncReindex bool) { // Create the Queue switch setting.Indexer.IssueType { - case "bleve": + case "bleve", "elasticsearch": handler := func(data ...queue.Data) { indexer := holder.get() if indexer == nil { @@ -160,6 +160,19 @@ func InitIssueIndexer(syncReindex bool) { log.Info("PID: %d Issue Indexer closed", os.Getpid()) }) log.Debug("Created Bleve Indexer") + case "elasticsearch": + graceful.GetManager().RunWithShutdownFns(func(_, atTerminate func(context.Context, func())) { + issueIndexer, err := NewElasticSearchIndexer(setting.Indexer.IssueConnStr, "gitea_issues") + if err != nil { + log.Fatal("Unable to initialize Elastic Search Issue Indexer: %v", err) + } + exist, err := issueIndexer.Init() + if err != nil { + log.Fatal("Unable to issueIndexer.Init: %v", err) + } + populate = !exist + holder.set(issueIndexer) + }) case "db": issueIndexer := &DBIndexer{} holder.set(issueIndexer) @@ -308,6 +321,7 @@ func DeleteRepoIssueIndexer(repo *models.Repository) { } // SearchIssuesByKeyword search issue ids by keywords and repo id +// WARNNING: You have to ensure user have permission to visit repoIDs' issues func SearchIssuesByKeyword(repoIDs []int64, keyword string) ([]int64, error) { var issueIDs []int64 indexer := holder.get() @@ -316,7 +330,7 @@ func SearchIssuesByKeyword(repoIDs []int64, keyword string) ([]int64, error) { log.Error("SearchIssuesByKeyword(): unable to get indexer!") return nil, fmt.Errorf("unable to get issue indexer") } - res, err := indexer.Search(keyword, repoIDs, 1000, 0) + res, err := indexer.Search(keyword, repoIDs, 50, 0) if err != nil { return nil, err } diff --git a/modules/setting/indexer.go b/modules/setting/indexer.go index 589e7bf864..859535281c 100644 --- a/modules/setting/indexer.go +++ b/modules/setting/indexer.go @@ -27,20 +27,25 @@ var ( Indexer = struct { IssueType string IssuePath string - RepoIndexerEnabled bool - RepoPath string - UpdateQueueLength int - MaxIndexerFileSize int64 + IssueConnStr string + IssueIndexerName string IssueQueueType string IssueQueueDir string IssueQueueConnStr string IssueQueueBatchNumber int StartupTimeout time.Duration - IncludePatterns []glob.Glob - ExcludePatterns []glob.Glob + + RepoIndexerEnabled bool + RepoPath string + UpdateQueueLength int + MaxIndexerFileSize int64 + IncludePatterns []glob.Glob + ExcludePatterns []glob.Glob }{ IssueType: "bleve", IssuePath: "indexers/issues.bleve", + IssueConnStr: "", + IssueIndexerName: "gitea_issues", IssueQueueType: LevelQueueType, IssueQueueDir: "indexers/issues.queue", IssueQueueConnStr: "", @@ -57,6 +62,14 @@ func newIndexerService() { if !filepath.IsAbs(Indexer.IssuePath) { Indexer.IssuePath = path.Join(AppWorkPath, Indexer.IssuePath) } + Indexer.IssueConnStr = sec.Key("ISSUE_INDEXER_CONN_STR").MustString(Indexer.IssueConnStr) + Indexer.IssueIndexerName = sec.Key("ISSUE_INDEXER_NAME").MustString(Indexer.IssueIndexerName) + + Indexer.IssueQueueType = sec.Key("ISSUE_INDEXER_QUEUE_TYPE").MustString(LevelQueueType) + Indexer.IssueQueueDir = sec.Key("ISSUE_INDEXER_QUEUE_DIR").MustString(path.Join(AppDataPath, "indexers/issues.queue")) + Indexer.IssueQueueConnStr = sec.Key("ISSUE_INDEXER_QUEUE_CONN_STR").MustString(path.Join(AppDataPath, "")) + Indexer.IssueQueueBatchNumber = sec.Key("ISSUE_INDEXER_QUEUE_BATCH_NUMBER").MustInt(20) + Indexer.RepoIndexerEnabled = sec.Key("REPO_INDEXER_ENABLED").MustBool(false) Indexer.RepoPath = sec.Key("REPO_INDEXER_PATH").MustString(path.Join(AppDataPath, "indexers/repos.bleve")) if !filepath.IsAbs(Indexer.RepoPath) { @@ -64,13 +77,8 @@ func newIndexerService() { } Indexer.IncludePatterns = IndexerGlobFromString(sec.Key("REPO_INDEXER_INCLUDE").MustString("")) Indexer.ExcludePatterns = IndexerGlobFromString(sec.Key("REPO_INDEXER_EXCLUDE").MustString("")) - Indexer.UpdateQueueLength = sec.Key("UPDATE_BUFFER_LEN").MustInt(20) Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(1024 * 1024) - Indexer.IssueQueueType = sec.Key("ISSUE_INDEXER_QUEUE_TYPE").MustString(LevelQueueType) - Indexer.IssueQueueDir = sec.Key("ISSUE_INDEXER_QUEUE_DIR").MustString(path.Join(AppDataPath, "indexers/issues.queue")) - Indexer.IssueQueueConnStr = sec.Key("ISSUE_INDEXER_QUEUE_CONN_STR").MustString(path.Join(AppDataPath, "")) - Indexer.IssueQueueBatchNumber = sec.Key("ISSUE_INDEXER_QUEUE_BATCH_NUMBER").MustInt(20) Indexer.StartupTimeout = sec.Key("STARTUP_TIMEOUT").MustDuration(30 * time.Second) } diff --git a/vendor/github.com/mailru/easyjson/.gitignore b/vendor/github.com/mailru/easyjson/.gitignore new file mode 100644 index 0000000000..26156fb4b4 --- /dev/null +++ b/vendor/github.com/mailru/easyjson/.gitignore @@ -0,0 +1,5 @@ +.root +*_easyjson.go +*.iml +.idea +*.swp diff --git a/vendor/github.com/mailru/easyjson/.travis.yml b/vendor/github.com/mailru/easyjson/.travis.yml new file mode 100644 index 0000000000..0ececa0718 --- /dev/null +++ b/vendor/github.com/mailru/easyjson/.travis.yml @@ -0,0 +1,12 @@ +language: go + +go: + - tip + - stable + +matrix: + allow_failures: + - go: tip + +install: + - go get golang.org/x/lint/golint diff --git a/vendor/github.com/mailru/easyjson/Makefile b/vendor/github.com/mailru/easyjson/Makefile new file mode 100644 index 0000000000..7b9ac94535 --- /dev/null +++ b/vendor/github.com/mailru/easyjson/Makefile @@ -0,0 +1,52 @@ +all: test + +clean: + rm -rf bin + rm -rf tests/*_easyjson.go + rm -rf benchmark/*_easyjson.go + +build: + go build -i -o ./bin/easyjson ./easyjson + +generate: build + bin/easyjson -stubs \ + ./tests/snake.go \ + ./tests/data.go \ + ./tests/omitempty.go \ + ./tests/nothing.go \ + ./tests/named_type.go \ + ./tests/custom_map_key_type.go \ + ./tests/embedded_type.go \ + ./tests/reference_to_pointer.go \ + + bin/easyjson -all ./tests/data.go + bin/easyjson -all ./tests/nothing.go + bin/easyjson -all ./tests/errors.go + bin/easyjson -snake_case ./tests/snake.go + bin/easyjson -omit_empty ./tests/omitempty.go + bin/easyjson -build_tags=use_easyjson ./benchmark/data.go + bin/easyjson ./tests/nested_easy.go + bin/easyjson ./tests/named_type.go + bin/easyjson ./tests/custom_map_key_type.go + bin/easyjson ./tests/embedded_type.go + bin/easyjson ./tests/reference_to_pointer.go + bin/easyjson ./tests/key_marshaler_map.go + bin/easyjson -disallow_unknown_fields ./tests/disallow_unknown.go + +test: generate + go test \ + ./tests \ + ./jlexer \ + ./gen \ + ./buffer + cd benchmark && go test -benchmem -tags use_easyjson -bench . + golint -set_exit_status ./tests/*_easyjson.go + +bench-other: generate + cd benchmark && make + +bench-python: + benchmark/ujson.sh + + +.PHONY: clean generate test build diff --git a/vendor/github.com/mailru/easyjson/README.md b/vendor/github.com/mailru/easyjson/README.md new file mode 100644 index 0000000000..3bdcf2d06c --- /dev/null +++ b/vendor/github.com/mailru/easyjson/README.md @@ -0,0 +1,336 @@ +# easyjson [![Build Status](https://travis-ci.org/mailru/easyjson.svg?branch=master)](https://travis-ci.org/mailru/easyjson) [![Go Report Card](https://goreportcard.com/badge/github.com/mailru/easyjson)](https://goreportcard.com/report/github.com/mailru/easyjson) + +Package easyjson provides a fast and easy way to marshal/unmarshal Go structs +to/from JSON without the use of reflection. In performance tests, easyjson +outperforms the standard `encoding/json` package by a factor of 4-5x, and other +JSON encoding packages by a factor of 2-3x. + +easyjson aims to keep generated Go code simple enough so that it can be easily +optimized or fixed. Another goal is to provide users with the ability to +customize the generated code by providing options not available with the +standard `encoding/json` package, such as generating "snake_case" names or +enabling `omitempty` behavior by default. + +## Usage +```sh +# install +go get -u github.com/mailru/easyjson/... + +# run +easyjson -all .go +``` + +The above will generate `_easyjson.go` containing the appropriate marshaler and +unmarshaler funcs for all structs contained in `.go`. + +Please note that easyjson requires a full Go build environment and the `GOPATH` +environment variable to be set. This is because easyjson code generation +invokes `go run` on a temporary file (an approach to code generation borrowed +from [ffjson](https://github.com/pquerna/ffjson)). + +## Options +```txt +Usage of easyjson: + -all + generate marshaler/unmarshalers for all structs in a file + -build_tags string + build tags to add to generated file + -leave_temps + do not delete temporary files + -no_std_marshalers + don't generate MarshalJSON/UnmarshalJSON funcs + -noformat + do not run 'gofmt -w' on output file + -omit_empty + omit empty fields by default + -output_filename string + specify the filename of the output + -pkg + process the whole package instead of just the given file + -snake_case + use snake_case names instead of CamelCase by default + -lower_camel_case + use lowerCamelCase instead of CamelCase by default + -stubs + only generate stubs for marshaler/unmarshaler funcs + -disallow_unknown_fields + return error if some unknown field in json appeared +``` + +Using `-all` will generate marshalers/unmarshalers for all Go structs in the +file. If `-all` is not provided, then only those structs whose preceding +comment starts with `easyjson:json` will have marshalers/unmarshalers +generated. For example: + +```go +//easyjson:json +type A struct {} +``` + +Additional option notes: + +* `-snake_case` tells easyjson to generate snake\_case field names by default + (unless overridden by a field tag). The CamelCase to snake\_case conversion + algorithm should work in most cases (ie, HTTPVersion will be converted to + "http_version"). + +* `-build_tags` will add the specified build tags to generated Go sources. + +## Generated Marshaler/Unmarshaler Funcs + +For Go struct types, easyjson generates the funcs `MarshalEasyJSON` / +`UnmarshalEasyJSON` for marshaling/unmarshaling JSON. In turn, these satisify +the `easyjson.Marshaler` and `easyjson.Unmarshaler` interfaces and when used in +conjunction with `easyjson.Marshal` / `easyjson.Unmarshal` avoid unnecessary +reflection / type assertions during marshaling/unmarshaling to/from JSON for Go +structs. + +easyjson also generates `MarshalJSON` and `UnmarshalJSON` funcs for Go struct +types compatible with the standard `json.Marshaler` and `json.Unmarshaler` +interfaces. Please be aware that using the standard `json.Marshal` / +`json.Unmarshal` for marshaling/unmarshaling will incur a significant +performance penalty when compared to using `easyjson.Marshal` / +`easyjson.Unmarshal`. + +Additionally, easyjson exposes utility funcs that use the `MarshalEasyJSON` and +`UnmarshalEasyJSON` for marshaling/unmarshaling to and from standard readers +and writers. For example, easyjson provides `easyjson.MarshalToHTTPResponseWriter` +which marshals to the standard `http.ResponseWriter`. Please see the [GoDoc +listing](https://godoc.org/github.com/mailru/easyjson) for the full listing of +utility funcs that are available. + +## Controlling easyjson Marshaling and Unmarshaling Behavior + +Go types can provide their own `MarshalEasyJSON` and `UnmarshalEasyJSON` funcs +that satisify the `easyjson.Marshaler` / `easyjson.Unmarshaler` interfaces. +These will be used by `easyjson.Marshal` and `easyjson.Unmarshal` when defined +for a Go type. + +Go types can also satisify the `easyjson.Optional` interface, which allows the +type to define its own `omitempty` logic. + +## Type Wrappers + +easyjson provides additional type wrappers defined in the `easyjson/opt` +package. These wrap the standard Go primitives and in turn satisify the +easyjson interfaces. + +The `easyjson/opt` type wrappers are useful when needing to distinguish between +a missing value and/or when needing to specifying a default value. Type +wrappers allow easyjson to avoid additional pointers and heap allocations and +can significantly increase performance when used properly. + +## Memory Pooling + +easyjson uses a buffer pool that allocates data in increasing chunks from 128 +to 32768 bytes. Chunks of 512 bytes and larger will be reused with the help of +`sync.Pool`. The maximum size of a chunk is bounded to reduce redundant memory +allocation and to allow larger reusable buffers. + +easyjson's custom allocation buffer pool is defined in the `easyjson/buffer` +package, and the default behavior pool behavior can be modified (if necessary) +through a call to `buffer.Init()` prior to any marshaling or unmarshaling. +Please see the [GoDoc listing](https://godoc.org/github.com/mailru/easyjson/buffer) +for more information. + +## Issues, Notes, and Limitations + +* easyjson is still early in its development. As such, there are likely to be + bugs and missing features when compared to `encoding/json`. In the case of a + missing feature or bug, please create a GitHub issue. Pull requests are + welcome! + +* Unlike `encoding/json`, object keys are case-sensitive. Case-insensitive + matching is not currently provided due to the significant performance hit + when doing case-insensitive key matching. In the future, case-insensitive + object key matching may be provided via an option to the generator. + +* easyjson makes use of `unsafe`, which simplifies the code and + provides significant performance benefits by allowing no-copy + conversion from `[]byte` to `string`. That said, `unsafe` is used + only when unmarshaling and parsing JSON, and any `unsafe` operations + / memory allocations done will be safely deallocated by + easyjson. Set the build tag `easyjson_nounsafe` to compile it + without `unsafe`. + +* easyjson is compatible with Google App Engine. The `appengine` build + tag (set by App Engine's environment) will automatically disable the + use of `unsafe`, which is not allowed in App Engine's Standard + Environment. Note that the use with App Engine is still experimental. + +* Floats are formatted using the default precision from Go's `strconv` package. + As such, easyjson will not correctly handle high precision floats when + marshaling/unmarshaling JSON. Note, however, that there are very few/limited + uses where this behavior is not sufficient for general use. That said, a + different package may be needed if precise marshaling/unmarshaling of high + precision floats to/from JSON is required. + +* While unmarshaling, the JSON parser does the minimal amount of work needed to + skip over unmatching parens, and as such full validation is not done for the + entire JSON value being unmarshaled/parsed. + +* Currently there is no true streaming support for encoding/decoding as + typically for many uses/protocols the final, marshaled length of the JSON + needs to be known prior to sending the data. Currently this is not possible + with easyjson's architecture. + +* easyjson parser and codegen based on reflection, so it wont works on `package main` + files, because they cant be imported by parser. + +## Benchmarks + +Most benchmarks were done using the example +[13kB example JSON](https://dev.twitter.com/rest/reference/get/search/tweets) +(9k after eliminating whitespace). This example is similar to real-world data, +is well-structured, and contains a healthy variety of different types, making +it ideal for JSON serialization benchmarks. + +Note: + +* For small request benchmarks, an 80 byte portion of the above example was + used. + +* For large request marshaling benchmarks, a struct containing 50 regular + samples was used, making a ~500kB output JSON. + +* Benchmarks are showing the results of easyjson's default behaviour, + which makes use of `unsafe`. + +Benchmarks are available in the repository and can be run by invoking `make`. + +### easyjson vs. encoding/json + +easyjson is roughly 5-6 times faster than the standard `encoding/json` for +unmarshaling, and 3-4 times faster for non-concurrent marshaling. Concurrent +marshaling is 6-7x faster if marshaling to a writer. + +### easyjson vs. ffjson + +easyjson uses the same approach for JSON marshaling as +[ffjson](https://github.com/pquerna/ffjson), but takes a significantly +different approach to lexing and parsing JSON during unmarshaling. This means +easyjson is roughly 2-3x faster for unmarshaling and 1.5-2x faster for +non-concurrent unmarshaling. + +As of this writing, `ffjson` seems to have issues when used concurrently: +specifically, large request pooling hurts `ffjson`'s performance and causes +scalability issues. These issues with `ffjson` can likely be fixed, but as of +writing remain outstanding/known issues with `ffjson`. + +easyjson and `ffjson` have similar performance for small requests, however +easyjson outperforms `ffjson` by roughly 2-5x times for large requests when +used with a writer. + +### easyjson vs. go/codec + +[go/codec](https://github.com/ugorji/go) provides +compile-time helpers for JSON generation. In this case, helpers do not work +like marshalers as they are encoding-independent. + +easyjson is generally 2x faster than `go/codec` for non-concurrent benchmarks +and about 3x faster for concurrent encoding (without marshaling to a writer). + +In an attempt to measure marshaling performance of `go/codec` (as opposed to +allocations/memcpy/writer interface invocations), a benchmark was done with +resetting length of a byte slice rather than resetting the whole slice to nil. +However, the optimization in this exact form may not be applicable in practice, +since the memory is not freed between marshaling operations. + +### easyjson vs 'ujson' python module + +[ujson](https://github.com/esnme/ultrajson) is using C code for parsing, so it +is interesting to see how plain golang compares to that. It is imporant to note +that the resulting object for python is slower to access, since the library +parses JSON object into dictionaries. + +easyjson is slightly faster for unmarshaling and 2-3x faster than `ujson` for +marshaling. + +### Benchmark Results + +`ffjson` results are from February 4th, 2016, using the latest `ffjson` and go1.6. +`go/codec` results are from March 4th, 2016, using the latest `go/codec` and go1.6. + +#### Unmarshaling + +| lib | json size | MB/s | allocs/op | B/op | +|:---------|:----------|-----:|----------:|------:| +| standard | regular | 22 | 218 | 10229 | +| standard | small | 9.7 | 14 | 720 | +| | | | | | +| easyjson | regular | 125 | 128 | 9794 | +| easyjson | small | 67 | 3 | 128 | +| | | | | | +| ffjson | regular | 66 | 141 | 9985 | +| ffjson | small | 17.6 | 10 | 488 | +| | | | | | +| codec | regular | 55 | 434 | 19299 | +| codec | small | 29 | 7 | 336 | +| | | | | | +| ujson | regular | 103 | N/A | N/A | + +#### Marshaling, one goroutine. + +| lib | json size | MB/s | allocs/op | B/op | +|:----------|:----------|-----:|----------:|------:| +| standard | regular | 75 | 9 | 23256 | +| standard | small | 32 | 3 | 328 | +| standard | large | 80 | 17 | 1.2M | +| | | | | | +| easyjson | regular | 213 | 9 | 10260 | +| easyjson* | regular | 263 | 8 | 742 | +| easyjson | small | 125 | 1 | 128 | +| easyjson | large | 212 | 33 | 490k | +| easyjson* | large | 262 | 25 | 2879 | +| | | | | | +| ffjson | regular | 122 | 153 | 21340 | +| ffjson** | regular | 146 | 152 | 4897 | +| ffjson | small | 36 | 5 | 384 | +| ffjson** | small | 64 | 4 | 128 | +| ffjson | large | 134 | 7317 | 818k | +| ffjson** | large | 125 | 7320 | 827k | +| | | | | | +| codec | regular | 80 | 17 | 33601 | +| codec*** | regular | 108 | 9 | 1153 | +| codec | small | 42 | 3 | 304 | +| codec*** | small | 56 | 1 | 48 | +| codec | large | 73 | 483 | 2.5M | +| codec*** | large | 103 | 451 | 66007 | +| | | | | | +| ujson | regular | 92 | N/A | N/A | + +\* marshaling to a writer, +\*\* using `ffjson.Pool()`, +\*\*\* reusing output slice instead of resetting it to nil + +#### Marshaling, concurrent. + +| lib | json size | MB/s | allocs/op | B/op | +|:----------|:----------|-----:|----------:|------:| +| standard | regular | 252 | 9 | 23257 | +| standard | small | 124 | 3 | 328 | +| standard | large | 289 | 17 | 1.2M | +| | | | | | +| easyjson | regular | 792 | 9 | 10597 | +| easyjson* | regular | 1748 | 8 | 779 | +| easyjson | small | 333 | 1 | 128 | +| easyjson | large | 718 | 36 | 548k | +| easyjson* | large | 2134 | 25 | 4957 | +| | | | | | +| ffjson | regular | 301 | 153 | 21629 | +| ffjson** | regular | 707 | 152 | 5148 | +| ffjson | small | 62 | 5 | 384 | +| ffjson** | small | 282 | 4 | 128 | +| ffjson | large | 438 | 7330 | 1.0M | +| ffjson** | large | 131 | 7319 | 820k | +| | | | | | +| codec | regular | 183 | 17 | 33603 | +| codec*** | regular | 671 | 9 | 1157 | +| codec | small | 147 | 3 | 304 | +| codec*** | small | 299 | 1 | 48 | +| codec | large | 190 | 483 | 2.5M | +| codec*** | large | 752 | 451 | 77574 | + +\* marshaling to a writer, +\*\* using `ffjson.Pool()`, +\*\*\* reusing output slice instead of resetting it to nil diff --git a/vendor/github.com/mailru/easyjson/go.mod b/vendor/github.com/mailru/easyjson/go.mod new file mode 100644 index 0000000000..7bc4a65844 --- /dev/null +++ b/vendor/github.com/mailru/easyjson/go.mod @@ -0,0 +1,3 @@ +module github.com/mailru/easyjson + +go 1.12 diff --git a/vendor/github.com/mailru/easyjson/helpers.go b/vendor/github.com/mailru/easyjson/helpers.go new file mode 100644 index 0000000000..b86b87d228 --- /dev/null +++ b/vendor/github.com/mailru/easyjson/helpers.go @@ -0,0 +1,78 @@ +// Package easyjson contains marshaler/unmarshaler interfaces and helper functions. +package easyjson + +import ( + "io" + "io/ioutil" + "net/http" + "strconv" + + "github.com/mailru/easyjson/jlexer" + "github.com/mailru/easyjson/jwriter" +) + +// Marshaler is an easyjson-compatible marshaler interface. +type Marshaler interface { + MarshalEasyJSON(w *jwriter.Writer) +} + +// Marshaler is an easyjson-compatible unmarshaler interface. +type Unmarshaler interface { + UnmarshalEasyJSON(w *jlexer.Lexer) +} + +// Optional defines an undefined-test method for a type to integrate with 'omitempty' logic. +type Optional interface { + IsDefined() bool +} + +// Marshal returns data as a single byte slice. Method is suboptimal as the data is likely to be copied +// from a chain of smaller chunks. +func Marshal(v Marshaler) ([]byte, error) { + w := jwriter.Writer{} + v.MarshalEasyJSON(&w) + return w.BuildBytes() +} + +// MarshalToWriter marshals the data to an io.Writer. +func MarshalToWriter(v Marshaler, w io.Writer) (written int, err error) { + jw := jwriter.Writer{} + v.MarshalEasyJSON(&jw) + return jw.DumpTo(w) +} + +// MarshalToHTTPResponseWriter sets Content-Length and Content-Type headers for the +// http.ResponseWriter, and send the data to the writer. started will be equal to +// false if an error occurred before any http.ResponseWriter methods were actually +// invoked (in this case a 500 reply is possible). +func MarshalToHTTPResponseWriter(v Marshaler, w http.ResponseWriter) (started bool, written int, err error) { + jw := jwriter.Writer{} + v.MarshalEasyJSON(&jw) + if jw.Error != nil { + return false, 0, jw.Error + } + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Length", strconv.Itoa(jw.Size())) + + started = true + written, err = jw.DumpTo(w) + return +} + +// Unmarshal decodes the JSON in data into the object. +func Unmarshal(data []byte, v Unmarshaler) error { + l := jlexer.Lexer{Data: data} + v.UnmarshalEasyJSON(&l) + return l.Error() +} + +// UnmarshalFromReader reads all the data in the reader and decodes as JSON into the object. +func UnmarshalFromReader(r io.Reader, v Unmarshaler) error { + data, err := ioutil.ReadAll(r) + if err != nil { + return err + } + l := jlexer.Lexer{Data: data} + v.UnmarshalEasyJSON(&l) + return l.Error() +} diff --git a/vendor/github.com/mailru/easyjson/raw.go b/vendor/github.com/mailru/easyjson/raw.go new file mode 100644 index 0000000000..81bd002e19 --- /dev/null +++ b/vendor/github.com/mailru/easyjson/raw.go @@ -0,0 +1,45 @@ +package easyjson + +import ( + "github.com/mailru/easyjson/jlexer" + "github.com/mailru/easyjson/jwriter" +) + +// RawMessage is a raw piece of JSON (number, string, bool, object, array or +// null) that is extracted without parsing and output as is during marshaling. +type RawMessage []byte + +// MarshalEasyJSON does JSON marshaling using easyjson interface. +func (v *RawMessage) MarshalEasyJSON(w *jwriter.Writer) { + if len(*v) == 0 { + w.RawString("null") + } else { + w.Raw(*v, nil) + } +} + +// UnmarshalEasyJSON does JSON unmarshaling using easyjson interface. +func (v *RawMessage) UnmarshalEasyJSON(l *jlexer.Lexer) { + *v = RawMessage(l.Raw()) +} + +// UnmarshalJSON implements encoding/json.Unmarshaler interface. +func (v *RawMessage) UnmarshalJSON(data []byte) error { + *v = data + return nil +} + +var nullBytes = []byte("null") + +// MarshalJSON implements encoding/json.Marshaler interface. +func (v RawMessage) MarshalJSON() ([]byte, error) { + if len(v) == 0 { + return nullBytes, nil + } + return v, nil +} + +// IsDefined is required for integration with omitempty easyjson logic. +func (v *RawMessage) IsDefined() bool { + return len(*v) > 0 +} diff --git a/vendor/github.com/olivere/elastic/v7/.fossa.yml b/vendor/github.com/olivere/elastic/v7/.fossa.yml new file mode 100644 index 0000000000..61669d85f8 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/.fossa.yml @@ -0,0 +1,38 @@ +# Generated by FOSSA CLI (https://github.com/fossas/fossa-cli) +# Visit https://fossa.io to learn more + +version: 1 +cli: + server: https://app.fossa.io + fetcher: git + project: git@github.com:olivere/elastic.git +analyze: + modules: + - name: github.com/olivere/elastic + path: . + target: github.com/olivere/elastic + type: go + - name: github.com/olivere/elastic/config + path: ./config + target: github.com/olivere/elastic/config + type: go + - name: github.com/olivere/elastic/uritemplates + path: ./uritemplates + target: github.com/olivere/elastic/uritemplates + type: go + - name: github.com/olivere/elastic/trace/opencensus + path: ./trace/opencensus + target: github.com/olivere/elastic/trace/opencensus + type: go + - name: github.com/olivere/elastic/trace/opentracing + path: ./trace/opentracing + target: github.com/olivere/elastic/trace/opentracing + type: go + - name: github.com/olivere/elastic/aws + path: ./aws + target: github.com/olivere/elastic/aws + type: go + - name: github.com/olivere/elastic/aws/v4 + path: ./aws/v4 + target: github.com/olivere/elastic/aws/v4 + type: go diff --git a/vendor/github.com/olivere/elastic/v7/.gitignore b/vendor/github.com/olivere/elastic/v7/.gitignore new file mode 100644 index 0000000000..5dc4d56810 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/.gitignore @@ -0,0 +1,35 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe + +/.vscode/ +/.idea/ +/debug.test +/generator +/cluster-test/cluster-test +/cluster-test/*.log +/cluster-test/es-chaos-monkey +/go.sum +/spec +/tmp +/CHANGELOG-3.0.html + diff --git a/vendor/github.com/olivere/elastic/v7/.travis.yml b/vendor/github.com/olivere/elastic/v7/.travis.yml new file mode 100644 index 0000000000..374a7b1f93 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/.travis.yml @@ -0,0 +1,32 @@ +sudo: required +language: go +go: +- "1.12.x" +- "1.13.x" +- tip +matrix: + allow_failures: + - go: tip +env: +- GO111MODULE=on +- GO111MODULE=off +addons: + apt: + update: true + packages: + - docker-ce +services: +- docker +before_install: +- if [[ "$TRAVIS_OS_NAME" == "linux" && ! $(which nc) ]] ; then sudo apt-get install -y netcat ; fi +- sudo sysctl -w vm.max_map_count=262144 +- docker-compose pull +- docker-compose up -d +- go get -u github.com/google/go-cmp/cmp +- go get -u github.com/fortytw2/leaktest +- go get . ./aws/... ./config/... ./trace/... ./uritemplates/... +- while ! nc -z localhost 9200; do sleep 1; done +- while ! nc -z localhost 9210; do sleep 1; done +install: true # ignore the go get -t -v ./... +script: +- go test -race -deprecations -strict-decoder -v . ./aws/... ./config/... ./trace/... ./uritemplates/... diff --git a/vendor/github.com/olivere/elastic/v7/CHANGELOG-3.0.md b/vendor/github.com/olivere/elastic/v7/CHANGELOG-3.0.md new file mode 100644 index 0000000000..07f3e66bf6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CHANGELOG-3.0.md @@ -0,0 +1,363 @@ +# Elastic 3.0 + +Elasticsearch 2.0 comes with some [breaking changes](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/breaking-changes-2.0.html). You will probably need to upgrade your application and/or rewrite part of it due to those changes. + +We use that window of opportunity to also update Elastic (the Go client) from version 2.0 to 3.0. This will introduce both changes due to the Elasticsearch 2.0 update as well as changes that make Elastic cleaner by removing some old cruft. + +So, to summarize: + +1. Elastic 2.0 is compatible with Elasticsearch 1.7+ and is still actively maintained. +2. Elastic 3.0 is compatible with Elasticsearch 2.0+ and will soon become the new master branch. + +The rest of the document is a list of all changes in Elastic 3.0. + +## Pointer types + +All types have changed to be pointer types, not value types. This not only is cleaner but also simplifies the API as illustrated by the following example: + +Example for Elastic 2.0 (old): + +```go +q := elastic.NewMatchAllQuery() +res, err := elastic.Search("one").Query(&q).Do() // notice the & here +``` + +Example for Elastic 3.0 (new): + +```go +q := elastic.NewMatchAllQuery() +res, err := elastic.Search("one").Query(q).Do() // no more & +// ... which can be simplified as: +res, err := elastic.Search("one").Query(elastic.NewMatchAllQuery()).Do() +``` + +It also helps to prevent [subtle issues](https://github.com/olivere/elastic/issues/115#issuecomment-130753046). + +## Query/filter merge + +One of the biggest changes in Elasticsearch 2.0 is the [merge of queries and filters](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_query_dsl_changes.html#_queries_and_filters_merged). In Elasticsearch 1.x, you had a whole range of queries and filters that were basically identical (e.g. `term_query` and `term_filter`). + +The practical aspect of the merge is that you can now basically use queries where once you had to use filters instead. For Elastic 3.0 this means: We could remove a whole bunch of files. Yay! + +Notice that some methods still come by "filter", e.g. `PostFilter`. However, they accept a `Query` now when they used to accept a `Filter` before. + +Example for Elastic 2.0 (old): + +```go +q := elastic.NewMatchAllQuery() +f := elastic.NewTermFilter("tag", "important") +res, err := elastic.Search().Index("one").Query(&q).PostFilter(f) +``` + +Example for Elastic 3.0 (new): + +```go +q := elastic.NewMatchAllQuery() +f := elastic.NewTermQuery("tag", "important") // it's a query now! +res, err := elastic.Search().Index("one").Query(q).PostFilter(f) +``` + +## Facets are removed + +[Facets have been removed](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_removed_features.html#_facets_have_been_removed) in Elasticsearch 2.0. You need to use aggregations now. + +## Errors + +Elasticsearch 2.0 returns more information about an error in the HTTP response body. Elastic 3.0 now reads this information and makes it accessible by the consumer. + +Errors and all its details are now returned in [`Error`](https://github.com/olivere/elastic/blob/release-branch.v3/errors.go#L59). + +### HTTP Status 404 (Not Found) + +When Elasticsearch does not find an entity or an index, it generally returns HTTP status code 404. In Elastic 2.0 this was a valid result and didn't raise an error from the `Do` functions. This has now changed in Elastic 3.0. + +Starting with Elastic 3.0, there are only two types of responses considered successful. First, responses with HTTP status codes [200..299]. Second, HEAD requests which return HTTP status 404. The latter is used by Elasticsearch to e.g. check for existence of indices or documents. All other responses will return an error. + +To check for HTTP Status 404 (with non-HEAD requests), e.g. when trying to get or delete a missing document, you can use the [`IsNotFound`](https://github.com/olivere/elastic/blob/release-branch.v3/errors.go#L84) helper (see below). + +The following example illustrates how to check for a missing document in Elastic 2.0 and what has changed in 3.0. + +Example for Elastic 2.0 (old): + +```go +res, err = client.Get().Index("one").Type("tweet").Id("no-such-id").Do() +if err != nil { + // Something else went wrong (but 404 is NOT an error in Elastic 2.0) +} +if !res.Found { + // Document has not been found +} +``` + +Example for Elastic 3.0 (new): + +```go +res, err = client.Get().Index("one").Type("tweet").Id("no-such-id").Do() +if err != nil { + if elastic.IsNotFound(err) { + // Document has not been found + } else { + // Something else went wrong + } +} +``` + +### HTTP Status 408 (Timeouts) + +Elasticsearch now responds with HTTP status code 408 (Timeout) when a request fails due to a timeout. E.g. if you specify a timeout with the Cluster Health API, the HTTP response status will be 408 if the timeout is raised. See [here](https://github.com/elastic/elasticsearch/commit/fe3179d9cccb569784434b2135ca9ae13d5158d3) for the specific commit to the Cluster Health API. + +To check for HTTP Status 408, we introduced the [`IsTimeout`](https://github.com/olivere/elastic/blob/release-branch.v3/errors.go#L101) helper. + +Example for Elastic 2.0 (old): + +```go +health, err := client.ClusterHealth().WaitForStatus("yellow").Timeout("1s").Do() +if err != nil { + // ... +} +if health.TimedOut { + // We have a timeout +} +``` + +Example for Elastic 3.0 (new): + +```go +health, err := client.ClusterHealth().WaitForStatus("yellow").Timeout("1s").Do() +if elastic.IsTimeout(err) { + // We have a timeout +} +``` + +### Bulk Errors + +The error response of a bulk operation used to be a simple string in Elasticsearch 1.x. +In Elasticsearch 2.0, it returns a structured JSON object with a lot more details about the error. +These errors are now captured in an object of type [`ErrorDetails`](https://github.com/olivere/elastic/blob/release-branch.v3/errors.go#L59) which is used in [`BulkResponseItem`](https://github.com/olivere/elastic/blob/release-branch.v3/bulk.go#L206). + +### Removed specific Elastic errors + +The specific error types `ErrMissingIndex`, `ErrMissingType`, and `ErrMissingId` have been removed. They were only used by `DeleteService` and are replaced by a generic error message. + +## Numeric types + +Elastic 3.0 has settled to use `float64` everywhere. It used to be a mix of `float32` and `float64` in Elastic 2.0. E.g. all boostable queries in Elastic 3.0 now have a boost type of `float64` where it used to be `float32`. + +## Pluralization + +Some services accept zero, one or more indices or types to operate on. +E.g. in the `SearchService` accepts a list of zero, one, or more indices to +search and therefor had a func called `Index(index string)` and a func +called `Indices(indices ...string)`. + +Elastic 3.0 now only uses the singular form that, when applicable, accepts a +variadic type. E.g. in the case of the `SearchService`, you now only have +one func with the following signature: `Index(indices ...string)`. + +Notice this is only limited to `Index(...)` and `Type(...)`. There are other +services with variadic functions. These have not been changed. + +## Multiple calls to variadic functions + +Some services with variadic functions have cleared the underlying slice when +called while other services just add to the existing slice. This has now been +normalized to always add to the underlying slice. + +Example for Elastic 2.0 (old): + +```go +// Would only cleared scroll id "two" +// because ScrollId cleared the values when called multiple times +client.ClearScroll().ScrollId("one").ScrollId("two").Do() +``` + +Example for Elastic 3.0 (new): + +```go +// Now (correctly) clears both scroll id "one" and "two" +// because ScrollId no longer clears the values when called multiple times +client.ClearScroll().ScrollId("one").ScrollId("two").Do() +``` + +## Ping service requires URL + +The `Ping` service raised some issues because it is different from all +other services. If not explicitly given a URL, it always pings `127.0.0.1:9200`. + +Users expected to ping the cluster, but that is not possible as the cluster +can be a set of many nodes: So which node do we ping then? + +To make it more clear, the `Ping` function on the client now requires users +to explicitly set the URL of the node to ping. + +## Meta fields + +Many of the meta fields e.g. `_parent` or `_routing` are now +[part of the top-level of a document](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_mapping_changes.html#migration-meta-fields) +and are no longer returned as parts of the `fields` object. We had to change +larger parts of e.g. the `Reindexer` to get it to work seamlessly with Elasticsearch 2.0. + +Notice that all stored meta-fields are now [returned by default](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_crud_and_routing_changes.html#_all_stored_meta_fields_returned_by_default). + +## HasParentQuery / HasChildQuery + +`NewHasParentQuery` and `NewHasChildQuery` must now include both parent/child type and query. It is now in line with the Java API. + +Example for Elastic 2.0 (old): + +```go +allQ := elastic.NewMatchAllQuery() +q := elastic.NewHasChildFilter("tweet").Query(&allQ) +``` + +Example for Elastic 3.0 (new): + +```go +q := elastic.NewHasChildQuery("tweet", elastic.NewMatchAllQuery()) +``` + +## SetBasicAuth client option + +You can now tell Elastic to pass HTTP Basic Auth credentials with each request. In previous versions of Elastic you had to set up your own `http.Transport` to do this. This should make it more convenient to use Elastic in combination with [Shield](https://www.elastic.co/products/shield) in its [basic setup](https://www.elastic.co/guide/en/shield/current/enable-basic-auth.html). + +Example: + +```go +client, err := elastic.NewClient(elastic.SetBasicAuth("user", "secret")) +if err != nil { + log.Fatal(err) +} +``` + +## Delete-by-Query API + +The Delete-by-Query API is [a plugin now](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_removed_features.html#_delete_by_query_is_now_a_plugin). It is no longer core part of Elasticsearch. You can [install it as a plugin as described here](https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugins-delete-by-query.html). + +Elastic 3.0 still contains the `DeleteByQueryService`, but you need to install the plugin first. If you don't install it and use `DeleteByQueryService` you will most probably get a 404. + +An older version of this document stated the following: + +> Elastic 3.0 still contains the `DeleteByQueryService` but it will fail with `ErrPluginNotFound` when the plugin is not installed. +> +> Example for Elastic 3.0 (new): +> +> ```go +> _, err := client.DeleteByQuery().Query(elastic.NewTermQuery("client", "1")).Do() +> if err == elastic.ErrPluginNotFound { +> // Delete By Query API is not available +> } +> ``` + +I have decided that this is not a good way to handle the case of a missing plugin. The main reason is that with this logic, you'd always have to check if the plugin is missing in case of an error. This is not only slow, but it also puts logic into a service where it should really be just opaque and return the response of Elasticsearch. + +If you rely on certain plugins to be installed, you should check on startup. That's where the following two helpers come into play. + +## HasPlugin and SetRequiredPlugins + +Some of the core functionality of Elasticsearch has now been moved into plugins. E.g. the Delete-by-Query API is [a plugin now](https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugins-delete-by-query.html). + +You need to make sure to add these plugins to your Elasticsearch installation to still be able to use the `DeleteByQueryService`. You can test this now with the `HasPlugin(name string)` helper in the client. + +Example for Elastic 3.0 (new): + +```go +err, found := client.HasPlugin("delete-by-query") +if err == nil && found { + // ... Delete By Query API is available +} +``` + +To simplify this process, there is now a `SetRequiredPlugins` helper that can be passed as an option func when creating a new client. If the plugin is not installed, the client wouldn't be created in the first place. + +```go +// Will raise an error if the "delete-by-query" plugin is NOT installed +client, err := elastic.NewClient(elastic.SetRequiredPlugins("delete-by-query")) +if err != nil { + log.Fatal(err) +} +``` + +Notice that there also is a way to define [mandatory plugins](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-plugins.html#_mandatory_plugins) in the Elasticsearch configuration file. + +## Common Query has been renamed to Common Terms Query + +The `CommonQuery` has been renamed to `CommonTermsQuery` to be in line with the [Java API](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_java_api_changes.html#_query_filter_refactoring). + +## Remove `MoreLikeThis` and `MoreLikeThisField` + +The More Like This API and the More Like This Field query [have been removed](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_query_dsl_changes.html#_more_like_this) and replaced with the `MoreLikeThisQuery`. + +## Remove Filtered Query + +With the merge of queries and filters, the [filtered query became deprecated](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_query_dsl_changes.html#_literal_filtered_literal_query_and_literal_query_literal_filter_deprecated). While it is only deprecated and therefore still available in Elasticsearch 2.0, we have decided to remove it from Elastic 3.0. Why? Because we think that when you're already forced to rewrite many of your application code, it might be a good chance to get rid of things that are deprecated as well. So you might simply change your filtered query with a boolean query as [described here](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_query_dsl_changes.html#_literal_filtered_literal_query_and_literal_query_literal_filter_deprecated). + +## Remove FuzzyLikeThis and FuzzyLikeThisField + +Both have been removed from Elasticsearch 2.0 as well. + +## Remove LimitFilter + +The `limit` filter is [deprecated in Elasticsearch 2.0](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_query_dsl_changes.html#_literal_limit_literal_filter_deprecated) and becomes a no-op. Now is a good chance to remove it from your application as well. Use the `terminate_after` parameter in your search [as described here](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/search-request-body.html) to achieve similar effects. + +## Remove `_cache` and `_cache_key` from filters + +Both have been [removed from Elasticsearch 2.0 as well](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_query_dsl_changes.html#_filter_auto_caching). + +## Partial fields are gone + +Partial fields are [removed in Elasticsearch 2.0](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/_search_changes.html#_partial_fields) in favor of [source filtering](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/search-request-source-filtering.html). + +## Scripting + +A [`Script`](https://github.com/olivere/elastic/blob/release-branch.v3/script.go) type has been added to Elastic 3.0. In Elastic 2.0, there were various places (e.g. aggregations) where you could just add the script as a string, specify the scripting language, add parameters etc. With Elastic 3.0, you should now always use the `Script` type. + +Example for Elastic 2.0 (old): + +```go +update, err := client.Update().Index("twitter").Type("tweet").Id("1"). + Script("ctx._source.retweets += num"). + ScriptParams(map[string]interface{}{"num": 1}). + Upsert(map[string]interface{}{"retweets": 0}). + Do() +``` + +Example for Elastic 3.0 (new): + +```go +update, err := client.Update().Index("twitter").Type("tweet").Id("1"). + Script(elastic.NewScript("ctx._source.retweets += num").Param("num", 1)). + Upsert(map[string]interface{}{"retweets": 0}). + Do() +``` + +## Cluster State + +The combination of `Metric(string)` and `Metrics(...string)` has been replaced by a single func with the signature `Metric(...string)`. + +## Unexported structs in response + +Services generally return a typed response from a `Do` func. Those structs are exported so that they can be passed around in your own application. In Elastic 3.0 however, we changed that (most) sub-structs are now unexported, meaning: You can only pass around the whole response, not sub-structures of it. This makes it easier for restructuring responses according to the Elasticsearch API. See [`ClusterStateResponse`](https://github.com/olivere/elastic/blob/release-branch.v3/cluster_state.go#L182) as an example. + +## Add offset to Histogram aggregation + +Histogram aggregations now have an [offset](https://github.com/elastic/elasticsearch/pull/9505) option. + +## Services + +### REST API specification + +As you might know, Elasticsearch comes with a REST API specification. The specification describes the endpoints in a JSON structure. + +Most services in Elastic predated the REST API specification. We are in the process of bringing all these services in line with the specification. Services can be generated by `go generate` (not 100% automatic though). This is an ongoing process. + +This probably doesn't mean a lot to you. However, you can now be more confident that Elastic supports all features that the REST API specification describes. + +At the same time, the file names of the services are renamed to match the REST API specification naming. + +### REST API Test Suite + +The REST API specification of Elasticsearch comes along with a test suite that official clients typically use to test for conformance. Up until now, Elastic didn't run this test suite. However, we are in the process of setting up infrastructure and tests to match this suite as well. + +This process in not completed though. + + diff --git a/vendor/github.com/olivere/elastic/v7/CHANGELOG-5.0.md b/vendor/github.com/olivere/elastic/v7/CHANGELOG-5.0.md new file mode 100644 index 0000000000..161c6a1ced --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CHANGELOG-5.0.md @@ -0,0 +1,195 @@ +# Changes in Elastic 5.0 + +## Enforce context.Context in PerformRequest and Do + +We enforce the usage of `context.Context` everywhere you execute a request. +You need to change all your `Do()` calls to pass a context: `Do(ctx)`. +This enables automatic request cancelation and many other patterns. + +If you don't need this, simply pass `context.TODO()` or `context.Background()`. + +## Warmers removed + +Warmers are no longer necessary and have been [removed in ES 5.0](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_index_apis.html#_warmers). + +## Optimize removed + +Optimize was deprecated in ES 2.0 and has been [removed in ES 5.0](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_rest_api_changes.html#_literal__optimize_literal_endpoint_removed). +Use [Force Merge](https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-forcemerge.html) instead. + +## Missing Query removed + +The `missing` query has been [removed](https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-exists-query.html#_literal_missing_literal_query). +Use `exists` query with `must_not` in `bool` query instead. + +## And Query removed + +The `and` query has been [removed](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_search_changes.html#_deprecated_queries_removed). +Use `must` clauses in a `bool` query instead. + +## Not Query removed + +TODO Is it removed? + +## Or Query removed + +The `or` query has been [removed](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_search_changes.html#_deprecated_queries_removed). +Use `should` clauses in a `bool` query instead. + +## Filtered Query removed + +The `filtered` query has been [removed](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_search_changes.html#_deprecated_queries_removed). +Use `bool` query instead, which supports `filter` clauses too. + +## Limit Query removed + +The `limit` query has been [removed](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_search_changes.html#_deprecated_queries_removed). +Use the `terminate_after` parameter instead. + +# Template Query removed + +The `template` query has been [deprecated](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/query-dsl-template-query.html). You should use +Search Templates instead. + +We remove it from Elastic 5.0 as the 5.0 update is already a good opportunity +to get rid of old stuff. + +## `_timestamp` and `_ttl` removed + +Both of these fields were deprecated and are now [removed](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_mapping_changes.html#_literal__timestamp_literal_and_literal__ttl_literal). + +## Search template Put/Delete API returns `acknowledged` only + +The response type for Put/Delete search templates has changed. +It only returns a single `acknowledged` flag now. + +## Fields has been renamed to Stored Fields + +The `fields` parameter has been renamed to `stored_fields`. +See [here](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/breaking_50_search_changes.html#_literal_fields_literal_parameter). + +## Fielddatafields has been renamed to Docvaluefields + +The `fielddata_fields` parameter [has been renamed](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/breaking_50_search_changes.html#_literal_fielddata_fields_literal_parameter) +to `docvalue_fields`. + +## Type exists endpoint changed + +The endpoint for checking whether a type exists has been changed from +`HEAD {index}/{type}` to `HEAD {index}/_mapping/{type}`. +See [here](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_rest_api_changes.html#_literal_head_index_type_literal_replaced_with_literal_head_index__mapping_type_literal). + +## Refresh parameter changed + +The `?refresh` parameter previously could be a boolean value. It indicated +whether changes made by a request (e.g. by the Bulk API) should be immediately +visible in search, or not. Using `refresh=true` had the positive effect of +immediately seeing the changes when searching; the negative effect is that +it is a rather big performance hit. + +With 5.0, you now have the choice between these 3 values. + +* `"true"` - Refresh immediately +* `"false"` - Do not refresh (the default value) +* `"wait_for"` - Wait until ES made the document visible in search + +See [?refresh](https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html) in the documentation. + +Notice that `true` and `false` (the boolean values) are no longer available +now in Elastic. You must use a string instead, with one of the above values. + +## ReindexerService removed + +The `ReindexerService` was a custom solution that was started in the ES 1.x era +to automate reindexing data, from one index to another or even between clusters. + +ES 2.3 introduced its own [Reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-reindex.html) +so we're going to remove our custom solution and ask you to use the native reindexer. + +The `ReindexService` is available via `client.Reindex()` (which used to point +to the custom reindexer). + +## Delete By Query back in core + +The [Delete By Query API](https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-delete-by-query.html) +was moved into a plugin in 2.0. Now its back in core with a complete rewrite based on the Bulk API. + +It has it's own endpoint at `/_delete_by_query`. + +Delete By Query, Reindex, and Update By Query are very similar under the hood. + +## Reindex, Delete By Query, and Update By Query response changed + +The response from the above APIs changed a bit. E.g. the `retries` value +used to be an `int64` and returns separate values for `bulk` and `search` now: + +``` +// Old +{ + ... + "retries": 123, + ... +} +``` + +``` +// New +{ + ... + "retries": { + "bulk": 123, + "search": 0 + }, + ... +} +``` + +## ScanService removed + +The `ScanService` is removed. Use the (new) `ScrollService` instead. + +## New ScrollService + +There was confusion around `ScanService` and `ScrollService` doing basically +the same. One was returning slices and didn't support all query details, the +other returned one document after another and wasn't safe for concurrent use. +So we merged the two and merged it into a new `ScrollService` that +removes all the problems with the older services. + +In other words: +If you used `ScanService`, switch to `ScrollService`. +If you used the old `ScrollService`, you might need to fix some things but +overall it should just work. + +Changes: +- We replaced `elastic.EOS` with `io.EOF` to indicate the "end of scroll". + +TODO Not implemented yet + +## Suggesters + +They have been [completely rewritten in ES 5.0](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_suggester.html). + +Some changes: +- Suggesters no longer have an [output](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking_50_suggester.html#_simpler_completion_indexing). + +TODO Fix all structural changes in suggesters + +## Percolator + +Percolator has [changed considerably](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/breaking_50_percolator.html). + +Elastic 5.0 adds the new +[Percolator Query](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/query-dsl-percolate-query.html) +which can be used in combination with the new +[Percolator type](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/percolator.html). + +The Percolate service is removed from Elastic 5.0. + +## Remove Consistency, add WaitForActiveShards + +The `consistency` parameter has been removed in a lot of places, e.g. the Bulk, +Index, Delete, Delete-by-Query, Reindex, Update, and Update-by-Query API. + +It has been replaced by a somewhat similar `wait_for_active_shards` parameter. +See https://github.com/elastic/elasticsearch/pull/19454. diff --git a/vendor/github.com/olivere/elastic/v7/CHANGELOG-6.0.md b/vendor/github.com/olivere/elastic/v7/CHANGELOG-6.0.md new file mode 100644 index 0000000000..255bda4ee1 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CHANGELOG-6.0.md @@ -0,0 +1,18 @@ +# Changes from 5.0 to 6.0 + +See [breaking changes](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-6.0.html). + +## _all removed + +6.0 has removed support for the `_all` field. + +## Boolean values coerced + +Only use `true` or `false` for boolean values, not `0` or `1` or `on` or `off`. + +## Single Type Indices + +Notice that 6.0 and future versions will default to single type indices, i.e. you may not use multiple types when e.g. adding an index with a mapping. + +See [here for details](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/removal-of-types.html#_what_are_mapping_types). + diff --git a/vendor/github.com/olivere/elastic/v7/CHANGELOG-7.0.md b/vendor/github.com/olivere/elastic/v7/CHANGELOG-7.0.md new file mode 100644 index 0000000000..1edd9dda94 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CHANGELOG-7.0.md @@ -0,0 +1,55 @@ +# Changes from 6.0 to 7.0 + +See [breaking changes](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/breaking-changes-7.0.html). + +## SearchHit.Source changed from `*json.RawMessage` to `json.RawMessage` + +The `SearchHit` structure changed from + +``` +// SearchHit is a single hit. +type SearchHit struct { + ... + Source *json.RawMessage `json:"_source,omitempty"` // stored document source + ... +} +``` + +to + +``` +// SearchHit is a single hit. +type SearchHit struct { + ... + Source json.RawMessage `json:"_source,omitempty"` // stored document source + ... +} +``` + +As `json.RawMessage` is a `[]byte`, there is no need to specify it +as `*json.RawMessage` as `json.RawMessage` is perfectly ok to represent +a `nil` value. + +So when deserializing the search hits, you need to change your code from: + +``` +for _, hit := range searchResult.Hits.Hits { + var doc Doc + err := json.Unmarshal(*hit.Source, &doc) // notice the * here + if err != nil { + // Deserialization failed + } +} +``` + +to + +``` +for _, hit := range searchResult.Hits.Hits { + var doc Doc + err := json.Unmarshal(hit.Source, &doc) // it's missing here + if err != nil { + // Deserialization failed + } +} +``` diff --git a/vendor/github.com/olivere/elastic/v7/CODE_OF_CONDUCT.md b/vendor/github.com/olivere/elastic/v7/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000..acefecee5c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at oliver@eilhard.net. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/vendor/github.com/olivere/elastic/v7/CONTRIBUTING.md b/vendor/github.com/olivere/elastic/v7/CONTRIBUTING.md new file mode 100644 index 0000000000..c7c425a3ef --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# How to contribute + +Elastic is an open-source project and we are looking forward to each +contribution. + +Notice that while the [official Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html) is rather good, it is a high-level +overview of the features of Elasticsearch. However, Elastic tries to resemble +the Java API of Elasticsearch which you can find [on GitHub](https://github.com/elastic/elasticsearch). + +This explains why you might think that some options are strange or missing +in Elastic, while often they're just different. Please check the Java API first. + +Having said that: Elasticsearch is moving fast and it might be very likely +that we missed some features or changes. Feel free to change that. + +## Your Pull Request + +To make it easy to review and understand your changes, please keep the +following things in mind before submitting your pull request: + +* You compared the existing implementation with the Java API, did you? +* Please work on the latest possible state of `olivere/elastic`. + Use `release-branch.v2` for targeting Elasticsearch 1.x and + `release-branch.v3` for targeting 2.x. +* Create a branch dedicated to your change. +* If possible, write a test case which confirms your change. +* Make sure your changes and your tests work with all recent versions of + Elasticsearch. We currently support Elasticsearch 1.7.x in the + release-branch.v2 and Elasticsearch 2.x in the release-branch.v3. +* Test your changes before creating a pull request (`go test ./...`). +* Don't mix several features or bug fixes in one pull request. +* Create a meaningful commit message. +* Explain your change, e.g. provide a link to the issue you are fixing and + probably a link to the Elasticsearch documentation and/or source code. +* Format your source with `go fmt`. + +## Additional Resources + +* [GitHub documentation](https://help.github.com/) +* [GitHub pull request documentation](https://help.github.com/en/articles/creating-a-pull-request) diff --git a/vendor/github.com/olivere/elastic/v7/CONTRIBUTORS b/vendor/github.com/olivere/elastic/v7/CONTRIBUTORS new file mode 100644 index 0000000000..6b65432d69 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/CONTRIBUTORS @@ -0,0 +1,193 @@ +# This is a list of people who have contributed code +# to the Elastic repository. +# +# It is just my small "thank you" to all those that helped +# making Elastic what it is. +# +# Please keep this list sorted. + +0x6875790d0a [@huydx](https://github.com/huydx) +Aaron Tami [@aarontami](https://github.com/aarontami) +Adam Alix [@adamalix](https://github.com/adamalix) +Adam Weiner [@adamweiner](https://github.com/adamweiner) +Adrian Lungu [@AdrianLungu](https://github.com/AdrianLungu) +alehano [@alehano](https://github.com/alehano) +Alejandro Carstens [@alejandro-carstens](https://github.com/alejandro-carstens) +Alex [@akotlar](https://github.com/akotlar) +Alexander Sack [@asac](https://github.com/asac) +Alexandre Olivier [@aliphen](https://github.com/aliphen) +Alexey Sharov [@nizsheanez](https://github.com/nizsheanez) +Anders [@ANerd](https://github.com/ANerd) +AndreKR [@AndreKR](https://github.com/AndreKR) +André Bierlein [@ligustah](https://github.com/ligustah) +Andrew Dunham [@andrew-d](https://github.com/andrew-d) +Andrew Gaul [@andrewgaul](https://github.com/andrewgaul) +Andy Walker [@alaska](https://github.com/alaska) +Arpit Agarwal [@arpiagar](https://github.com/arpiagar) +Arquivei [@arquivei](https://github.com/arquivei) +Artemiy Elozhenko [@artezh](https://github.com/artezh) +arthurgustin [@arthurgustin](https://github.com/arthurgustin) +Bas van Dijk [@basvandijk](https://github.com/basvandijk) +Benjamin Fernandes [@LotharSee](https://github.com/LotharSee) +Benjamin Zarzycki [@kf6nux](https://github.com/kf6nux) +Björn Gerdau [@kernle32dll](https://github.com/kernle32dll) +Boris Popovschi [@Zyqsempai](https://github.com/Zyqsempai) +Bowei Xu [@vancexu](https://github.com/vancexu) +Braden Bassingthwaite [@bbassingthwaite-va](https://github.com/bbassingthwaite-va) +Brady Love [@bradylove](https://github.com/bradylove) +Bryan Conklin [@bmconklin](https://github.com/bmconklin) +Bruce Zhou [@brucez-isell](https://github.com/brucez-isell) +Carl Dunham [@carldunham](https://github.com/carldunham) +Carl Johan Gustavsson [@cjgu](https://github.com/cjgu) +Cat [@cat-turner](https://github.com/cat-turner) +César Jiménez [@cesarjimenez](https://github.com/cesarjimenez) +cforbes [@cforbes](https://github.com/cforbes) +張泰瑋(Chang Tai Wei) [@david30907d](https://github.com/david30907d) +cheshire [@NikitaSerenko](https://github.com/NikitaSerenko) +Chris M [@tebriel](https://github.com/tebriel) +Chris Rice [@donutmonger](https://github.com/donutmonger) +Claudiu Olteanu [@claudiuolteanu](https://github.com/claudiuolteanu) +Chris Duncan [@veqryn](https://github.com/veqryn) +Chris Ludden [@cludden](https://github.com/cludden) +Christophe Courtaut [@kri5](https://github.com/kri5) +cmitchell [@cmitchell](https://github.com/cmitchell) +Connor Peet [@connor4312](https://github.com/connor4312) +Conrad Pankoff [@deoxxa](https://github.com/deoxxa) +Corey Scott [@corsc](https://github.com/corsc) +Chris Petersen [@ex-nerd](https://github.com/ex-nerd) +Daniel Barrett [@shendaras](https://github.com/shendaras) +Daniel Heckrath [@DanielHeckrath](https://github.com/DanielHeckrath) +Daniel Imfeld [@dimfeld](https://github.com/dimfeld) +Daniel Santos [@danlsgiga](https://github.com/danlsgiga) +David Emanuel Buchmann [@wuurrd](https://github.com/wuurrd) +diacone [@diacone](https://github.com/diacone) +Diego Becciolini [@itizir](https://github.com/itizir) +Dwayne Schultz [@myshkin5](https://github.com/myshkin5) +Elliot Williams [@elliotwms](https://github.com/elliotwms) +Ellison Leão [@ellisonleao](https://github.com/ellisonleao) +Emil Gedda [@EmilGedda](https://github.com/EmilGedda) +Erik Grinaker [@erikgrinaker](https://github.com/erikgrinaker) +Erwin [@eticzon](https://github.com/eticzon) +Etienne Lafarge [@elafarge](https://github.com/elafarge) +Eugene Egorov [@EugeneEgorov](https://github.com/EugeneEgorov) +Evan Shaw [@edsrzf](https://github.com/edsrzf) +Fanfan [@wenpos](https://github.com/wenpos) +Faolan C-P [@fcheslack](https://github.com/fcheslack) +Filip Tepper [@filiptepper](https://github.com/filiptepper) +Garrett Kelley [@GarrettKelley](https://github.com/GarrettKelley) +Gaspard Douady [@plopik](https://github.com/plopik) +Gaylord Aulke [@blafasel42](https://github.com/blafasel42) +Gerhard Häring [@ghaering](https://github.com/ghaering) +gregoryfranklin [@gregoryfranklin](https://github.com/gregoryfranklin) +Guilherme Silveira [@guilherme-santos](https://github.com/guilherme-santos) +Guillaume J. Charmes [@creack](https://github.com/creack) +Guiseppe [@gm42](https://github.com/gm42) +Han Yu [@MoonighT](https://github.com/MoonighT) +Harmen [@alicebob](https://github.com/alicebob) +Harrison Wright [@wright8191](https://github.com/wright8191) +Henry Clifford [@hcliff](https://github.com/hcliff) +Henry Stern [@hstern](https://github.com/hstern) +Igor Dubinskiy [@idubinskiy](https://github.com/idubinskiy) +initialcontext [@initialcontext](https://github.com/initialcontext) +Isaac Saldana [@isaldana](https://github.com/isaldana) +J Barkey Wolf [@jjhbw](https://github.com/jjhbw) +Jack Lindamood [@cep21](https://github.com/cep21) +Jacob [@jdelgad](https://github.com/jdelgad) +Jan Düpmeier [@jduepmeier](https://github.com/jduepmeier) +Jayme Rotsaert [@jrots](https://github.com/jrots) +Jean-Alexandre Beaumont [@Enteris](https://github.com/Enteris) +Jean-François Roche [@jfroche](https://github.com/jfroche) +Jeff Rand [@jeffrand](https://github.com/jeffrand) +Jeremy Canady [@jrmycanady](https://github.com/jrmycanady) +Jérémie Vexiau [@texvex](https://github.com/texvex) +Jesper Bränn [@Yopi](https://github.com/Yopi) +Jim Berlage [@jimberlage](https://github.com/jimberlage) +Joe Buck [@four2five](https://github.com/four2five) +John Barker [@j16r](https://github.com/j16r) +John Goodall [@jgoodall](https://github.com/jgoodall) +John Stanford [@jxstanford](https://github.com/jxstanford) +Jonas Groenaas Drange [@semafor](https://github.com/semafor) +Josef Fröhle [@Dexus](https://github.com/Dexus) +José Martínez [@xose](https://github.com/xose) +Josh Chorlton [@jchorl](https://github.com/jchorl) +Jpnock [@Jpnock](https://github.com/Jpnock) +jun [@coseyo](https://github.com/coseyo) +Junpei Tsuji [@jun06t](https://github.com/jun06t) +kartlee [@kartlee](https://github.com/kartlee) +Keith Hatton [@khatton-ft](https://github.com/khatton-ft) +kel [@liketic](https://github.com/liketic) +Kenta SUZUKI [@suzuken](https://github.com/suzuken) +Kevin Mulvey [@kmulvey](https://github.com/kmulvey) +Kyle Brandt [@kylebrandt](https://github.com/kylebrandt) +Larry Cinnabar [@larrycinnabar](https://github.com/larrycinnabar) +Leandro Piccilli [@lpic10](https://github.com/lpic10) +Lee [@leezhm](https://github.com/leezhm) +lechnertech [@lechnertech](https://github.com/lechnertech) +M. Zulfa Achsani [@misterciput](https://github.com/misterciput) +Maciej Lisiewski [@c2h5oh](https://github.com/c2h5oh) +Mara Kim [@autochthe](https://github.com/autochthe) +Marcy Buccellato [@marcybuccellato](https://github.com/marcybuccellato) +Mark Costello [@mcos](https://github.com/mcos) +Martin Häger [@protomouse](https://github.com/protomouse) +Matt Braymer-Hayes [@mattayes](https://github.com/mattayes) +Medhi Bechina [@mdzor](https://github.com/mdzor) +Mike Beshai [@mbesh](https://github.com/mbesh) +mmfrb [@mmfrb](https://github.com/mmfrb) +mnpritula [@mnpritula](https://github.com/mnpritula) +mosa [@mosasiru](https://github.com/mosasiru) +Muhammet Çakır [@cakirmuha](https://github.com/cakirmuha) +naimulhaider [@naimulhaider](https://github.com/naimulhaider) +Naoya Yoshizawa [@azihsoyn](https://github.com/azihsoyn) +navins [@ishare](https://github.com/ishare) +Naoya Tsutsumi [@tutuming](https://github.com/tutuming) +Nathan Lacey [@nlacey](https://github.com/nlacey) +NeoCN [@NeoCN](https://github.com/NeoCN) +Nicholas Wolff [@nwolff](https://github.com/nwolff) +Nick K [@utrack](https://github.com/utrack) +Nick Whyte [@nickw444](https://github.com/nickw444) +Nicolae Vartolomei [@nvartolomei](https://github.com/nvartolomei) +okhowang [@okhowang](https://github.com/okhowang) +Orne Brocaar [@brocaar](https://github.com/brocaar) +Paul [@eyeamera](https://github.com/eyeamera) +Paul Oldenburg [@lr-paul](https://github.com/lr-paul) +Pete C [@peteclark-ft](https://github.com/peteclark-ft) +Peter Nagy [@nagypeterjob](https://github.com/nagypeterjob) +Paolo [@ppiccolo](https://github.com/ppiccolo) +Igor Panychek [@panychek](https://github.com/panychek) +Radoslaw Wesolowski [@r--w](https://github.com/r--w) +Rafał Gałus [@rgalus](https://github.com/rgalus) +rchicoli [@rchicoli](https://github.com/rchicoli) +Roman Colohanin [@zuzmic](https://github.com/zuzmic) +Ryan Schmukler [@rschmukler](https://github.com/rschmukler) +Ryan Wynn [@rwynn](https://github.com/rwynn) +Sacheendra talluri [@sacheendra](https://github.com/sacheendra) +Sean DuBois [@Sean-Der](https://github.com/Sean-Der) +Sagan Yaroslav [@sgnrslv](https://github.com/sgnrslv) +Shalin LK [@shalinlk](https://github.com/shalinlk) +Simon Schneider [@raynigon](https://github.com/raynigon) +singham [@zhaochenxiao90](https://github.com/zhaochenxiao90) +Slawomir CALUCH [@slawo](https://github.com/slawo) +soarpenguin [@soarpenguin](https://github.com/soarpenguin) +Stephan Krynauw [@skrynauw](https://github.com/skrynauw) +Stephen Kubovic [@stephenkubovic](https://github.com/stephenkubovic) +Stuart Warren [@Woz](https://github.com/stuart-warren) +Sulaiman [@salajlan](https://github.com/salajlan) +Sundar [@sundarv85](https://github.com/sundarv85) +Swarlston [@Swarlston](https://github.com/Swarlston) +Take [ww24](https://github.com/ww24) +Tetsuya Morimoto [@t2y](https://github.com/t2y) +TheZeroSlave [@TheZeroSlave](https://github.com/TheZeroSlave) +Tomasz Elendt [@telendt](https://github.com/telendt) +TimeEmit [@TimeEmit](https://github.com/timeemit) +TusharM [@tusharm](https://github.com/tusharm) +wangtuo [@wangtuo](https://github.com/wangtuo) +Wédney Yuri [@wedneyyuri](https://github.com/wedneyyuri) +Wesley Kim [@wesleyk](https://github.com/wesleyk) +wolfkdy [@wolfkdy](https://github.com/wolfkdy) +Wyndham Blanton [@wyndhblb](https://github.com/wyndhblb) +Yarden Bar [@ayashjorden](https://github.com/ayashjorden) +Yuya Kusakabe [@higebu](https://github.com/higebu) +zakthomas [@zakthomas](https://github.com/zakthomas) +Zach [@snowzach](https://github.com/snowzach) +zhangxin [@visaxin](https://github.com/visaxin) +@林 [@zplzpl](https://github.com/zplzpl) diff --git a/vendor/github.com/olivere/elastic/v7/ISSUE_TEMPLATE.md b/vendor/github.com/olivere/elastic/v7/ISSUE_TEMPLATE.md new file mode 100644 index 0000000000..11d6e2ca14 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/ISSUE_TEMPLATE.md @@ -0,0 +1,19 @@ +Please use the following questions as a guideline to help me answer +your issue/question without further inquiry. Thank you. + +### Which version of Elastic are you using? + +[ ] elastic.v7 (for Elasticsearch 7.x) +[ ] elastic.v6 (for Elasticsearch 6.x) +[ ] elastic.v5 (for Elasticsearch 5.x) +[ ] elastic.v3 (for Elasticsearch 2.x) +[ ] elastic.v2 (for Elasticsearch 1.x) + +### Please describe the expected behavior + + +### Please describe the actual behavior + + +### Any steps to reproduce the behavior? + diff --git a/vendor/github.com/olivere/elastic/v7/LICENSE b/vendor/github.com/olivere/elastic/v7/LICENSE new file mode 100644 index 0000000000..8b22cdb601 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) +Copyright © 2012-2015 Oliver Eilhard + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the “Software”), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/vendor/github.com/olivere/elastic/v7/README.md b/vendor/github.com/olivere/elastic/v7/README.md new file mode 100644 index 0000000000..02b3d1004e --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/README.md @@ -0,0 +1,432 @@ +# Elastic + +**This is a development branch that is actively being worked on. DO NOT USE IN PRODUCTION! If you want to use stable versions of Elastic, please use Go modules for the 7.x release (or later) or a dependency manager like [dep](https://github.com/golang/dep) for earlier releases.** + +Elastic is an [Elasticsearch](http://www.elasticsearch.org/) client for the +[Go](http://www.golang.org/) programming language. + +[![Build Status](https://travis-ci.org/olivere/elastic.svg?branch=release-branch.v6)](https://travis-ci.org/olivere/elastic) +[![Godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](http://godoc.org/github.com/olivere/elastic) +[![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/olivere/elastic/master/LICENSE) +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Folivere%2Felastic.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Folivere%2Felastic?ref=badge_shield) + +See the [wiki](https://github.com/olivere/elastic/wiki) for additional information about Elastic. + +Buy Me A Coffee + +## Releases + +**The release branches (e.g. [`release-branch.v7`](https://github.com/olivere/elastic/tree/release-branch.v7)) +are actively being worked on and can break at any time. +If you want to use stable versions of Elastic, please use Go modules.** + +Here's the version matrix: + +Elasticsearch version | Elastic version | Package URL | Remarks | +----------------------|------------------|-------------|---------| +7.x                   | 7.0             | [`github.com/olivere/elastic/v7`](https://github.com/olivere/elastic) ([source](https://github.com/olivere/elastic/tree/release-branch.v7) [doc](http://godoc.org/github.com/olivere/elastic)) | Use Go modules. +6.x                   | 6.0             | [`github.com/olivere/elastic`](https://github.com/olivere/elastic) ([source](https://github.com/olivere/elastic/tree/release-branch.v6) [doc](http://godoc.org/github.com/olivere/elastic)) | Use a dependency manager (see below). +5.x | 5.0 | [`gopkg.in/olivere/elastic.v5`](https://gopkg.in/olivere/elastic.v5) ([source](https://github.com/olivere/elastic/tree/release-branch.v5) [doc](http://godoc.org/gopkg.in/olivere/elastic.v5)) | Actively maintained. +2.x | 3.0 | [`gopkg.in/olivere/elastic.v3`](https://gopkg.in/olivere/elastic.v3) ([source](https://github.com/olivere/elastic/tree/release-branch.v3) [doc](http://godoc.org/gopkg.in/olivere/elastic.v3)) | Deprecated. Please update. +1.x | 2.0 | [`gopkg.in/olivere/elastic.v2`](https://gopkg.in/olivere/elastic.v2) ([source](https://github.com/olivere/elastic/tree/release-branch.v2) [doc](http://godoc.org/gopkg.in/olivere/elastic.v2)) | Deprecated. Please update. +0.9-1.3 | 1.0 | [`gopkg.in/olivere/elastic.v1`](https://gopkg.in/olivere/elastic.v1) ([source](https://github.com/olivere/elastic/tree/release-branch.v1) [doc](http://godoc.org/gopkg.in/olivere/elastic.v1)) | Deprecated. Please update. + +**Example:** + +You have installed Elasticsearch 7.0.0 and want to use Elastic. +As listed above, you should use Elastic 7.0 (code is in `release-branch.v7`). + +To use the required version of Elastic in your application, you +should use [Go modules](https://github.com/golang/go/wiki/Modules) +to manage dependencies. Make sure to use a version such as `7.0.0` or later. + +To use Elastic, import: + +```go +import "github.com/olivere/elastic/v7" +``` + +### Elastic 7.0 + +Elastic 7.0 targets Elasticsearch 7.x which [was released on April 10th 2019](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/release-notes-7.0.0.html). + +As always with major version, there are a lot of [breaking changes](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/release-notes-7.0.0.html#breaking-7.0.0). +We will use this as an opportunity to [clean up and refactor Elastic](https://github.com/olivere/elastic/blob/release-branch.v7/CHANGELOG-7.0.md), +as we already did in earlier (major) releases. + +### Elastic 6.0 + +Elastic 6.0 targets Elasticsearch 6.x which was [released on 14th November 2017](https://www.elastic.co/blog/elasticsearch-6-0-0-released). + +Notice that there are a lot of [breaking changes in Elasticsearch 6.0](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/breaking-changes-6.0.html) +and we used this as an opportunity to [clean up and refactor Elastic](https://github.com/olivere/elastic/blob/release-branch.v6/CHANGELOG-6.0.md) +as we did in the transition from earlier versions of Elastic. + +### Elastic 5.0 + +Elastic 5.0 targets Elasticsearch 5.0.0 and later. Elasticsearch 5.0.0 was +[released on 26th October 2016](https://www.elastic.co/blog/elasticsearch-5-0-0-released). + +Notice that there are will be a lot of [breaking changes in Elasticsearch 5.0](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking-changes-5.0.html) +and we used this as an opportunity to [clean up and refactor Elastic](https://github.com/olivere/elastic/blob/release-branch.v5/CHANGELOG-5.0.md) +as we did in the transition from Elastic 2.0 (for Elasticsearch 1.x) to Elastic 3.0 (for Elasticsearch 2.x). + +Furthermore, the jump in version numbers will give us a chance to be in sync with the Elastic Stack. + +### Elastic 3.0 + +Elastic 3.0 targets Elasticsearch 2.x and is published via [`gopkg.in/olivere/elastic.v3`](https://gopkg.in/olivere/elastic.v3). + +Elastic 3.0 will only get critical bug fixes. You should update to a recent version. + +### Elastic 2.0 + +Elastic 2.0 targets Elasticsearch 1.x and is published via [`gopkg.in/olivere/elastic.v2`](https://gopkg.in/olivere/elastic.v2). + +Elastic 2.0 will only get critical bug fixes. You should update to a recent version. + +### Elastic 1.0 + +Elastic 1.0 is deprecated. You should really update Elasticsearch and Elastic +to a recent version. + +However, if you cannot update for some reason, don't worry. Version 1.0 is +still available. All you need to do is go-get it and change your import path +as described above. + + +## Status + +We use Elastic in production since 2012. Elastic is stable but the API changes +now and then. We strive for API compatibility. +However, Elasticsearch sometimes introduces [breaking changes](https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes.html) +and we sometimes have to adapt. + +Having said that, there have been no big API changes that required you +to rewrite your application big time. More often than not it's renaming APIs +and adding/removing features so that Elastic is in sync with Elasticsearch. + +Elastic has been used in production starting with Elasticsearch 0.90 up to recent 7.x +versions. Furthermore, we use [Travis CI](https://travis-ci.org/) +to test Elastic with the most recent versions of Elasticsearch and Go. +See the [.travis.yml](https://github.com/olivere/elastic/blob/master/.travis.yml) +file for the exact matrix and [Travis](https://travis-ci.org/olivere/elastic) +for the results. + +Elasticsearch has quite a few features. Most of them are implemented +by Elastic. I add features and APIs as required. It's straightforward +to implement missing pieces. I'm accepting pull requests :-) + +Having said that, I hope you find the project useful. + + +## Getting Started + +The first thing you do is to create a [Client](https://github.com/olivere/elastic/blob/master/client.go). +The client connects to Elasticsearch on `http://127.0.0.1:9200` by default. + +You typically create one client for your app. Here's a complete example of +creating a client, creating an index, adding a document, executing a search etc. + +An example is available [here](https://olivere.github.io/elastic/). + +Here's a [link to a complete working example for v6](https://gist.github.com/olivere/e4a376b4783c0914e44ea4f745ce2ebf). + +Here are a few tips on how to get used to Elastic: + +1. Head over to the [Wiki](https://github.com/olivere/elastic/wiki) for detailed information and + topics like e.g. [how to add a middleware](https://github.com/olivere/elastic/wiki/HttpTransport) + or how to [connect to AWS](https://github.com/olivere/elastic/wiki/Using-with-AWS-Elasticsearch-Service). +2. If you are unsure how to implement something, read the tests (all `_test.go` files). + They not only serve as a guard against changes, but also as a reference. +3. The [recipes](https://github.com/olivere/elastic/tree/release-branch.v6/recipes) + contains small examples on how to implement something, e.g. bulk indexing, scrolling etc. + + +## API Status + +### Document APIs + +- [x] Index API +- [x] Get API +- [x] Delete API +- [x] Delete By Query API +- [x] Update API +- [x] Update By Query API +- [x] Multi Get API +- [x] Bulk API +- [x] Reindex API +- [x] Term Vectors +- [x] Multi termvectors API + +### Search APIs + +- [x] Search +- [x] Search Template +- [ ] Multi Search Template +- [x] Search Shards API +- [x] Suggesters + - [x] Term Suggester + - [x] Phrase Suggester + - [x] Completion Suggester + - [x] Context Suggester +- [x] Multi Search API +- [x] Count API +- [x] Validate API +- [x] Explain API +- [x] Profile API +- [x] Field Capabilities API + +### Aggregations + +- Metrics Aggregations + - [x] Avg + - [x] Cardinality + - [x] Extended Stats + - [x] Geo Bounds + - [x] Geo Centroid + - [x] Max + - [x] Min + - [x] Percentiles + - [x] Percentile Ranks + - [ ] Scripted Metric + - [x] Stats + - [x] Sum + - [x] Top Hits + - [x] Value Count +- Bucket Aggregations + - [x] Adjacency Matrix + - [x] Children + - [x] Auto-interval Date Histogram + - [x] Date Histogram + - [x] Date Range + - [x] Diversified Sampler + - [x] Filter + - [x] Filters + - [x] Geo Distance + - [ ] GeoHash Grid + - [x] Global + - [x] Histogram + - [x] IP Range + - [x] Missing + - [x] Nested + - [x] Range + - [x] Reverse Nested + - [x] Sampler + - [x] Significant Terms + - [x] Significant Text + - [x] Terms + - [x] Composite +- Pipeline Aggregations + - [x] Avg Bucket + - [x] Derivative + - [x] Max Bucket + - [x] Min Bucket + - [x] Sum Bucket + - [x] Stats Bucket + - [ ] Extended Stats Bucket + - [x] Percentiles Bucket + - [x] Moving Average + - [x] Cumulative Sum + - [x] Bucket Script + - [x] Bucket Selector + - [x] Bucket Sort + - [x] Serial Differencing +- [x] Matrix Aggregations + - [x] Matrix Stats +- [x] Aggregation Metadata + +### Indices APIs + +- [x] Create Index +- [x] Delete Index +- [x] Get Index +- [x] Indices Exists +- [x] Open / Close Index +- [x] Shrink Index +- [x] Rollover Index +- [x] Put Mapping +- [x] Get Mapping +- [x] Get Field Mapping +- [x] Types Exists +- [x] Index Aliases +- [x] Update Indices Settings +- [x] Get Settings +- [x] Analyze + - [x] Explain Analyze +- [x] Index Templates +- [x] Indices Stats +- [x] Indices Segments +- [ ] Indices Recovery +- [ ] Indices Shard Stores +- [ ] Clear Cache +- [x] Flush + - [x] Synced Flush +- [x] Refresh +- [x] Force Merge + +### Index Lifecycle Management APIs + +- [x] Create Policy +- [x] Get Policy +- [x] Delete Policy +- [ ] Move to Step +- [ ] Remove Policy +- [ ] Retry Policy +- [ ] Get Ilm Status +- [ ] Explain Lifecycle +- [ ] Start Ilm +- [ ] Stop Ilm + +### cat APIs + +- [X] cat aliases +- [X] cat allocation +- [X] cat count +- [ ] cat fielddata +- [X] cat health +- [X] cat indices +- [ ] cat master +- [ ] cat nodeattrs +- [ ] cat nodes +- [ ] cat pending tasks +- [ ] cat plugins +- [ ] cat recovery +- [ ] cat repositories +- [ ] cat thread pool +- [ ] cat shards +- [ ] cat segments +- [ ] cat snapshots +- [ ] cat templates + +### Cluster APIs + +- [x] Cluster Health +- [x] Cluster State +- [x] Cluster Stats +- [ ] Pending Cluster Tasks +- [x] Cluster Reroute +- [ ] Cluster Update Settings +- [x] Nodes Stats +- [x] Nodes Info +- [ ] Nodes Feature Usage +- [ ] Remote Cluster Info +- [x] Task Management API +- [ ] Nodes hot_threads +- [ ] Cluster Allocation Explain API + +### Query DSL + +- [x] Match All Query +- [x] Inner hits +- Full text queries + - [x] Match Query + - [x] Match Phrase Query + - [x] Match Phrase Prefix Query + - [x] Multi Match Query + - [x] Common Terms Query + - [x] Query String Query + - [x] Simple Query String Query +- Term level queries + - [x] Term Query + - [x] Terms Query + - [x] Terms Set Query + - [x] Range Query + - [x] Exists Query + - [x] Prefix Query + - [x] Wildcard Query + - [x] Regexp Query + - [x] Fuzzy Query + - [x] Type Query + - [x] Ids Query +- Compound queries + - [x] Constant Score Query + - [x] Bool Query + - [x] Dis Max Query + - [x] Function Score Query + - [x] Boosting Query +- Joining queries + - [x] Nested Query + - [x] Has Child Query + - [x] Has Parent Query + - [x] Parent Id Query +- Geo queries + - [ ] GeoShape Query + - [x] Geo Bounding Box Query + - [x] Geo Distance Query + - [x] Geo Polygon Query +- Specialized queries + - [x] Distance Feature Query + - [x] More Like This Query + - [x] Script Query + - [x] Script Score Query + - [x] Percolate Query +- Span queries + - [ ] Span Term Query + - [ ] Span Multi Term Query + - [ ] Span First Query + - [ ] Span Near Query + - [ ] Span Or Query + - [ ] Span Not Query + - [ ] Span Containing Query + - [ ] Span Within Query + - [ ] Span Field Masking Query +- [ ] Minimum Should Match +- [ ] Multi Term Query Rewrite + +### Modules + +- Snapshot and Restore + - [x] Repositories + - [x] Snapshot get + - [x] Snapshot create + - [x] Snapshot delete + - [ ] Restore + - [ ] Snapshot status + - [ ] Monitoring snapshot/restore status + - [ ] Stopping currently running snapshot and restore +- Scripting + - [x] GetScript + - [x] PutScript + - [x] DeleteScript + +### Sorting + +- [x] Sort by score +- [x] Sort by field +- [x] Sort by geo distance +- [x] Sort by script +- [x] Sort by doc + +### Scrolling + +Scrolling is supported via a `ScrollService`. It supports an iterator-like interface. +The `ClearScroll` API is implemented as well. + +A pattern for [efficiently scrolling in parallel](https://github.com/olivere/elastic/wiki/ScrollParallel) +is described in the [Wiki](https://github.com/olivere/elastic/wiki). + +## How to contribute + +Read [the contribution guidelines](https://github.com/olivere/elastic/blob/master/CONTRIBUTING.md). + +## Credits + +Thanks a lot for the great folks working hard on +[Elasticsearch](https://www.elastic.co/products/elasticsearch) +and +[Go](https://golang.org/). + +Elastic uses portions of the +[uritemplates](https://github.com/jtacoma/uritemplates) library +by Joshua Tacoma, +[backoff](https://github.com/cenkalti/backoff) by Cenk Altı and +[leaktest](https://github.com/fortytw2/leaktest) by Ian Chiles. + +## LICENSE + +MIT-LICENSE. See [LICENSE](http://olivere.mit-license.org/) +or the LICENSE file provided in the repository for details. + + +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Folivere%2Felastic.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Folivere%2Felastic?ref=badge_large) diff --git a/vendor/github.com/olivere/elastic/v7/acknowledged_response.go b/vendor/github.com/olivere/elastic/v7/acknowledged_response.go new file mode 100644 index 0000000000..2045ab85e4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/acknowledged_response.go @@ -0,0 +1,13 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// AcknowledgedResponse is returned from various APIs. It simply indicates +// whether the operation is ack'd or not. +type AcknowledgedResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/backoff.go b/vendor/github.com/olivere/elastic/v7/backoff.go new file mode 100644 index 0000000000..736959f6db --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/backoff.go @@ -0,0 +1,148 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "math" + "math/rand" + "sync" + "time" +) + +// BackoffFunc specifies the signature of a function that returns the +// time to wait before the next call to a resource. To stop retrying +// return false in the 2nd return value. +type BackoffFunc func(retry int) (time.Duration, bool) + +// Backoff allows callers to implement their own Backoff strategy. +type Backoff interface { + // Next implements a BackoffFunc. + Next(retry int) (time.Duration, bool) +} + +// -- ZeroBackoff -- + +// ZeroBackoff is a fixed backoff policy whose backoff time is always zero, +// meaning that the operation is retried immediately without waiting, +// indefinitely. +type ZeroBackoff struct{} + +// Next implements BackoffFunc for ZeroBackoff. +func (b ZeroBackoff) Next(retry int) (time.Duration, bool) { + return 0, true +} + +// -- StopBackoff -- + +// StopBackoff is a fixed backoff policy that always returns false for +// Next(), meaning that the operation should never be retried. +type StopBackoff struct{} + +// Next implements BackoffFunc for StopBackoff. +func (b StopBackoff) Next(retry int) (time.Duration, bool) { + return 0, false +} + +// -- ConstantBackoff -- + +// ConstantBackoff is a backoff policy that always returns the same delay. +type ConstantBackoff struct { + interval time.Duration +} + +// NewConstantBackoff returns a new ConstantBackoff. +func NewConstantBackoff(interval time.Duration) *ConstantBackoff { + return &ConstantBackoff{interval: interval} +} + +// Next implements BackoffFunc for ConstantBackoff. +func (b *ConstantBackoff) Next(retry int) (time.Duration, bool) { + return b.interval, true +} + +// -- Exponential -- + +// ExponentialBackoff implements the simple exponential backoff described by +// Douglas Thain at http://dthain.blogspot.de/2009/02/exponential-backoff-in-distributed.html. +type ExponentialBackoff struct { + t float64 // initial timeout (in msec) + f float64 // exponential factor (e.g. 2) + m float64 // maximum timeout (in msec) +} + +// NewExponentialBackoff returns a ExponentialBackoff backoff policy. +// Use initialTimeout to set the first/minimal interval +// and maxTimeout to set the maximum wait interval. +func NewExponentialBackoff(initialTimeout, maxTimeout time.Duration) *ExponentialBackoff { + return &ExponentialBackoff{ + t: float64(int64(initialTimeout / time.Millisecond)), + f: 2.0, + m: float64(int64(maxTimeout / time.Millisecond)), + } +} + +// Next implements BackoffFunc for ExponentialBackoff. +func (b *ExponentialBackoff) Next(retry int) (time.Duration, bool) { + r := 1.0 + rand.Float64() // random number in [1..2] + m := math.Min(r*b.t*math.Pow(b.f, float64(retry)), b.m) + if m >= b.m { + return 0, false + } + d := time.Duration(int64(m)) * time.Millisecond + return d, true +} + +// -- Simple Backoff -- + +// SimpleBackoff takes a list of fixed values for backoff intervals. +// Each call to Next returns the next value from that fixed list. +// After each value is returned, subsequent calls to Next will only return +// the last element. The values are optionally "jittered" (off by default). +type SimpleBackoff struct { + sync.Mutex + ticks []int + jitter bool +} + +// NewSimpleBackoff creates a SimpleBackoff algorithm with the specified +// list of fixed intervals in milliseconds. +func NewSimpleBackoff(ticks ...int) *SimpleBackoff { + return &SimpleBackoff{ + ticks: ticks, + jitter: false, + } +} + +// Jitter enables or disables jittering values. +func (b *SimpleBackoff) Jitter(flag bool) *SimpleBackoff { + b.Lock() + b.jitter = flag + b.Unlock() + return b +} + +// jitter randomizes the interval to return a value of [0.5*millis .. 1.5*millis]. +func jitter(millis int) int { + if millis <= 0 { + return 0 + } + return millis/2 + rand.Intn(millis) +} + +// Next implements BackoffFunc for SimpleBackoff. +func (b *SimpleBackoff) Next(retry int) (time.Duration, bool) { + b.Lock() + defer b.Unlock() + + if retry >= len(b.ticks) { + return 0, false + } + + ms := b.ticks[retry] + if b.jitter { + ms = jitter(ms) + } + return time.Duration(ms) * time.Millisecond, true +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk.go b/vendor/github.com/olivere/elastic/v7/bulk.go new file mode 100644 index 0000000000..03e8ae8610 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk.go @@ -0,0 +1,470 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// BulkService allows for batching bulk requests and sending them to +// Elasticsearch in one roundtrip. Use the Add method with BulkIndexRequest, +// BulkUpdateRequest, and BulkDeleteRequest to add bulk requests to a batch, +// then use Do to send them to Elasticsearch. +// +// BulkService will be reset after each Do call. In other words, you can +// reuse BulkService to send many batches. You do not have to create a new +// BulkService for each batch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for more details. +type BulkService struct { + client *Client + retrier Retrier + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + typ string + requests []BulkableRequest + pipeline string + timeout string + refresh string + routing string + waitForActiveShards string + + // estimated bulk size in bytes, up to the request index sizeInBytesCursor + sizeInBytes int64 + sizeInBytesCursor int +} + +// NewBulkService initializes a new BulkService. +func NewBulkService(client *Client) *BulkService { + builder := &BulkService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *BulkService) Pretty(pretty bool) *BulkService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *BulkService) Human(human bool) *BulkService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *BulkService) ErrorTrace(errorTrace bool) *BulkService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *BulkService) FilterPath(filterPath ...string) *BulkService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *BulkService) Header(name string, value string) *BulkService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *BulkService) Headers(headers http.Header) *BulkService { + s.headers = headers + return s +} + +// Reset cleans up the request queue +func (s *BulkService) Reset() { + s.requests = make([]BulkableRequest, 0) + s.sizeInBytes = 0 + s.sizeInBytesCursor = 0 +} + +// Retrier allows to set specific retry logic for this BulkService. +// If not specified, it will use the client's default retrier. +func (s *BulkService) Retrier(retrier Retrier) *BulkService { + s.retrier = retrier + return s +} + +// Index specifies the index to use for all batches. You may also leave +// this blank and specify the index in the individual bulk requests. +func (s *BulkService) Index(index string) *BulkService { + s.index = index + return s +} + +// Type specifies the type to use for all batches. You may also leave +// this blank and specify the type in the individual bulk requests. +func (s *BulkService) Type(typ string) *BulkService { + s.typ = typ + return s +} + +// Timeout is a global timeout for processing bulk requests. This is a +// server-side timeout, i.e. it tells Elasticsearch the time after which +// it should stop processing. +func (s *BulkService) Timeout(timeout string) *BulkService { + s.timeout = timeout + return s +} + +// Refresh controls when changes made by this request are made visible +// to search. The allowed values are: "true" (refresh the relevant +// primary and replica shards immediately), "wait_for" (wait for the +// changes to be made visible by a refresh before reying), or "false" +// (no refresh related actions). The default value is "false". +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *BulkService) Refresh(refresh string) *BulkService { + s.refresh = refresh + return s +} + +// Routing specifies the routing value. +func (s *BulkService) Routing(routing string) *BulkService { + s.routing = routing + return s +} + +// Pipeline specifies the pipeline id to preprocess incoming documents with. +func (s *BulkService) Pipeline(pipeline string) *BulkService { + s.pipeline = pipeline + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active +// before proceeding with the bulk operation. Defaults to 1, meaning the +// primary shard only. Set to `all` for all shard copies, otherwise set to +// any non-negative value less than or equal to the total number of copies +// for the shard (number of replicas + 1). +func (s *BulkService) WaitForActiveShards(waitForActiveShards string) *BulkService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// Add adds bulkable requests, i.e. BulkIndexRequest, BulkUpdateRequest, +// and/or BulkDeleteRequest. +func (s *BulkService) Add(requests ...BulkableRequest) *BulkService { + s.requests = append(s.requests, requests...) + return s +} + +// EstimatedSizeInBytes returns the estimated size of all bulkable +// requests added via Add. +func (s *BulkService) EstimatedSizeInBytes() int64 { + if s.sizeInBytesCursor == len(s.requests) { + return s.sizeInBytes + } + for _, r := range s.requests[s.sizeInBytesCursor:] { + s.sizeInBytes += s.estimateSizeInBytes(r) + s.sizeInBytesCursor++ + } + return s.sizeInBytes +} + +// estimateSizeInBytes returns the estimates size of the given +// bulkable request, i.e. BulkIndexRequest, BulkUpdateRequest, and +// BulkDeleteRequest. +func (s *BulkService) estimateSizeInBytes(r BulkableRequest) int64 { + lines, _ := r.Source() + size := 0 + for _, line := range lines { + // +1 for the \n + size += len(line) + 1 + } + return int64(size) +} + +// NumberOfActions returns the number of bulkable requests that need to +// be sent to Elasticsearch on the next batch. +func (s *BulkService) NumberOfActions() int { + return len(s.requests) +} + +func (s *BulkService) bodyAsString() (string, error) { + // Pre-allocate to reduce allocs + var buf strings.Builder + buf.Grow(int(s.EstimatedSizeInBytes())) + + for _, req := range s.requests { + source, err := req.Source() + if err != nil { + return "", err + } + for _, line := range source { + buf.WriteString(line) + buf.WriteByte('\n') + } + } + + return buf.String(), nil +} + +// Do sends the batched requests to Elasticsearch. Note that, when successful, +// you can reuse the BulkService for the next batch as the list of bulk +// requests is cleared on success. +func (s *BulkService) Do(ctx context.Context) (*BulkResponse, error) { + // No actions? + if s.NumberOfActions() == 0 { + return nil, errors.New("elastic: No bulk actions to commit") + } + + // Get body + body, err := s.bodyAsString() + if err != nil { + return nil, err + } + + // Build url + path := "/" + if len(s.index) > 0 { + index, err := uritemplates.Expand("{index}", map[string]string{ + "index": s.index, + }) + if err != nil { + return nil, err + } + path += index + "/" + } + if len(s.typ) > 0 { + typ, err := uritemplates.Expand("{type}", map[string]string{ + "type": s.typ, + }) + if err != nil { + return nil, err + } + path += typ + "/" + } + path += "_bulk" + + // Parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.pipeline != "" { + params.Set("pipeline", s.pipeline) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + ContentType: "application/x-ndjson", + Retrier: s.retrier, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return results + ret := new(BulkResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + + // Reset so the request can be reused + s.Reset() + + return ret, nil +} + +// BulkResponse is a response to a bulk execution. +// +// Example: +// { +// "took":3, +// "errors":false, +// "items":[{ +// "index":{ +// "_index":"index1", +// "_type":"tweet", +// "_id":"1", +// "_version":3, +// "status":201 +// } +// },{ +// "index":{ +// "_index":"index2", +// "_type":"tweet", +// "_id":"2", +// "_version":3, +// "status":200 +// } +// },{ +// "delete":{ +// "_index":"index1", +// "_type":"tweet", +// "_id":"1", +// "_version":4, +// "status":200, +// "found":true +// } +// },{ +// "update":{ +// "_index":"index2", +// "_type":"tweet", +// "_id":"2", +// "_version":4, +// "status":200 +// } +// }] +// } +type BulkResponse struct { + Took int `json:"took,omitempty"` + Errors bool `json:"errors,omitempty"` + Items []map[string]*BulkResponseItem `json:"items,omitempty"` +} + +// BulkResponseItem is the result of a single bulk request. +type BulkResponseItem struct { + Index string `json:"_index,omitempty"` + Type string `json:"_type,omitempty"` + Id string `json:"_id,omitempty"` + Version int64 `json:"_version,omitempty"` + Result string `json:"result,omitempty"` + Shards *ShardsInfo `json:"_shards,omitempty"` + SeqNo int64 `json:"_seq_no,omitempty"` + PrimaryTerm int64 `json:"_primary_term,omitempty"` + Status int `json:"status,omitempty"` + ForcedRefresh bool `json:"forced_refresh,omitempty"` + Error *ErrorDetails `json:"error,omitempty"` + GetResult *GetResult `json:"get,omitempty"` +} + +// Indexed returns all bulk request results of "index" actions. +func (r *BulkResponse) Indexed() []*BulkResponseItem { + return r.ByAction("index") +} + +// Created returns all bulk request results of "create" actions. +func (r *BulkResponse) Created() []*BulkResponseItem { + return r.ByAction("create") +} + +// Updated returns all bulk request results of "update" actions. +func (r *BulkResponse) Updated() []*BulkResponseItem { + return r.ByAction("update") +} + +// Deleted returns all bulk request results of "delete" actions. +func (r *BulkResponse) Deleted() []*BulkResponseItem { + return r.ByAction("delete") +} + +// ByAction returns all bulk request results of a certain action, +// e.g. "index" or "delete". +func (r *BulkResponse) ByAction(action string) []*BulkResponseItem { + if r.Items == nil { + return nil + } + var items []*BulkResponseItem + for _, item := range r.Items { + if result, found := item[action]; found { + items = append(items, result) + } + } + return items +} + +// ById returns all bulk request results of a given document id, +// regardless of the action ("index", "delete" etc.). +func (r *BulkResponse) ById(id string) []*BulkResponseItem { + if r.Items == nil { + return nil + } + var items []*BulkResponseItem + for _, item := range r.Items { + for _, result := range item { + if result.Id == id { + items = append(items, result) + } + } + } + return items +} + +// Failed returns those items of a bulk response that have errors, +// i.e. those that don't have a status code between 200 and 299. +func (r *BulkResponse) Failed() []*BulkResponseItem { + if r.Items == nil { + return nil + } + var errors []*BulkResponseItem + for _, item := range r.Items { + for _, result := range item { + if !(result.Status >= 200 && result.Status <= 299) { + errors = append(errors, result) + } + } + } + return errors +} + +// Succeeded returns those items of a bulk response that have no errors, +// i.e. those have a status code between 200 and 299. +func (r *BulkResponse) Succeeded() []*BulkResponseItem { + if r.Items == nil { + return nil + } + var succeeded []*BulkResponseItem + for _, item := range r.Items { + for _, result := range item { + if result.Status >= 200 && result.Status <= 299 { + succeeded = append(succeeded, result) + } + } + } + return succeeded +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_delete_request.go b/vendor/github.com/olivere/elastic/v7/bulk_delete_request.go new file mode 100644 index 0000000000..55b2cbcb00 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_delete_request.go @@ -0,0 +1,186 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +//go:generate easyjson bulk_delete_request.go + +import ( + "encoding/json" + "fmt" + "strings" +) + +// -- Bulk delete request -- + +// BulkDeleteRequest is a request to remove a document from Elasticsearch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for details. +type BulkDeleteRequest struct { + BulkableRequest + index string + typ string + id string + parent string + routing string + version int64 // default is MATCH_ANY + versionType string // default is "internal" + ifSeqNo *int64 + ifPrimaryTerm *int64 + + source []string + + useEasyJSON bool +} + +//easyjson:json +type bulkDeleteRequestCommand map[string]bulkDeleteRequestCommandOp + +//easyjson:json +type bulkDeleteRequestCommandOp struct { + Index string `json:"_index,omitempty"` + Type string `json:"_type,omitempty"` + Id string `json:"_id,omitempty"` + Parent string `json:"parent,omitempty"` + Routing string `json:"routing,omitempty"` + Version int64 `json:"version,omitempty"` + VersionType string `json:"version_type,omitempty"` + IfSeqNo *int64 `json:"if_seq_no,omitempty"` + IfPrimaryTerm *int64 `json:"if_primary_term,omitempty"` +} + +// NewBulkDeleteRequest returns a new BulkDeleteRequest. +func NewBulkDeleteRequest() *BulkDeleteRequest { + return &BulkDeleteRequest{} +} + +// UseEasyJSON is an experimental setting that enables serialization +// with github.com/mailru/easyjson, which should in faster serialization +// time and less allocations, but removed compatibility with encoding/json, +// usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations +// for details. This setting is disabled by default. +func (r *BulkDeleteRequest) UseEasyJSON(enable bool) *BulkDeleteRequest { + r.useEasyJSON = enable + return r +} + +// Index specifies the Elasticsearch index to use for this delete request. +// If unspecified, the index set on the BulkService will be used. +func (r *BulkDeleteRequest) Index(index string) *BulkDeleteRequest { + r.index = index + r.source = nil + return r +} + +// Type specifies the Elasticsearch type to use for this delete request. +// If unspecified, the type set on the BulkService will be used. +func (r *BulkDeleteRequest) Type(typ string) *BulkDeleteRequest { + r.typ = typ + r.source = nil + return r +} + +// Id specifies the identifier of the document to delete. +func (r *BulkDeleteRequest) Id(id string) *BulkDeleteRequest { + r.id = id + r.source = nil + return r +} + +// Parent specifies the parent of the request, which is used in parent/child +// mappings. +func (r *BulkDeleteRequest) Parent(parent string) *BulkDeleteRequest { + r.parent = parent + r.source = nil + return r +} + +// Routing specifies a routing value for the request. +func (r *BulkDeleteRequest) Routing(routing string) *BulkDeleteRequest { + r.routing = routing + r.source = nil + return r +} + +// Version indicates the version to be deleted as part of an optimistic +// concurrency model. +func (r *BulkDeleteRequest) Version(version int64) *BulkDeleteRequest { + r.version = version + r.source = nil + return r +} + +// VersionType can be "internal" (default), "external", "external_gte", +// or "external_gt". +func (r *BulkDeleteRequest) VersionType(versionType string) *BulkDeleteRequest { + r.versionType = versionType + r.source = nil + return r +} + +// IfSeqNo indicates to only perform the delete operation if the last +// operation that has changed the document has the specified sequence number. +func (r *BulkDeleteRequest) IfSeqNo(ifSeqNo int64) *BulkDeleteRequest { + r.ifSeqNo = &ifSeqNo + return r +} + +// IfPrimaryTerm indicates to only perform the delete operation if the +// last operation that has changed the document has the specified primary term. +func (r *BulkDeleteRequest) IfPrimaryTerm(ifPrimaryTerm int64) *BulkDeleteRequest { + r.ifPrimaryTerm = &ifPrimaryTerm + return r +} + +// String returns the on-wire representation of the delete request, +// concatenated as a single string. +func (r *BulkDeleteRequest) String() string { + lines, err := r.Source() + if err != nil { + return fmt.Sprintf("error: %v", err) + } + return strings.Join(lines, "\n") +} + +// Source returns the on-wire representation of the delete request, +// split into an action-and-meta-data line and an (optional) source line. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for details. +func (r *BulkDeleteRequest) Source() ([]string, error) { + if r.source != nil { + return r.source, nil + } + command := bulkDeleteRequestCommand{ + "delete": bulkDeleteRequestCommandOp{ + Index: r.index, + Type: r.typ, + Id: r.id, + Routing: r.routing, + Parent: r.parent, + Version: r.version, + VersionType: r.versionType, + IfSeqNo: r.ifSeqNo, + IfPrimaryTerm: r.ifPrimaryTerm, + }, + } + + var err error + var body []byte + if r.useEasyJSON { + // easyjson + body, err = command.MarshalJSON() + } else { + // encoding/json + body, err = json.Marshal(command) + } + if err != nil { + return nil, err + } + + lines := []string{string(body)} + r.source = lines + + return lines, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_delete_request_easyjson.go b/vendor/github.com/olivere/elastic/v7/bulk_delete_request_easyjson.go new file mode 100644 index 0000000000..55ccd883c2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_delete_request_easyjson.go @@ -0,0 +1,266 @@ +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. + +package elastic + +import ( + json "encoding/json" + easyjson "github.com/mailru/easyjson" + jlexer "github.com/mailru/easyjson/jlexer" + jwriter "github.com/mailru/easyjson/jwriter" +) + +// suppress unused package warning +var ( + _ *json.RawMessage + _ *jlexer.Lexer + _ *jwriter.Writer + _ easyjson.Marshaler +) + +func easyjson8092efb6DecodeGithubComOlivereElasticV7(in *jlexer.Lexer, out *bulkDeleteRequestCommandOp) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "_index": + out.Index = string(in.String()) + case "_type": + out.Type = string(in.String()) + case "_id": + out.Id = string(in.String()) + case "parent": + out.Parent = string(in.String()) + case "routing": + out.Routing = string(in.String()) + case "version": + out.Version = int64(in.Int64()) + case "version_type": + out.VersionType = string(in.String()) + case "if_seq_no": + if in.IsNull() { + in.Skip() + out.IfSeqNo = nil + } else { + if out.IfSeqNo == nil { + out.IfSeqNo = new(int64) + } + *out.IfSeqNo = int64(in.Int64()) + } + case "if_primary_term": + if in.IsNull() { + in.Skip() + out.IfPrimaryTerm = nil + } else { + if out.IfPrimaryTerm == nil { + out.IfPrimaryTerm = new(int64) + } + *out.IfPrimaryTerm = int64(in.Int64()) + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson8092efb6EncodeGithubComOlivereElasticV7(out *jwriter.Writer, in bulkDeleteRequestCommandOp) { + out.RawByte('{') + first := true + _ = first + if in.Index != "" { + const prefix string = ",\"_index\":" + first = false + out.RawString(prefix[1:]) + out.String(string(in.Index)) + } + if in.Type != "" { + const prefix string = ",\"_type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Type)) + } + if in.Id != "" { + const prefix string = ",\"_id\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Id)) + } + if in.Parent != "" { + const prefix string = ",\"parent\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Parent)) + } + if in.Routing != "" { + const prefix string = ",\"routing\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Routing)) + } + if in.Version != 0 { + const prefix string = ",\"version\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Version)) + } + if in.VersionType != "" { + const prefix string = ",\"version_type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.VersionType)) + } + if in.IfSeqNo != nil { + const prefix string = ",\"if_seq_no\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.IfSeqNo)) + } + if in.IfPrimaryTerm != nil { + const prefix string = ",\"if_primary_term\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.IfPrimaryTerm)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkDeleteRequestCommandOp) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson8092efb6EncodeGithubComOlivereElasticV7(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkDeleteRequestCommandOp) MarshalEasyJSON(w *jwriter.Writer) { + easyjson8092efb6EncodeGithubComOlivereElasticV7(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkDeleteRequestCommandOp) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson8092efb6DecodeGithubComOlivereElasticV7(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkDeleteRequestCommandOp) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson8092efb6DecodeGithubComOlivereElasticV7(l, v) +} +func easyjson8092efb6DecodeGithubComOlivereElasticV71(in *jlexer.Lexer, out *bulkDeleteRequestCommand) { + isTopLevel := in.IsStart() + if in.IsNull() { + in.Skip() + } else { + in.Delim('{') + if !in.IsDelim('}') { + *out = make(bulkDeleteRequestCommand) + } else { + *out = nil + } + for !in.IsDelim('}') { + key := string(in.String()) + in.WantColon() + var v1 bulkDeleteRequestCommandOp + (v1).UnmarshalEasyJSON(in) + (*out)[key] = v1 + in.WantComma() + } + in.Delim('}') + } + if isTopLevel { + in.Consumed() + } +} +func easyjson8092efb6EncodeGithubComOlivereElasticV71(out *jwriter.Writer, in bulkDeleteRequestCommand) { + if in == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { + out.RawString(`null`) + } else { + out.RawByte('{') + v2First := true + for v2Name, v2Value := range in { + if v2First { + v2First = false + } else { + out.RawByte(',') + } + out.String(string(v2Name)) + out.RawByte(':') + (v2Value).MarshalEasyJSON(out) + } + out.RawByte('}') + } +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkDeleteRequestCommand) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson8092efb6EncodeGithubComOlivereElasticV71(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkDeleteRequestCommand) MarshalEasyJSON(w *jwriter.Writer) { + easyjson8092efb6EncodeGithubComOlivereElasticV71(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkDeleteRequestCommand) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson8092efb6DecodeGithubComOlivereElasticV71(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkDeleteRequestCommand) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson8092efb6DecodeGithubComOlivereElasticV71(l, v) +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_index_request.go b/vendor/github.com/olivere/elastic/v7/bulk_index_request.go new file mode 100644 index 0000000000..fcbc717330 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_index_request.go @@ -0,0 +1,260 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +//go:generate easyjson bulk_index_request.go + +import ( + "encoding/json" + "fmt" + "strings" +) + +// BulkIndexRequest is a request to add a document to Elasticsearch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for details. +type BulkIndexRequest struct { + BulkableRequest + index string + typ string + id string + opType string + routing string + parent string + version *int64 // default is MATCH_ANY + versionType string // default is "internal" + doc interface{} + pipeline string + retryOnConflict *int + ifSeqNo *int64 + ifPrimaryTerm *int64 + + source []string + + useEasyJSON bool +} + +//easyjson:json +type bulkIndexRequestCommand map[string]bulkIndexRequestCommandOp + +//easyjson:json +type bulkIndexRequestCommandOp struct { + Index string `json:"_index,omitempty"` + Id string `json:"_id,omitempty"` + Type string `json:"_type,omitempty"` + Parent string `json:"parent,omitempty"` + // RetryOnConflict is "_retry_on_conflict" for 6.0 and "retry_on_conflict" for 6.1+. + RetryOnConflict *int `json:"retry_on_conflict,omitempty"` + Routing string `json:"routing,omitempty"` + Version *int64 `json:"version,omitempty"` + VersionType string `json:"version_type,omitempty"` + Pipeline string `json:"pipeline,omitempty"` + IfSeqNo *int64 `json:"if_seq_no,omitempty"` + IfPrimaryTerm *int64 `json:"if_primary_term,omitempty"` +} + +// NewBulkIndexRequest returns a new BulkIndexRequest. +// The operation type is "index" by default. +func NewBulkIndexRequest() *BulkIndexRequest { + return &BulkIndexRequest{ + opType: "index", + } +} + +// UseEasyJSON is an experimental setting that enables serialization +// with github.com/mailru/easyjson, which should in faster serialization +// time and less allocations, but removed compatibility with encoding/json, +// usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations +// for details. This setting is disabled by default. +func (r *BulkIndexRequest) UseEasyJSON(enable bool) *BulkIndexRequest { + r.useEasyJSON = enable + return r +} + +// Index specifies the Elasticsearch index to use for this index request. +// If unspecified, the index set on the BulkService will be used. +func (r *BulkIndexRequest) Index(index string) *BulkIndexRequest { + r.index = index + r.source = nil + return r +} + +// Type specifies the Elasticsearch type to use for this index request. +// If unspecified, the type set on the BulkService will be used. +func (r *BulkIndexRequest) Type(typ string) *BulkIndexRequest { + r.typ = typ + r.source = nil + return r +} + +// Id specifies the identifier of the document to index. +func (r *BulkIndexRequest) Id(id string) *BulkIndexRequest { + r.id = id + r.source = nil + return r +} + +// OpType specifies if this request should follow create-only or upsert +// behavior. This follows the OpType of the standard document index API. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-index_.html#operation-type +// for details. +func (r *BulkIndexRequest) OpType(opType string) *BulkIndexRequest { + r.opType = opType + r.source = nil + return r +} + +// Routing specifies a routing value for the request. +func (r *BulkIndexRequest) Routing(routing string) *BulkIndexRequest { + r.routing = routing + r.source = nil + return r +} + +// Parent specifies the identifier of the parent document (if available). +func (r *BulkIndexRequest) Parent(parent string) *BulkIndexRequest { + r.parent = parent + r.source = nil + return r +} + +// Version indicates the version of the document as part of an optimistic +// concurrency model. +func (r *BulkIndexRequest) Version(version int64) *BulkIndexRequest { + v := version + r.version = &v + r.source = nil + return r +} + +// VersionType specifies how versions are created. It can be e.g. internal, +// external, external_gte, or force. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-index_.html#index-versioning +// for details. +func (r *BulkIndexRequest) VersionType(versionType string) *BulkIndexRequest { + r.versionType = versionType + r.source = nil + return r +} + +// Doc specifies the document to index. +func (r *BulkIndexRequest) Doc(doc interface{}) *BulkIndexRequest { + r.doc = doc + r.source = nil + return r +} + +// RetryOnConflict specifies how often to retry in case of a version conflict. +func (r *BulkIndexRequest) RetryOnConflict(retryOnConflict int) *BulkIndexRequest { + r.retryOnConflict = &retryOnConflict + r.source = nil + return r +} + +// Pipeline to use while processing the request. +func (r *BulkIndexRequest) Pipeline(pipeline string) *BulkIndexRequest { + r.pipeline = pipeline + r.source = nil + return r +} + +// IfSeqNo indicates to only perform the index operation if the last +// operation that has changed the document has the specified sequence number. +func (r *BulkIndexRequest) IfSeqNo(ifSeqNo int64) *BulkIndexRequest { + r.ifSeqNo = &ifSeqNo + return r +} + +// IfPrimaryTerm indicates to only perform the index operation if the +// last operation that has changed the document has the specified primary term. +func (r *BulkIndexRequest) IfPrimaryTerm(ifPrimaryTerm int64) *BulkIndexRequest { + r.ifPrimaryTerm = &ifPrimaryTerm + return r +} + +// String returns the on-wire representation of the index request, +// concatenated as a single string. +func (r *BulkIndexRequest) String() string { + lines, err := r.Source() + if err != nil { + return fmt.Sprintf("error: %v", err) + } + return strings.Join(lines, "\n") +} + +// Source returns the on-wire representation of the index request, +// split into an action-and-meta-data line and an (optional) source line. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for details. +func (r *BulkIndexRequest) Source() ([]string, error) { + // { "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } } + // { "field1" : "value1" } + + if r.source != nil { + return r.source, nil + } + + lines := make([]string, 2) + + // "index" ... + indexCommand := bulkIndexRequestCommandOp{ + Index: r.index, + Type: r.typ, + Id: r.id, + Routing: r.routing, + Parent: r.parent, + Version: r.version, + VersionType: r.versionType, + RetryOnConflict: r.retryOnConflict, + Pipeline: r.pipeline, + IfSeqNo: r.ifSeqNo, + IfPrimaryTerm: r.ifPrimaryTerm, + } + command := bulkIndexRequestCommand{ + r.opType: indexCommand, + } + + var err error + var body []byte + if r.useEasyJSON { + // easyjson + body, err = command.MarshalJSON() + } else { + // encoding/json + body, err = json.Marshal(command) + } + if err != nil { + return nil, err + } + + lines[0] = string(body) + + // "field1" ... + if r.doc != nil { + switch t := r.doc.(type) { + default: + body, err := json.Marshal(r.doc) + if err != nil { + return nil, err + } + lines[1] = string(body) + case json.RawMessage: + lines[1] = string(t) + case *json.RawMessage: + lines[1] = string(*t) + case string: + lines[1] = t + case *string: + lines[1] = *t + } + } else { + lines[1] = "{}" + } + + r.source = lines + return lines, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_index_request_easyjson.go b/vendor/github.com/olivere/elastic/v7/bulk_index_request_easyjson.go new file mode 100644 index 0000000000..68f3fb9693 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_index_request_easyjson.go @@ -0,0 +1,306 @@ +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. + +package elastic + +import ( + json "encoding/json" + easyjson "github.com/mailru/easyjson" + jlexer "github.com/mailru/easyjson/jlexer" + jwriter "github.com/mailru/easyjson/jwriter" +) + +// suppress unused package warning +var ( + _ *json.RawMessage + _ *jlexer.Lexer + _ *jwriter.Writer + _ easyjson.Marshaler +) + +func easyjson9de0fcbfDecodeGithubComOlivereElasticV7(in *jlexer.Lexer, out *bulkIndexRequestCommandOp) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "_index": + out.Index = string(in.String()) + case "_id": + out.Id = string(in.String()) + case "_type": + out.Type = string(in.String()) + case "parent": + out.Parent = string(in.String()) + case "retry_on_conflict": + if in.IsNull() { + in.Skip() + out.RetryOnConflict = nil + } else { + if out.RetryOnConflict == nil { + out.RetryOnConflict = new(int) + } + *out.RetryOnConflict = int(in.Int()) + } + case "routing": + out.Routing = string(in.String()) + case "version": + if in.IsNull() { + in.Skip() + out.Version = nil + } else { + if out.Version == nil { + out.Version = new(int64) + } + *out.Version = int64(in.Int64()) + } + case "version_type": + out.VersionType = string(in.String()) + case "pipeline": + out.Pipeline = string(in.String()) + case "if_seq_no": + if in.IsNull() { + in.Skip() + out.IfSeqNo = nil + } else { + if out.IfSeqNo == nil { + out.IfSeqNo = new(int64) + } + *out.IfSeqNo = int64(in.Int64()) + } + case "if_primary_term": + if in.IsNull() { + in.Skip() + out.IfPrimaryTerm = nil + } else { + if out.IfPrimaryTerm == nil { + out.IfPrimaryTerm = new(int64) + } + *out.IfPrimaryTerm = int64(in.Int64()) + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson9de0fcbfEncodeGithubComOlivereElasticV7(out *jwriter.Writer, in bulkIndexRequestCommandOp) { + out.RawByte('{') + first := true + _ = first + if in.Index != "" { + const prefix string = ",\"_index\":" + first = false + out.RawString(prefix[1:]) + out.String(string(in.Index)) + } + if in.Id != "" { + const prefix string = ",\"_id\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Id)) + } + if in.Type != "" { + const prefix string = ",\"_type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Type)) + } + if in.Parent != "" { + const prefix string = ",\"parent\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Parent)) + } + if in.RetryOnConflict != nil { + const prefix string = ",\"retry_on_conflict\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(*in.RetryOnConflict)) + } + if in.Routing != "" { + const prefix string = ",\"routing\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Routing)) + } + if in.Version != nil { + const prefix string = ",\"version\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.Version)) + } + if in.VersionType != "" { + const prefix string = ",\"version_type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.VersionType)) + } + if in.Pipeline != "" { + const prefix string = ",\"pipeline\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Pipeline)) + } + if in.IfSeqNo != nil { + const prefix string = ",\"if_seq_no\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.IfSeqNo)) + } + if in.IfPrimaryTerm != nil { + const prefix string = ",\"if_primary_term\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.IfPrimaryTerm)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkIndexRequestCommandOp) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson9de0fcbfEncodeGithubComOlivereElasticV7(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkIndexRequestCommandOp) MarshalEasyJSON(w *jwriter.Writer) { + easyjson9de0fcbfEncodeGithubComOlivereElasticV7(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkIndexRequestCommandOp) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson9de0fcbfDecodeGithubComOlivereElasticV7(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkIndexRequestCommandOp) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson9de0fcbfDecodeGithubComOlivereElasticV7(l, v) +} +func easyjson9de0fcbfDecodeGithubComOlivereElasticV71(in *jlexer.Lexer, out *bulkIndexRequestCommand) { + isTopLevel := in.IsStart() + if in.IsNull() { + in.Skip() + } else { + in.Delim('{') + if !in.IsDelim('}') { + *out = make(bulkIndexRequestCommand) + } else { + *out = nil + } + for !in.IsDelim('}') { + key := string(in.String()) + in.WantColon() + var v1 bulkIndexRequestCommandOp + (v1).UnmarshalEasyJSON(in) + (*out)[key] = v1 + in.WantComma() + } + in.Delim('}') + } + if isTopLevel { + in.Consumed() + } +} +func easyjson9de0fcbfEncodeGithubComOlivereElasticV71(out *jwriter.Writer, in bulkIndexRequestCommand) { + if in == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { + out.RawString(`null`) + } else { + out.RawByte('{') + v2First := true + for v2Name, v2Value := range in { + if v2First { + v2First = false + } else { + out.RawByte(',') + } + out.String(string(v2Name)) + out.RawByte(':') + (v2Value).MarshalEasyJSON(out) + } + out.RawByte('}') + } +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkIndexRequestCommand) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson9de0fcbfEncodeGithubComOlivereElasticV71(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkIndexRequestCommand) MarshalEasyJSON(w *jwriter.Writer) { + easyjson9de0fcbfEncodeGithubComOlivereElasticV71(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkIndexRequestCommand) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson9de0fcbfDecodeGithubComOlivereElasticV71(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkIndexRequestCommand) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson9de0fcbfDecodeGithubComOlivereElasticV71(l, v) +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_processor.go b/vendor/github.com/olivere/elastic/v7/bulk_processor.go new file mode 100644 index 0000000000..f2711f829d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_processor.go @@ -0,0 +1,656 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "errors" + "net" + "sync" + "sync/atomic" + "time" +) + +var ( + // ErrBulkItemRetry is returned in BulkProcessor from a worker when + // a response item needs to be retried. + ErrBulkItemRetry = errors.New("elastic: uncommitted bulk response items") + + defaultRetryItemStatusCodes = []int{408, 429, 503, 507} +) + +// BulkProcessorService allows to easily process bulk requests. It allows setting +// policies when to flush new bulk requests, e.g. based on a number of actions, +// on the size of the actions, and/or to flush periodically. It also allows +// to control the number of concurrent bulk requests allowed to be executed +// in parallel. +// +// BulkProcessorService, by default, commits either every 1000 requests or when the +// (estimated) size of the bulk requests exceeds 5 MB. However, it does not +// commit periodically. BulkProcessorService also does retry by default, using +// an exponential backoff algorithm. It also will automatically re-enqueue items +// returned with a status of 408, 429, 503 or 507. You can change this +// behavior with RetryItemStatusCodes. +// +// The caller is responsible for setting the index and type on every +// bulk request added to BulkProcessorService. +// +// BulkProcessorService takes ideas from the BulkProcessor of the +// Elasticsearch Java API as documented in +// https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-bulk-processor.html. +type BulkProcessorService struct { + c *Client + beforeFn BulkBeforeFunc + afterFn BulkAfterFunc + name string // name of processor + numWorkers int // # of workers (>= 1) + bulkActions int // # of requests after which to commit + bulkSize int // # of bytes after which to commit + flushInterval time.Duration // periodic flush interval + wantStats bool // indicates whether to gather statistics + backoff Backoff // a custom Backoff to use for errors + retryItemStatusCodes []int // array of status codes for bulk response line items that may be retried +} + +// NewBulkProcessorService creates a new BulkProcessorService. +func NewBulkProcessorService(client *Client) *BulkProcessorService { + return &BulkProcessorService{ + c: client, + numWorkers: 1, + bulkActions: 1000, + bulkSize: 5 << 20, // 5 MB + backoff: NewExponentialBackoff( + time.Duration(200)*time.Millisecond, + time.Duration(10000)*time.Millisecond, + ), + retryItemStatusCodes: defaultRetryItemStatusCodes, + } +} + +// BulkBeforeFunc defines the signature of callbacks that are executed +// before a commit to Elasticsearch. +type BulkBeforeFunc func(executionId int64, requests []BulkableRequest) + +// BulkAfterFunc defines the signature of callbacks that are executed +// after a commit to Elasticsearch. The err parameter signals an error. +type BulkAfterFunc func(executionId int64, requests []BulkableRequest, response *BulkResponse, err error) + +// Before specifies a function to be executed before bulk requests get committed +// to Elasticsearch. +func (s *BulkProcessorService) Before(fn BulkBeforeFunc) *BulkProcessorService { + s.beforeFn = fn + return s +} + +// After specifies a function to be executed when bulk requests have been +// committed to Elasticsearch. The After callback executes both when the +// commit was successful as well as on failures. +func (s *BulkProcessorService) After(fn BulkAfterFunc) *BulkProcessorService { + s.afterFn = fn + return s +} + +// Name is an optional name to identify this bulk processor. +func (s *BulkProcessorService) Name(name string) *BulkProcessorService { + s.name = name + return s +} + +// Workers is the number of concurrent workers allowed to be +// executed. Defaults to 1 and must be greater or equal to 1. +func (s *BulkProcessorService) Workers(num int) *BulkProcessorService { + s.numWorkers = num + return s +} + +// BulkActions specifies when to flush based on the number of actions +// currently added. Defaults to 1000 and can be set to -1 to be disabled. +func (s *BulkProcessorService) BulkActions(bulkActions int) *BulkProcessorService { + s.bulkActions = bulkActions + return s +} + +// BulkSize specifies when to flush based on the size (in bytes) of the actions +// currently added. Defaults to 5 MB and can be set to -1 to be disabled. +func (s *BulkProcessorService) BulkSize(bulkSize int) *BulkProcessorService { + s.bulkSize = bulkSize + return s +} + +// FlushInterval specifies when to flush at the end of the given interval. +// This is disabled by default. If you want the bulk processor to +// operate completely asynchronously, set both BulkActions and BulkSize to +// -1 and set the FlushInterval to a meaningful interval. +func (s *BulkProcessorService) FlushInterval(interval time.Duration) *BulkProcessorService { + s.flushInterval = interval + return s +} + +// Stats tells bulk processor to gather stats while running. +// Use Stats to return the stats. This is disabled by default. +func (s *BulkProcessorService) Stats(wantStats bool) *BulkProcessorService { + s.wantStats = wantStats + return s +} + +// Backoff sets the backoff strategy to use for errors. +func (s *BulkProcessorService) Backoff(backoff Backoff) *BulkProcessorService { + s.backoff = backoff + return s +} + +// RetryItemStatusCodes sets an array of status codes that indicate that a bulk +// response line item should be retried. +func (s *BulkProcessorService) RetryItemStatusCodes(retryItemStatusCodes ...int) *BulkProcessorService { + s.retryItemStatusCodes = retryItemStatusCodes + return s +} + +// Do creates a new BulkProcessor and starts it. +// Consider the BulkProcessor as a running instance that accepts bulk requests +// and commits them to Elasticsearch, spreading the work across one or more +// workers. +// +// You can interoperate with the BulkProcessor returned by Do, e.g. Start and +// Stop (or Close) it. +// +// Context is an optional context that is passed into the bulk request +// service calls. In contrast to other operations, this context is used in +// a long running process. You could use it to pass e.g. loggers, but you +// shouldn't use it for cancellation. +// +// Calling Do several times returns new BulkProcessors. You probably don't +// want to do this. BulkProcessorService implements just a builder pattern. +func (s *BulkProcessorService) Do(ctx context.Context) (*BulkProcessor, error) { + + retryItemStatusCodes := make(map[int]struct{}) + for _, code := range s.retryItemStatusCodes { + retryItemStatusCodes[code] = struct{}{} + } + + p := newBulkProcessor( + s.c, + s.beforeFn, + s.afterFn, + s.name, + s.numWorkers, + s.bulkActions, + s.bulkSize, + s.flushInterval, + s.wantStats, + s.backoff, + retryItemStatusCodes) + + err := p.Start(ctx) + if err != nil { + return nil, err + } + return p, nil +} + +// -- Bulk Processor Statistics -- + +// BulkProcessorStats contains various statistics of a bulk processor +// while it is running. Use the Stats func to return it while running. +type BulkProcessorStats struct { + Flushed int64 // number of times the flush interval has been invoked + Committed int64 // # of times workers committed bulk requests + Indexed int64 // # of requests indexed + Created int64 // # of requests that ES reported as creates (201) + Updated int64 // # of requests that ES reported as updates + Deleted int64 // # of requests that ES reported as deletes + Succeeded int64 // # of requests that ES reported as successful + Failed int64 // # of requests that ES reported as failed + + Workers []*BulkProcessorWorkerStats // stats for each worker +} + +// BulkProcessorWorkerStats represents per-worker statistics. +type BulkProcessorWorkerStats struct { + Queued int64 // # of requests queued in this worker + LastDuration time.Duration // duration of last commit +} + +// newBulkProcessorStats initializes and returns a BulkProcessorStats struct. +func newBulkProcessorStats(workers int) *BulkProcessorStats { + stats := &BulkProcessorStats{ + Workers: make([]*BulkProcessorWorkerStats, workers), + } + for i := 0; i < workers; i++ { + stats.Workers[i] = &BulkProcessorWorkerStats{} + } + return stats +} + +func (st *BulkProcessorStats) dup() *BulkProcessorStats { + dst := new(BulkProcessorStats) + dst.Flushed = st.Flushed + dst.Committed = st.Committed + dst.Indexed = st.Indexed + dst.Created = st.Created + dst.Updated = st.Updated + dst.Deleted = st.Deleted + dst.Succeeded = st.Succeeded + dst.Failed = st.Failed + for _, src := range st.Workers { + dst.Workers = append(dst.Workers, src.dup()) + } + return dst +} + +func (st *BulkProcessorWorkerStats) dup() *BulkProcessorWorkerStats { + dst := new(BulkProcessorWorkerStats) + dst.Queued = st.Queued + dst.LastDuration = st.LastDuration + return dst +} + +// -- Bulk Processor -- + +// BulkProcessor encapsulates a task that accepts bulk requests and +// orchestrates committing them to Elasticsearch via one or more workers. +// +// BulkProcessor is returned by setting up a BulkProcessorService and +// calling the Do method. +type BulkProcessor struct { + c *Client + beforeFn BulkBeforeFunc + afterFn BulkAfterFunc + name string + bulkActions int + bulkSize int + numWorkers int + executionId int64 + requestsC chan BulkableRequest + workerWg sync.WaitGroup + workers []*bulkWorker + flushInterval time.Duration + flusherStopC chan struct{} + wantStats bool + retryItemStatusCodes map[int]struct{} + backoff Backoff + + startedMu sync.Mutex // guards the following block + started bool + + statsMu sync.Mutex // guards the following block + stats *BulkProcessorStats + + stopReconnC chan struct{} // channel to signal stop reconnection attempts +} + +func newBulkProcessor( + client *Client, + beforeFn BulkBeforeFunc, + afterFn BulkAfterFunc, + name string, + numWorkers int, + bulkActions int, + bulkSize int, + flushInterval time.Duration, + wantStats bool, + backoff Backoff, + retryItemStatusCodes map[int]struct{}) *BulkProcessor { + return &BulkProcessor{ + c: client, + beforeFn: beforeFn, + afterFn: afterFn, + name: name, + numWorkers: numWorkers, + bulkActions: bulkActions, + bulkSize: bulkSize, + flushInterval: flushInterval, + wantStats: wantStats, + retryItemStatusCodes: retryItemStatusCodes, + backoff: backoff, + } +} + +// Start starts the bulk processor. If the processor is already started, +// nil is returned. +func (p *BulkProcessor) Start(ctx context.Context) error { + p.startedMu.Lock() + defer p.startedMu.Unlock() + + if p.started { + return nil + } + + // We must have at least one worker. + if p.numWorkers < 1 { + p.numWorkers = 1 + } + + p.requestsC = make(chan BulkableRequest) + p.executionId = 0 + p.stats = newBulkProcessorStats(p.numWorkers) + p.stopReconnC = make(chan struct{}) + + // Create and start up workers. + p.workers = make([]*bulkWorker, p.numWorkers) + for i := 0; i < p.numWorkers; i++ { + p.workerWg.Add(1) + p.workers[i] = newBulkWorker(p, i) + go p.workers[i].work(ctx) + } + + // Start the ticker for flush (if enabled) + if int64(p.flushInterval) > 0 { + p.flusherStopC = make(chan struct{}) + go p.flusher(p.flushInterval) + } + + p.started = true + + return nil +} + +// Stop is an alias for Close. +func (p *BulkProcessor) Stop() error { + return p.Close() +} + +// Close stops the bulk processor previously started with Do. +// If it is already stopped, this is a no-op and nil is returned. +// +// By implementing Close, BulkProcessor implements the io.Closer interface. +func (p *BulkProcessor) Close() error { + p.startedMu.Lock() + defer p.startedMu.Unlock() + + // Already stopped? Do nothing. + if !p.started { + return nil + } + + // Tell connection checkers to stop + if p.stopReconnC != nil { + close(p.stopReconnC) + p.stopReconnC = nil + } + + // Stop flusher (if enabled) + if p.flusherStopC != nil { + p.flusherStopC <- struct{}{} + <-p.flusherStopC + close(p.flusherStopC) + p.flusherStopC = nil + } + + // Stop all workers. + close(p.requestsC) + p.workerWg.Wait() + + p.started = false + + return nil +} + +// Stats returns the latest bulk processor statistics. +// Collecting stats must be enabled first by calling Stats(true) on +// the service that created this processor. +func (p *BulkProcessor) Stats() BulkProcessorStats { + p.statsMu.Lock() + defer p.statsMu.Unlock() + return *p.stats.dup() +} + +// Add adds a single request to commit by the BulkProcessorService. +// +// The caller is responsible for setting the index and type on the request. +func (p *BulkProcessor) Add(request BulkableRequest) { + p.requestsC <- request +} + +// Flush manually asks all workers to commit their outstanding requests. +// It returns only when all workers acknowledge completion. +func (p *BulkProcessor) Flush() error { + p.statsMu.Lock() + p.stats.Flushed++ + p.statsMu.Unlock() + + for _, w := range p.workers { + w.flushC <- struct{}{} + <-w.flushAckC // wait for completion + } + return nil +} + +// flusher is a single goroutine that periodically asks all workers to +// commit their outstanding bulk requests. It is only started if +// FlushInterval is greater than 0. +func (p *BulkProcessor) flusher(interval time.Duration) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: // Periodic flush + p.Flush() // TODO swallow errors here? + + case <-p.flusherStopC: + p.flusherStopC <- struct{}{} + return + } + } +} + +// -- Bulk Worker -- + +// bulkWorker encapsulates a single worker, running in a goroutine, +// receiving bulk requests and eventually committing them to Elasticsearch. +// It is strongly bound to a BulkProcessor. +type bulkWorker struct { + p *BulkProcessor + i int + bulkActions int + bulkSize int + service *BulkService + flushC chan struct{} + flushAckC chan struct{} +} + +// newBulkWorker creates a new bulkWorker instance. +func newBulkWorker(p *BulkProcessor, i int) *bulkWorker { + return &bulkWorker{ + p: p, + i: i, + bulkActions: p.bulkActions, + bulkSize: p.bulkSize, + service: NewBulkService(p.c), + flushC: make(chan struct{}), + flushAckC: make(chan struct{}), + } +} + +// work waits for bulk requests and manual flush calls on the respective +// channels and is invoked as a goroutine when the bulk processor is started. +func (w *bulkWorker) work(ctx context.Context) { + defer func() { + w.p.workerWg.Done() + close(w.flushAckC) + close(w.flushC) + }() + + var stop bool + for !stop { + var err error + select { + case req, open := <-w.p.requestsC: + if open { + // Received a new request + if _, err = req.Source(); err == nil { + w.service.Add(req) + if w.commitRequired() { + err = w.commit(ctx) + } + } + } else { + // Channel closed: Stop. + stop = true + if w.service.NumberOfActions() > 0 { + err = w.commit(ctx) + } + } + + case <-w.flushC: + // Commit outstanding requests + if w.service.NumberOfActions() > 0 { + err = w.commit(ctx) + } + w.flushAckC <- struct{}{} + } + if err != nil { + w.p.c.errorf("elastic: bulk processor %q was unable to perform work: %v", w.p.name, err) + if !stop { + waitForActive := func() { + // Add back pressure to prevent Add calls from filling up the request queue + ready := make(chan struct{}) + go w.waitForActiveConnection(ready) + <-ready + } + if _, ok := err.(net.Error); ok { + waitForActive() + } else if IsConnErr(err) { + waitForActive() + } + } + } + } +} + +// commit commits the bulk requests in the given service, +// invoking callbacks as specified. +func (w *bulkWorker) commit(ctx context.Context) error { + var res *BulkResponse + + // commitFunc will commit bulk requests and, on failure, be retried + // via exponential backoff + commitFunc := func() error { + var err error + // Save requests because they will be reset in service.Do + reqs := w.service.requests + res, err = w.service.Do(ctx) + if err == nil { + // Overall bulk request was OK. But each bulk response item also has a status + if w.p.retryItemStatusCodes != nil && len(w.p.retryItemStatusCodes) > 0 { + // Check res.Items since some might be soft failures + if res.Items != nil && res.Errors { + // res.Items will be 1 to 1 with reqs in same order + for i, item := range res.Items { + for _, result := range item { + if _, found := w.p.retryItemStatusCodes[result.Status]; found { + w.service.Add(reqs[i]) + if err == nil { + err = ErrBulkItemRetry + } + } + } + } + } + } + } + return err + } + // notifyFunc will be called if retry fails + notifyFunc := func(err error) { + w.p.c.errorf("elastic: bulk processor %q failed but may retry: %v", w.p.name, err) + } + + id := atomic.AddInt64(&w.p.executionId, 1) + + // Update # documents in queue before eventual retries + w.p.statsMu.Lock() + if w.p.wantStats { + w.p.stats.Workers[w.i].Queued = int64(len(w.service.requests)) + } + w.p.statsMu.Unlock() + + // Save requests because they will be reset in commitFunc + reqs := w.service.requests + + // Invoke before callback + if w.p.beforeFn != nil { + w.p.beforeFn(id, reqs) + } + + // Commit bulk requests + err := RetryNotify(commitFunc, w.p.backoff, notifyFunc) + w.updateStats(res) + if err != nil { + w.p.c.errorf("elastic: bulk processor %q failed: %v", w.p.name, err) + } + + // Invoke after callback + if w.p.afterFn != nil { + w.p.afterFn(id, reqs, res, err) + } + + return err +} + +func (w *bulkWorker) waitForActiveConnection(ready chan<- struct{}) { + defer close(ready) + + t := time.NewTicker(5 * time.Second) + defer t.Stop() + + client := w.p.c + stopReconnC := w.p.stopReconnC + w.p.c.errorf("elastic: bulk processor %q is waiting for an active connection", w.p.name) + + // loop until a health check finds at least 1 active connection or the reconnection channel is closed + for { + select { + case _, ok := <-stopReconnC: + if !ok { + w.p.c.errorf("elastic: bulk processor %q active connection check interrupted", w.p.name) + return + } + case <-t.C: + client.healthcheck(context.Background(), 3*time.Second, true) + if client.mustActiveConn() == nil { + // found an active connection + // exit and signal done to the WaitGroup + return + } + } + } +} + +func (w *bulkWorker) updateStats(res *BulkResponse) { + // Update stats + if res != nil { + w.p.statsMu.Lock() + if w.p.wantStats { + w.p.stats.Committed++ + if res != nil { + w.p.stats.Indexed += int64(len(res.Indexed())) + w.p.stats.Created += int64(len(res.Created())) + w.p.stats.Updated += int64(len(res.Updated())) + w.p.stats.Deleted += int64(len(res.Deleted())) + w.p.stats.Succeeded += int64(len(res.Succeeded())) + w.p.stats.Failed += int64(len(res.Failed())) + } + w.p.stats.Workers[w.i].Queued = int64(len(w.service.requests)) + w.p.stats.Workers[w.i].LastDuration = time.Duration(int64(res.Took)) * time.Millisecond + } + w.p.statsMu.Unlock() + } +} + +// commitRequired returns true if the service has to commit its +// bulk requests. This can be either because the number of actions +// or the estimated size in bytes is larger than specified in the +// BulkProcessorService. +func (w *bulkWorker) commitRequired() bool { + if w.bulkActions >= 0 && w.service.NumberOfActions() >= w.bulkActions { + return true + } + if w.bulkSize >= 0 && w.service.EstimatedSizeInBytes() >= int64(w.bulkSize) { + return true + } + return false +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_request.go b/vendor/github.com/olivere/elastic/v7/bulk_request.go new file mode 100644 index 0000000000..ce3bf0768b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_request.go @@ -0,0 +1,17 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" +) + +// -- Bulkable request (index/update/delete) -- + +// BulkableRequest is a generic interface to bulkable requests. +type BulkableRequest interface { + fmt.Stringer + Source() ([]string, error) +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_update_request.go b/vendor/github.com/olivere/elastic/v7/bulk_update_request.go new file mode 100644 index 0000000000..a0ddf18051 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_update_request.go @@ -0,0 +1,334 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +//go:generate easyjson bulk_update_request.go + +import ( + "encoding/json" + "fmt" + "strings" +) + +// BulkUpdateRequest is a request to update a document in Elasticsearch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for details. +type BulkUpdateRequest struct { + BulkableRequest + index string + typ string + id string + + routing string + parent string + script *Script + scriptedUpsert *bool + version int64 // default is MATCH_ANY + versionType string // default is "internal" + retryOnConflict *int + upsert interface{} + docAsUpsert *bool + detectNoop *bool + doc interface{} + returnSource *bool + ifSeqNo *int64 + ifPrimaryTerm *int64 + + source []string + + useEasyJSON bool +} + +//easyjson:json +type bulkUpdateRequestCommand map[string]bulkUpdateRequestCommandOp + +//easyjson:json +type bulkUpdateRequestCommandOp struct { + Index string `json:"_index,omitempty"` + Type string `json:"_type,omitempty"` + Id string `json:"_id,omitempty"` + Parent string `json:"parent,omitempty"` + // RetryOnConflict is "_retry_on_conflict" for 6.0 and "retry_on_conflict" for 6.1+. + RetryOnConflict *int `json:"retry_on_conflict,omitempty"` + Routing string `json:"routing,omitempty"` + Version int64 `json:"version,omitempty"` + VersionType string `json:"version_type,omitempty"` + IfSeqNo *int64 `json:"if_seq_no,omitempty"` + IfPrimaryTerm *int64 `json:"if_primary_term,omitempty"` +} + +//easyjson:json +type bulkUpdateRequestCommandData struct { + DetectNoop *bool `json:"detect_noop,omitempty"` + Doc interface{} `json:"doc,omitempty"` + DocAsUpsert *bool `json:"doc_as_upsert,omitempty"` + Script interface{} `json:"script,omitempty"` + ScriptedUpsert *bool `json:"scripted_upsert,omitempty"` + Upsert interface{} `json:"upsert,omitempty"` + Source *bool `json:"_source,omitempty"` +} + +// NewBulkUpdateRequest returns a new BulkUpdateRequest. +func NewBulkUpdateRequest() *BulkUpdateRequest { + return &BulkUpdateRequest{} +} + +// UseEasyJSON is an experimental setting that enables serialization +// with github.com/mailru/easyjson, which should in faster serialization +// time and less allocations, but removed compatibility with encoding/json, +// usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations +// for details. This setting is disabled by default. +func (r *BulkUpdateRequest) UseEasyJSON(enable bool) *BulkUpdateRequest { + r.useEasyJSON = enable + return r +} + +// Index specifies the Elasticsearch index to use for this update request. +// If unspecified, the index set on the BulkService will be used. +func (r *BulkUpdateRequest) Index(index string) *BulkUpdateRequest { + r.index = index + r.source = nil + return r +} + +// Type specifies the Elasticsearch type to use for this update request. +// If unspecified, the type set on the BulkService will be used. +func (r *BulkUpdateRequest) Type(typ string) *BulkUpdateRequest { + r.typ = typ + r.source = nil + return r +} + +// Id specifies the identifier of the document to update. +func (r *BulkUpdateRequest) Id(id string) *BulkUpdateRequest { + r.id = id + r.source = nil + return r +} + +// Routing specifies a routing value for the request. +func (r *BulkUpdateRequest) Routing(routing string) *BulkUpdateRequest { + r.routing = routing + r.source = nil + return r +} + +// Parent specifies the identifier of the parent document (if available). +func (r *BulkUpdateRequest) Parent(parent string) *BulkUpdateRequest { + r.parent = parent + r.source = nil + return r +} + +// Script specifies an update script. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html#bulk-update +// and https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html +// for details. +func (r *BulkUpdateRequest) Script(script *Script) *BulkUpdateRequest { + r.script = script + r.source = nil + return r +} + +// ScripedUpsert specifies if your script will run regardless of +// whether the document exists or not. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-update.html#_literal_scripted_upsert_literal +func (r *BulkUpdateRequest) ScriptedUpsert(upsert bool) *BulkUpdateRequest { + r.scriptedUpsert = &upsert + r.source = nil + return r +} + +// RetryOnConflict specifies how often to retry in case of a version conflict. +func (r *BulkUpdateRequest) RetryOnConflict(retryOnConflict int) *BulkUpdateRequest { + r.retryOnConflict = &retryOnConflict + r.source = nil + return r +} + +// Version indicates the version of the document as part of an optimistic +// concurrency model. +func (r *BulkUpdateRequest) Version(version int64) *BulkUpdateRequest { + r.version = version + r.source = nil + return r +} + +// VersionType can be "internal" (default), "external", "external_gte", +// or "external_gt". +func (r *BulkUpdateRequest) VersionType(versionType string) *BulkUpdateRequest { + r.versionType = versionType + r.source = nil + return r +} + +// IfSeqNo indicates to only perform the index operation if the last +// operation that has changed the document has the specified sequence number. +func (r *BulkUpdateRequest) IfSeqNo(ifSeqNo int64) *BulkUpdateRequest { + r.ifSeqNo = &ifSeqNo + return r +} + +// IfPrimaryTerm indicates to only perform the index operation if the +// last operation that has changed the document has the specified primary term. +func (r *BulkUpdateRequest) IfPrimaryTerm(ifPrimaryTerm int64) *BulkUpdateRequest { + r.ifPrimaryTerm = &ifPrimaryTerm + return r +} + +// Doc specifies the updated document. +func (r *BulkUpdateRequest) Doc(doc interface{}) *BulkUpdateRequest { + r.doc = doc + r.source = nil + return r +} + +// DocAsUpsert indicates whether the contents of Doc should be used as +// the Upsert value. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-update.html#_literal_doc_as_upsert_literal +// for details. +func (r *BulkUpdateRequest) DocAsUpsert(docAsUpsert bool) *BulkUpdateRequest { + r.docAsUpsert = &docAsUpsert + r.source = nil + return r +} + +// DetectNoop specifies whether changes that don't affect the document +// should be ignored (true) or unignored (false). This is enabled by default +// in Elasticsearch. +func (r *BulkUpdateRequest) DetectNoop(detectNoop bool) *BulkUpdateRequest { + r.detectNoop = &detectNoop + r.source = nil + return r +} + +// Upsert specifies the document to use for upserts. It will be used for +// create if the original document does not exist. +func (r *BulkUpdateRequest) Upsert(doc interface{}) *BulkUpdateRequest { + r.upsert = doc + r.source = nil + return r +} + +// ReturnSource specifies whether Elasticsearch should return the source +// after the update. In the request, this responds to the `_source` field. +// It is false by default. +func (r *BulkUpdateRequest) ReturnSource(source bool) *BulkUpdateRequest { + r.returnSource = &source + r.source = nil + return r +} + +// String returns the on-wire representation of the update request, +// concatenated as a single string. +func (r *BulkUpdateRequest) String() string { + lines, err := r.Source() + if err != nil { + return fmt.Sprintf("error: %v", err) + } + return strings.Join(lines, "\n") +} + +// Source returns the on-wire representation of the update request, +// split into an action-and-meta-data line and an (optional) source line. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-bulk.html +// for details. +func (r *BulkUpdateRequest) Source() ([]string, error) { + // { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } } + // { "doc" : { "field1" : "value1", ... } } + // or + // { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } } + // { "script" : { ... } } + + if r.source != nil { + return r.source, nil + } + + lines := make([]string, 2) + + // "update" ... + updateCommand := bulkUpdateRequestCommandOp{ + Index: r.index, + Type: r.typ, + Id: r.id, + Routing: r.routing, + Parent: r.parent, + Version: r.version, + VersionType: r.versionType, + RetryOnConflict: r.retryOnConflict, + IfSeqNo: r.ifSeqNo, + IfPrimaryTerm: r.ifPrimaryTerm, + } + command := bulkUpdateRequestCommand{ + "update": updateCommand, + } + + var err error + var body []byte + if r.useEasyJSON { + // easyjson + body, err = command.MarshalJSON() + } else { + // encoding/json + body, err = json.Marshal(command) + } + if err != nil { + return nil, err + } + + lines[0] = string(body) + + // 2nd line: {"doc" : { ... }} or {"script": {...}} + var doc interface{} + if r.doc != nil { + // Automatically serialize strings as raw JSON + switch t := r.doc.(type) { + default: + doc = r.doc + case string: + if len(t) > 0 { + doc = json.RawMessage(t) + } + case *string: + if t != nil && len(*t) > 0 { + doc = json.RawMessage(*t) + } + } + } + data := bulkUpdateRequestCommandData{ + DocAsUpsert: r.docAsUpsert, + DetectNoop: r.detectNoop, + Upsert: r.upsert, + ScriptedUpsert: r.scriptedUpsert, + Doc: doc, + Source: r.returnSource, + } + if r.script != nil { + script, err := r.script.Source() + if err != nil { + return nil, err + } + data.Script = script + } + + if r.useEasyJSON { + // easyjson + body, err = data.MarshalJSON() + } else { + // encoding/json + body, err = json.Marshal(data) + } + if err != nil { + return nil, err + } + + lines[1] = string(body) + + r.source = lines + return lines, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/bulk_update_request_easyjson.go b/vendor/github.com/olivere/elastic/v7/bulk_update_request_easyjson.go new file mode 100644 index 0000000000..5ca7c1974f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/bulk_update_request_easyjson.go @@ -0,0 +1,493 @@ +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. + +package elastic + +import ( + json "encoding/json" + easyjson "github.com/mailru/easyjson" + jlexer "github.com/mailru/easyjson/jlexer" + jwriter "github.com/mailru/easyjson/jwriter" +) + +// suppress unused package warning +var ( + _ *json.RawMessage + _ *jlexer.Lexer + _ *jwriter.Writer + _ easyjson.Marshaler +) + +func easyjson1ed00e60DecodeGithubComOlivereElasticV7(in *jlexer.Lexer, out *bulkUpdateRequestCommandOp) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "_index": + out.Index = string(in.String()) + case "_type": + out.Type = string(in.String()) + case "_id": + out.Id = string(in.String()) + case "parent": + out.Parent = string(in.String()) + case "retry_on_conflict": + if in.IsNull() { + in.Skip() + out.RetryOnConflict = nil + } else { + if out.RetryOnConflict == nil { + out.RetryOnConflict = new(int) + } + *out.RetryOnConflict = int(in.Int()) + } + case "routing": + out.Routing = string(in.String()) + case "version": + out.Version = int64(in.Int64()) + case "version_type": + out.VersionType = string(in.String()) + case "if_seq_no": + if in.IsNull() { + in.Skip() + out.IfSeqNo = nil + } else { + if out.IfSeqNo == nil { + out.IfSeqNo = new(int64) + } + *out.IfSeqNo = int64(in.Int64()) + } + case "if_primary_term": + if in.IsNull() { + in.Skip() + out.IfPrimaryTerm = nil + } else { + if out.IfPrimaryTerm == nil { + out.IfPrimaryTerm = new(int64) + } + *out.IfPrimaryTerm = int64(in.Int64()) + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson1ed00e60EncodeGithubComOlivereElasticV7(out *jwriter.Writer, in bulkUpdateRequestCommandOp) { + out.RawByte('{') + first := true + _ = first + if in.Index != "" { + const prefix string = ",\"_index\":" + first = false + out.RawString(prefix[1:]) + out.String(string(in.Index)) + } + if in.Type != "" { + const prefix string = ",\"_type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Type)) + } + if in.Id != "" { + const prefix string = ",\"_id\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Id)) + } + if in.Parent != "" { + const prefix string = ",\"parent\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Parent)) + } + if in.RetryOnConflict != nil { + const prefix string = ",\"retry_on_conflict\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(*in.RetryOnConflict)) + } + if in.Routing != "" { + const prefix string = ",\"routing\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Routing)) + } + if in.Version != 0 { + const prefix string = ",\"version\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Version)) + } + if in.VersionType != "" { + const prefix string = ",\"version_type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.VersionType)) + } + if in.IfSeqNo != nil { + const prefix string = ",\"if_seq_no\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.IfSeqNo)) + } + if in.IfPrimaryTerm != nil { + const prefix string = ",\"if_primary_term\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(*in.IfPrimaryTerm)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkUpdateRequestCommandOp) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson1ed00e60EncodeGithubComOlivereElasticV7(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkUpdateRequestCommandOp) MarshalEasyJSON(w *jwriter.Writer) { + easyjson1ed00e60EncodeGithubComOlivereElasticV7(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkUpdateRequestCommandOp) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson1ed00e60DecodeGithubComOlivereElasticV7(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkUpdateRequestCommandOp) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson1ed00e60DecodeGithubComOlivereElasticV7(l, v) +} +func easyjson1ed00e60DecodeGithubComOlivereElasticV71(in *jlexer.Lexer, out *bulkUpdateRequestCommandData) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "detect_noop": + if in.IsNull() { + in.Skip() + out.DetectNoop = nil + } else { + if out.DetectNoop == nil { + out.DetectNoop = new(bool) + } + *out.DetectNoop = bool(in.Bool()) + } + case "doc": + if m, ok := out.Doc.(easyjson.Unmarshaler); ok { + m.UnmarshalEasyJSON(in) + } else if m, ok := out.Doc.(json.Unmarshaler); ok { + _ = m.UnmarshalJSON(in.Raw()) + } else { + out.Doc = in.Interface() + } + case "doc_as_upsert": + if in.IsNull() { + in.Skip() + out.DocAsUpsert = nil + } else { + if out.DocAsUpsert == nil { + out.DocAsUpsert = new(bool) + } + *out.DocAsUpsert = bool(in.Bool()) + } + case "script": + if m, ok := out.Script.(easyjson.Unmarshaler); ok { + m.UnmarshalEasyJSON(in) + } else if m, ok := out.Script.(json.Unmarshaler); ok { + _ = m.UnmarshalJSON(in.Raw()) + } else { + out.Script = in.Interface() + } + case "scripted_upsert": + if in.IsNull() { + in.Skip() + out.ScriptedUpsert = nil + } else { + if out.ScriptedUpsert == nil { + out.ScriptedUpsert = new(bool) + } + *out.ScriptedUpsert = bool(in.Bool()) + } + case "upsert": + if m, ok := out.Upsert.(easyjson.Unmarshaler); ok { + m.UnmarshalEasyJSON(in) + } else if m, ok := out.Upsert.(json.Unmarshaler); ok { + _ = m.UnmarshalJSON(in.Raw()) + } else { + out.Upsert = in.Interface() + } + case "_source": + if in.IsNull() { + in.Skip() + out.Source = nil + } else { + if out.Source == nil { + out.Source = new(bool) + } + *out.Source = bool(in.Bool()) + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson1ed00e60EncodeGithubComOlivereElasticV71(out *jwriter.Writer, in bulkUpdateRequestCommandData) { + out.RawByte('{') + first := true + _ = first + if in.DetectNoop != nil { + const prefix string = ",\"detect_noop\":" + first = false + out.RawString(prefix[1:]) + out.Bool(bool(*in.DetectNoop)) + } + if in.Doc != nil { + const prefix string = ",\"doc\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + if m, ok := in.Doc.(easyjson.Marshaler); ok { + m.MarshalEasyJSON(out) + } else if m, ok := in.Doc.(json.Marshaler); ok { + out.Raw(m.MarshalJSON()) + } else { + out.Raw(json.Marshal(in.Doc)) + } + } + if in.DocAsUpsert != nil { + const prefix string = ",\"doc_as_upsert\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Bool(bool(*in.DocAsUpsert)) + } + if in.Script != nil { + const prefix string = ",\"script\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + if m, ok := in.Script.(easyjson.Marshaler); ok { + m.MarshalEasyJSON(out) + } else if m, ok := in.Script.(json.Marshaler); ok { + out.Raw(m.MarshalJSON()) + } else { + out.Raw(json.Marshal(in.Script)) + } + } + if in.ScriptedUpsert != nil { + const prefix string = ",\"scripted_upsert\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Bool(bool(*in.ScriptedUpsert)) + } + if in.Upsert != nil { + const prefix string = ",\"upsert\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + if m, ok := in.Upsert.(easyjson.Marshaler); ok { + m.MarshalEasyJSON(out) + } else if m, ok := in.Upsert.(json.Marshaler); ok { + out.Raw(m.MarshalJSON()) + } else { + out.Raw(json.Marshal(in.Upsert)) + } + } + if in.Source != nil { + const prefix string = ",\"_source\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Bool(bool(*in.Source)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkUpdateRequestCommandData) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson1ed00e60EncodeGithubComOlivereElasticV71(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkUpdateRequestCommandData) MarshalEasyJSON(w *jwriter.Writer) { + easyjson1ed00e60EncodeGithubComOlivereElasticV71(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkUpdateRequestCommandData) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson1ed00e60DecodeGithubComOlivereElasticV71(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkUpdateRequestCommandData) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson1ed00e60DecodeGithubComOlivereElasticV71(l, v) +} +func easyjson1ed00e60DecodeGithubComOlivereElasticV72(in *jlexer.Lexer, out *bulkUpdateRequestCommand) { + isTopLevel := in.IsStart() + if in.IsNull() { + in.Skip() + } else { + in.Delim('{') + if !in.IsDelim('}') { + *out = make(bulkUpdateRequestCommand) + } else { + *out = nil + } + for !in.IsDelim('}') { + key := string(in.String()) + in.WantColon() + var v1 bulkUpdateRequestCommandOp + (v1).UnmarshalEasyJSON(in) + (*out)[key] = v1 + in.WantComma() + } + in.Delim('}') + } + if isTopLevel { + in.Consumed() + } +} +func easyjson1ed00e60EncodeGithubComOlivereElasticV72(out *jwriter.Writer, in bulkUpdateRequestCommand) { + if in == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { + out.RawString(`null`) + } else { + out.RawByte('{') + v2First := true + for v2Name, v2Value := range in { + if v2First { + v2First = false + } else { + out.RawByte(',') + } + out.String(string(v2Name)) + out.RawByte(':') + (v2Value).MarshalEasyJSON(out) + } + out.RawByte('}') + } +} + +// MarshalJSON supports json.Marshaler interface +func (v bulkUpdateRequestCommand) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson1ed00e60EncodeGithubComOlivereElasticV72(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v bulkUpdateRequestCommand) MarshalEasyJSON(w *jwriter.Writer) { + easyjson1ed00e60EncodeGithubComOlivereElasticV72(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *bulkUpdateRequestCommand) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson1ed00e60DecodeGithubComOlivereElasticV72(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *bulkUpdateRequestCommand) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson1ed00e60DecodeGithubComOlivereElasticV72(l, v) +} diff --git a/vendor/github.com/olivere/elastic/v7/canonicalize.go b/vendor/github.com/olivere/elastic/v7/canonicalize.go new file mode 100644 index 0000000000..a436f03b6a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/canonicalize.go @@ -0,0 +1,34 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "net/url" + +// canonicalize takes a list of URLs and returns its canonicalized form, i.e. +// remove anything but scheme, userinfo, host, path, and port. +// It also removes all trailing slashes. Invalid URLs or URLs that do not +// use protocol http or https are skipped. +// +// Example: +// http://127.0.0.1:9200/?query=1 -> http://127.0.0.1:9200 +// http://127.0.0.1:9200/db1/ -> http://127.0.0.1:9200/db1 +func canonicalize(rawurls ...string) []string { + var canonicalized []string + for _, rawurl := range rawurls { + u, err := url.Parse(rawurl) + if err == nil { + if u.Scheme == "http" || u.Scheme == "https" { + // Trim trailing slashes + for len(u.Path) > 0 && u.Path[len(u.Path)-1] == '/' { + u.Path = u.Path[0 : len(u.Path)-1] + } + u.Fragment = "" + u.RawQuery = "" + canonicalized = append(canonicalized, u.String()) + } + } + } + return canonicalized +} diff --git a/vendor/github.com/olivere/elastic/v7/cat_aliases.go b/vendor/github.com/olivere/elastic/v7/cat_aliases.go new file mode 100644 index 0000000000..ddadef5b28 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cat_aliases.go @@ -0,0 +1,222 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// CatAliasesService shows information about currently configured aliases +// to indices including filter and routing infos. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-aliases.html +// for details. +type CatAliasesService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + local *bool + masterTimeout string + aliases []string + columns []string + sort []string // list of columns for sort order +} + +// NewCatAliasesService creates a new CatAliasesService. +func NewCatAliasesService(client *Client) *CatAliasesService { + return &CatAliasesService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *CatAliasesService) Pretty(pretty bool) *CatAliasesService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *CatAliasesService) Human(human bool) *CatAliasesService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *CatAliasesService) ErrorTrace(errorTrace bool) *CatAliasesService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *CatAliasesService) FilterPath(filterPath ...string) *CatAliasesService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *CatAliasesService) Header(name string, value string) *CatAliasesService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *CatAliasesService) Headers(headers http.Header) *CatAliasesService { + s.headers = headers + return s +} + +// Alias specifies one or more aliases to which information should be returned. +func (s *CatAliasesService) Alias(alias ...string) *CatAliasesService { + s.aliases = alias + return s +} + +// Local indicates to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *CatAliasesService) Local(local bool) *CatAliasesService { + s.local = &local + return s +} + +// MasterTimeout is the explicit operation timeout for connection to master node. +func (s *CatAliasesService) MasterTimeout(masterTimeout string) *CatAliasesService { + s.masterTimeout = masterTimeout + return s +} + +// Columns to return in the response. +// To get a list of all possible columns to return, run the following command +// in your terminal: +// +// Example: +// curl 'http://localhost:9200/_cat/aliases?help' +// +// You can use Columns("*") to return all possible columns. That might take +// a little longer than the default set of columns. +func (s *CatAliasesService) Columns(columns ...string) *CatAliasesService { + s.columns = columns + return s +} + +// Sort is a list of fields to sort by. +func (s *CatAliasesService) Sort(fields ...string) *CatAliasesService { + s.sort = fields + return s +} + +// buildURL builds the URL for the operation. +func (s *CatAliasesService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + + if len(s.aliases) > 0 { + path, err = uritemplates.Expand("/_cat/aliases/{name}", map[string]string{ + "name": strings.Join(s.aliases, ","), + }) + } else { + path = "/_cat/aliases" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{ + "format": []string{"json"}, // always returns as JSON + } + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if len(s.sort) > 0 { + params.Set("s", strings.Join(s.sort, ",")) + } + if len(s.columns) > 0 { + params.Set("h", strings.Join(s.columns, ",")) + } + return path, params, nil +} + +// Do executes the operation. +func (s *CatAliasesService) Do(ctx context.Context) (CatAliasesResponse, error) { + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret CatAliasesResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a get request. + +// CatAliasesResponse is the outcome of CatAliasesService.Do. +type CatAliasesResponse []CatAliasesResponseRow + +// CatAliasesResponseRow is a single row in a CatAliasesResponse. +// Notice that not all of these fields might be filled; that depends +// on the number of columns chose in the request (see CatAliasesService.Columns). +type CatAliasesResponseRow struct { + // Alias name. + Alias string `json:"alias"` + // Index the alias points to. + Index string `json:"index"` + // Filter, e.g. "*" or "-". + Filter string `json:"filter"` + // RoutingIndex specifies the index routing (or "-"). + RoutingIndex string `json:"routing.index"` + // RoutingSearch specifies the search routing (or "-"). + RoutingSearch string `json:"routing.search"` + // IsWriteIndex indicates whether the index can be written to (or "-"). + IsWriteIndex string `json:"is_write_index"` +} diff --git a/vendor/github.com/olivere/elastic/v7/cat_allocation.go b/vendor/github.com/olivere/elastic/v7/cat_allocation.go new file mode 100644 index 0000000000..0e49707a21 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cat_allocation.go @@ -0,0 +1,239 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// CatAllocationService provides a snapshot of how many shards are allocated +// to each data node and how much disk space they are using. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-allocation.html +// for details. +type CatAllocationService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + bytes string // b, k, m, or g + local *bool + masterTimeout string + nodes []string + columns []string + sort []string // list of columns for sort order +} + +// NewCatAllocationService creates a new CatAllocationService. +func NewCatAllocationService(client *Client) *CatAllocationService { + return &CatAllocationService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *CatAllocationService) Pretty(pretty bool) *CatAllocationService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *CatAllocationService) Human(human bool) *CatAllocationService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *CatAllocationService) ErrorTrace(errorTrace bool) *CatAllocationService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *CatAllocationService) FilterPath(filterPath ...string) *CatAllocationService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *CatAllocationService) Header(name string, value string) *CatAllocationService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *CatAllocationService) Headers(headers http.Header) *CatAllocationService { + s.headers = headers + return s +} + +// NodeID specifies one or more node IDs to for information should be returned. +func (s *CatAllocationService) NodeID(nodes ...string) *CatAllocationService { + s.nodes = nodes + return s +} + +// Bytes represents the unit in which to display byte values. +// Valid values are: "b", "k", "m", or "g". +func (s *CatAllocationService) Bytes(bytes string) *CatAllocationService { + s.bytes = bytes + return s +} + +// Local indicates to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *CatAllocationService) Local(local bool) *CatAllocationService { + s.local = &local + return s +} + +// MasterTimeout is the explicit operation timeout for connection to master node. +func (s *CatAllocationService) MasterTimeout(masterTimeout string) *CatAllocationService { + s.masterTimeout = masterTimeout + return s +} + +// Columns to return in the response. +// To get a list of all possible columns to return, run the following command +// in your terminal: +// +// Example: +// curl 'http://localhost:9200/_cat/aliases?help' +// +// You can use Columns("*") to return all possible columns. That might take +// a little longer than the default set of columns. +func (s *CatAllocationService) Columns(columns ...string) *CatAllocationService { + s.columns = columns + return s +} + +// Sort is a list of fields to sort by. +func (s *CatAllocationService) Sort(fields ...string) *CatAllocationService { + s.sort = fields + return s +} + +// buildURL builds the URL for the operation. +func (s *CatAllocationService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + + if len(s.nodes) > 0 { + path, err = uritemplates.Expand("/_cat/allocation/{node_id}", map[string]string{ + "node_id": strings.Join(s.nodes, ","), + }) + } else { + path = "/_cat/allocation" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{ + "format": []string{"json"}, // always returns as JSON + } + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.bytes != "" { + params.Set("bytes", s.bytes) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if len(s.sort) > 0 { + params.Set("s", strings.Join(s.sort, ",")) + } + if len(s.columns) > 0 { + params.Set("h", strings.Join(s.columns, ",")) + } + return path, params, nil +} + +// Do executes the operation. +func (s *CatAllocationService) Do(ctx context.Context) (CatAllocationResponse, error) { + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret CatAllocationResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a get request. + +// CatAllocationResponse is the outcome of CatAllocationService.Do. +type CatAllocationResponse []CatAllocationResponseRow + +// CatAllocationResponseRow is a single row in a CatAllocationResponse. +// Notice that not all of these fields might be filled; that depends +// on the number of columns chose in the request (see CatAllocationService.Columns). +type CatAllocationResponseRow struct { + // Shards represents the number of shards on a node. + Shards int `json:"shards,string"` + // DiskIndices represents the disk used by ES indices, e.g. "46.1kb". + DiskIndices string `json:"disk.indices"` + // DiskUsed represents the disk used (total, not just ES), e.g. "34.5gb" + DiskUsed string `json:"disk.used"` + // DiskAvail represents the disk available, e.g. "53.2gb". + DiskAvail string `json:"disk.avail"` + // DiskTotal represents the total capacity of all volumes, e.g. "87.7gb". + DiskTotal string `json:"disk.total"` + // DiskPercent represents the percent of disk used, e.g. 39. + DiskPercent int `json:"disk.percent,string"` + // Host represents the hostname of the node. + Host string `json:"host"` + // IP represents the IP address of the node. + IP string `json:"ip"` + // Node represents the node ID. + Node string `json:"node"` +} diff --git a/vendor/github.com/olivere/elastic/v7/cat_count.go b/vendor/github.com/olivere/elastic/v7/cat_count.go new file mode 100644 index 0000000000..82fffd964f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cat_count.go @@ -0,0 +1,215 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// CatCountService provides quick access to the document count of the entire cluster, +// or individual indices. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-count.html +// for details. +type CatCountService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + local *bool + masterTimeout string + columns []string + sort []string // list of columns for sort order +} + +// NewCatCountService creates a new CatCountService. +func NewCatCountService(client *Client) *CatCountService { + return &CatCountService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *CatCountService) Pretty(pretty bool) *CatCountService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *CatCountService) Human(human bool) *CatCountService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *CatCountService) ErrorTrace(errorTrace bool) *CatCountService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *CatCountService) FilterPath(filterPath ...string) *CatCountService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *CatCountService) Header(name string, value string) *CatCountService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *CatCountService) Headers(headers http.Header) *CatCountService { + s.headers = headers + return s +} + +// Index specifies zero or more indices for which to return counts +// (by default counts for all indices are returned). +func (s *CatCountService) Index(index ...string) *CatCountService { + s.index = index + return s +} + +// Local indicates to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *CatCountService) Local(local bool) *CatCountService { + s.local = &local + return s +} + +// MasterTimeout is the explicit operation timeout for connection to master node. +func (s *CatCountService) MasterTimeout(masterTimeout string) *CatCountService { + s.masterTimeout = masterTimeout + return s +} + +// Columns to return in the response. +// To get a list of all possible columns to return, run the following command +// in your terminal: +// +// Example: +// curl 'http://localhost:9200/_cat/count?help' +// +// You can use Columns("*") to return all possible columns. That might take +// a little longer than the default set of columns. +func (s *CatCountService) Columns(columns ...string) *CatCountService { + s.columns = columns + return s +} + +// Sort is a list of fields to sort by. +func (s *CatCountService) Sort(fields ...string) *CatCountService { + s.sort = fields + return s +} + +// buildURL builds the URL for the operation. +func (s *CatCountService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/_cat/count/{index}", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_cat/count" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{ + "format": []string{"json"}, // always returns as JSON + } + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if len(s.sort) > 0 { + params.Set("s", strings.Join(s.sort, ",")) + } + if len(s.columns) > 0 { + params.Set("h", strings.Join(s.columns, ",")) + } + return path, params, nil +} + +// Do executes the operation. +func (s *CatCountService) Do(ctx context.Context) (CatCountResponse, error) { + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret CatCountResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a get request. + +// CatCountResponse is the outcome of CatCountService.Do. +type CatCountResponse []CatCountResponseRow + +// CatCountResponseRow specifies the data returned for one index +// of a CatCountResponse. Notice that not all of these fields might +// be filled; that depends on the number of columns chose in the +// request (see CatCountService.Columns). +type CatCountResponseRow struct { + Epoch int64 `json:"epoch,string"` // e.g. 1527077996 + Timestamp string `json:"timestamp"` // e.g. "12:19:56" + Count int `json:"count,string"` // number of documents +} diff --git a/vendor/github.com/olivere/elastic/v7/cat_health.go b/vendor/github.com/olivere/elastic/v7/cat_health.go new file mode 100644 index 0000000000..2d6bf893cb --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cat_health.go @@ -0,0 +1,211 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" +) + +// CatHealthService returns a terse representation of the same information +// as /_cluster/health. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-health.html +// for details. +type CatHealthService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + local *bool + masterTimeout string + columns []string + sort []string // list of columns for sort order + disableTimestamping *bool +} + +// NewCatHealthService creates a new CatHealthService. +func NewCatHealthService(client *Client) *CatHealthService { + return &CatHealthService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *CatHealthService) Pretty(pretty bool) *CatHealthService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *CatHealthService) Human(human bool) *CatHealthService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *CatHealthService) ErrorTrace(errorTrace bool) *CatHealthService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *CatHealthService) FilterPath(filterPath ...string) *CatHealthService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *CatHealthService) Header(name string, value string) *CatHealthService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *CatHealthService) Headers(headers http.Header) *CatHealthService { + s.headers = headers + return s +} + +// Local indicates to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *CatHealthService) Local(local bool) *CatHealthService { + s.local = &local + return s +} + +// MasterTimeout is the explicit operation timeout for connection to master node. +func (s *CatHealthService) MasterTimeout(masterTimeout string) *CatHealthService { + s.masterTimeout = masterTimeout + return s +} + +// Columns to return in the response. +// To get a list of all possible columns to return, run the following command +// in your terminal: +// +// Example: +// curl 'http://localhost:9200/_cat/indices?help' +// +// You can use Columns("*") to return all possible columns. That might take +// a little longer than the default set of columns. +func (s *CatHealthService) Columns(columns ...string) *CatHealthService { + s.columns = columns + return s +} + +// Sort is a list of fields to sort by. +func (s *CatHealthService) Sort(fields ...string) *CatHealthService { + s.sort = fields + return s +} + +// DisableTimestamping disables timestamping (default: true). +func (s *CatHealthService) DisableTimestamping(disable bool) *CatHealthService { + s.disableTimestamping = &disable + return s +} + +// buildURL builds the URL for the operation. +func (s *CatHealthService) buildURL() (string, url.Values, error) { + // Build URL + path := "/_cat/health" + + // Add query string parameters + params := url.Values{ + "format": []string{"json"}, // always returns as JSON + } + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if len(s.sort) > 0 { + params.Set("s", strings.Join(s.sort, ",")) + } + if v := s.disableTimestamping; v != nil { + params.Set("ts", fmt.Sprint(*v)) + } + if len(s.columns) > 0 { + params.Set("h", strings.Join(s.columns, ",")) + } + return path, params, nil +} + +// Do executes the operation. +func (s *CatHealthService) Do(ctx context.Context) (CatHealthResponse, error) { + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret CatHealthResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a get request. + +// CatHealthResponse is the outcome of CatHealthService.Do. +type CatHealthResponse []CatHealthResponseRow + +// CatHealthResponseRow is a single row in a CatHealthResponse. +// Notice that not all of these fields might be filled; that depends +// on the number of columns chose in the request (see CatHealthService.Columns). +type CatHealthResponseRow struct { + Epoch int64 `json:"epoch,string"` // e.g. 1527077996 + Timestamp string `json:"timestamp"` // e.g. "12:19:56" + Cluster string `json:"cluster"` // cluster name, e.g. "elasticsearch" + Status string `json:"status"` // health status, e.g. "green", "yellow", or "red" + NodeTotal int `json:"node.total,string"` // total number of nodes + NodeData int `json:"node.data,string"` // number of nodes that can store data + Shards int `json:"shards,string"` // total number of shards + Pri int `json:"pri,string"` // number of primary shards + Relo int `json:"relo,string"` // number of relocating nodes + Init int `json:"init,string"` // number of initializing nodes + Unassign int `json:"unassign,string"` // number of unassigned shards + PendingTasks int `json:"pending_tasks,string"` // number of pending tasks + MaxTaskWaitTime string `json:"max_task_wait_time"` // wait time of longest task pending, e.g. "-" or time in millis + ActiveShardsPercent string `json:"active_shards_percent"` // active number of shards in percent, e.g. "100%" +} diff --git a/vendor/github.com/olivere/elastic/v7/cat_indices.go b/vendor/github.com/olivere/elastic/v7/cat_indices.go new file mode 100644 index 0000000000..11a4c6f649 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cat_indices.go @@ -0,0 +1,374 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// CatIndicesService returns the list of indices plus some additional +// information about them. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-indices.html +// for details. +type CatIndicesService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + + index string + bytes string // b, k, m, or g + local *bool + masterTimeout string + columns []string + health string // green, yellow, or red + primaryOnly *bool // true for primary shards only + sort []string // list of columns for sort order + headers http.Header +} + +// NewCatIndicesService creates a new CatIndicesService. +func NewCatIndicesService(client *Client) *CatIndicesService { + return &CatIndicesService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *CatIndicesService) Pretty(pretty bool) *CatIndicesService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *CatIndicesService) Human(human bool) *CatIndicesService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *CatIndicesService) ErrorTrace(errorTrace bool) *CatIndicesService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *CatIndicesService) FilterPath(filterPath ...string) *CatIndicesService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *CatIndicesService) Header(name string, value string) *CatIndicesService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *CatIndicesService) Headers(headers http.Header) *CatIndicesService { + s.headers = headers + return s +} + +// Index is the name of the index to list (by default all indices are returned). +func (s *CatIndicesService) Index(index string) *CatIndicesService { + s.index = index + return s +} + +// Bytes represents the unit in which to display byte values. +// Valid values are: "b", "k", "m", or "g". +func (s *CatIndicesService) Bytes(bytes string) *CatIndicesService { + s.bytes = bytes + return s +} + +// Local indicates to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *CatIndicesService) Local(local bool) *CatIndicesService { + s.local = &local + return s +} + +// MasterTimeout is the explicit operation timeout for connection to master node. +func (s *CatIndicesService) MasterTimeout(masterTimeout string) *CatIndicesService { + s.masterTimeout = masterTimeout + return s +} + +// Columns to return in the response. +// To get a list of all possible columns to return, run the following command +// in your terminal: +// +// Example: +// curl 'http://localhost:9200/_cat/indices?help' +// +// You can use Columns("*") to return all possible columns. That might take +// a little longer than the default set of columns. +func (s *CatIndicesService) Columns(columns ...string) *CatIndicesService { + s.columns = columns + return s +} + +// Health filters indices by their health status. +// Valid values are: "green", "yellow", or "red". +func (s *CatIndicesService) Health(healthState string) *CatIndicesService { + s.health = healthState + return s +} + +// PrimaryOnly when set to true returns stats only for primary shards (default: false). +func (s *CatIndicesService) PrimaryOnly(primaryOnly bool) *CatIndicesService { + s.primaryOnly = &primaryOnly + return s +} + +// Sort is a list of fields to sort by. +func (s *CatIndicesService) Sort(fields ...string) *CatIndicesService { + s.sort = fields + return s +} + +// buildURL builds the URL for the operation. +func (s *CatIndicesService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + + if s.index != "" { + path, err = uritemplates.Expand("/_cat/indices/{index}", map[string]string{ + "index": s.index, + }) + } else { + path = "/_cat/indices" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{ + "format": []string{"json"}, // always returns as JSON + } + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.bytes != "" { + params.Set("bytes", s.bytes) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if len(s.columns) > 0 { + params.Set("h", strings.Join(s.columns, ",")) + } + if s.health != "" { + params.Set("health", s.health) + } + if v := s.primaryOnly; v != nil { + params.Set("pri", fmt.Sprint(*v)) + } + if len(s.sort) > 0 { + params.Set("s", strings.Join(s.sort, ",")) + } + return path, params, nil +} + +// Do executes the operation. +func (s *CatIndicesService) Do(ctx context.Context) (CatIndicesResponse, error) { + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret CatIndicesResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a get request. + +// CatIndicesResponse is the outcome of CatIndicesService.Do. +type CatIndicesResponse []CatIndicesResponseRow + +// CatIndicesResponseRow specifies the data returned for one index +// of a CatIndicesResponse. Notice that not all of these fields might +// be filled; that depends on the number of columns chose in the +// request (see CatIndicesService.Columns). +type CatIndicesResponseRow struct { + Health string `json:"health"` // "green", "yellow", or "red" + Status string `json:"status"` // "open" or "closed" + Index string `json:"index"` // index name + UUID string `json:"uuid"` // index uuid + Pri int `json:"pri,string"` // number of primary shards + Rep int `json:"rep,string"` // number of replica shards + DocsCount int `json:"docs.count,string"` // number of available documents + DocsDeleted int `json:"docs.deleted,string"` // number of deleted documents + CreationDate int64 `json:"creation.date,string"` // index creation date (millisecond value), e.g. 1527077221644 + CreationDateString string `json:"creation.date.string"` // index creation date (as string), e.g. "2018-05-23T12:07:01.644Z" + StoreSize string `json:"store.size"` // store size of primaries & replicas, e.g. "4.6kb" + PriStoreSize string `json:"pri.store.size"` // store size of primaries, e.g. "230b" + CompletionSize string `json:"completion.size"` // size of completion on primaries & replicas + PriCompletionSize string `json:"pri.completion.size"` // size of completion on primaries + FielddataMemorySize string `json:"fielddata.memory_size"` // used fielddata cache on primaries & replicas + PriFielddataMemorySize string `json:"pri.fielddata.memory_size"` // used fielddata cache on primaries + FielddataEvictions int `json:"fielddata.evictions,string"` // fielddata evictions on primaries & replicas + PriFielddataEvictions int `json:"pri.fielddata.evictions,string"` // fielddata evictions on primaries + QueryCacheMemorySize string `json:"query_cache.memory_size"` // used query cache on primaries & replicas + PriQueryCacheMemorySize string `json:"pri.query_cache.memory_size"` // used query cache on primaries + QueryCacheEvictions int `json:"query_cache.evictions,string"` // query cache evictions on primaries & replicas + PriQueryCacheEvictions int `json:"pri.query_cache.evictions,string"` // query cache evictions on primaries + RequestCacheMemorySize string `json:"request_cache.memory_size"` // used request cache on primaries & replicas + PriRequestCacheMemorySize string `json:"pri.request_cache.memory_size"` // used request cache on primaries + RequestCacheEvictions int `json:"request_cache.evictions,string"` // request cache evictions on primaries & replicas + PriRequestCacheEvictions int `json:"pri.request_cache.evictions,string"` // request cache evictions on primaries + RequestCacheHitCount int `json:"request_cache.hit_count,string"` // request cache hit count on primaries & replicas + PriRequestCacheHitCount int `json:"pri.request_cache.hit_count,string"` // request cache hit count on primaries + RequestCacheMissCount int `json:"request_cache.miss_count,string"` // request cache miss count on primaries & replicas + PriRequestCacheMissCount int `json:"pri.request_cache.miss_count,string"` // request cache miss count on primaries + FlushTotal int `json:"flush.total,string"` // number of flushes on primaries & replicas + PriFlushTotal int `json:"pri.flush.total,string"` // number of flushes on primaries + FlushTotalTime string `json:"flush.total_time"` // time spent in flush on primaries & replicas + PriFlushTotalTime string `json:"pri.flush.total_time"` // time spent in flush on primaries + GetCurrent int `json:"get.current,string"` // number of current get ops on primaries & replicas + PriGetCurrent int `json:"pri.get.current,string"` // number of current get ops on primaries + GetTime string `json:"get.time"` // time spent in get on primaries & replicas + PriGetTime string `json:"pri.get.time"` // time spent in get on primaries + GetTotal int `json:"get.total,string"` // number of get ops on primaries & replicas + PriGetTotal int `json:"pri.get.total,string"` // number of get ops on primaries + GetExistsTime string `json:"get.exists_time"` // time spent in successful gets on primaries & replicas + PriGetExistsTime string `json:"pri.get.exists_time"` // time spent in successful gets on primaries + GetExistsTotal int `json:"get.exists_total,string"` // number of successful gets on primaries & replicas + PriGetExistsTotal int `json:"pri.get.exists_total,string"` // number of successful gets on primaries + GetMissingTime string `json:"get.missing_time"` // time spent in failed gets on primaries & replicas + PriGetMissingTime string `json:"pri.get.missing_time"` // time spent in failed gets on primaries + GetMissingTotal int `json:"get.missing_total,string"` // number of failed gets on primaries & replicas + PriGetMissingTotal int `json:"pri.get.missing_total,string"` // number of failed gets on primaries + IndexingDeleteCurrent int `json:"indexing.delete_current,string"` // number of current deletions on primaries & replicas + PriIndexingDeleteCurrent int `json:"pri.indexing.delete_current,string"` // number of current deletions on primaries + IndexingDeleteTime string `json:"indexing.delete_time"` // time spent in deletions on primaries & replicas + PriIndexingDeleteTime string `json:"pri.indexing.delete_time"` // time spent in deletions on primaries + IndexingDeleteTotal int `json:"indexing.delete_total,string"` // number of delete ops on primaries & replicas + PriIndexingDeleteTotal int `json:"pri.indexing.delete_total,string"` // number of delete ops on primaries + IndexingIndexCurrent int `json:"indexing.index_current,string"` // number of current indexing on primaries & replicas + PriIndexingIndexCurrent int `json:"pri.indexing.index_current,string"` // number of current indexing on primaries + IndexingIndexTime string `json:"indexing.index_time"` // time spent in indexing on primaries & replicas + PriIndexingIndexTime string `json:"pri.indexing.index_time"` // time spent in indexing on primaries + IndexingIndexTotal int `json:"indexing.index_total,string"` // number of index ops on primaries & replicas + PriIndexingIndexTotal int `json:"pri.indexing.index_total,string"` // number of index ops on primaries + IndexingIndexFailed int `json:"indexing.index_failed,string"` // number of failed indexing ops on primaries & replicas + PriIndexingIndexFailed int `json:"pri.indexing.index_failed,string"` // number of failed indexing ops on primaries + MergesCurrent int `json:"merges.current,string"` // number of current merges on primaries & replicas + PriMergesCurrent int `json:"pri.merges.current,string"` // number of current merges on primaries + MergesCurrentDocs int `json:"merges.current_docs,string"` // number of current merging docs on primaries & replicas + PriMergesCurrentDocs int `json:"pri.merges.current_docs,string"` // number of current merging docs on primaries + MergesCurrentSize string `json:"merges.current_size"` // size of current merges on primaries & replicas + PriMergesCurrentSize string `json:"pri.merges.current_size"` // size of current merges on primaries + MergesTotal int `json:"merges.total,string"` // number of completed merge ops on primaries & replicas + PriMergesTotal int `json:"pri.merges.total,string"` // number of completed merge ops on primaries + MergesTotalDocs int `json:"merges.total_docs,string"` // docs merged on primaries & replicas + PriMergesTotalDocs int `json:"pri.merges.total_docs,string"` // docs merged on primaries + MergesTotalSize string `json:"merges.total_size"` // size merged on primaries & replicas + PriMergesTotalSize string `json:"pri.merges.total_size"` // size merged on primaries + MergesTotalTime string `json:"merges.total_time"` // time spent in merges on primaries & replicas + PriMergesTotalTime string `json:"pri.merges.total_time"` // time spent in merges on primaries + RefreshTotal int `json:"refresh.total,string"` // total refreshes on primaries & replicas + PriRefreshTotal int `json:"pri.refresh.total,string"` // total refreshes on primaries + RefreshExternalTotal int `json:"refresh.external_total,string"` // total external refreshes on primaries & replicas + PriRefreshExternalTotal int `json:"pri.refresh.external_total,string"` // total external refreshes on primaries + RefreshTime string `json:"refresh.time"` // time spent in refreshes on primaries & replicas + PriRefreshTime string `json:"pri.refresh.time"` // time spent in refreshes on primaries + RefreshExternalTime string `json:"refresh.external_time"` // external time spent in refreshes on primaries & replicas + PriRefreshExternalTime string `json:"pri.refresh.external_time"` // external time spent in refreshes on primaries + RefreshListeners int `json:"refresh.listeners,string"` // number of pending refresh listeners on primaries & replicas + PriRefreshListeners int `json:"pri.refresh.listeners,string"` // number of pending refresh listeners on primaries + SearchFetchCurrent int `json:"search.fetch_current,string"` // current fetch phase ops on primaries & replicas + PriSearchFetchCurrent int `json:"pri.search.fetch_current,string"` // current fetch phase ops on primaries + SearchFetchTime string `json:"search.fetch_time"` // time spent in fetch phase on primaries & replicas + PriSearchFetchTime string `json:"pri.search.fetch_time"` // time spent in fetch phase on primaries + SearchFetchTotal int `json:"search.fetch_total,string"` // total fetch ops on primaries & replicas + PriSearchFetchTotal int `json:"pri.search.fetch_total,string"` // total fetch ops on primaries + SearchOpenContexts int `json:"search.open_contexts,string"` // open search contexts on primaries & replicas + PriSearchOpenContexts int `json:"pri.search.open_contexts,string"` // open search contexts on primaries + SearchQueryCurrent int `json:"search.query_current,string"` // current query phase ops on primaries & replicas + PriSearchQueryCurrent int `json:"pri.search.query_current,string"` // current query phase ops on primaries + SearchQueryTime string `json:"search.query_time"` // time spent in query phase on primaries & replicas, e.g. "0s" + PriSearchQueryTime string `json:"pri.search.query_time"` // time spent in query phase on primaries, e.g. "0s" + SearchQueryTotal int `json:"search.query_total,string"` // total query phase ops on primaries & replicas + PriSearchQueryTotal int `json:"pri.search.query_total,string"` // total query phase ops on primaries + SearchScrollCurrent int `json:"search.scroll_current,string"` // open scroll contexts on primaries & replicas + PriSearchScrollCurrent int `json:"pri.search.scroll_current,string"` // open scroll contexts on primaries + SearchScrollTime string `json:"search.scroll_time"` // time scroll contexts held open on primaries & replicas, e.g. "0s" + PriSearchScrollTime string `json:"pri.search.scroll_time"` // time scroll contexts held open on primaries, e.g. "0s" + SearchScrollTotal int `json:"search.scroll_total,string"` // completed scroll contexts on primaries & replicas + PriSearchScrollTotal int `json:"pri.search.scroll_total,string"` // completed scroll contexts on primaries + SearchThrottled bool `json:"search.throttled,string"` // indicates if the index is search throttled + SegmentsCount int `json:"segments.count,string"` // number of segments on primaries & replicas + PriSegmentsCount int `json:"pri.segments.count,string"` // number of segments on primaries + SegmentsMemory string `json:"segments.memory"` // memory used by segments on primaries & replicas, e.g. "1.3kb" + PriSegmentsMemory string `json:"pri.segments.memory"` // memory used by segments on primaries, e.g. "1.3kb" + SegmentsIndexWriterMemory string `json:"segments.index_writer_memory"` // memory used by index writer on primaries & replicas, e.g. "0b" + PriSegmentsIndexWriterMemory string `json:"pri.segments.index_writer_memory"` // memory used by index writer on primaries, e.g. "0b" + SegmentsVersionMapMemory string `json:"segments.version_map_memory"` // memory used by version map on primaries & replicas, e.g. "0b" + PriSegmentsVersionMapMemory string `json:"pri.segments.version_map_memory"` // memory used by version map on primaries, e.g. "0b" + SegmentsFixedBitsetMemory string `json:"segments.fixed_bitset_memory"` // memory used by fixed bit sets for nested object field types and type filters for types referred in _parent fields on primaries & replicas, e.g. "0b" + PriSegmentsFixedBitsetMemory string `json:"pri.segments.fixed_bitset_memory"` // memory used by fixed bit sets for nested object field types and type filters for types referred in _parent fields on primaries, e.g. "0b" + WarmerCurrent int `json:"warmer.current,string"` // current warmer ops on primaries & replicas + PriWarmerCurrent int `json:"pri.warmer.current,string"` // current warmer ops on primaries + WarmerTotal int `json:"warmer.total,string"` // total warmer ops on primaries & replicas + PriWarmerTotal int `json:"pri.warmer.total,string"` // total warmer ops on primaries + WarmerTotalTime string `json:"warmer.total_time"` // time spent in warmers on primaries & replicas, e.g. "47s" + PriWarmerTotalTime string `json:"pri.warmer.total_time"` // time spent in warmers on primaries, e.g. "47s" + SuggestCurrent int `json:"suggest.current,string"` // number of current suggest ops on primaries & replicas + PriSuggestCurrent int `json:"pri.suggest.current,string"` // number of current suggest ops on primaries + SuggestTime string `json:"suggest.time"` // time spend in suggest on primaries & replicas, "31s" + PriSuggestTime string `json:"pri.suggest.time"` // time spend in suggest on primaries, e.g. "31s" + SuggestTotal int `json:"suggest.total,string"` // number of suggest ops on primaries & replicas + PriSuggestTotal int `json:"pri.suggest.total,string"` // number of suggest ops on primaries + MemoryTotal string `json:"memory.total"` // total user memory on primaries & replicas, e.g. "1.5kb" + PriMemoryTotal string `json:"pri.memory.total"` // total user memory on primaries, e.g. "1.5kb" +} diff --git a/vendor/github.com/olivere/elastic/v7/clear_scroll.go b/vendor/github.com/olivere/elastic/v7/clear_scroll.go new file mode 100644 index 0000000000..a71cad6454 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/clear_scroll.go @@ -0,0 +1,162 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" +) + +// ClearScrollService clears one or more scroll contexts by their ids. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-scroll.html#_clear_scroll_api +// for details. +type ClearScrollService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + scrollId []string +} + +// NewClearScrollService creates a new ClearScrollService. +func NewClearScrollService(client *Client) *ClearScrollService { + return &ClearScrollService{ + client: client, + scrollId: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ClearScrollService) Pretty(pretty bool) *ClearScrollService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ClearScrollService) Human(human bool) *ClearScrollService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ClearScrollService) ErrorTrace(errorTrace bool) *ClearScrollService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ClearScrollService) FilterPath(filterPath ...string) *ClearScrollService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ClearScrollService) Header(name string, value string) *ClearScrollService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ClearScrollService) Headers(headers http.Header) *ClearScrollService { + s.headers = headers + return s +} + +// ScrollId is a list of scroll IDs to clear. +// Use _all to clear all search contexts. +func (s *ClearScrollService) ScrollId(scrollIds ...string) *ClearScrollService { + s.scrollId = append(s.scrollId, scrollIds...) + return s +} + +// buildURL builds the URL for the operation. +func (s *ClearScrollService) buildURL() (string, url.Values, error) { + // Build URL + path := "/_search/scroll/" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ClearScrollService) Validate() error { + var invalid []string + if len(s.scrollId) == 0 { + invalid = append(invalid, "ScrollId") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *ClearScrollService) Do(ctx context.Context) (*ClearScrollResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body := map[string][]string{ + "scroll_id": s.scrollId, + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ClearScrollResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ClearScrollResponse is the response of ClearScrollService.Do. +type ClearScrollResponse struct { + Succeeded bool `json:"succeeded,omitempty"` + NumFreed int `json:"num_freed,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/client.go b/vendor/github.com/olivere/elastic/v7/client.go new file mode 100644 index 0000000000..565704e92d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/client.go @@ -0,0 +1,2099 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httputil" + "net/url" + "os" + "regexp" + "strings" + "sync" + "time" + + "github.com/pkg/errors" + + "github.com/olivere/elastic/v7/config" +) + +const ( + // Version is the current version of Elastic. + Version = "7.0.9" + + // DefaultURL is the default endpoint of Elasticsearch on the local machine. + // It is used e.g. when initializing a new Client without a specific URL. + DefaultURL = "http://127.0.0.1:9200" + + // DefaultScheme is the default protocol scheme to use when sniffing + // the Elasticsearch cluster. + DefaultScheme = "http" + + // DefaultHealthcheckEnabled specifies if healthchecks are enabled by default. + DefaultHealthcheckEnabled = true + + // DefaultHealthcheckTimeoutStartup is the time the healthcheck waits + // for a response from Elasticsearch on startup, i.e. when creating a + // client. After the client is started, a shorter timeout is commonly used + // (its default is specified in DefaultHealthcheckTimeout). + DefaultHealthcheckTimeoutStartup = 5 * time.Second + + // DefaultHealthcheckTimeout specifies the time a running client waits for + // a response from Elasticsearch. Notice that the healthcheck timeout + // when a client is created is larger by default (see DefaultHealthcheckTimeoutStartup). + DefaultHealthcheckTimeout = 1 * time.Second + + // DefaultHealthcheckInterval is the default interval between + // two health checks of the nodes in the cluster. + DefaultHealthcheckInterval = 60 * time.Second + + // DefaultSnifferEnabled specifies if the sniffer is enabled by default. + DefaultSnifferEnabled = true + + // DefaultSnifferInterval is the interval between two sniffing procedures, + // i.e. the lookup of all nodes in the cluster and their addition/removal + // from the list of actual connections. + DefaultSnifferInterval = 15 * time.Minute + + // DefaultSnifferTimeoutStartup is the default timeout for the sniffing + // process that is initiated while creating a new client. For subsequent + // sniffing processes, DefaultSnifferTimeout is used (by default). + DefaultSnifferTimeoutStartup = 5 * time.Second + + // DefaultSnifferTimeout is the default timeout after which the + // sniffing process times out. Notice that for the initial sniffing + // process, DefaultSnifferTimeoutStartup is used. + DefaultSnifferTimeout = 2 * time.Second + + // DefaultSendGetBodyAs is the HTTP method to use when elastic is sending + // a GET request with a body. + DefaultSendGetBodyAs = "GET" + + // DefaultGzipEnabled specifies if gzip compression is enabled by default. + DefaultGzipEnabled = false + + // off is used to disable timeouts. + off = -1 * time.Second +) + +var ( + // ErrNoClient is raised when no Elasticsearch node is available. + ErrNoClient = errors.New("no Elasticsearch node available") + + // ErrRetry is raised when a request cannot be executed after the configured + // number of retries. + ErrRetry = errors.New("cannot connect after several retries") + + // ErrTimeout is raised when a request timed out, e.g. when WaitForStatus + // didn't return in time. + ErrTimeout = errors.New("timeout") + + // noRetries is a retrier that does not retry. + noRetries = NewStopRetrier() + + // noDeprecationLog is a no-op for logging deprecations. + noDeprecationLog = func(*http.Request, *http.Response) {} +) + +// Doer is an interface to perform HTTP requests. +// It can be used for mocking. +type Doer interface { + Do(*http.Request) (*http.Response, error) +} + +// ClientOptionFunc is a function that configures a Client. +// It is used in NewClient. +type ClientOptionFunc func(*Client) error + +// Client is an Elasticsearch client. Create one by calling NewClient. +type Client struct { + c Doer // e.g. a net/*http.Client to use for requests + + connsMu sync.RWMutex // connsMu guards the next block + conns []*conn // all connections + cindex int // index into conns + + mu sync.RWMutex // guards the next block + urls []string // set of URLs passed initially to the client + running bool // true if the client's background processes are running + errorlog Logger // error log for critical messages + infolog Logger // information log for e.g. response times + tracelog Logger // trace log for debugging + deprecationlog func(*http.Request, *http.Response) + scheme string // http or https + healthcheckEnabled bool // healthchecks enabled or disabled + healthcheckTimeoutStartup time.Duration // time the healthcheck waits for a response from Elasticsearch on startup + healthcheckTimeout time.Duration // time the healthcheck waits for a response from Elasticsearch + healthcheckInterval time.Duration // interval between healthchecks + healthcheckStop chan bool // notify healthchecker to stop, and notify back + snifferEnabled bool // sniffer enabled or disabled + snifferTimeoutStartup time.Duration // time the sniffer waits for a response from nodes info API on startup + snifferTimeout time.Duration // time the sniffer waits for a response from nodes info API + snifferInterval time.Duration // interval between sniffing + snifferCallback SnifferCallback // callback to modify the sniffing decision + snifferStop chan bool // notify sniffer to stop, and notify back + decoder Decoder // used to decode data sent from Elasticsearch + basicAuth bool // indicates whether to send HTTP Basic Auth credentials + basicAuthUsername string // username for HTTP Basic Auth + basicAuthPassword string // password for HTTP Basic Auth + sendGetBodyAs string // override for when sending a GET with a body + gzipEnabled bool // gzip compression enabled or disabled (default) + requiredPlugins []string // list of required plugins + retrier Retrier // strategy for retries + headers http.Header // a list of default headers to add to each request +} + +// NewClient creates a new client to work with Elasticsearch. +// +// NewClient, by default, is meant to be long-lived and shared across +// your application. If you need a short-lived client, e.g. for request-scope, +// consider using NewSimpleClient instead. +// +// The caller can configure the new client by passing configuration options +// to the func. +// +// Example: +// +// client, err := elastic.NewClient( +// elastic.SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201"), +// elastic.SetBasicAuth("user", "secret")) +// +// If no URL is configured, Elastic uses DefaultURL by default. +// +// If the sniffer is enabled (the default), the new client then sniffes +// the cluster via the Nodes Info API +// (see https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-nodes-info.html#cluster-nodes-info). +// It uses the URLs specified by the caller. The caller is responsible +// to only pass a list of URLs of nodes that belong to the same cluster. +// This sniffing process is run on startup and periodically. +// Use SnifferInterval to set the interval between two sniffs (default is +// 15 minutes). In other words: By default, the client will find new nodes +// in the cluster and remove those that are no longer available every +// 15 minutes. Disable the sniffer by passing SetSniff(false) to NewClient. +// +// The list of nodes found in the sniffing process will be used to make +// connections to the REST API of Elasticsearch. These nodes are also +// periodically checked in a shorter time frame. This process is called +// a health check. By default, a health check is done every 60 seconds. +// You can set a shorter or longer interval by SetHealthcheckInterval. +// Disabling health checks is not recommended, but can be done by +// SetHealthcheck(false). +// +// Connections are automatically marked as dead or healthy while +// making requests to Elasticsearch. When a request fails, Elastic will +// call into the Retry strategy which can be specified with SetRetry. +// The Retry strategy is also responsible for handling backoff i.e. the time +// to wait before starting the next request. There are various standard +// backoff implementations, e.g. ExponentialBackoff or SimpleBackoff. +// Retries are disabled by default. +// +// If no HttpClient is configured, then http.DefaultClient is used. +// You can use your own http.Client with some http.Transport for +// advanced scenarios. +// +// An error is also returned when some configuration option is invalid or +// the new client cannot sniff the cluster (if enabled). +func NewClient(options ...ClientOptionFunc) (*Client, error) { + return DialContext(context.Background(), options...) +} + +// NewClientFromConfig initializes a client from a configuration. +func NewClientFromConfig(cfg *config.Config) (*Client, error) { + options, err := configToOptions(cfg) + if err != nil { + return nil, err + } + return DialContext(context.Background(), options...) +} + +// NewSimpleClient creates a new short-lived Client that can be used in +// use cases where you need e.g. one client per request. +// +// While NewClient by default sets up e.g. periodic health checks +// and sniffing for new nodes in separate goroutines, NewSimpleClient does +// not and is meant as a simple replacement where you don't need all the +// heavy lifting of NewClient. +// +// NewSimpleClient does the following by default: First, all health checks +// are disabled, including timeouts and periodic checks. Second, sniffing +// is disabled, including timeouts and periodic checks. The number of retries +// is set to 1. NewSimpleClient also does not start any goroutines. +// +// Notice that you can still override settings by passing additional options, +// just like with NewClient. +func NewSimpleClient(options ...ClientOptionFunc) (*Client, error) { + c := &Client{ + c: http.DefaultClient, + conns: make([]*conn, 0), + cindex: -1, + scheme: DefaultScheme, + decoder: &DefaultDecoder{}, + healthcheckEnabled: false, + healthcheckTimeoutStartup: off, + healthcheckTimeout: off, + healthcheckInterval: off, + healthcheckStop: make(chan bool), + snifferEnabled: false, + snifferTimeoutStartup: off, + snifferTimeout: off, + snifferInterval: off, + snifferCallback: nopSnifferCallback, + snifferStop: make(chan bool), + sendGetBodyAs: DefaultSendGetBodyAs, + gzipEnabled: DefaultGzipEnabled, + retrier: noRetries, // no retries by default + deprecationlog: noDeprecationLog, + } + + // Run the options on it + for _, option := range options { + if err := option(c); err != nil { + return nil, err + } + } + + // Use a default URL and normalize them + if len(c.urls) == 0 { + c.urls = []string{DefaultURL} + } + c.urls = canonicalize(c.urls...) + + // If the URLs have auth info, use them here as an alternative to SetBasicAuth + if !c.basicAuth { + for _, urlStr := range c.urls { + u, err := url.Parse(urlStr) + if err == nil && u.User != nil { + c.basicAuth = true + c.basicAuthUsername = u.User.Username() + c.basicAuthPassword, _ = u.User.Password() + break + } + } + } + + for _, url := range c.urls { + c.conns = append(c.conns, newConn(url, url)) + } + + // Ensure that we have at least one connection available + if err := c.mustActiveConn(); err != nil { + return nil, err + } + + // Check the required plugins + for _, plugin := range c.requiredPlugins { + found, err := c.HasPlugin(plugin) + if err != nil { + return nil, err + } + if !found { + return nil, fmt.Errorf("elastic: plugin %s not found", plugin) + } + } + + c.mu.Lock() + c.running = true + c.mu.Unlock() + + return c, nil +} + +// Dial will call DialContext with a background context. +func Dial(options ...ClientOptionFunc) (*Client, error) { + return DialContext(context.Background(), options...) +} + +// DialContext will connect to Elasticsearch, just like NewClient does. +// +// The context is honoured in terms of e.g. cancellation. +func DialContext(ctx context.Context, options ...ClientOptionFunc) (*Client, error) { + // Set up the client + c := &Client{ + c: http.DefaultClient, + conns: make([]*conn, 0), + cindex: -1, + scheme: DefaultScheme, + decoder: &DefaultDecoder{}, + healthcheckEnabled: DefaultHealthcheckEnabled, + healthcheckTimeoutStartup: DefaultHealthcheckTimeoutStartup, + healthcheckTimeout: DefaultHealthcheckTimeout, + healthcheckInterval: DefaultHealthcheckInterval, + healthcheckStop: make(chan bool), + snifferEnabled: DefaultSnifferEnabled, + snifferTimeoutStartup: DefaultSnifferTimeoutStartup, + snifferTimeout: DefaultSnifferTimeout, + snifferInterval: DefaultSnifferInterval, + snifferCallback: nopSnifferCallback, + snifferStop: make(chan bool), + sendGetBodyAs: DefaultSendGetBodyAs, + gzipEnabled: DefaultGzipEnabled, + retrier: noRetries, // no retries by default + deprecationlog: noDeprecationLog, + } + + // Run the options on it + for _, option := range options { + if err := option(c); err != nil { + return nil, err + } + } + + // Use a default URL and normalize them + if len(c.urls) == 0 { + c.urls = []string{DefaultURL} + } + c.urls = canonicalize(c.urls...) + + // If the URLs have auth info, use them here as an alternative to SetBasicAuth + if !c.basicAuth { + for _, urlStr := range c.urls { + u, err := url.Parse(urlStr) + if err == nil && u.User != nil { + c.basicAuth = true + c.basicAuthUsername = u.User.Username() + c.basicAuthPassword, _ = u.User.Password() + break + } + } + } + + // Check if we can make a request to any of the specified URLs + if c.healthcheckEnabled { + if err := c.startupHealthcheck(ctx, c.healthcheckTimeoutStartup); err != nil { + return nil, err + } + } + + if c.snifferEnabled { + // Sniff the cluster initially + if err := c.sniff(ctx, c.snifferTimeoutStartup); err != nil { + return nil, err + } + } else { + // Do not sniff the cluster initially. Use the provided URLs instead. + for _, url := range c.urls { + c.conns = append(c.conns, newConn(url, url)) + } + } + + if c.healthcheckEnabled { + // Perform an initial health check + c.healthcheck(ctx, c.healthcheckTimeoutStartup, true) + } + // Ensure that we have at least one connection available + if err := c.mustActiveConn(); err != nil { + return nil, err + } + + // Check the required plugins + for _, plugin := range c.requiredPlugins { + found, err := c.HasPlugin(plugin) + if err != nil { + return nil, err + } + if !found { + return nil, fmt.Errorf("elastic: plugin %s not found", plugin) + } + } + + if c.snifferEnabled { + go c.sniffer() // periodically update cluster information + } + if c.healthcheckEnabled { + go c.healthchecker() // start goroutine periodically ping all nodes of the cluster + } + + c.mu.Lock() + c.running = true + c.mu.Unlock() + + return c, nil +} + +// DialWithConfig will use the configuration settings parsed from config package +// to connect to Elasticsearch. +// +// The context is honoured in terms of e.g. cancellation. +func DialWithConfig(ctx context.Context, cfg *config.Config) (*Client, error) { + options, err := configToOptions(cfg) + if err != nil { + return nil, err + } + return DialContext(ctx, options...) +} + +func configToOptions(cfg *config.Config) ([]ClientOptionFunc, error) { + var options []ClientOptionFunc + if cfg != nil { + if cfg.URL != "" { + options = append(options, SetURL(cfg.URL)) + } + if cfg.Errorlog != "" { + f, err := os.OpenFile(cfg.Errorlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, errors.Wrap(err, "unable to initialize error log") + } + l := log.New(f, "", 0) + options = append(options, SetErrorLog(l)) + } + if cfg.Tracelog != "" { + f, err := os.OpenFile(cfg.Tracelog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, errors.Wrap(err, "unable to initialize trace log") + } + l := log.New(f, "", 0) + options = append(options, SetTraceLog(l)) + } + if cfg.Infolog != "" { + f, err := os.OpenFile(cfg.Infolog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, errors.Wrap(err, "unable to initialize info log") + } + l := log.New(f, "", 0) + options = append(options, SetInfoLog(l)) + } + if cfg.Username != "" || cfg.Password != "" { + options = append(options, SetBasicAuth(cfg.Username, cfg.Password)) + } + if cfg.Sniff != nil { + options = append(options, SetSniff(*cfg.Sniff)) + } + /* + if cfg.Healthcheck != nil { + options = append(options, SetHealthcheck(*cfg.Healthcheck)) + } + */ + } + return options, nil +} + +// SetHttpClient can be used to specify the http.Client to use when making +// HTTP requests to Elasticsearch. +func SetHttpClient(httpClient Doer) ClientOptionFunc { + return func(c *Client) error { + if httpClient != nil { + c.c = httpClient + } else { + c.c = http.DefaultClient + } + return nil + } +} + +// SetBasicAuth can be used to specify the HTTP Basic Auth credentials to +// use when making HTTP requests to Elasticsearch. +func SetBasicAuth(username, password string) ClientOptionFunc { + return func(c *Client) error { + c.basicAuthUsername = username + c.basicAuthPassword = password + c.basicAuth = c.basicAuthUsername != "" || c.basicAuthPassword != "" + return nil + } +} + +// SetURL defines the URL endpoints of the Elasticsearch nodes. Notice that +// when sniffing is enabled, these URLs are used to initially sniff the +// cluster on startup. +func SetURL(urls ...string) ClientOptionFunc { + return func(c *Client) error { + switch len(urls) { + case 0: + c.urls = []string{DefaultURL} + default: + c.urls = urls + } + return nil + } +} + +// SetScheme sets the HTTP scheme to look for when sniffing (http or https). +// This is http by default. +func SetScheme(scheme string) ClientOptionFunc { + return func(c *Client) error { + c.scheme = scheme + return nil + } +} + +// SetSniff enables or disables the sniffer (enabled by default). +func SetSniff(enabled bool) ClientOptionFunc { + return func(c *Client) error { + c.snifferEnabled = enabled + return nil + } +} + +// SetSnifferTimeoutStartup sets the timeout for the sniffer that is used +// when creating a new client. The default is 5 seconds. Notice that the +// timeout being used for subsequent sniffing processes is set with +// SetSnifferTimeout. +func SetSnifferTimeoutStartup(timeout time.Duration) ClientOptionFunc { + return func(c *Client) error { + c.snifferTimeoutStartup = timeout + return nil + } +} + +// SetSnifferTimeout sets the timeout for the sniffer that finds the +// nodes in a cluster. The default is 2 seconds. Notice that the timeout +// used when creating a new client on startup is usually greater and can +// be set with SetSnifferTimeoutStartup. +func SetSnifferTimeout(timeout time.Duration) ClientOptionFunc { + return func(c *Client) error { + c.snifferTimeout = timeout + return nil + } +} + +// SetSnifferInterval sets the interval between two sniffing processes. +// The default interval is 15 minutes. +func SetSnifferInterval(interval time.Duration) ClientOptionFunc { + return func(c *Client) error { + c.snifferInterval = interval + return nil + } +} + +// SnifferCallback defines the protocol for sniffing decisions. +type SnifferCallback func(*NodesInfoNode) bool + +// nopSnifferCallback is the default sniffer callback: It accepts +// all nodes the sniffer finds. +var nopSnifferCallback = func(*NodesInfoNode) bool { return true } + +// SetSnifferCallback allows the caller to modify sniffer decisions. +// When setting the callback, the given SnifferCallback is called for +// each (healthy) node found during the sniffing process. +// If the callback returns false, the node is ignored: No requests +// are routed to it. +func SetSnifferCallback(f SnifferCallback) ClientOptionFunc { + return func(c *Client) error { + if f != nil { + c.snifferCallback = f + } + return nil + } +} + +// SetHealthcheck enables or disables healthchecks (enabled by default). +func SetHealthcheck(enabled bool) ClientOptionFunc { + return func(c *Client) error { + c.healthcheckEnabled = enabled + return nil + } +} + +// SetHealthcheckTimeoutStartup sets the timeout for the initial health check. +// The default timeout is 5 seconds (see DefaultHealthcheckTimeoutStartup). +// Notice that timeouts for subsequent health checks can be modified with +// SetHealthcheckTimeout. +func SetHealthcheckTimeoutStartup(timeout time.Duration) ClientOptionFunc { + return func(c *Client) error { + c.healthcheckTimeoutStartup = timeout + return nil + } +} + +// SetHealthcheckTimeout sets the timeout for periodic health checks. +// The default timeout is 1 second (see DefaultHealthcheckTimeout). +// Notice that a different (usually larger) timeout is used for the initial +// healthcheck, which is initiated while creating a new client. +// The startup timeout can be modified with SetHealthcheckTimeoutStartup. +func SetHealthcheckTimeout(timeout time.Duration) ClientOptionFunc { + return func(c *Client) error { + c.healthcheckTimeout = timeout + return nil + } +} + +// SetHealthcheckInterval sets the interval between two health checks. +// The default interval is 60 seconds. +func SetHealthcheckInterval(interval time.Duration) ClientOptionFunc { + return func(c *Client) error { + c.healthcheckInterval = interval + return nil + } +} + +// SetMaxRetries sets the maximum number of retries before giving up when +// performing a HTTP request to Elasticsearch. +// +// Deprecated: Replace with a Retry implementation. +func SetMaxRetries(maxRetries int) ClientOptionFunc { + return func(c *Client) error { + if maxRetries < 0 { + return errors.New("MaxRetries must be greater than or equal to 0") + } else if maxRetries == 0 { + c.retrier = noRetries + } else { + // Create a Retrier that will wait for 100ms (+/- jitter) between requests. + // This resembles the old behavior with maxRetries. + ticks := make([]int, maxRetries) + for i := 0; i < len(ticks); i++ { + ticks[i] = 100 + } + backoff := NewSimpleBackoff(ticks...) + c.retrier = NewBackoffRetrier(backoff) + } + return nil + } +} + +// SetGzip enables or disables gzip compression (disabled by default). +func SetGzip(enabled bool) ClientOptionFunc { + return func(c *Client) error { + c.gzipEnabled = enabled + return nil + } +} + +// SetDecoder sets the Decoder to use when decoding data from Elasticsearch. +// DefaultDecoder is used by default. +func SetDecoder(decoder Decoder) ClientOptionFunc { + return func(c *Client) error { + if decoder != nil { + c.decoder = decoder + } else { + c.decoder = &DefaultDecoder{} + } + return nil + } +} + +// SetRequiredPlugins can be used to indicate that some plugins are required +// before a Client will be created. +func SetRequiredPlugins(plugins ...string) ClientOptionFunc { + return func(c *Client) error { + if c.requiredPlugins == nil { + c.requiredPlugins = make([]string, 0) + } + c.requiredPlugins = append(c.requiredPlugins, plugins...) + return nil + } +} + +// SetErrorLog sets the logger for critical messages like nodes joining +// or leaving the cluster or failing requests. It is nil by default. +func SetErrorLog(logger Logger) ClientOptionFunc { + return func(c *Client) error { + c.errorlog = logger + return nil + } +} + +// SetInfoLog sets the logger for informational messages, e.g. requests +// and their response times. It is nil by default. +func SetInfoLog(logger Logger) ClientOptionFunc { + return func(c *Client) error { + c.infolog = logger + return nil + } +} + +// SetTraceLog specifies the log.Logger to use for output of HTTP requests +// and responses which is helpful during debugging. It is nil by default. +func SetTraceLog(logger Logger) ClientOptionFunc { + return func(c *Client) error { + c.tracelog = logger + return nil + } +} + +// SetSendGetBodyAs specifies the HTTP method to use when sending a GET request +// with a body. It is GET by default. +func SetSendGetBodyAs(httpMethod string) ClientOptionFunc { + return func(c *Client) error { + c.sendGetBodyAs = httpMethod + return nil + } +} + +// SetRetrier specifies the retry strategy that handles errors during +// HTTP request/response with Elasticsearch. +func SetRetrier(retrier Retrier) ClientOptionFunc { + return func(c *Client) error { + if retrier == nil { + retrier = noRetries // no retries by default + } + c.retrier = retrier + return nil + } +} + +// SetHeaders adds a list of default HTTP headers that will be added to +// each requests executed by PerformRequest. +func SetHeaders(headers http.Header) ClientOptionFunc { + return func(c *Client) error { + c.headers = headers + return nil + } +} + +// String returns a string representation of the client status. +func (c *Client) String() string { + c.connsMu.Lock() + conns := c.conns + c.connsMu.Unlock() + + var buf bytes.Buffer + for i, conn := range conns { + if i > 0 { + buf.WriteString(", ") + } + buf.WriteString(conn.String()) + } + return buf.String() +} + +// IsRunning returns true if the background processes of the client are +// running, false otherwise. +func (c *Client) IsRunning() bool { + c.mu.RLock() + defer c.mu.RUnlock() + return c.running +} + +// Start starts the background processes like sniffing the cluster and +// periodic health checks. You don't need to run Start when creating a +// client with NewClient; the background processes are run by default. +// +// If the background processes are already running, this is a no-op. +func (c *Client) Start() { + c.mu.RLock() + if c.running { + c.mu.RUnlock() + return + } + c.mu.RUnlock() + + if c.snifferEnabled { + go c.sniffer() + } + if c.healthcheckEnabled { + go c.healthchecker() + } + + c.mu.Lock() + c.running = true + c.mu.Unlock() + + c.infof("elastic: client started") +} + +// Stop stops the background processes that the client is running, +// i.e. sniffing the cluster periodically and running health checks +// on the nodes. +// +// If the background processes are not running, this is a no-op. +func (c *Client) Stop() { + c.mu.RLock() + if !c.running { + c.mu.RUnlock() + return + } + c.mu.RUnlock() + + if c.healthcheckEnabled { + c.healthcheckStop <- true + <-c.healthcheckStop + } + + if c.snifferEnabled { + c.snifferStop <- true + <-c.snifferStop + } + + c.mu.Lock() + c.running = false + c.mu.Unlock() + + c.infof("elastic: client stopped") +} + +var logDeprecation = func(*http.Request, *http.Response) {} + +// errorf logs to the error log. +func (c *Client) errorf(format string, args ...interface{}) { + if c.errorlog != nil { + c.errorlog.Printf(format, args...) + } +} + +// infof logs informational messages. +func (c *Client) infof(format string, args ...interface{}) { + if c.infolog != nil { + c.infolog.Printf(format, args...) + } +} + +// tracef logs to the trace log. +func (c *Client) tracef(format string, args ...interface{}) { + if c.tracelog != nil { + c.tracelog.Printf(format, args...) + } +} + +// dumpRequest dumps the given HTTP request to the trace log. +func (c *Client) dumpRequest(r *http.Request) { + if c.tracelog != nil { + out, err := httputil.DumpRequestOut(r, true) + if err == nil { + c.tracef("%s\n", string(out)) + } + } +} + +// dumpResponse dumps the given HTTP response to the trace log. +func (c *Client) dumpResponse(resp *http.Response) { + if c.tracelog != nil { + out, err := httputil.DumpResponse(resp, true) + if err == nil { + c.tracef("%s\n", string(out)) + } + } +} + +// sniffer periodically runs sniff. +func (c *Client) sniffer() { + c.mu.RLock() + timeout := c.snifferTimeout + interval := c.snifferInterval + c.mu.RUnlock() + + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-c.snifferStop: + // we are asked to stop, so we signal back that we're stopping now + c.snifferStop <- true + return + case <-ticker.C: + c.sniff(context.Background(), timeout) + } + } +} + +// sniff uses the Node Info API to return the list of nodes in the cluster. +// It uses the list of URLs passed on startup plus the list of URLs found +// by the preceding sniffing process (if sniffing is enabled). +// +// If sniffing is disabled, this is a no-op. +func (c *Client) sniff(parentCtx context.Context, timeout time.Duration) error { + c.mu.RLock() + if !c.snifferEnabled { + c.mu.RUnlock() + return nil + } + + // Use all available URLs provided to sniff the cluster. + var urls []string + urlsMap := make(map[string]bool) + + // Add all URLs provided on startup + for _, url := range c.urls { + urlsMap[url] = true + urls = append(urls, url) + } + c.mu.RUnlock() + + // Add all URLs found by sniffing + c.connsMu.RLock() + for _, conn := range c.conns { + if !conn.IsDead() { + url := conn.URL() + if _, found := urlsMap[url]; !found { + urls = append(urls, url) + } + } + } + c.connsMu.RUnlock() + + if len(urls) == 0 { + return errors.Wrap(ErrNoClient, "no URLs found") + } + + // Start sniffing on all found URLs + ch := make(chan []*conn, len(urls)) + + ctx, cancel := context.WithTimeout(parentCtx, timeout) + defer cancel() + + for _, url := range urls { + go func(url string) { ch <- c.sniffNode(ctx, url) }(url) + } + + // Wait for the results to come back, or the process times out. + for { + select { + case conns := <-ch: + if len(conns) > 0 { + c.updateConns(conns) + return nil + } + case <-ctx.Done(): + if err := ctx.Err(); err != nil { + switch { + case IsContextErr(err): + return err + } + return errors.Wrapf(ErrNoClient, "sniff timeout: %v", err) + } + // We get here if no cluster responds in time + return errors.Wrap(ErrNoClient, "sniff timeout") + } + } +} + +// sniffNode sniffs a single node. This method is run as a goroutine +// in sniff. If successful, it returns the list of node URLs extracted +// from the result of calling Nodes Info API. Otherwise, an empty array +// is returned. +func (c *Client) sniffNode(ctx context.Context, url string) []*conn { + var nodes []*conn + + // Call the Nodes Info API at /_nodes/http + req, err := NewRequest("GET", url+"/_nodes/http") + if err != nil { + return nodes + } + + c.mu.RLock() + if c.basicAuth { + req.SetBasicAuth(c.basicAuthUsername, c.basicAuthPassword) + } + c.mu.RUnlock() + + res, err := c.c.Do((*http.Request)(req).WithContext(ctx)) + if err != nil { + return nodes + } + defer res.Body.Close() + + var info NodesInfoResponse + if err := json.NewDecoder(res.Body).Decode(&info); err == nil { + if len(info.Nodes) > 0 { + for nodeID, node := range info.Nodes { + if c.snifferCallback(node) { + if node.HTTP != nil && len(node.HTTP.PublishAddress) > 0 { + url := c.extractHostname(c.scheme, node.HTTP.PublishAddress) + if url != "" { + nodes = append(nodes, newConn(nodeID, url)) + } + } + } + } + } + } + return nodes +} + +// reSniffHostAndPort is used to extract hostname and port from a result +// from a Nodes Info API (example: "inet[/127.0.0.1:9200]"). +var reSniffHostAndPort = regexp.MustCompile(`\/([^:]*):([0-9]+)\]`) + +func (c *Client) extractHostname(scheme, address string) string { + if strings.HasPrefix(address, "inet") { + m := reSniffHostAndPort.FindStringSubmatch(address) + if len(m) == 3 { + return fmt.Sprintf("%s://%s:%s", scheme, m[1], m[2]) + } + } + s := address + if idx := strings.Index(s, "/"); idx >= 0 { + s = s[idx+1:] + } + if !strings.Contains(s, ":") { + return "" + } + return fmt.Sprintf("%s://%s", scheme, s) +} + +// updateConns updates the clients' connections with new information +// gather by a sniff operation. +func (c *Client) updateConns(conns []*conn) { + c.connsMu.Lock() + + // Build up new connections: + // If we find an existing connection, use that (including no. of failures etc.). + // If we find a new connection, add it. + var newConns []*conn + for _, conn := range conns { + var found bool + for _, oldConn := range c.conns { + // Notice that e.g. in a Kubernetes cluster the NodeID might be + // stable while the URL has changed. + if oldConn.NodeID() == conn.NodeID() && oldConn.URL() == conn.URL() { + // Take over the old connection + newConns = append(newConns, oldConn) + found = true + break + } + } + if !found { + // New connection didn't exist, so add it to our list of new conns. + c.infof("elastic: %s joined the cluster", conn.URL()) + newConns = append(newConns, conn) + } + } + + c.conns = newConns + c.cindex = -1 + c.connsMu.Unlock() +} + +// healthchecker periodically runs healthcheck. +func (c *Client) healthchecker() { + c.mu.RLock() + timeout := c.healthcheckTimeout + interval := c.healthcheckInterval + c.mu.RUnlock() + + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-c.healthcheckStop: + // we are asked to stop, so we signal back that we're stopping now + c.healthcheckStop <- true + return + case <-ticker.C: + c.healthcheck(context.Background(), timeout, false) + } + } +} + +// healthcheck does a health check on all nodes in the cluster. Depending on +// the node state, it marks connections as dead, sets them alive etc. +// If healthchecks are disabled and force is false, this is a no-op. +// The timeout specifies how long to wait for a response from Elasticsearch. +func (c *Client) healthcheck(parentCtx context.Context, timeout time.Duration, force bool) { + c.mu.RLock() + if !c.healthcheckEnabled && !force { + c.mu.RUnlock() + return + } + basicAuth := c.basicAuth + basicAuthUsername := c.basicAuthUsername + basicAuthPassword := c.basicAuthPassword + c.mu.RUnlock() + + c.connsMu.RLock() + conns := c.conns + c.connsMu.RUnlock() + + for _, conn := range conns { + // Run the HEAD request against ES with a timeout + ctx, cancel := context.WithTimeout(parentCtx, timeout) + defer cancel() + + // Goroutine executes the HTTP request, returns an error and sets status + var status int + errc := make(chan error, 1) + go func(url string) { + req, err := NewRequest("HEAD", url) + if err != nil { + errc <- err + return + } + if basicAuth { + req.SetBasicAuth(basicAuthUsername, basicAuthPassword) + } + res, err := c.c.Do((*http.Request)(req).WithContext(ctx)) + if res != nil { + status = res.StatusCode + if res.Body != nil { + res.Body.Close() + } + } + errc <- err + }(conn.URL()) + + // Wait for the Goroutine (or its timeout) + select { + case <-ctx.Done(): // timeout + c.errorf("elastic: %s is dead", conn.URL()) + conn.MarkAsDead() + case err := <-errc: + if err != nil { + c.errorf("elastic: %s is dead", conn.URL()) + conn.MarkAsDead() + break + } + if status >= 200 && status < 300 { + conn.MarkAsAlive() + } else { + conn.MarkAsDead() + c.errorf("elastic: %s is dead [status=%d]", conn.URL(), status) + } + } + } +} + +// startupHealthcheck is used at startup to check if the server is available +// at all. +func (c *Client) startupHealthcheck(parentCtx context.Context, timeout time.Duration) error { + c.mu.Lock() + urls := c.urls + basicAuth := c.basicAuth + basicAuthUsername := c.basicAuthUsername + basicAuthPassword := c.basicAuthPassword + c.mu.Unlock() + + // If we don't get a connection after "timeout", we bail. + var lastErr error + start := time.Now() + done := false + for !done { + for _, url := range urls { + req, err := http.NewRequest("HEAD", url, nil) + if err != nil { + return err + } + if basicAuth { + req.SetBasicAuth(basicAuthUsername, basicAuthPassword) + } + ctx, cancel := context.WithTimeout(parentCtx, timeout) + defer cancel() + req = req.WithContext(ctx) + res, err := c.c.Do(req) + if err == nil && res != nil && res.StatusCode >= 200 && res.StatusCode < 300 { + return nil + } else if err != nil { + lastErr = err + } + } + select { + case <-parentCtx.Done(): + lastErr = parentCtx.Err() + done = true + case <-time.After(1 * time.Second): + if time.Since(start) > timeout { + done = true + } + } + } + if lastErr != nil { + if IsContextErr(lastErr) { + return lastErr + } + return errors.Wrapf(ErrNoClient, "health check timeout: %v", lastErr) + } + return errors.Wrap(ErrNoClient, "health check timeout") +} + +// next returns the next available connection, or ErrNoClient. +func (c *Client) next() (*conn, error) { + // We do round-robin here. + // TODO(oe) This should be a pluggable strategy, like the Selector in the official clients. + c.connsMu.Lock() + defer c.connsMu.Unlock() + + i := 0 + numConns := len(c.conns) + for { + i++ + if i > numConns { + break // we visited all conns: they all seem to be dead + } + c.cindex++ + if c.cindex >= numConns { + c.cindex = 0 + } + conn := c.conns[c.cindex] + if !conn.IsDead() { + return conn, nil + } + } + + // We have a deadlock here: All nodes are marked as dead. + // If sniffing is disabled, connections will never be marked alive again. + // So we are marking them as alive--if sniffing is disabled. + // They'll then be picked up in the next call to PerformRequest. + if !c.snifferEnabled { + c.errorf("elastic: all %d nodes marked as dead; resurrecting them to prevent deadlock", len(c.conns)) + for _, conn := range c.conns { + conn.MarkAsAlive() + } + } + + // We tried hard, but there is no node available + return nil, errors.Wrap(ErrNoClient, "no available connection") +} + +// mustActiveConn returns nil if there is an active connection, +// otherwise ErrNoClient is returned. +func (c *Client) mustActiveConn() error { + c.connsMu.Lock() + defer c.connsMu.Unlock() + + for _, c := range c.conns { + if !c.IsDead() { + return nil + } + } + return errors.Wrap(ErrNoClient, "no active connection found") +} + +// -- PerformRequest -- + +// PerformRequestOptions must be passed into PerformRequest. +type PerformRequestOptions struct { + Method string + Path string + Params url.Values + Body interface{} + ContentType string + IgnoreErrors []int + Retrier Retrier + Headers http.Header + MaxResponseSize int64 +} + +// PerformRequest does a HTTP request to Elasticsearch. +// It returns a response (which might be nil) and an error on failure. +// +// Optionally, a list of HTTP error codes to ignore can be passed. +// This is necessary for services that expect e.g. HTTP status 404 as a +// valid outcome (Exists, IndicesExists, IndicesTypeExists). +func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions) (*Response, error) { + start := time.Now().UTC() + + c.mu.RLock() + timeout := c.healthcheckTimeout + basicAuth := c.basicAuth + basicAuthUsername := c.basicAuthUsername + basicAuthPassword := c.basicAuthPassword + sendGetBodyAs := c.sendGetBodyAs + gzipEnabled := c.gzipEnabled + retrier := c.retrier + if opt.Retrier != nil { + retrier = opt.Retrier + } + defaultHeaders := c.headers + c.mu.RUnlock() + + var err error + var conn *conn + var req *Request + var resp *Response + var retried bool + var n int + + // Change method if sendGetBodyAs is specified. + if opt.Method == "GET" && opt.Body != nil && sendGetBodyAs != "GET" { + opt.Method = sendGetBodyAs + } + + for { + pathWithParams := opt.Path + if len(opt.Params) > 0 { + pathWithParams += "?" + opt.Params.Encode() + } + + // Get a connection + conn, err = c.next() + if errors.Cause(err) == ErrNoClient { + n++ + if !retried { + // Force a healtcheck as all connections seem to be dead. + c.healthcheck(ctx, timeout, false) + } + wait, ok, rerr := retrier.Retry(ctx, n, nil, nil, err) + if rerr != nil { + return nil, rerr + } + if !ok { + return nil, err + } + retried = true + time.Sleep(wait) + continue // try again + } + if err != nil { + c.errorf("elastic: cannot get connection from pool") + return nil, err + } + + req, err = NewRequest(opt.Method, conn.URL()+pathWithParams) + if err != nil { + c.errorf("elastic: cannot create request for %s %s: %v", strings.ToUpper(opt.Method), conn.URL()+pathWithParams, err) + return nil, err + } + if basicAuth { + req.SetBasicAuth(basicAuthUsername, basicAuthPassword) + } + if opt.ContentType != "" { + req.Header.Set("Content-Type", opt.ContentType) + } + if len(opt.Headers) > 0 { + for key, value := range opt.Headers { + for _, v := range value { + req.Header.Add(key, v) + } + } + } + if len(defaultHeaders) > 0 { + for key, value := range defaultHeaders { + for _, v := range value { + req.Header.Add(key, v) + } + } + } + + // Set body + if opt.Body != nil { + err = req.SetBody(opt.Body, gzipEnabled) + if err != nil { + c.errorf("elastic: couldn't set body %+v for request: %v", opt.Body, err) + return nil, err + } + } + + // Tracing + c.dumpRequest((*http.Request)(req)) + + // Get response + res, err := c.c.Do((*http.Request)(req).WithContext(ctx)) + if IsContextErr(err) { + // Proceed, but don't mark the node as dead + return nil, err + } + if err != nil { + n++ + wait, ok, rerr := retrier.Retry(ctx, n, (*http.Request)(req), res, err) + if rerr != nil { + c.errorf("elastic: %s is dead", conn.URL()) + conn.MarkAsDead() + return nil, rerr + } + if !ok { + c.errorf("elastic: %s is dead", conn.URL()) + conn.MarkAsDead() + return nil, err + } + retried = true + time.Sleep(wait) + continue // try again + } + defer res.Body.Close() + + // Tracing + c.dumpResponse(res) + + // Log deprecation warnings as errors + if len(res.Header["Warning"]) > 0 { + c.deprecationlog((*http.Request)(req), res) + for _, warning := range res.Header["Warning"] { + c.errorf("Deprecation warning: %s", warning) + } + } + + // Check for errors + if err := checkResponse((*http.Request)(req), res, opt.IgnoreErrors...); err != nil { + // No retry if request succeeded + // We still try to return a response. + resp, _ = c.newResponse(res, opt.MaxResponseSize) + return resp, err + } + + // We successfully made a request with this connection + conn.MarkAsHealthy() + + resp, err = c.newResponse(res, opt.MaxResponseSize) + if err != nil { + return nil, err + } + + break + } + + duration := time.Now().UTC().Sub(start) + c.infof("%s %s [status:%d, request:%.3fs]", + strings.ToUpper(opt.Method), + req.URL, + resp.StatusCode, + float64(int64(duration/time.Millisecond))/1000) + + return resp, nil +} + +// -- Document APIs -- + +// Index a document. +func (c *Client) Index() *IndexService { + return NewIndexService(c) +} + +// Get a document. +func (c *Client) Get() *GetService { + return NewGetService(c) +} + +// MultiGet retrieves multiple documents in one roundtrip. +func (c *Client) MultiGet() *MgetService { + return NewMgetService(c) +} + +// Mget retrieves multiple documents in one roundtrip. +func (c *Client) Mget() *MgetService { + return NewMgetService(c) +} + +// Delete a document. +func (c *Client) Delete() *DeleteService { + return NewDeleteService(c) +} + +// DeleteByQuery deletes documents as found by a query. +func (c *Client) DeleteByQuery(indices ...string) *DeleteByQueryService { + return NewDeleteByQueryService(c).Index(indices...) +} + +// Update a document. +func (c *Client) Update() *UpdateService { + return NewUpdateService(c) +} + +// UpdateByQuery performs an update on a set of documents. +func (c *Client) UpdateByQuery(indices ...string) *UpdateByQueryService { + return NewUpdateByQueryService(c).Index(indices...) +} + +// Bulk is the entry point to mass insert/update/delete documents. +func (c *Client) Bulk() *BulkService { + return NewBulkService(c) +} + +// BulkProcessor allows setting up a concurrent processor of bulk requests. +func (c *Client) BulkProcessor() *BulkProcessorService { + return NewBulkProcessorService(c) +} + +// Reindex copies data from a source index into a destination index. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-reindex.html +// for details on the Reindex API. +func (c *Client) Reindex() *ReindexService { + return NewReindexService(c) +} + +// TermVectors returns information and statistics on terms in the fields +// of a particular document. +func (c *Client) TermVectors(index string) *TermvectorsService { + builder := NewTermvectorsService(c) + builder = builder.Index(index) + return builder +} + +// MultiTermVectors returns information and statistics on terms in the fields +// of multiple documents. +func (c *Client) MultiTermVectors() *MultiTermvectorService { + return NewMultiTermvectorService(c) +} + +// -- Search APIs -- + +// Search is the entry point for searches. +func (c *Client) Search(indices ...string) *SearchService { + return NewSearchService(c).Index(indices...) +} + +// MultiSearch is the entry point for multi searches. +func (c *Client) MultiSearch() *MultiSearchService { + return NewMultiSearchService(c) +} + +// Count documents. +func (c *Client) Count(indices ...string) *CountService { + return NewCountService(c).Index(indices...) +} + +// Explain computes a score explanation for a query and a specific document. +func (c *Client) Explain(index, typ, id string) *ExplainService { + return NewExplainService(c).Index(index).Type(typ).Id(id) +} + +// TODO Search Template +// TODO Search Exists API + +// Validate allows a user to validate a potentially expensive query without executing it. +func (c *Client) Validate(indices ...string) *ValidateService { + return NewValidateService(c).Index(indices...) +} + +// SearchShards returns statistical information about nodes and shards. +func (c *Client) SearchShards(indices ...string) *SearchShardsService { + return NewSearchShardsService(c).Index(indices...) +} + +// FieldCaps returns statistical information about fields in indices. +func (c *Client) FieldCaps(indices ...string) *FieldCapsService { + return NewFieldCapsService(c).Index(indices...) +} + +// Exists checks if a document exists. +func (c *Client) Exists() *ExistsService { + return NewExistsService(c) +} + +// Scroll through documents. Use this to efficiently scroll through results +// while returning the results to a client. +func (c *Client) Scroll(indices ...string) *ScrollService { + return NewScrollService(c).Index(indices...) +} + +// ClearScroll can be used to clear search contexts manually. +func (c *Client) ClearScroll(scrollIds ...string) *ClearScrollService { + return NewClearScrollService(c).ScrollId(scrollIds...) +} + +// -- Indices APIs -- + +// CreateIndex returns a service to create a new index. +func (c *Client) CreateIndex(name string) *IndicesCreateService { + return NewIndicesCreateService(c).Index(name) +} + +// DeleteIndex returns a service to delete an index. +func (c *Client) DeleteIndex(indices ...string) *IndicesDeleteService { + return NewIndicesDeleteService(c).Index(indices) +} + +// IndexExists allows to check if an index exists. +func (c *Client) IndexExists(indices ...string) *IndicesExistsService { + return NewIndicesExistsService(c).Index(indices) +} + +// ShrinkIndex returns a service to shrink one index into another. +func (c *Client) ShrinkIndex(source, target string) *IndicesShrinkService { + return NewIndicesShrinkService(c).Source(source).Target(target) +} + +// RolloverIndex rolls an alias over to a new index when the existing index +// is considered to be too large or too old. +func (c *Client) RolloverIndex(alias string) *IndicesRolloverService { + return NewIndicesRolloverService(c).Alias(alias) +} + +// IndexStats provides statistics on different operations happining +// in one or more indices. +func (c *Client) IndexStats(indices ...string) *IndicesStatsService { + return NewIndicesStatsService(c).Index(indices...) +} + +// OpenIndex opens an index. +func (c *Client) OpenIndex(name string) *IndicesOpenService { + return NewIndicesOpenService(c).Index(name) +} + +// CloseIndex closes an index. +func (c *Client) CloseIndex(name string) *IndicesCloseService { + return NewIndicesCloseService(c).Index(name) +} + +// FreezeIndex freezes an index. +func (c *Client) FreezeIndex(name string) *IndicesFreezeService { + return NewIndicesFreezeService(c).Index(name) +} + +// UnfreezeIndex unfreezes an index. +func (c *Client) UnfreezeIndex(name string) *IndicesUnfreezeService { + return NewIndicesUnfreezeService(c).Index(name) +} + +// IndexGet retrieves information about one or more indices. +// IndexGet is only available for Elasticsearch 1.4 or later. +func (c *Client) IndexGet(indices ...string) *IndicesGetService { + return NewIndicesGetService(c).Index(indices...) +} + +// IndexGetSettings retrieves settings of all, one or more indices. +func (c *Client) IndexGetSettings(indices ...string) *IndicesGetSettingsService { + return NewIndicesGetSettingsService(c).Index(indices...) +} + +// IndexPutSettings sets settings for all, one or more indices. +func (c *Client) IndexPutSettings(indices ...string) *IndicesPutSettingsService { + return NewIndicesPutSettingsService(c).Index(indices...) +} + +// IndexSegments retrieves low level segment information for all, one or more indices. +func (c *Client) IndexSegments(indices ...string) *IndicesSegmentsService { + return NewIndicesSegmentsService(c).Index(indices...) +} + +// IndexAnalyze performs the analysis process on a text and returns the +// token breakdown of the text. +func (c *Client) IndexAnalyze() *IndicesAnalyzeService { + return NewIndicesAnalyzeService(c) +} + +// Forcemerge optimizes one or more indices. +// It replaces the deprecated Optimize API. +func (c *Client) Forcemerge(indices ...string) *IndicesForcemergeService { + return NewIndicesForcemergeService(c).Index(indices...) +} + +// Refresh asks Elasticsearch to refresh one or more indices. +func (c *Client) Refresh(indices ...string) *RefreshService { + return NewRefreshService(c).Index(indices...) +} + +// Flush asks Elasticsearch to free memory from the index and +// flush data to disk. +func (c *Client) Flush(indices ...string) *IndicesFlushService { + return NewIndicesFlushService(c).Index(indices...) +} + +// SyncedFlush performs a synced flush. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-synced-flush.html +// for more details on synched flushes and how they differ from a normal +// Flush. +func (c *Client) SyncedFlush(indices ...string) *IndicesSyncedFlushService { + return NewIndicesSyncedFlushService(c).Index(indices...) +} + +// Alias enables the caller to add and/or remove aliases. +func (c *Client) Alias() *AliasService { + return NewAliasService(c) +} + +// Aliases returns aliases by index name(s). +func (c *Client) Aliases() *AliasesService { + return NewAliasesService(c) +} + +// IndexGetTemplate gets an index template. +// Use XXXTemplate funcs to manage search templates. +func (c *Client) IndexGetTemplate(names ...string) *IndicesGetTemplateService { + return NewIndicesGetTemplateService(c).Name(names...) +} + +// IndexTemplateExists gets check if an index template exists. +// Use XXXTemplate funcs to manage search templates. +func (c *Client) IndexTemplateExists(name string) *IndicesExistsTemplateService { + return NewIndicesExistsTemplateService(c).Name(name) +} + +// IndexPutTemplate creates or updates an index template. +// Use XXXTemplate funcs to manage search templates. +func (c *Client) IndexPutTemplate(name string) *IndicesPutTemplateService { + return NewIndicesPutTemplateService(c).Name(name) +} + +// IndexDeleteTemplate deletes an index template. +// Use XXXTemplate funcs to manage search templates. +func (c *Client) IndexDeleteTemplate(name string) *IndicesDeleteTemplateService { + return NewIndicesDeleteTemplateService(c).Name(name) +} + +// GetMapping gets a mapping. +func (c *Client) GetMapping() *IndicesGetMappingService { + return NewIndicesGetMappingService(c) +} + +// PutMapping registers a mapping. +func (c *Client) PutMapping() *IndicesPutMappingService { + return NewIndicesPutMappingService(c) +} + +// GetFieldMapping gets mapping for fields. +func (c *Client) GetFieldMapping() *IndicesGetFieldMappingService { + return NewIndicesGetFieldMappingService(c) +} + +// -- cat APIs -- + +// TODO cat fielddata +// TODO cat master +// TODO cat nodes +// TODO cat pending tasks +// TODO cat plugins +// TODO cat recovery +// TODO cat thread pool +// TODO cat shards +// TODO cat segments + +// CatAliases returns information about aliases. +func (c *Client) CatAliases() *CatAliasesService { + return NewCatAliasesService(c) +} + +// CatAllocation returns information about the allocation across nodes. +func (c *Client) CatAllocation() *CatAllocationService { + return NewCatAllocationService(c) +} + +// CatCount returns document counts for indices. +func (c *Client) CatCount() *CatCountService { + return NewCatCountService(c) +} + +// CatHealth returns information about cluster health. +func (c *Client) CatHealth() *CatHealthService { + return NewCatHealthService(c) +} + +// CatIndices returns information about indices. +func (c *Client) CatIndices() *CatIndicesService { + return NewCatIndicesService(c) +} + +// -- Ingest APIs -- + +// IngestPutPipeline adds pipelines and updates existing pipelines in +// the cluster. +func (c *Client) IngestPutPipeline(id string) *IngestPutPipelineService { + return NewIngestPutPipelineService(c).Id(id) +} + +// IngestGetPipeline returns pipelines based on ID. +func (c *Client) IngestGetPipeline(ids ...string) *IngestGetPipelineService { + return NewIngestGetPipelineService(c).Id(ids...) +} + +// IngestDeletePipeline deletes a pipeline by ID. +func (c *Client) IngestDeletePipeline(id string) *IngestDeletePipelineService { + return NewIngestDeletePipelineService(c).Id(id) +} + +// IngestSimulatePipeline executes a specific pipeline against the set of +// documents provided in the body of the request. +func (c *Client) IngestSimulatePipeline() *IngestSimulatePipelineService { + return NewIngestSimulatePipelineService(c) +} + +// -- Cluster APIs -- + +// ClusterHealth retrieves the health of the cluster. +func (c *Client) ClusterHealth() *ClusterHealthService { + return NewClusterHealthService(c) +} + +// ClusterReroute allows for manual changes to the allocation of +// individual shards in the cluster. +func (c *Client) ClusterReroute() *ClusterRerouteService { + return NewClusterRerouteService(c) +} + +// ClusterState retrieves the state of the cluster. +func (c *Client) ClusterState() *ClusterStateService { + return NewClusterStateService(c) +} + +// ClusterStats retrieves cluster statistics. +func (c *Client) ClusterStats() *ClusterStatsService { + return NewClusterStatsService(c) +} + +// NodesInfo retrieves one or more or all of the cluster nodes information. +func (c *Client) NodesInfo() *NodesInfoService { + return NewNodesInfoService(c) +} + +// NodesStats retrieves one or more or all of the cluster nodes statistics. +func (c *Client) NodesStats() *NodesStatsService { + return NewNodesStatsService(c) +} + +// TasksCancel cancels tasks running on the specified nodes. +func (c *Client) TasksCancel() *TasksCancelService { + return NewTasksCancelService(c) +} + +// TasksList retrieves the list of tasks running on the specified nodes. +func (c *Client) TasksList() *TasksListService { + return NewTasksListService(c) +} + +// TasksGetTask retrieves a task running on the cluster. +func (c *Client) TasksGetTask() *TasksGetTaskService { + return NewTasksGetTaskService(c) +} + +// TODO Pending cluster tasks +// TODO Cluster Reroute +// TODO Cluster Update Settings +// TODO Nodes Stats +// TODO Nodes hot_threads + +// -- Snapshot and Restore -- + +// TODO Snapshot Delete +// TODO Snapshot Get +// TODO Snapshot Restore +// TODO Snapshot Status + +// SnapshotCreate creates a snapshot. +func (c *Client) SnapshotCreate(repository string, snapshot string) *SnapshotCreateService { + return NewSnapshotCreateService(c).Repository(repository).Snapshot(snapshot) +} + +// SnapshotCreateRepository creates or updates a snapshot repository. +func (c *Client) SnapshotCreateRepository(repository string) *SnapshotCreateRepositoryService { + return NewSnapshotCreateRepositoryService(c).Repository(repository) +} + +// SnapshotDelete deletes a snapshot in a snapshot repository. +func (c *Client) SnapshotDelete(repository string, snapshot string) *SnapshotDeleteService { + return NewSnapshotDeleteService(c).Repository(repository).Snapshot(snapshot) +} + +// SnapshotDeleteRepository deletes a snapshot repository. +func (c *Client) SnapshotDeleteRepository(repositories ...string) *SnapshotDeleteRepositoryService { + return NewSnapshotDeleteRepositoryService(c).Repository(repositories...) +} + +// SnapshotGetRepository gets a snapshot repository. +func (c *Client) SnapshotGetRepository(repositories ...string) *SnapshotGetRepositoryService { + return NewSnapshotGetRepositoryService(c).Repository(repositories...) +} + +// SnapshotGet lists snapshot for a repository. +func (c *Client) SnapshotGet(repository string) *SnapshotGetService { + return NewSnapshotGetService(c).Repository(repository) +} + +// SnapshotVerifyRepository verifies a snapshot repository. +func (c *Client) SnapshotVerifyRepository(repository string) *SnapshotVerifyRepositoryService { + return NewSnapshotVerifyRepositoryService(c).Repository(repository) +} + +// SnapshotRestore restores the specified indices from a given snapshot +func (c *Client) SnapshotRestore(repository string, snapshot string) *SnapshotRestoreService { + return NewSnapshotRestoreService(c).Repository(repository).Snapshot(snapshot) +} + +// -- Scripting APIs -- + +// GetScript reads a stored script in Elasticsearch. +// Use PutScript for storing a script. +func (c *Client) GetScript() *GetScriptService { + return NewGetScriptService(c) +} + +// PutScript allows saving a stored script in Elasticsearch. +func (c *Client) PutScript() *PutScriptService { + return NewPutScriptService(c) +} + +// DeleteScript allows removing a stored script from Elasticsearch. +func (c *Client) DeleteScript() *DeleteScriptService { + return NewDeleteScriptService(c) +} + +// -- X-Pack General -- + +// XPackInfo gets information on the xpack plugins enabled on the cluster + +func (c *Client) XPackInfo() *XPackInfoService { + return NewXPackInfoService(c) +} + +// -- X-Pack Index Lifecycle Management -- + +// XPackIlmPutLifecycle adds or modifies an ilm policy. +func (c *Client) XPackIlmPutLifecycle() *XPackIlmPutLifecycleService { + return NewXPackIlmPutLifecycleService(c) +} + +// XPackIlmGettLifecycle gets an ilm policy. +func (c *Client) XPackIlmGetLifecycle() *XPackIlmGetLifecycleService { + return NewXPackIlmGetLifecycleService(c) +} + +// XPackIlmDeleteLifecycle deletes an ilm policy. +func (c *Client) XPackIlmDeleteLifecycle() *XPackIlmDeleteLifecycleService { + return NewXPackIlmDeleteLifecycleService(c) +} + +// -- X-Pack Security -- + +// XPackSecurityGetRoleMapping gets a role mapping. +func (c *Client) XPackSecurityGetRoleMapping(roleMappingName string) *XPackSecurityGetRoleMappingService { + return NewXPackSecurityGetRoleMappingService(c).Name(roleMappingName) +} + +// XPackSecurityPutRoleMapping adds a role mapping. +func (c *Client) XPackSecurityPutRoleMapping(roleMappingName string) *XPackSecurityPutRoleMappingService { + return NewXPackSecurityPutRoleMappingService(c).Name(roleMappingName) +} + +// XPackSecurityDeleteRoleMapping deletes a role mapping. +func (c *Client) XPackSecurityDeleteRoleMapping(roleMappingName string) *XPackSecurityDeleteRoleMappingService { + return NewXPackSecurityDeleteRoleMappingService(c).Name(roleMappingName) +} + +// XPackSecurityGetRole gets a role. +func (c *Client) XPackSecurityGetRole(roleName string) *XPackSecurityGetRoleService { + return NewXPackSecurityGetRoleService(c).Name(roleName) +} + +// XPackSecurityPutRole adds a role. +func (c *Client) XPackSecurityPutRole(roleName string) *XPackSecurityPutRoleService { + return NewXPackSecurityPutRoleService(c).Name(roleName) +} + +// XPackSecurityDeleteRole deletes a role. +func (c *Client) XPackSecurityDeleteRole(roleName string) *XPackSecurityDeleteRoleService { + return NewXPackSecurityDeleteRoleService(c).Name(roleName) +} + +// TODO: Clear role cache API +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-clear-role-cache.html + +// XPackSecurityChangePassword changes the password of users in the native realm. +func (c *Client) XPackSecurityChangePassword(username string) *XPackSecurityChangePasswordService { + return NewXPackSecurityChangePasswordService(c).Username(username) +} + +// XPackSecurityGetUser gets details about one or more users. +func (c *Client) XPackSecurityGetUser(usernames ...string) *XPackSecurityGetUserService { + return NewXPackSecurityGetUserService(c).Usernames(usernames...) +} + +// XPackSecurityPutUser adds or updates a user. +func (c *Client) XPackSecurityPutUser(username string) *XPackSecurityPutUserService { + return NewXPackSecurityPutUserService(c).Username(username) +} + +// XPackSecurityEnableUser enables a user. +func (c *Client) XPackSecurityEnableUser(username string) *XPackSecurityEnableUserService { + return NewXPackSecurityEnableUserService(c).Username(username) +} + +// XPackSecurityDisableUser disables a user. +func (c *Client) XPackSecurityDisableUser(username string) *XPackSecurityDisableUserService { + return NewXPackSecurityDisableUserService(c).Username(username) +} + +// XPackSecurityDeleteUser deletes a user. +func (c *Client) XPackSecurityDeleteUser(username string) *XPackSecurityDeleteUserService { + return NewXPackSecurityDeleteUserService(c).Username(username) +} + +// -- X-Pack Watcher -- + +// XPackWatchPut adds a watch. +func (c *Client) XPackWatchPut(watchId string) *XPackWatcherPutWatchService { + return NewXPackWatcherPutWatchService(c).Id(watchId) +} + +// XPackWatchGet gets a watch. +func (c *Client) XPackWatchGet(watchId string) *XPackWatcherGetWatchService { + return NewXPackWatcherGetWatchService(c).Id(watchId) +} + +// XPackWatchDelete deletes a watch. +func (c *Client) XPackWatchDelete(watchId string) *XPackWatcherDeleteWatchService { + return NewXPackWatcherDeleteWatchService(c).Id(watchId) +} + +// XPackWatchExecute executes a watch. +func (c *Client) XPackWatchExecute() *XPackWatcherExecuteWatchService { + return NewXPackWatcherExecuteWatchService(c) +} + +// XPackWatchAck acknowledging a watch. +func (c *Client) XPackWatchAck(watchId string) *XPackWatcherAckWatchService { + return NewXPackWatcherAckWatchService(c).WatchId(watchId) +} + +// XPackWatchActivate activates a watch. +func (c *Client) XPackWatchActivate(watchId string) *XPackWatcherActivateWatchService { + return NewXPackWatcherActivateWatchService(c).WatchId(watchId) +} + +// XPackWatchDeactivate deactivates a watch. +func (c *Client) XPackWatchDeactivate(watchId string) *XPackWatcherDeactivateWatchService { + return NewXPackWatcherDeactivateWatchService(c).WatchId(watchId) +} + +// XPackWatchStats returns the current Watcher metrics. +func (c *Client) XPackWatchStats() *XPackWatcherStatsService { + return NewXPackWatcherStatsService(c) +} + +// XPackWatchStart starts a watch. +func (c *Client) XPackWatchStart() *XPackWatcherStartService { + return NewXPackWatcherStartService(c) +} + +// XPackWatchStop stops a watch. +func (c *Client) XPackWatchStop() *XPackWatcherStopService { + return NewXPackWatcherStopService(c) +} + +// -- Helpers and shortcuts -- + +// ElasticsearchVersion returns the version number of Elasticsearch +// running on the given URL. +func (c *Client) ElasticsearchVersion(url string) (string, error) { + res, _, err := c.Ping(url).Do(context.Background()) + if err != nil { + return "", err + } + return res.Version.Number, nil +} + +// IndexNames returns the names of all indices in the cluster. +func (c *Client) IndexNames() ([]string, error) { + res, err := c.IndexGetSettings().Index("_all").Do(context.Background()) + if err != nil { + return nil, err + } + var names []string + for name := range res { + names = append(names, name) + } + return names, nil +} + +// Ping checks if a given node in a cluster exists and (optionally) +// returns some basic information about the Elasticsearch server, +// e.g. the Elasticsearch version number. +// +// Notice that you need to specify a URL here explicitly. +func (c *Client) Ping(url string) *PingService { + return NewPingService(c).URL(url) +} + +// WaitForStatus waits for the cluster to have the given status. +// This is a shortcut method for the ClusterHealth service. +// +// WaitForStatus waits for the specified timeout, e.g. "10s". +// If the cluster will have the given state within the timeout, nil is returned. +// If the request timed out, ErrTimeout is returned. +func (c *Client) WaitForStatus(status string, timeout string) error { + health, err := c.ClusterHealth().WaitForStatus(status).Timeout(timeout).Do(context.Background()) + if err != nil { + return err + } + if health.TimedOut { + return ErrTimeout + } + return nil +} + +// WaitForGreenStatus waits for the cluster to have the "green" status. +// See WaitForStatus for more details. +func (c *Client) WaitForGreenStatus(timeout string) error { + return c.WaitForStatus("green", timeout) +} + +// WaitForYellowStatus waits for the cluster to have the "yellow" status. +// See WaitForStatus for more details. +func (c *Client) WaitForYellowStatus(timeout string) error { + return c.WaitForStatus("yellow", timeout) +} diff --git a/vendor/github.com/olivere/elastic/v7/cluster_health.go b/vendor/github.com/olivere/elastic/v7/cluster_health.go new file mode 100644 index 0000000000..eba5e22120 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cluster_health.go @@ -0,0 +1,296 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// ClusterHealthService allows to get a very simple status on the health of the cluster. +// +// See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-health.html +// for details. +type ClusterHealthService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + indices []string + level string + local *bool + masterTimeout string + timeout string + waitForActiveShards *int + waitForNodes string + waitForNoRelocatingShards *bool + waitForStatus string +} + +// NewClusterHealthService creates a new ClusterHealthService. +func NewClusterHealthService(client *Client) *ClusterHealthService { + return &ClusterHealthService{ + client: client, + indices: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ClusterHealthService) Pretty(pretty bool) *ClusterHealthService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ClusterHealthService) Human(human bool) *ClusterHealthService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ClusterHealthService) ErrorTrace(errorTrace bool) *ClusterHealthService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ClusterHealthService) FilterPath(filterPath ...string) *ClusterHealthService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ClusterHealthService) Header(name string, value string) *ClusterHealthService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ClusterHealthService) Headers(headers http.Header) *ClusterHealthService { + s.headers = headers + return s +} + +// Index limits the information returned to specific indices. +func (s *ClusterHealthService) Index(indices ...string) *ClusterHealthService { + s.indices = append(s.indices, indices...) + return s +} + +// Level specifies the level of detail for returned information. +func (s *ClusterHealthService) Level(level string) *ClusterHealthService { + s.level = level + return s +} + +// Local indicates whether to return local information. If it is true, +// we do not retrieve the state from master node (default: false). +func (s *ClusterHealthService) Local(local bool) *ClusterHealthService { + s.local = &local + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *ClusterHealthService) MasterTimeout(masterTimeout string) *ClusterHealthService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout specifies an explicit operation timeout. +func (s *ClusterHealthService) Timeout(timeout string) *ClusterHealthService { + s.timeout = timeout + return s +} + +// WaitForActiveShards can be used to wait until the specified number of shards are active. +func (s *ClusterHealthService) WaitForActiveShards(waitForActiveShards int) *ClusterHealthService { + s.waitForActiveShards = &waitForActiveShards + return s +} + +// WaitForNodes can be used to wait until the specified number of nodes are available. +// Example: "12" to wait for exact values, ">12" and "<12" for ranges. +func (s *ClusterHealthService) WaitForNodes(waitForNodes string) *ClusterHealthService { + s.waitForNodes = waitForNodes + return s +} + +// WaitForNoRelocatingShards can be used to wait until all shard relocations are finished. +func (s *ClusterHealthService) WaitForNoRelocatingShards(waitForNoRelocatingShards bool) *ClusterHealthService { + s.waitForNoRelocatingShards = &waitForNoRelocatingShards + return s +} + +// WaitForStatus can be used to wait until the cluster is in a specific state. +// Valid values are: green, yellow, or red. +func (s *ClusterHealthService) WaitForStatus(waitForStatus string) *ClusterHealthService { + s.waitForStatus = waitForStatus + return s +} + +// WaitForGreenStatus will wait for the "green" state. +func (s *ClusterHealthService) WaitForGreenStatus() *ClusterHealthService { + return s.WaitForStatus("green") +} + +// WaitForYellowStatus will wait for the "yellow" state. +func (s *ClusterHealthService) WaitForYellowStatus() *ClusterHealthService { + return s.WaitForStatus("yellow") +} + +// buildURL builds the URL for the operation. +func (s *ClusterHealthService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.indices) > 0 { + path, err = uritemplates.Expand("/_cluster/health/{index}", map[string]string{ + "index": strings.Join(s.indices, ","), + }) + } else { + path = "/_cluster/health" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.level != "" { + params.Set("level", s.level) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.waitForActiveShards != nil { + params.Set("wait_for_active_shards", fmt.Sprintf("%v", s.waitForActiveShards)) + } + if s.waitForNodes != "" { + params.Set("wait_for_nodes", s.waitForNodes) + } + if s.waitForNoRelocatingShards != nil { + params.Set("wait_for_no_relocating_shards", fmt.Sprintf("%v", *s.waitForNoRelocatingShards)) + } + if s.waitForStatus != "" { + params.Set("wait_for_status", s.waitForStatus) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ClusterHealthService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *ClusterHealthService) Do(ctx context.Context) (*ClusterHealthResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ClusterHealthResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ClusterHealthResponse is the response of ClusterHealthService.Do. +type ClusterHealthResponse struct { + ClusterName string `json:"cluster_name"` + Status string `json:"status"` + TimedOut bool `json:"timed_out"` + NumberOfNodes int `json:"number_of_nodes"` + NumberOfDataNodes int `json:"number_of_data_nodes"` + ActivePrimaryShards int `json:"active_primary_shards"` + ActiveShards int `json:"active_shards"` + RelocatingShards int `json:"relocating_shards"` + InitializingShards int `json:"initializing_shards"` + UnassignedShards int `json:"unassigned_shards"` + DelayedUnassignedShards int `json:"delayed_unassigned_shards"` + NumberOfPendingTasks int `json:"number_of_pending_tasks"` + NumberOfInFlightFetch int `json:"number_of_in_flight_fetch"` + TaskMaxWaitTimeInQueue string `json:"task_max_waiting_in_queue"` // "0s" + TaskMaxWaitTimeInQueueInMillis int `json:"task_max_waiting_in_queue_millis"` // 0 + ActiveShardsPercent string `json:"active_shards_percent"` // "100.0%" + ActiveShardsPercentAsNumber float64 `json:"active_shards_percent_as_number"` // 100.0 + + // Index name -> index health + Indices map[string]*ClusterIndexHealth `json:"indices"` +} + +// ClusterIndexHealth will be returned as part of ClusterHealthResponse. +type ClusterIndexHealth struct { + Status string `json:"status"` + NumberOfShards int `json:"number_of_shards"` + NumberOfReplicas int `json:"number_of_replicas"` + ActivePrimaryShards int `json:"active_primary_shards"` + ActiveShards int `json:"active_shards"` + RelocatingShards int `json:"relocating_shards"` + InitializingShards int `json:"initializing_shards"` + UnassignedShards int `json:"unassigned_shards"` + // Shards by id, e.g. "0" or "1" + Shards map[string]*ClusterShardHealth `json:"shards"` +} + +// ClusterShardHealth will be returned as part of ClusterHealthResponse. +type ClusterShardHealth struct { + Status string `json:"status"` + PrimaryActive bool `json:"primary_active"` + ActiveShards int `json:"active_shards"` + RelocatingShards int `json:"relocating_shards"` + InitializingShards int `json:"initializing_shards"` + UnassignedShards int `json:"unassigned_shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/cluster_reroute.go b/vendor/github.com/olivere/elastic/v7/cluster_reroute.go new file mode 100644 index 0000000000..92a96255fe --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cluster_reroute.go @@ -0,0 +1,438 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/url" + "strings" +) + +// ClusterRerouteService allows for manual changes to the allocation of +// individual shards in the cluster. For example, a shard can be moved from +// one node to another explicitly, an allocation can be cancelled, and +// an unassigned shard can be explicitly allocated to a specific node. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-reroute.html +// for details. +type ClusterRerouteService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + metrics []string + dryRun *bool + explain *bool + retryFailed *bool + masterTimeout string + timeout string + commands []AllocationCommand + body interface{} +} + +// NewClusterRerouteService creates a new ClusterRerouteService. +func NewClusterRerouteService(client *Client) *ClusterRerouteService { + return &ClusterRerouteService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ClusterRerouteService) Pretty(pretty bool) *ClusterRerouteService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ClusterRerouteService) Human(human bool) *ClusterRerouteService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ClusterRerouteService) ErrorTrace(errorTrace bool) *ClusterRerouteService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ClusterRerouteService) FilterPath(filterPath ...string) *ClusterRerouteService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ClusterRerouteService) Header(name string, value string) *ClusterRerouteService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ClusterRerouteService) Headers(headers http.Header) *ClusterRerouteService { + s.headers = headers + return s +} + +// Metric limits the information returned to the specified metric. +// It can be one of: "_all", "blocks", "metadata", "nodes", "routing_table", "master_node", "version". +// Defaults to all but metadata. +func (s *ClusterRerouteService) Metric(metrics ...string) *ClusterRerouteService { + s.metrics = append(s.metrics, metrics...) + return s +} + +// DryRun indicates whether to simulate the operation only and return the +// resulting state. +func (s *ClusterRerouteService) DryRun(dryRun bool) *ClusterRerouteService { + s.dryRun = &dryRun + return s +} + +// Explain, when set to true, returns an explanation of why the commands +// can or cannot be executed. +func (s *ClusterRerouteService) Explain(explain bool) *ClusterRerouteService { + s.explain = &explain + return s +} + +// RetryFailed indicates whether to retry allocation of shards that are blocked +// due to too many subsequent allocation failures. +func (s *ClusterRerouteService) RetryFailed(retryFailed bool) *ClusterRerouteService { + s.retryFailed = &retryFailed + return s +} + +// MasterTimeout specifies an explicit timeout for connection to master. +func (s *ClusterRerouteService) MasterTimeout(masterTimeout string) *ClusterRerouteService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout specifies an explicit operationtimeout. +func (s *ClusterRerouteService) Timeout(timeout string) *ClusterRerouteService { + s.timeout = timeout + return s +} + +// Add adds one or more commands to be executed. +func (s *ClusterRerouteService) Add(commands ...AllocationCommand) *ClusterRerouteService { + s.commands = append(s.commands, commands...) + return s +} + +// Body specifies the body to be sent. +// If you specify Body, the commands passed via Add are ignored. +// In other words: Body takes precedence over Add. +func (s *ClusterRerouteService) Body(body interface{}) *ClusterRerouteService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *ClusterRerouteService) buildURL() (string, url.Values, error) { + // Build URL + path := "/_cluster/reroute" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.dryRun; v != nil { + params.Set("dry_run", fmt.Sprint(*v)) + } + if v := s.explain; v != nil { + params.Set("explain", fmt.Sprint(*v)) + } + if v := s.retryFailed; v != nil { + params.Set("retry_failed", fmt.Sprint(*v)) + } + if len(s.metrics) > 0 { + params.Set("metric", strings.Join(s.metrics, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ClusterRerouteService) Validate() error { + if s.body == nil && len(s.commands) == 0 { + return errors.New("missing allocate commands or raw body") + } + return nil +} + +// Do executes the operation. +func (s *ClusterRerouteService) Do(ctx context.Context) (*ClusterRerouteResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.body != nil { + body = s.body + } else { + commands := make([]interface{}, len(s.commands)) + for i, cmd := range s.commands { + src, err := cmd.Source() + if err != nil { + return nil, err + } + commands[i] = map[string]interface{}{ + cmd.Name(): src, + } + } + query := make(map[string]interface{}) + query["commands"] = commands + body = query + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ClusterRerouteResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ClusterRerouteResponse is the response of ClusterRerouteService.Do. +type ClusterRerouteResponse struct { + State *ClusterStateResponse `json:"state"` + Explanations []RerouteExplanation `json:"explanations,omitempty"` +} + +// RerouteExplanation is returned in ClusterRerouteResponse if +// the "explain" parameter is set to "true". +type RerouteExplanation struct { + Command string `json:"command"` + Parameters map[string]interface{} `json:"parameters"` + Decisions []RerouteDecision `json:"decisions"` +} + +// RerouteDecision is a decision the decider made while rerouting. +type RerouteDecision interface{} + +// -- Allocation commands -- + +// AllocationCommand is a command to be executed in a call +// to Cluster Reroute API. +type AllocationCommand interface { + Name() string + Source() (interface{}, error) +} + +var _ AllocationCommand = (*MoveAllocationCommand)(nil) + +// MoveAllocationCommand moves a shard from a specific node to +// another node. +type MoveAllocationCommand struct { + index string + shardId int + fromNode string + toNode string +} + +// NewMoveAllocationCommand creates a new MoveAllocationCommand. +func NewMoveAllocationCommand(index string, shardId int, fromNode, toNode string) *MoveAllocationCommand { + return &MoveAllocationCommand{ + index: index, + shardId: shardId, + fromNode: fromNode, + toNode: toNode, + } +} + +// Name of the command in a request to the Cluster Reroute API. +func (cmd *MoveAllocationCommand) Name() string { return "move" } + +// Source generates the (inner) JSON to be used when serializing the command. +func (cmd *MoveAllocationCommand) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["index"] = cmd.index + source["shard"] = cmd.shardId + source["from_node"] = cmd.fromNode + source["to_node"] = cmd.toNode + return source, nil +} + +var _ AllocationCommand = (*CancelAllocationCommand)(nil) + +// CancelAllocationCommand cancels relocation, or recovery of a given shard on a node. +type CancelAllocationCommand struct { + index string + shardId int + node string + allowPrimary bool +} + +// NewCancelAllocationCommand creates a new CancelAllocationCommand. +func NewCancelAllocationCommand(index string, shardId int, node string, allowPrimary bool) *CancelAllocationCommand { + return &CancelAllocationCommand{ + index: index, + shardId: shardId, + node: node, + allowPrimary: allowPrimary, + } +} + +// Name of the command in a request to the Cluster Reroute API. +func (cmd *CancelAllocationCommand) Name() string { return "cancel" } + +// Source generates the (inner) JSON to be used when serializing the command. +func (cmd *CancelAllocationCommand) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["index"] = cmd.index + source["shard"] = cmd.shardId + source["node"] = cmd.node + source["allow_primary"] = cmd.allowPrimary + return source, nil +} + +var _ AllocationCommand = (*AllocateStalePrimaryAllocationCommand)(nil) + +// AllocateStalePrimaryAllocationCommand allocates an unassigned stale +// primary shard to a specific node. Use with extreme care as this will +// result in data loss. Allocation deciders are ignored. +type AllocateStalePrimaryAllocationCommand struct { + index string + shardId int + node string + acceptDataLoss bool +} + +// NewAllocateStalePrimaryAllocationCommand creates a new +// AllocateStalePrimaryAllocationCommand. +func NewAllocateStalePrimaryAllocationCommand(index string, shardId int, node string, acceptDataLoss bool) *AllocateStalePrimaryAllocationCommand { + return &AllocateStalePrimaryAllocationCommand{ + index: index, + shardId: shardId, + node: node, + acceptDataLoss: acceptDataLoss, + } +} + +// Name of the command in a request to the Cluster Reroute API. +func (cmd *AllocateStalePrimaryAllocationCommand) Name() string { return "allocate_stale_primary" } + +// Source generates the (inner) JSON to be used when serializing the command. +func (cmd *AllocateStalePrimaryAllocationCommand) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["index"] = cmd.index + source["shard"] = cmd.shardId + source["node"] = cmd.node + source["accept_data_loss"] = cmd.acceptDataLoss + return source, nil +} + +var _ AllocationCommand = (*AllocateReplicaAllocationCommand)(nil) + +// AllocateReplicaAllocationCommand allocates an unassigned replica shard +// to a specific node. Checks if allocation deciders allow allocation. +type AllocateReplicaAllocationCommand struct { + index string + shardId int + node string +} + +// NewAllocateReplicaAllocationCommand creates a new +// AllocateReplicaAllocationCommand. +func NewAllocateReplicaAllocationCommand(index string, shardId int, node string) *AllocateReplicaAllocationCommand { + return &AllocateReplicaAllocationCommand{ + index: index, + shardId: shardId, + node: node, + } +} + +// Name of the command in a request to the Cluster Reroute API. +func (cmd *AllocateReplicaAllocationCommand) Name() string { return "allocate_replica" } + +// Source generates the (inner) JSON to be used when serializing the command. +func (cmd *AllocateReplicaAllocationCommand) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["index"] = cmd.index + source["shard"] = cmd.shardId + source["node"] = cmd.node + return source, nil +} + +// AllocateEmptyPrimaryAllocationCommand allocates an unassigned empty +// primary shard to a specific node. Use with extreme care as this will +// result in data loss. Allocation deciders are ignored. +type AllocateEmptyPrimaryAllocationCommand struct { + index string + shardId int + node string + acceptDataLoss bool +} + +// NewAllocateEmptyPrimaryAllocationCommand creates a new +// AllocateEmptyPrimaryAllocationCommand. +func NewAllocateEmptyPrimaryAllocationCommand(index string, shardId int, node string, acceptDataLoss bool) *AllocateEmptyPrimaryAllocationCommand { + return &AllocateEmptyPrimaryAllocationCommand{ + index: index, + shardId: shardId, + node: node, + acceptDataLoss: acceptDataLoss, + } +} + +// Name of the command in a request to the Cluster Reroute API. +func (cmd *AllocateEmptyPrimaryAllocationCommand) Name() string { return "allocate_empty_primary" } + +// Source generates the (inner) JSON to be used when serializing the command. +func (cmd *AllocateEmptyPrimaryAllocationCommand) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["index"] = cmd.index + source["shard"] = cmd.shardId + source["node"] = cmd.node + source["accept_data_loss"] = cmd.acceptDataLoss + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/cluster_state.go b/vendor/github.com/olivere/elastic/v7/cluster_state.go new file mode 100644 index 0000000000..7d30086f6b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cluster_state.go @@ -0,0 +1,355 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// ClusterStateService allows to get a comprehensive state information of the whole cluster. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-state.html +// for details. +type ClusterStateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + indices []string + metrics []string + allowNoIndices *bool + expandWildcards string + flatSettings *bool + ignoreUnavailable *bool + local *bool + masterTimeout string +} + +// NewClusterStateService creates a new ClusterStateService. +func NewClusterStateService(client *Client) *ClusterStateService { + return &ClusterStateService{ + client: client, + indices: make([]string, 0), + metrics: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ClusterStateService) Pretty(pretty bool) *ClusterStateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ClusterStateService) Human(human bool) *ClusterStateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ClusterStateService) ErrorTrace(errorTrace bool) *ClusterStateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ClusterStateService) FilterPath(filterPath ...string) *ClusterStateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ClusterStateService) Header(name string, value string) *ClusterStateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ClusterStateService) Headers(headers http.Header) *ClusterStateService { + s.headers = headers + return s +} + +// Index is a list of index names. Use _all or an empty string to +// perform the operation on all indices. +func (s *ClusterStateService) Index(indices ...string) *ClusterStateService { + s.indices = append(s.indices, indices...) + return s +} + +// Metric limits the information returned to the specified metric. +// It can be one of: version, master_node, nodes, routing_table, metadata, +// blocks, or customs. +func (s *ClusterStateService) Metric(metrics ...string) *ClusterStateService { + s.metrics = append(s.metrics, metrics...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// (This includes `_all` string or when no indices have been specified). +func (s *ClusterStateService) AllowNoIndices(allowNoIndices bool) *ClusterStateService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *ClusterStateService) ExpandWildcards(expandWildcards string) *ClusterStateService { + s.expandWildcards = expandWildcards + return s +} + +// FlatSettings, when set, returns settings in flat format (default: false). +func (s *ClusterStateService) FlatSettings(flatSettings bool) *ClusterStateService { + s.flatSettings = &flatSettings + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *ClusterStateService) IgnoreUnavailable(ignoreUnavailable bool) *ClusterStateService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// Local indicates whether to return local information. When set, it does not +// retrieve the state from master node (default: false). +func (s *ClusterStateService) Local(local bool) *ClusterStateService { + s.local = &local + return s +} + +// MasterTimeout specifies timeout for connection to master. +func (s *ClusterStateService) MasterTimeout(masterTimeout string) *ClusterStateService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *ClusterStateService) buildURL() (string, url.Values, error) { + // Build URL + metrics := strings.Join(s.metrics, ",") + if metrics == "" { + metrics = "_all" + } + indices := strings.Join(s.indices, ",") + if indices == "" { + indices = "_all" + } + path, err := uritemplates.Expand("/_cluster/state/{metrics}/{indices}", map[string]string{ + "metrics": metrics, + "indices": indices, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ClusterStateService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *ClusterStateService) Do(ctx context.Context) (*ClusterStateResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ClusterStateResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ClusterStateResponse is the response of ClusterStateService.Do. +type ClusterStateResponse struct { + ClusterName string `json:"cluster_name"` + ClusterUUID string `json:"cluster_uuid"` + Version int64 `json:"version"` + StateUUID string `json:"state_uuid"` + MasterNode string `json:"master_node"` + Blocks map[string]*clusterBlocks `json:"blocks"` + Nodes map[string]*discoveryNode `json:"nodes"` + Metadata *clusterStateMetadata `json:"metadata"` + RoutingTable *clusterStateRoutingTable `json:"routing_table"` + RoutingNodes *clusterStateRoutingNode `json:"routing_nodes"` + Customs map[string]interface{} `json:"customs"` +} + +type clusterBlocks struct { + Global map[string]*clusterBlock `json:"global"` // id -> cluster block + Indices map[string]*clusterBlock `json:"indices"` // index name -> cluster block +} + +type clusterBlock struct { + Description string `json:"description"` + Retryable bool `json:"retryable"` + DisableStatePersistence bool `json:"disable_state_persistence"` + Levels []string `json:"levels"` +} + +type clusterStateMetadata struct { + ClusterUUID string `json:"cluster_uuid"` + ClusterUUIDCommitted string `json:"cluster_uuid_committed"` + ClusterCoordination *clusterCoordinationMetaData `json:"cluster_coordination"` + Templates map[string]*indexTemplateMetaData `json:"templates"` // template name -> index template metadata + Indices map[string]*indexMetaData `json:"indices"` // index name _> meta data + RoutingTable struct { + Indices map[string]*indexRoutingTable `json:"indices"` // index name -> routing table + } `json:"routing_table"` + RoutingNodes struct { + Unassigned []*shardRouting `json:"unassigned"` + Nodes []*shardRouting `json:"nodes"` + } `json:"routing_nodes"` + Customs map[string]interface{} `json:"customs"` + Ingest map[string]interface{} `json:"ingest"` + StoredScripts map[string]interface{} `json:"stored_scripts"` + IndexGraveyard map[string]interface{} `json:"index-graveyard"` +} + +type clusterCoordinationMetaData struct { + Term int64 `json:"term"` + LastCommittedConfig interface{} `json:"last_committed_config,omitempty"` + LastAcceptedConfig interface{} `json:"last_accepted_config,omitempty"` + VotingConfigExclusions []interface{} `json:"voting_config_exclusions,omitempty"` +} + +type discoveryNode struct { + Name string `json:"name"` // server name, e.g. "es1" + EphemeralID string `json:"ephemeral_id"` // e.g. "paHSLpn6QyuVy_n-GM1JAQ" + TransportAddress string `json:"transport_address"` // e.g. inet[/1.2.3.4:9300] + Attributes map[string]interface{} `json:"attributes"` // e.g. { "data": true, "master": true } +} + +type clusterStateRoutingTable struct { + Indices map[string]interface{} `json:"indices"` +} + +type clusterStateRoutingNode struct { + Unassigned []*shardRouting `json:"unassigned"` + // Node Id -> shardRouting + Nodes map[string][]*shardRouting `json:"nodes"` +} + +type indexTemplateMetaData struct { + IndexPatterns []string `json:"index_patterns"` // e.g. ["store-*"] + Order int `json:"order"` + Settings map[string]interface{} `json:"settings"` // index settings + Mappings map[string]interface{} `json:"mappings"` // type name -> mapping +} + +type indexMetaData struct { + State string `json:"state"` + Settings map[string]interface{} `json:"settings"` + Mappings map[string]interface{} `json:"mappings"` + Aliases []string `json:"aliases"` // e.g. [ "alias1", "alias2" ] + PrimaryTerms map[string]interface{} `json:"primary_terms"` + InSyncAllocations map[string]interface{} `json:"in_sync_allocations"` +} + +type indexRoutingTable struct { + Shards map[string]*shardRouting `json:"shards"` +} + +type shardRouting struct { + State string `json:"state"` + Primary bool `json:"primary"` + Node string `json:"node"` + RelocatingNode string `json:"relocating_node"` + Shard int `json:"shard"` + Index string `json:"index"` + Version int64 `json:"version"` + RestoreSource *RestoreSource `json:"restore_source"` + AllocationId *allocationId `json:"allocation_id"` + UnassignedInfo *unassignedInfo `json:"unassigned_info"` +} + +type RestoreSource struct { + Repository string `json:"repository"` + Snapshot string `json:"snapshot"` + Version string `json:"version"` + Index string `json:"index"` +} + +type allocationId struct { + Id string `json:"id"` + RelocationId string `json:"relocation_id"` +} + +type unassignedInfo struct { + Reason string `json:"reason"` + At string `json:"at"` + Details string `json:"details"` +} diff --git a/vendor/github.com/olivere/elastic/v7/cluster_stats.go b/vendor/github.com/olivere/elastic/v7/cluster_stats.go new file mode 100644 index 0000000000..711c2a76de --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/cluster_stats.go @@ -0,0 +1,409 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// ClusterStatsService is documented at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-stats.html. +type ClusterStatsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + nodeId []string + flatSettings *bool +} + +// NewClusterStatsService creates a new ClusterStatsService. +func NewClusterStatsService(client *Client) *ClusterStatsService { + return &ClusterStatsService{ + client: client, + nodeId: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ClusterStatsService) Pretty(pretty bool) *ClusterStatsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ClusterStatsService) Human(human bool) *ClusterStatsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ClusterStatsService) ErrorTrace(errorTrace bool) *ClusterStatsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ClusterStatsService) FilterPath(filterPath ...string) *ClusterStatsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ClusterStatsService) Header(name string, value string) *ClusterStatsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ClusterStatsService) Headers(headers http.Header) *ClusterStatsService { + s.headers = headers + return s +} + +// NodeId is documented as: A comma-separated list of node IDs or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes. +func (s *ClusterStatsService) NodeId(nodeId []string) *ClusterStatsService { + s.nodeId = nodeId + return s +} + +// FlatSettings is documented as: Return settings in flat format (default: false). +func (s *ClusterStatsService) FlatSettings(flatSettings bool) *ClusterStatsService { + s.flatSettings = &flatSettings + return s +} + +// buildURL builds the URL for the operation. +func (s *ClusterStatsService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + + if len(s.nodeId) > 0 { + path, err = uritemplates.Expand("/_cluster/stats/nodes/{node_id}", map[string]string{ + "node_id": strings.Join(s.nodeId, ","), + }) + if err != nil { + return "", url.Values{}, err + } + } else { + path, err = uritemplates.Expand("/_cluster/stats", map[string]string{}) + if err != nil { + return "", url.Values{}, err + } + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ClusterStatsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *ClusterStatsService) Do(ctx context.Context) (*ClusterStatsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ClusterStatsResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ClusterStatsResponse is the response of ClusterStatsService.Do. +type ClusterStatsResponse struct { + NodesStats *ShardsInfo `json:"_nodes,omitempty"` + Timestamp int64 `json:"timestamp"` + ClusterName string `json:"cluster_name"` + ClusterUUID string `json:"cluster_uuid"` + Status string `json:"status,omitempty"` // e.g. green + Indices *ClusterStatsIndices `json:"indices"` + Nodes *ClusterStatsNodes `json:"nodes"` +} + +type ClusterStatsIndices struct { + Count int `json:"count"` // number of indices + Shards *ClusterStatsIndicesShards `json:"shards"` + Docs *ClusterStatsIndicesDocs `json:"docs"` + Store *ClusterStatsIndicesStore `json:"store"` + FieldData *ClusterStatsIndicesFieldData `json:"fielddata"` + QueryCache *ClusterStatsIndicesQueryCache `json:"query_cache"` + Completion *ClusterStatsIndicesCompletion `json:"completion"` + Segments *IndexStatsSegments `json:"segments"` +} + +type ClusterStatsIndicesShards struct { + Total int `json:"total"` + Primaries int `json:"primaries"` + Replication float64 `json:"replication"` + Index *ClusterStatsIndicesShardsIndex `json:"index"` +} + +type ClusterStatsIndicesShardsIndex struct { + Shards *ClusterStatsIndicesShardsIndexIntMinMax `json:"shards"` + Primaries *ClusterStatsIndicesShardsIndexIntMinMax `json:"primaries"` + Replication *ClusterStatsIndicesShardsIndexFloat64MinMax `json:"replication"` +} + +type ClusterStatsIndicesShardsIndexIntMinMax struct { + Min int `json:"min"` + Max int `json:"max"` + Avg float64 `json:"avg"` +} + +type ClusterStatsIndicesShardsIndexFloat64MinMax struct { + Min float64 `json:"min"` + Max float64 `json:"max"` + Avg float64 `json:"avg"` +} + +type ClusterStatsIndicesDocs struct { + Count int `json:"count"` + Deleted int `json:"deleted"` +} + +type ClusterStatsIndicesStore struct { + Size string `json:"size"` // e.g. "5.3gb" + SizeInBytes int64 `json:"size_in_bytes"` +} + +type ClusterStatsIndicesFieldData struct { + MemorySize string `json:"memory_size"` // e.g. "61.3kb" + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + Evictions int64 `json:"evictions"` + Fields map[string]struct { + MemorySize string `json:"memory_size"` // e.g. "61.3kb" + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + } `json:"fields,omitempty"` +} + +type ClusterStatsIndicesQueryCache struct { + MemorySize string `json:"memory_size"` // e.g. "61.3kb" + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + TotalCount int64 `json:"total_count"` + HitCount int64 `json:"hit_count"` + MissCount int64 `json:"miss_count"` + CacheSize int64 `json:"cache_size"` + CacheCount int64 `json:"cache_count"` + Evictions int64 `json:"evictions"` +} + +type ClusterStatsIndicesCompletion struct { + Size string `json:"size"` // e.g. "61.3kb" + SizeInBytes int64 `json:"size_in_bytes"` + Fields map[string]struct { + Size string `json:"size"` // e.g. "61.3kb" + SizeInBytes int64 `json:"size_in_bytes"` + } `json:"fields,omitempty"` +} + +type ClusterStatsIndicesSegmentsFile struct { + Size string `json:"size"` // e.g. "61.3kb" + SizeInBytes int64 `json:"size_in_bytes"` + Description string `json:"description,omitempty"` +} + +// --- + +type ClusterStatsNodes struct { + Count *ClusterStatsNodesCount `json:"count"` + Versions []string `json:"versions"` + OS *ClusterStatsNodesOsStats `json:"os"` + Process *ClusterStatsNodesProcessStats `json:"process"` + JVM *ClusterStatsNodesJvmStats `json:"jvm"` + FS *ClusterStatsNodesFsStats `json:"fs"` + Plugins []*ClusterStatsNodesPlugin `json:"plugins"` + + NetworkTypes *ClusterStatsNodesNetworkTypes `json:"network_types"` + DiscoveryTypes *ClusterStatsNodesDiscoveryTypes `json:"discovery_types"` + PackagingTypes *ClusterStatsNodesPackagingTypes `json:"packaging_types"` +} + +type ClusterStatsNodesCount struct { + Total int `json:"total"` + Data int `json:"data"` + CoordinatingOnly int `json:"coordinating_only"` + Master int `json:"master"` + Ingest int `json:"ingest"` +} + +type ClusterStatsNodesOsStats struct { + AvailableProcessors int `json:"available_processors"` + AllocatedProcessors int `json:"allocated_processors"` + Names []struct { + Name string `json:"name"` + Value int `json:"count"` + } `json:"names"` + PrettyNames []struct { + PrettyName string `json:"pretty_name"` + Value int `json:"count"` + } `json:"pretty_names"` + Mem *ClusterStatsNodesOsStatsMem `json:"mem"` + // CPU []*ClusterStatsNodesOsStatsCPU `json:"cpu"` +} + +type ClusterStatsNodesOsStatsMem struct { + Total string `json:"total"` // e.g. "16gb" + TotalInBytes int64 `json:"total_in_bytes"` + Free string `json:"free"` // e.g. "12gb" + FreeInBytes int64 `json:"free_in_bytes"` + Used string `json:"used"` // e.g. "4gb" + UsedInBytes int64 `json:"used_in_bytes"` + FreePercent int `json:"free_percent"` + UsedPercent int `json:"used_percent"` +} + +type ClusterStatsNodesOsStatsCPU struct { + Vendor string `json:"vendor"` + Model string `json:"model"` + MHz int `json:"mhz"` + TotalCores int `json:"total_cores"` + TotalSockets int `json:"total_sockets"` + CoresPerSocket int `json:"cores_per_socket"` + CacheSize string `json:"cache_size"` // e.g. "256b" + CacheSizeInBytes int64 `json:"cache_size_in_bytes"` + Count int `json:"count"` +} + +type ClusterStatsNodesProcessStats struct { + CPU *ClusterStatsNodesProcessStatsCPU `json:"cpu"` + OpenFileDescriptors *ClusterStatsNodesProcessStatsOpenFileDescriptors `json:"open_file_descriptors"` +} + +type ClusterStatsNodesProcessStatsCPU struct { + Percent float64 `json:"percent"` +} + +type ClusterStatsNodesProcessStatsOpenFileDescriptors struct { + Min int64 `json:"min"` + Max int64 `json:"max"` + Avg int64 `json:"avg"` +} + +type ClusterStatsNodesJvmStats struct { + MaxUptime string `json:"max_uptime"` // e.g. "5h" + MaxUptimeInMillis int64 `json:"max_uptime_in_millis"` + Versions []*ClusterStatsNodesJvmStatsVersion `json:"versions"` + Mem *ClusterStatsNodesJvmStatsMem `json:"mem"` + Threads int64 `json:"threads"` +} + +type ClusterStatsNodesJvmStatsVersion struct { + Version string `json:"version"` // e.g. "1.8.0_45" + VMName string `json:"vm_name"` // e.g. "Java HotSpot(TM) 64-Bit Server VM" + VMVersion string `json:"vm_version"` // e.g. "25.45-b02" + VMVendor string `json:"vm_vendor"` // e.g. "Oracle Corporation" + BundledJDK bool `json:"bundled_jdk"` + UsingBundledJDK bool `json:"using_bundled_jdk"` + Count int `json:"count"` +} + +type ClusterStatsNodesJvmStatsMem struct { + HeapUsed string `json:"heap_used"` + HeapUsedInBytes int64 `json:"heap_used_in_bytes"` + HeapMax string `json:"heap_max"` + HeapMaxInBytes int64 `json:"heap_max_in_bytes"` +} + +type ClusterStatsNodesFsStats struct { + Path string `json:"path"` + Mount string `json:"mount"` + Dev string `json:"dev"` + Total string `json:"total"` // e.g. "930.7gb"` + TotalInBytes int64 `json:"total_in_bytes"` + Free string `json:"free"` // e.g. "930.7gb"` + FreeInBytes int64 `json:"free_in_bytes"` + Available string `json:"available"` // e.g. "930.7gb"` + AvailableInBytes int64 `json:"available_in_bytes"` + DiskReads int64 `json:"disk_reads"` + DiskWrites int64 `json:"disk_writes"` + DiskIOOp int64 `json:"disk_io_op"` + DiskReadSize string `json:"disk_read_size"` // e.g. "0b"` + DiskReadSizeInBytes int64 `json:"disk_read_size_in_bytes"` + DiskWriteSize string `json:"disk_write_size"` // e.g. "0b"` + DiskWriteSizeInBytes int64 `json:"disk_write_size_in_bytes"` + DiskIOSize string `json:"disk_io_size"` // e.g. "0b"` + DiskIOSizeInBytes int64 `json:"disk_io_size_in_bytes"` + DiskQueue string `json:"disk_queue"` + DiskServiceTime string `json:"disk_service_time"` +} + +type ClusterStatsNodesPlugin struct { + Name string `json:"name"` + Version string `json:"version"` + Description string `json:"description"` + URL string `json:"url"` + JVM bool `json:"jvm"` + Site bool `json:"site"` +} + +type ClusterStatsNodesNetworkTypes struct { + TransportTypes map[string]interface{} `json:"transport_types"` // e.g. "netty4": 1 + HTTPTypes map[string]interface{} `json:"http_types"` // e.g. "netty4": 1 +} + +type ClusterStatsNodesDiscoveryTypes interface{} + +type ClusterStatsNodesPackagingTypes []*ClusterStatsNodesPackagingType + +type ClusterStatsNodesPackagingType struct { + Flavor string `json:"flavor"` // e.g. "oss" + Type string `json:"type"` // e.g. "docker" + Count int `json:"count"` // e.g. 1 +} diff --git a/vendor/github.com/olivere/elastic/v7/config/config.go b/vendor/github.com/olivere/elastic/v7/config/config.go new file mode 100644 index 0000000000..208eb2b5dd --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/config/config.go @@ -0,0 +1,87 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package config + +import ( + "fmt" + "net/url" + "strconv" + "strings" +) + +// Config represents an Elasticsearch configuration. +type Config struct { + URL string + Index string + Username string + Password string + Shards int + Replicas int + Sniff *bool + Healthcheck *bool + Infolog string + Errorlog string + Tracelog string +} + +// Parse returns the Elasticsearch configuration by extracting it +// from the URL, its path, and its query string. +// +// Example: +// http://127.0.0.1:9200/store-blobs?shards=1&replicas=0&sniff=false&tracelog=elastic.trace.log +// +// The code above will return a URL of http://127.0.0.1:9200, an index name +// of store-blobs, and the related settings from the query string. +func Parse(elasticURL string) (*Config, error) { + cfg := &Config{ + Shards: 1, + Replicas: 0, + Sniff: nil, + } + + uri, err := url.Parse(elasticURL) + if err != nil { + return nil, fmt.Errorf("error parsing elastic parameter %q: %v", elasticURL, err) + } + index := strings.TrimSuffix(strings.TrimPrefix(uri.Path, "/"), "/") + if uri.User != nil { + cfg.Username = uri.User.Username() + cfg.Password, _ = uri.User.Password() + } + uri.User = nil + + if i, err := strconv.Atoi(uri.Query().Get("shards")); err == nil { + cfg.Shards = i + } + if i, err := strconv.Atoi(uri.Query().Get("replicas")); err == nil { + cfg.Replicas = i + } + if s := uri.Query().Get("sniff"); s != "" { + if b, err := strconv.ParseBool(s); err == nil { + cfg.Sniff = &b + } + } + if s := uri.Query().Get("healthcheck"); s != "" { + if b, err := strconv.ParseBool(s); err == nil { + cfg.Healthcheck = &b + } + } + if s := uri.Query().Get("infolog"); s != "" { + cfg.Infolog = s + } + if s := uri.Query().Get("errorlog"); s != "" { + cfg.Errorlog = s + } + if s := uri.Query().Get("tracelog"); s != "" { + cfg.Tracelog = s + } + + uri.Path = "" + uri.RawQuery = "" + cfg.URL = uri.String() + cfg.Index = index + + return cfg, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/config/doc.go b/vendor/github.com/olivere/elastic/v7/config/doc.go new file mode 100644 index 0000000000..c9acd5ff19 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/config/doc.go @@ -0,0 +1,9 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +/* +Package config allows parsing a configuration for Elasticsearch +from a URL. +*/ +package config diff --git a/vendor/github.com/olivere/elastic/v7/connection.go b/vendor/github.com/olivere/elastic/v7/connection.go new file mode 100644 index 0000000000..0f27a8756b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/connection.go @@ -0,0 +1,90 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" + "sync" + "time" +) + +// conn represents a single connection to a node in a cluster. +type conn struct { + sync.RWMutex + nodeID string // node ID + url string + failures int + dead bool + deadSince *time.Time +} + +// newConn creates a new connection to the given URL. +func newConn(nodeID, url string) *conn { + c := &conn{ + nodeID: nodeID, + url: url, + } + return c +} + +// String returns a representation of the connection status. +func (c *conn) String() string { + c.RLock() + defer c.RUnlock() + return fmt.Sprintf("%s [dead=%v,failures=%d,deadSince=%v]", c.url, c.dead, c.failures, c.deadSince) +} + +// NodeID returns the ID of the node of this connection. +func (c *conn) NodeID() string { + c.RLock() + defer c.RUnlock() + return c.nodeID +} + +// URL returns the URL of this connection. +func (c *conn) URL() string { + c.RLock() + defer c.RUnlock() + return c.url +} + +// IsDead returns true if this connection is marked as dead, i.e. a previous +// request to the URL has been unsuccessful. +func (c *conn) IsDead() bool { + c.RLock() + defer c.RUnlock() + return c.dead +} + +// MarkAsDead marks this connection as dead, increments the failures +// counter and stores the current time in dead since. +func (c *conn) MarkAsDead() { + c.Lock() + c.dead = true + if c.deadSince == nil { + utcNow := time.Now().UTC() + c.deadSince = &utcNow + } + c.failures += 1 + c.Unlock() +} + +// MarkAsAlive marks this connection as eligible to be returned from the +// pool of connections by the selector. +func (c *conn) MarkAsAlive() { + c.Lock() + c.dead = false + c.Unlock() +} + +// MarkAsHealthy marks this connection as healthy, i.e. a request has been +// successfully performed with it. +func (c *conn) MarkAsHealthy() { + c.Lock() + c.dead = false + c.deadSince = nil + c.failures = 0 + c.Unlock() +} diff --git a/vendor/github.com/olivere/elastic/v7/count.go b/vendor/github.com/olivere/elastic/v7/count.go new file mode 100644 index 0000000000..d1c75be166 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/count.go @@ -0,0 +1,381 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// CountService is a convenient service for determining the +// number of documents in an index. Use SearchService with +// a SearchType of count for counting with queries etc. +type CountService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + typ []string + allowNoIndices *bool + analyzeWildcard *bool + analyzer string + defaultOperator string + df string + expandWildcards string + ignoreUnavailable *bool + lenient *bool + lowercaseExpandedTerms *bool + minScore interface{} + preference string + q string + query Query + routing string + terminateAfter *int + bodyJson interface{} + bodyString string +} + +// NewCountService creates a new CountService. +func NewCountService(client *Client) *CountService { + return &CountService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *CountService) Pretty(pretty bool) *CountService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *CountService) Human(human bool) *CountService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *CountService) ErrorTrace(errorTrace bool) *CountService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *CountService) FilterPath(filterPath ...string) *CountService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *CountService) Header(name string, value string) *CountService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *CountService) Headers(headers http.Header) *CountService { + s.headers = headers + return s +} + +// Index sets the names of the indices to restrict the results. +func (s *CountService) Index(index ...string) *CountService { + if s.index == nil { + s.index = make([]string, 0) + } + s.index = append(s.index, index...) + return s +} + +// Type sets the types to use to restrict the results. +// +// Deprecated: Types are in the process of being removed. Instead of using a type, prefer to +// filter on a field on the document. +func (s *CountService) Type(typ ...string) *CountService { + if s.typ == nil { + s.typ = make([]string, 0) + } + s.typ = append(s.typ, typ...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes "_all" string +// or when no indices have been specified). +func (s *CountService) AllowNoIndices(allowNoIndices bool) *CountService { + s.allowNoIndices = &allowNoIndices + return s +} + +// AnalyzeWildcard specifies whether wildcard and prefix queries should be +// analyzed (default: false). +func (s *CountService) AnalyzeWildcard(analyzeWildcard bool) *CountService { + s.analyzeWildcard = &analyzeWildcard + return s +} + +// Analyzer specifies the analyzer to use for the query string. +func (s *CountService) Analyzer(analyzer string) *CountService { + s.analyzer = analyzer + return s +} + +// DefaultOperator specifies the default operator for query string query (AND or OR). +func (s *CountService) DefaultOperator(defaultOperator string) *CountService { + s.defaultOperator = defaultOperator + return s +} + +// Df specifies the field to use as default where no field prefix is given +// in the query string. +func (s *CountService) Df(df string) *CountService { + s.df = df + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *CountService) ExpandWildcards(expandWildcards string) *CountService { + s.expandWildcards = expandWildcards + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *CountService) IgnoreUnavailable(ignoreUnavailable bool) *CountService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// Lenient specifies whether format-based query failures (such as +// providing text to a numeric field) should be ignored. +func (s *CountService) Lenient(lenient bool) *CountService { + s.lenient = &lenient + return s +} + +// LowercaseExpandedTerms specifies whether query terms should be lowercased. +func (s *CountService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *CountService { + s.lowercaseExpandedTerms = &lowercaseExpandedTerms + return s +} + +// MinScore indicates to include only documents with a specific `_score` +// value in the result. +func (s *CountService) MinScore(minScore interface{}) *CountService { + s.minScore = minScore + return s +} + +// Preference specifies the node or shard the operation should be +// performed on (default: random). +func (s *CountService) Preference(preference string) *CountService { + s.preference = preference + return s +} + +// Q in the Lucene query string syntax. You can also use Query to pass +// a Query struct. +func (s *CountService) Q(q string) *CountService { + s.q = q + return s +} + +// Query specifies the query to pass. You can also pass a query string with Q. +func (s *CountService) Query(query Query) *CountService { + s.query = query + return s +} + +// Routing specifies the routing value. +func (s *CountService) Routing(routing string) *CountService { + s.routing = routing + return s +} + +// TerminateAfter indicates the maximum count for each shard, upon reaching +// which the query execution will terminate early. +func (s *CountService) TerminateAfter(terminateAfter int) *CountService { + s.terminateAfter = &terminateAfter + return s +} + +// BodyJson specifies the query to restrict the results specified with the +// Query DSL (optional). The interface{} will be serialized to a JSON document, +// so use a map[string]interface{}. +func (s *CountService) BodyJson(body interface{}) *CountService { + s.bodyJson = body + return s +} + +// Body specifies a query to restrict the results specified with +// the Query DSL (optional). +func (s *CountService) BodyString(body string) *CountService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *CountService) buildURL() (string, url.Values, error) { + var err error + var path string + + if len(s.index) > 0 && len(s.typ) > 0 { + path, err = uritemplates.Expand("/{index}/{type}/_count", map[string]string{ + "index": strings.Join(s.index, ","), + "type": strings.Join(s.typ, ","), + }) + } else if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_count", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else if len(s.typ) > 0 { + path, err = uritemplates.Expand("/_all/{type}/_count", map[string]string{ + "type": strings.Join(s.typ, ","), + }) + } else { + path = "/_all/_count" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.analyzeWildcard != nil { + params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard)) + } + if s.analyzer != "" { + params.Set("analyzer", s.analyzer) + } + if s.defaultOperator != "" { + params.Set("default_operator", s.defaultOperator) + } + if s.df != "" { + params.Set("df", s.df) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.lenient != nil { + params.Set("lenient", fmt.Sprintf("%v", *s.lenient)) + } + if s.lowercaseExpandedTerms != nil { + params.Set("lowercase_expanded_terms", fmt.Sprintf("%v", *s.lowercaseExpandedTerms)) + } + if s.minScore != nil { + params.Set("min_score", fmt.Sprintf("%v", s.minScore)) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if s.q != "" { + params.Set("q", s.q) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.terminateAfter != nil { + params.Set("terminate_after", fmt.Sprintf("%v", *s.terminateAfter)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *CountService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *CountService) Do(ctx context.Context) (int64, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return 0, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return 0, err + } + + // Setup HTTP request body + var body interface{} + if s.query != nil { + src, err := s.query.Source() + if err != nil { + return 0, err + } + query := make(map[string]interface{}) + query["query"] = src + body = query + } else if s.bodyJson != nil { + body = s.bodyJson + } else if s.bodyString != "" { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return 0, err + } + + // Return result + ret := new(CountResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return 0, err + } + if ret != nil { + return ret.Count, nil + } + + return int64(0), nil +} + +// CountResponse is the response of using the Count API. +type CountResponse struct { + Count int64 `json:"count"` + TerminatedEarly bool `json:"terminated_early,omitempty"` + Shards *ShardsInfo `json:"_shards,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/decoder.go b/vendor/github.com/olivere/elastic/v7/decoder.go new file mode 100644 index 0000000000..9133ccd910 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/decoder.go @@ -0,0 +1,38 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "bytes" + "encoding/json" +) + +// Decoder is used to decode responses from Elasticsearch. +// Users of elastic can implement their own marshaler for advanced purposes +// and set them per Client (see SetDecoder). If none is specified, +// DefaultDecoder is used. +type Decoder interface { + Decode(data []byte, v interface{}) error +} + +// DefaultDecoder uses json.Unmarshal from the Go standard library +// to decode JSON data. +type DefaultDecoder struct{} + +// Decode decodes with json.Unmarshal from the Go standard library. +func (u *DefaultDecoder) Decode(data []byte, v interface{}) error { + return json.Unmarshal(data, v) +} + +// NumberDecoder uses json.NewDecoder, with UseNumber() enabled, from +// the Go standard library to decode JSON data. +type NumberDecoder struct{} + +// Decode decodes with json.Unmarshal from the Go standard library. +func (u *NumberDecoder) Decode(data []byte, v interface{}) error { + dec := json.NewDecoder(bytes.NewReader(data)) + dec.UseNumber() + return dec.Decode(v) +} diff --git a/vendor/github.com/olivere/elastic/v7/delete.go b/vendor/github.com/olivere/elastic/v7/delete.go new file mode 100644 index 0000000000..f694701774 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/delete.go @@ -0,0 +1,305 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// DeleteService allows to delete a typed JSON document from a specified +// index based on its id. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-delete.html +// for details. +type DeleteService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + index string + typ string + routing string + timeout string + version interface{} + versionType string + waitForActiveShards string + parent string + refresh string + ifSeqNo *int64 + ifPrimaryTerm *int64 +} + +// NewDeleteService creates a new DeleteService. +func NewDeleteService(client *Client) *DeleteService { + return &DeleteService{ + client: client, + typ: "_doc", + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *DeleteService) Pretty(pretty bool) *DeleteService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *DeleteService) Human(human bool) *DeleteService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *DeleteService) ErrorTrace(errorTrace bool) *DeleteService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *DeleteService) FilterPath(filterPath ...string) *DeleteService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *DeleteService) Header(name string, value string) *DeleteService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *DeleteService) Headers(headers http.Header) *DeleteService { + s.headers = headers + return s +} + +// Type is the type of the document. +// +// Deprecated: Types are in the process of being removed. +func (s *DeleteService) Type(typ string) *DeleteService { + s.typ = typ + return s +} + +// Id is the document ID. +func (s *DeleteService) Id(id string) *DeleteService { + s.id = id + return s +} + +// Index is the name of the index. +func (s *DeleteService) Index(index string) *DeleteService { + s.index = index + return s +} + +// Routing is a specific routing value. +func (s *DeleteService) Routing(routing string) *DeleteService { + s.routing = routing + return s +} + +// Timeout is an explicit operation timeout. +func (s *DeleteService) Timeout(timeout string) *DeleteService { + s.timeout = timeout + return s +} + +// Version is an explicit version number for concurrency control. +func (s *DeleteService) Version(version interface{}) *DeleteService { + s.version = version + return s +} + +// VersionType is a specific version type. +func (s *DeleteService) VersionType(versionType string) *DeleteService { + s.versionType = versionType + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active +// before proceeding with the delete operation. Defaults to 1, meaning the +// primary shard only. Set to `all` for all shard copies, otherwise set to +// any non-negative value less than or equal to the total number of copies +// for the shard (number of replicas + 1). +func (s *DeleteService) WaitForActiveShards(waitForActiveShards string) *DeleteService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// Parent is the ID of parent document. +func (s *DeleteService) Parent(parent string) *DeleteService { + s.parent = parent + return s +} + +// Refresh the index after performing the operation. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *DeleteService) Refresh(refresh string) *DeleteService { + s.refresh = refresh + return s +} + +// IfSeqNo indicates to only perform the delete operation if the last +// operation that has changed the document has the specified sequence number. +func (s *DeleteService) IfSeqNo(seqNo int64) *DeleteService { + s.ifSeqNo = &seqNo + return s +} + +// IfPrimaryTerm indicates to only perform the delete operation if the +// last operation that has changed the document has the specified primary term. +func (s *DeleteService) IfPrimaryTerm(primaryTerm int64) *DeleteService { + s.ifPrimaryTerm = &primaryTerm + return s +} + +// buildURL builds the URL for the operation. +func (s *DeleteService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{ + "index": s.index, + "type": s.typ, + "id": s.id, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if v := s.version; v != nil { + params.Set("version", fmt.Sprint(v)) + } + if s.versionType != "" { + params.Set("version_type", s.versionType) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if v := s.ifSeqNo; v != nil { + params.Set("if_seq_no", fmt.Sprintf("%d", *v)) + } + if v := s.ifPrimaryTerm; v != nil { + params.Set("if_primary_term", fmt.Sprintf("%d", *v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *DeleteService) Validate() error { + var invalid []string + if s.typ == "" { + invalid = append(invalid, "Type") + } + if s.id == "" { + invalid = append(invalid, "Id") + } + if s.index == "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. If the document is not found (404), Elasticsearch will +// still return a response. This response is serialized and returned as well. In other +// words, for HTTP status code 404, both an error and a response might be returned. +func (s *DeleteService) Do(ctx context.Context) (*DeleteResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + IgnoreErrors: []int{http.StatusNotFound}, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(DeleteResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + + // If we have a 404, we return both a result and an error, just like ES does + if res.StatusCode == http.StatusNotFound { + return ret, &Error{Status: http.StatusNotFound} + } + + return ret, nil +} + +// -- Result of a delete request. + +// DeleteResponse is the outcome of running DeleteService.Do. +type DeleteResponse struct { + Index string `json:"_index,omitempty"` + Type string `json:"_type,omitempty"` + Id string `json:"_id,omitempty"` + Version int64 `json:"_version,omitempty"` + Result string `json:"result,omitempty"` + Shards *ShardsInfo `json:"_shards,omitempty"` + SeqNo int64 `json:"_seq_no,omitempty"` + PrimaryTerm int64 `json:"_primary_term,omitempty"` + Status int `json:"status,omitempty"` + ForcedRefresh bool `json:"forced_refresh,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/delete_by_query.go b/vendor/github.com/olivere/elastic/v7/delete_by_query.go new file mode 100644 index 0000000000..4c98444f4e --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/delete_by_query.go @@ -0,0 +1,781 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// DeleteByQueryService deletes documents that match a query. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-delete-by-query.html. +type DeleteByQueryService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + typ []string + query Query + body interface{} + xSource []string + xSourceExclude []string + xSourceInclude []string + analyzer string + analyzeWildcard *bool + allowNoIndices *bool + conflicts string + defaultOperator string + df string + docvalueFields []string + expandWildcards string + explain *bool + from *int + ignoreUnavailable *bool + lenient *bool + lowercaseExpandedTerms *bool + preference string + q string + refresh string + requestCache *bool + requestsPerSecond *int + routing []string + scroll string + scrollSize *int + searchTimeout string + searchType string + size *int + slices interface{} + sort []string + stats []string + storedFields []string + suggestField string + suggestMode string + suggestSize *int + suggestText string + terminateAfter *int + timeout string + trackScores *bool + version *bool + waitForActiveShards string + waitForCompletion *bool +} + +// NewDeleteByQueryService creates a new DeleteByQueryService. +// You typically use the client's DeleteByQuery to get a reference to +// the service. +func NewDeleteByQueryService(client *Client) *DeleteByQueryService { + builder := &DeleteByQueryService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *DeleteByQueryService) Pretty(pretty bool) *DeleteByQueryService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *DeleteByQueryService) Human(human bool) *DeleteByQueryService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *DeleteByQueryService) ErrorTrace(errorTrace bool) *DeleteByQueryService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *DeleteByQueryService) FilterPath(filterPath ...string) *DeleteByQueryService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *DeleteByQueryService) Header(name string, value string) *DeleteByQueryService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *DeleteByQueryService) Headers(headers http.Header) *DeleteByQueryService { + s.headers = headers + return s +} + +// Index sets the indices on which to perform the delete operation. +func (s *DeleteByQueryService) Index(index ...string) *DeleteByQueryService { + s.index = append(s.index, index...) + return s +} + +// Type limits the delete operation to the given types. +// +// Deprecated: Types are in the process of being removed. Instead of +// using a type, prefer to filter on a field of the document. +func (s *DeleteByQueryService) Type(typ ...string) *DeleteByQueryService { + s.typ = append(s.typ, typ...) + return s +} + +// XSource is true or false to return the _source field or not, +// or a list of fields to return. +func (s *DeleteByQueryService) XSource(xSource ...string) *DeleteByQueryService { + s.xSource = append(s.xSource, xSource...) + return s +} + +// XSourceExclude represents a list of fields to exclude from the returned _source field. +func (s *DeleteByQueryService) XSourceExclude(xSourceExclude ...string) *DeleteByQueryService { + s.xSourceExclude = append(s.xSourceExclude, xSourceExclude...) + return s +} + +// XSourceInclude represents a list of fields to extract and return from the _source field. +func (s *DeleteByQueryService) XSourceInclude(xSourceInclude ...string) *DeleteByQueryService { + s.xSourceInclude = append(s.xSourceInclude, xSourceInclude...) + return s +} + +// Analyzer to use for the query string. +func (s *DeleteByQueryService) Analyzer(analyzer string) *DeleteByQueryService { + s.analyzer = analyzer + return s +} + +// AnalyzeWildcard specifies whether wildcard and prefix queries should be +// analyzed (default: false). +func (s *DeleteByQueryService) AnalyzeWildcard(analyzeWildcard bool) *DeleteByQueryService { + s.analyzeWildcard = &analyzeWildcard + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices (including the _all string +// or when no indices have been specified). +func (s *DeleteByQueryService) AllowNoIndices(allow bool) *DeleteByQueryService { + s.allowNoIndices = &allow + return s +} + +// Conflicts indicates what to do when the process detects version conflicts. +// Possible values are "proceed" and "abort". +func (s *DeleteByQueryService) Conflicts(conflicts string) *DeleteByQueryService { + s.conflicts = conflicts + return s +} + +// AbortOnVersionConflict aborts the request on version conflicts. +// It is an alias to setting Conflicts("abort"). +func (s *DeleteByQueryService) AbortOnVersionConflict() *DeleteByQueryService { + s.conflicts = "abort" + return s +} + +// ProceedOnVersionConflict aborts the request on version conflicts. +// It is an alias to setting Conflicts("proceed"). +func (s *DeleteByQueryService) ProceedOnVersionConflict() *DeleteByQueryService { + s.conflicts = "proceed" + return s +} + +// DefaultOperator for query string query (AND or OR). +func (s *DeleteByQueryService) DefaultOperator(defaultOperator string) *DeleteByQueryService { + s.defaultOperator = defaultOperator + return s +} + +// DF is the field to use as default where no field prefix is given in the query string. +func (s *DeleteByQueryService) DF(defaultField string) *DeleteByQueryService { + s.df = defaultField + return s +} + +// DefaultField is the field to use as default where no field prefix is given in the query string. +// It is an alias to the DF func. +func (s *DeleteByQueryService) DefaultField(defaultField string) *DeleteByQueryService { + s.df = defaultField + return s +} + +// DocvalueFields specifies the list of fields to return as the docvalue representation of a field for each hit. +func (s *DeleteByQueryService) DocvalueFields(docvalueFields ...string) *DeleteByQueryService { + s.docvalueFields = docvalueFields + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. It can be "open" or "closed". +func (s *DeleteByQueryService) ExpandWildcards(expand string) *DeleteByQueryService { + s.expandWildcards = expand + return s +} + +// Explain specifies whether to return detailed information about score +// computation as part of a hit. +func (s *DeleteByQueryService) Explain(explain bool) *DeleteByQueryService { + s.explain = &explain + return s +} + +// From is the starting offset (default: 0). +func (s *DeleteByQueryService) From(from int) *DeleteByQueryService { + s.from = &from + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *DeleteByQueryService) IgnoreUnavailable(ignore bool) *DeleteByQueryService { + s.ignoreUnavailable = &ignore + return s +} + +// Lenient specifies whether format-based query failures +// (such as providing text to a numeric field) should be ignored. +func (s *DeleteByQueryService) Lenient(lenient bool) *DeleteByQueryService { + s.lenient = &lenient + return s +} + +// LowercaseExpandedTerms specifies whether query terms should be lowercased. +func (s *DeleteByQueryService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *DeleteByQueryService { + s.lowercaseExpandedTerms = &lowercaseExpandedTerms + return s +} + +// Preference specifies the node or shard the operation should be performed on +// (default: random). +func (s *DeleteByQueryService) Preference(preference string) *DeleteByQueryService { + s.preference = preference + return s +} + +// Q specifies the query in Lucene query string syntax. You can also use +// Query to programmatically specify the query. +func (s *DeleteByQueryService) Q(query string) *DeleteByQueryService { + s.q = query + return s +} + +// QueryString is an alias to Q. Notice that you can also use Query to +// programmatically set the query. +func (s *DeleteByQueryService) QueryString(query string) *DeleteByQueryService { + s.q = query + return s +} + +// Query sets the query programmatically. +func (s *DeleteByQueryService) Query(query Query) *DeleteByQueryService { + s.query = query + return s +} + +// Refresh indicates whether the effected indexes should be refreshed. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *DeleteByQueryService) Refresh(refresh string) *DeleteByQueryService { + s.refresh = refresh + return s +} + +// RequestCache specifies if request cache should be used for this request +// or not, defaults to index level setting. +func (s *DeleteByQueryService) RequestCache(requestCache bool) *DeleteByQueryService { + s.requestCache = &requestCache + return s +} + +// RequestsPerSecond sets the throttle on this request in sub-requests per second. +// -1 means set no throttle as does "unlimited" which is the only non-float this accepts. +func (s *DeleteByQueryService) RequestsPerSecond(requestsPerSecond int) *DeleteByQueryService { + s.requestsPerSecond = &requestsPerSecond + return s +} + +// Routing is a list of specific routing values. +func (s *DeleteByQueryService) Routing(routing ...string) *DeleteByQueryService { + s.routing = append(s.routing, routing...) + return s +} + +// Scroll specifies how long a consistent view of the index should be maintained +// for scrolled search. +func (s *DeleteByQueryService) Scroll(scroll string) *DeleteByQueryService { + s.scroll = scroll + return s +} + +// ScrollSize is the size on the scroll request powering the update_by_query. +func (s *DeleteByQueryService) ScrollSize(scrollSize int) *DeleteByQueryService { + s.scrollSize = &scrollSize + return s +} + +// SearchTimeout defines an explicit timeout for each search request. +// Defaults to no timeout. +func (s *DeleteByQueryService) SearchTimeout(searchTimeout string) *DeleteByQueryService { + s.searchTimeout = searchTimeout + return s +} + +// SearchType is the search operation type. Possible values are +// "query_then_fetch" and "dfs_query_then_fetch". +func (s *DeleteByQueryService) SearchType(searchType string) *DeleteByQueryService { + s.searchType = searchType + return s +} + +// Size represents the number of hits to return (default: 10). +func (s *DeleteByQueryService) Size(size int) *DeleteByQueryService { + s.size = &size + return s +} + +// Slices represents the number of slices (default: 1). +// It used to be a number, but can be set to "auto" as of 6.7. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-delete-by-query.html#docs-delete-by-query-automatic-slice +// for details. +func (s *DeleteByQueryService) Slices(slices interface{}) *DeleteByQueryService { + s.slices = slices + return s +} + +// Sort is a list of : pairs. +func (s *DeleteByQueryService) Sort(sort ...string) *DeleteByQueryService { + s.sort = append(s.sort, sort...) + return s +} + +// SortByField adds a sort order. +func (s *DeleteByQueryService) SortByField(field string, ascending bool) *DeleteByQueryService { + if ascending { + s.sort = append(s.sort, fmt.Sprintf("%s:asc", field)) + } else { + s.sort = append(s.sort, fmt.Sprintf("%s:desc", field)) + } + return s +} + +// Stats specifies specific tag(s) of the request for logging and statistical purposes. +func (s *DeleteByQueryService) Stats(stats ...string) *DeleteByQueryService { + s.stats = append(s.stats, stats...) + return s +} + +// StoredFields specifies the list of stored fields to return as part of a hit. +func (s *DeleteByQueryService) StoredFields(storedFields ...string) *DeleteByQueryService { + s.storedFields = storedFields + return s +} + +// SuggestField specifies which field to use for suggestions. +func (s *DeleteByQueryService) SuggestField(suggestField string) *DeleteByQueryService { + s.suggestField = suggestField + return s +} + +// SuggestMode specifies the suggest mode. Possible values are +// "missing", "popular", and "always". +func (s *DeleteByQueryService) SuggestMode(suggestMode string) *DeleteByQueryService { + s.suggestMode = suggestMode + return s +} + +// SuggestSize specifies how many suggestions to return in response. +func (s *DeleteByQueryService) SuggestSize(suggestSize int) *DeleteByQueryService { + s.suggestSize = &suggestSize + return s +} + +// SuggestText specifies the source text for which the suggestions should be returned. +func (s *DeleteByQueryService) SuggestText(suggestText string) *DeleteByQueryService { + s.suggestText = suggestText + return s +} + +// TerminateAfter indicates the maximum number of documents to collect +// for each shard, upon reaching which the query execution will terminate early. +func (s *DeleteByQueryService) TerminateAfter(terminateAfter int) *DeleteByQueryService { + s.terminateAfter = &terminateAfter + return s +} + +// Timeout is the time each individual bulk request should wait for shards +// that are unavailable. +func (s *DeleteByQueryService) Timeout(timeout string) *DeleteByQueryService { + s.timeout = timeout + return s +} + +// TimeoutInMillis sets the timeout in milliseconds. +func (s *DeleteByQueryService) TimeoutInMillis(timeoutInMillis int) *DeleteByQueryService { + s.timeout = fmt.Sprintf("%dms", timeoutInMillis) + return s +} + +// TrackScores indicates whether to calculate and return scores even if +// they are not used for sorting. +func (s *DeleteByQueryService) TrackScores(trackScores bool) *DeleteByQueryService { + s.trackScores = &trackScores + return s +} + +// Version specifies whether to return document version as part of a hit. +func (s *DeleteByQueryService) Version(version bool) *DeleteByQueryService { + s.version = &version + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active before proceeding +// with the update by query operation. Defaults to 1, meaning the primary shard only. +// Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal +// to the total number of copies for the shard (number of replicas + 1). +func (s *DeleteByQueryService) WaitForActiveShards(waitForActiveShards string) *DeleteByQueryService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// WaitForCompletion indicates if the request should block until the reindex is complete. +func (s *DeleteByQueryService) WaitForCompletion(waitForCompletion bool) *DeleteByQueryService { + s.waitForCompletion = &waitForCompletion + return s +} + +// Body specifies the body of the request. It overrides data being specified via SearchService. +func (s *DeleteByQueryService) Body(body string) *DeleteByQueryService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *DeleteByQueryService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.typ) > 0 { + path, err = uritemplates.Expand("/{index}/{type}/_delete_by_query", map[string]string{ + "index": strings.Join(s.index, ","), + "type": strings.Join(s.typ, ","), + }) + } else { + path, err = uritemplates.Expand("/{index}/_delete_by_query", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.xSource) > 0 { + params.Set("_source", strings.Join(s.xSource, ",")) + } + if len(s.xSourceExclude) > 0 { + params.Set("_source_excludes", strings.Join(s.xSourceExclude, ",")) + } + if len(s.xSourceInclude) > 0 { + params.Set("_source_includes", strings.Join(s.xSourceInclude, ",")) + } + if s.analyzer != "" { + params.Set("analyzer", s.analyzer) + } + if s.analyzeWildcard != nil { + params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard)) + } + if s.defaultOperator != "" { + params.Set("default_operator", s.defaultOperator) + } + if s.df != "" { + params.Set("df", s.df) + } + if s.explain != nil { + params.Set("explain", fmt.Sprintf("%v", *s.explain)) + } + if len(s.storedFields) > 0 { + params.Set("stored_fields", strings.Join(s.storedFields, ",")) + } + if len(s.docvalueFields) > 0 { + params.Set("docvalue_fields", strings.Join(s.docvalueFields, ",")) + } + if s.from != nil { + params.Set("from", fmt.Sprintf("%d", *s.from)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.conflicts != "" { + params.Set("conflicts", s.conflicts) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.lenient != nil { + params.Set("lenient", fmt.Sprintf("%v", *s.lenient)) + } + if s.lowercaseExpandedTerms != nil { + params.Set("lowercase_expanded_terms", fmt.Sprintf("%v", *s.lowercaseExpandedTerms)) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if s.q != "" { + params.Set("q", s.q) + } + if len(s.routing) > 0 { + params.Set("routing", strings.Join(s.routing, ",")) + } + if s.scroll != "" { + params.Set("scroll", s.scroll) + } + if s.searchType != "" { + params.Set("search_type", s.searchType) + } + if s.searchTimeout != "" { + params.Set("search_timeout", s.searchTimeout) + } + if s.size != nil { + params.Set("size", fmt.Sprintf("%d", *s.size)) + } + if s.slices != nil { + params.Set("slices", fmt.Sprintf("%v", s.slices)) + } + if len(s.sort) > 0 { + params.Set("sort", strings.Join(s.sort, ",")) + } + if s.terminateAfter != nil { + params.Set("terminate_after", fmt.Sprintf("%v", *s.terminateAfter)) + } + if len(s.stats) > 0 { + params.Set("stats", strings.Join(s.stats, ",")) + } + if s.suggestField != "" { + params.Set("suggest_field", s.suggestField) + } + if s.suggestMode != "" { + params.Set("suggest_mode", s.suggestMode) + } + if s.suggestSize != nil { + params.Set("suggest_size", fmt.Sprintf("%v", *s.suggestSize)) + } + if s.suggestText != "" { + params.Set("suggest_text", s.suggestText) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.trackScores != nil { + params.Set("track_scores", fmt.Sprintf("%v", *s.trackScores)) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%v", *s.version)) + } + if s.requestCache != nil { + params.Set("request_cache", fmt.Sprintf("%v", *s.requestCache)) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + if s.scrollSize != nil { + params.Set("scroll_size", fmt.Sprintf("%d", *s.scrollSize)) + } + if s.waitForCompletion != nil { + params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion)) + } + if s.requestsPerSecond != nil { + params.Set("requests_per_second", fmt.Sprintf("%v", *s.requestsPerSecond)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *DeleteByQueryService) Validate() error { + var invalid []string + if len(s.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the delete-by-query operation. +func (s *DeleteByQueryService) Do(ctx context.Context) (*BulkIndexByScrollResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Set body if there is a query set + var body interface{} + if s.body != nil { + body = s.body + } else if s.query != nil { + src, err := s.query.Source() + if err != nil { + return nil, err + } + body = map[string]interface{}{ + "query": src, + } + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return result + ret := new(BulkIndexByScrollResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// DoAsync executes the delete-by-query operation asynchronously by starting a new task. +// Callers need to use the Task Management API to watch the outcome of the reindexing +// operation. +func (s *DeleteByQueryService) DoAsync(ctx context.Context) (*StartTaskResult, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // DoAsync only makes sense with WaitForCompletion set to true + if s.waitForCompletion != nil && *s.waitForCompletion { + return nil, fmt.Errorf("cannot start a task with WaitForCompletion set to true") + } + f := false + s.waitForCompletion = &f + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Set body if there is a query set + var body interface{} + if s.body != nil { + body = s.body + } else if s.query != nil { + src, err := s.query.Source() + if err != nil { + return nil, err + } + body = map[string]interface{}{ + "query": src, + } + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(StartTaskResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// BulkIndexByScrollResponse is the outcome of executing Do with +// DeleteByQueryService and UpdateByQueryService. +type BulkIndexByScrollResponse struct { + Header http.Header `json:"-"` + Took int64 `json:"took"` + SliceId *int64 `json:"slice_id,omitempty"` + TimedOut bool `json:"timed_out"` + Total int64 `json:"total"` + Updated int64 `json:"updated,omitempty"` + Created int64 `json:"created,omitempty"` + Deleted int64 `json:"deleted"` + Batches int64 `json:"batches"` + VersionConflicts int64 `json:"version_conflicts"` + Noops int64 `json:"noops"` + Retries struct { + Bulk int64 `json:"bulk"` + Search int64 `json:"search"` + } `json:"retries,omitempty"` + Throttled string `json:"throttled"` + ThrottledMillis int64 `json:"throttled_millis"` + RequestsPerSecond float64 `json:"requests_per_second"` + Canceled string `json:"canceled,omitempty"` + ThrottledUntil string `json:"throttled_until"` + ThrottledUntilMillis int64 `json:"throttled_until_millis"` + Failures []bulkIndexByScrollResponseFailure `json:"failures"` +} + +type bulkIndexByScrollResponseFailure struct { + Index string `json:"index,omitempty"` + Type string `json:"type,omitempty"` + Id string `json:"id,omitempty"` + Status int `json:"status,omitempty"` + Shard int `json:"shard,omitempty"` + Node int `json:"node,omitempty"` + // TOOD "cause" contains exception details + // TOOD "reason" contains exception details +} diff --git a/vendor/github.com/olivere/elastic/v7/doc.go b/vendor/github.com/olivere/elastic/v7/doc.go new file mode 100644 index 0000000000..ea16d66982 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/doc.go @@ -0,0 +1,51 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +/* +Package elastic provides an interface to the Elasticsearch server +(https://www.elastic.co/products/elasticsearch). + +The first thing you do is to create a Client. If you have Elasticsearch +installed and running with its default settings +(i.e. available at http://127.0.0.1:9200), all you need to do is: + + client, err := elastic.NewClient() + if err != nil { + // Handle error + } + +If your Elasticsearch server is running on a different IP and/or port, +just provide a URL to NewClient: + + // Create a client and connect to http://192.168.2.10:9201 + client, err := elastic.NewClient(elastic.SetURL("http://192.168.2.10:9201")) + if err != nil { + // Handle error + } + +You can pass many more configuration parameters to NewClient. Review the +documentation of NewClient for more information. + +If no Elasticsearch server is available, services will fail when creating +a new request and will return ErrNoClient. + +A Client provides services. The services usually come with a variety of +methods to prepare the query and a Do function to execute it against the +Elasticsearch REST interface and return a response. Here is an example +of the IndexExists service that checks if a given index already exists. + + exists, err := client.IndexExists("twitter").Do(context.Background()) + if err != nil { + // Handle error + } + if !exists { + // Index does not exist yet. + } + +Look up the documentation for Client to get an idea of the services provided +and what kinds of responses you get when executing the Do function of a service. +Also see the wiki on Github for more details. + +*/ +package elastic diff --git a/vendor/github.com/olivere/elastic/v7/docker-compose.yml b/vendor/github.com/olivere/elastic/v7/docker-compose.yml new file mode 100644 index 0000000000..7e171e9a87 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/docker-compose.yml @@ -0,0 +1,59 @@ +version: '3' + +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.4.2 + hostname: elasticsearch + environment: + - cluster.name=elasticsearch + - bootstrap.memory_lock=true + - discovery.type=single-node + # - http.host=0.0.0.0 + # - transport.host=127.0.0.1 + # - network.host=_local_ + - network.publish_host=127.0.0.1 + - logger.org.elasticsearch=warn + - "ES_JAVA_OPTS=-Xms1g -Xmx1g" + ulimits: + nproc: 65536 + nofile: + soft: 65536 + hard: 65536 + memlock: + soft: -1 + hard: -1 + # volumes: + # - ./data/elasticsearch:/usr/share/elasticsearch/data + ports: + - 9200:9200 + platinum: + image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2 + hostname: elasticsearch-platinum + environment: + - cluster.name=platinum + - bootstrap.memory_lock=true + - discovery.type=single-node + - xpack.ilm.enabled=true + - xpack.license.self_generated.type=trial + - xpack.security.enabled=true + - xpack.watcher.enabled=true + # - http.host=0.0.0.0 + # - transport.host=127.0.0.1 + # - network.host=_local_ + - http.port=9210 + - network.publish_host=127.0.0.1 + - logger.org.elasticsearch=warn + - "ES_JAVA_OPTS=-Xms1g -Xmx1g" + - ELASTIC_PASSWORD=elastic + ulimits: + nproc: 65536 + nofile: + soft: 65536 + hard: 65536 + memlock: + soft: -1 + hard: -1 + # volumes: + # - ./data/elasticsearch-platinum:/usr/share/elasticsearch/data + ports: + - 9210:9210 diff --git a/vendor/github.com/olivere/elastic/v7/docvalue_field.go b/vendor/github.com/olivere/elastic/v7/docvalue_field.go new file mode 100644 index 0000000000..e73cd24b15 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/docvalue_field.go @@ -0,0 +1,42 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// DocvalueField represents a docvalue field, its name and +// its format (optional). +type DocvalueField struct { + Field string + Format string +} + +// Source serializes the DocvalueField into JSON. +func (d DocvalueField) Source() (interface{}, error) { + if d.Format == "" { + return d.Field, nil + } + return map[string]interface{}{ + "field": d.Field, + "format": d.Format, + }, nil +} + +// DocvalueFields is a slice of DocvalueField instances. +type DocvalueFields []DocvalueField + +// Source serializes the DocvalueFields into JSON. +func (d DocvalueFields) Source() (interface{}, error) { + if d == nil { + return nil, nil + } + v := make([]interface{}, 0) + for _, f := range d { + src, err := f.Source() + if err != nil { + return nil, err + } + v = append(v, src) + } + return v, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/errors.go b/vendor/github.com/olivere/elastic/v7/errors.go new file mode 100644 index 0000000000..224357602b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/errors.go @@ -0,0 +1,184 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/url" + + "github.com/pkg/errors" +) + +// checkResponse will return an error if the request/response indicates +// an error returned from Elasticsearch. +// +// HTTP status codes between in the range [200..299] are considered successful. +// All other errors are considered errors except they are specified in +// ignoreErrors. This is necessary because for some services, HTTP status 404 +// is a valid response from Elasticsearch (e.g. the Exists service). +// +// The func tries to parse error details as returned from Elasticsearch +// and encapsulates them in type elastic.Error. +func checkResponse(req *http.Request, res *http.Response, ignoreErrors ...int) error { + // 200-299 are valid status codes + if res.StatusCode >= 200 && res.StatusCode <= 299 { + return nil + } + // Ignore certain errors? + for _, code := range ignoreErrors { + if code == res.StatusCode { + return nil + } + } + return createResponseError(res) +} + +// createResponseError creates an Error structure from the HTTP response, +// its status code and the error information sent by Elasticsearch. +func createResponseError(res *http.Response) error { + if res.Body == nil { + return &Error{Status: res.StatusCode} + } + data, err := ioutil.ReadAll(res.Body) + if err != nil { + return &Error{Status: res.StatusCode} + } + errReply := new(Error) + err = json.Unmarshal(data, errReply) + if err != nil { + return &Error{Status: res.StatusCode} + } + if errReply != nil { + if errReply.Status == 0 { + errReply.Status = res.StatusCode + } + return errReply + } + return &Error{Status: res.StatusCode} +} + +// Error encapsulates error details as returned from Elasticsearch. +type Error struct { + Status int `json:"status"` + Details *ErrorDetails `json:"error,omitempty"` +} + +// ErrorDetails encapsulate error details from Elasticsearch. +// It is used in e.g. elastic.Error and elastic.BulkResponseItem. +type ErrorDetails struct { + Type string `json:"type"` + Reason string `json:"reason"` + ResourceType string `json:"resource.type,omitempty"` + ResourceId string `json:"resource.id,omitempty"` + Index string `json:"index,omitempty"` + Phase string `json:"phase,omitempty"` + Grouped bool `json:"grouped,omitempty"` + CausedBy map[string]interface{} `json:"caused_by,omitempty"` + RootCause []*ErrorDetails `json:"root_cause,omitempty"` + FailedShards []map[string]interface{} `json:"failed_shards,omitempty"` +} + +// Error returns a string representation of the error. +func (e *Error) Error() string { + if e.Details != nil && e.Details.Reason != "" { + return fmt.Sprintf("elastic: Error %d (%s): %s [type=%s]", e.Status, http.StatusText(e.Status), e.Details.Reason, e.Details.Type) + } + return fmt.Sprintf("elastic: Error %d (%s)", e.Status, http.StatusText(e.Status)) +} + +// IsContextErr returns true if the error is from a context that was canceled or deadline exceeded +func IsContextErr(err error) bool { + if err == context.Canceled || err == context.DeadlineExceeded { + return true + } + // This happens e.g. on redirect errors, see https://golang.org/src/net/http/client_test.go#L329 + if ue, ok := err.(*url.Error); ok { + if ue.Temporary() { + return true + } + // Use of an AWS Signing Transport can result in a wrapped url.Error + return IsContextErr(ue.Err) + } + return false +} + +// IsConnErr returns true if the error indicates that Elastic could not +// find an Elasticsearch host to connect to. +func IsConnErr(err error) bool { + return err == ErrNoClient || errors.Cause(err) == ErrNoClient +} + +// IsNotFound returns true if the given error indicates that Elasticsearch +// returned HTTP status 404. The err parameter can be of type *elastic.Error, +// elastic.Error, *http.Response or int (indicating the HTTP status code). +func IsNotFound(err interface{}) bool { + return IsStatusCode(err, http.StatusNotFound) +} + +// IsTimeout returns true if the given error indicates that Elasticsearch +// returned HTTP status 408. The err parameter can be of type *elastic.Error, +// elastic.Error, *http.Response or int (indicating the HTTP status code). +func IsTimeout(err interface{}) bool { + return IsStatusCode(err, http.StatusRequestTimeout) +} + +// IsConflict returns true if the given error indicates that the Elasticsearch +// operation resulted in a version conflict. This can occur in operations like +// `update` or `index` with `op_type=create`. The err parameter can be of +// type *elastic.Error, elastic.Error, *http.Response or int (indicating the +// HTTP status code). +func IsConflict(err interface{}) bool { + return IsStatusCode(err, http.StatusConflict) +} + +// IsForbidden returns true if the given error indicates that Elasticsearch +// returned HTTP status 403. This happens e.g. due to a missing license. +// The err parameter can be of type *elastic.Error, elastic.Error, +// *http.Response or int (indicating the HTTP status code). +func IsForbidden(err interface{}) bool { + return IsStatusCode(err, http.StatusForbidden) +} + +// IsStatusCode returns true if the given error indicates that the Elasticsearch +// operation returned the specified HTTP status code. The err parameter can be of +// type *http.Response, *Error, Error, or int (indicating the HTTP status code). +func IsStatusCode(err interface{}, code int) bool { + switch e := err.(type) { + case *http.Response: + return e.StatusCode == code + case *Error: + return e.Status == code + case Error: + return e.Status == code + case int: + return e == code + } + return false +} + +// -- General errors -- + +// ShardsInfo represents information from a shard. +type ShardsInfo struct { + Total int `json:"total"` + Successful int `json:"successful"` + Failed int `json:"failed"` + Failures []*ShardFailure `json:"failures,omitempty"` + Skipped int `json:"skipped,omitempty"` +} + +// ShardFailure represents details about a failure. +type ShardFailure struct { + Index string `json:"_index,omitempty"` + Shard int `json:"_shard,omitempty"` + Node string `json:"_node,omitempty"` + Reason map[string]interface{} `json:"reason,omitempty"` + Status string `json:"status,omitempty"` + Primary bool `json:"primary,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/exists.go b/vendor/github.com/olivere/elastic/v7/exists.go new file mode 100644 index 0000000000..dbc353a7b4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/exists.go @@ -0,0 +1,236 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// ExistsService checks for the existence of a document using HEAD. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-get.html +// for details. +type ExistsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + index string + typ string + preference string + realtime *bool + refresh string + routing string + parent string +} + +// NewExistsService creates a new ExistsService. +func NewExistsService(client *Client) *ExistsService { + return &ExistsService{ + client: client, + typ: "_doc", + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ExistsService) Pretty(pretty bool) *ExistsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ExistsService) Human(human bool) *ExistsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ExistsService) ErrorTrace(errorTrace bool) *ExistsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ExistsService) FilterPath(filterPath ...string) *ExistsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ExistsService) Header(name string, value string) *ExistsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ExistsService) Headers(headers http.Header) *ExistsService { + s.headers = headers + return s +} + +// Id is the document ID. +func (s *ExistsService) Id(id string) *ExistsService { + s.id = id + return s +} + +// Index is the name of the index. +func (s *ExistsService) Index(index string) *ExistsService { + s.index = index + return s +} + +// Type is the type of the document (use `_all` to fetch the first document +// matching the ID across all types). +func (s *ExistsService) Type(typ string) *ExistsService { + s.typ = typ + return s +} + +// Preference specifies the node or shard the operation should be performed on (default: random). +func (s *ExistsService) Preference(preference string) *ExistsService { + s.preference = preference + return s +} + +// Realtime specifies whether to perform the operation in realtime or search mode. +func (s *ExistsService) Realtime(realtime bool) *ExistsService { + s.realtime = &realtime + return s +} + +// Refresh the shard containing the document before performing the operation. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *ExistsService) Refresh(refresh string) *ExistsService { + s.refresh = refresh + return s +} + +// Routing is a specific routing value. +func (s *ExistsService) Routing(routing string) *ExistsService { + s.routing = routing + return s +} + +// Parent is the ID of the parent document. +func (s *ExistsService) Parent(parent string) *ExistsService { + s.parent = parent + return s +} + +// buildURL builds the URL for the operation. +func (s *ExistsService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{ + "id": s.id, + "index": s.index, + "type": s.typ, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.realtime != nil { + params.Set("realtime", fmt.Sprint(*s.realtime)) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ExistsService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if s.index == "" { + invalid = append(invalid, "Index") + } + if s.typ == "" { + invalid = append(invalid, "Type") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *ExistsService) Do(ctx context.Context) (bool, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return false, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return false, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "HEAD", + Path: path, + Params: params, + IgnoreErrors: []int{404}, + Headers: s.headers, + }) + if err != nil { + return false, err + } + + // Return operation response + switch res.StatusCode { + case http.StatusOK: + return true, nil + case http.StatusNotFound: + return false, nil + default: + return false, fmt.Errorf("elastic: got HTTP code %d when it should have been either 200 or 404", res.StatusCode) + } +} diff --git a/vendor/github.com/olivere/elastic/v7/explain.go b/vendor/github.com/olivere/elastic/v7/explain.go new file mode 100644 index 0000000000..dc8795eb43 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/explain.go @@ -0,0 +1,390 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// ExplainService computes a score explanation for a query and +// a specific document. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-explain.html. +type ExplainService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + index string + typ string + q string + routing string + lenient *bool + analyzer string + df string + fields []string + lowercaseExpandedTerms *bool + xSourceInclude []string + analyzeWildcard *bool + parent string + preference string + xSource []string + defaultOperator string + xSourceExclude []string + source string + bodyJson interface{} + bodyString string +} + +// NewExplainService creates a new ExplainService. +func NewExplainService(client *Client) *ExplainService { + return &ExplainService{ + client: client, + typ: "_doc", + xSource: make([]string, 0), + xSourceExclude: make([]string, 0), + fields: make([]string, 0), + xSourceInclude: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ExplainService) Pretty(pretty bool) *ExplainService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ExplainService) Human(human bool) *ExplainService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ExplainService) ErrorTrace(errorTrace bool) *ExplainService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ExplainService) FilterPath(filterPath ...string) *ExplainService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ExplainService) Header(name string, value string) *ExplainService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ExplainService) Headers(headers http.Header) *ExplainService { + s.headers = headers + return s +} + +// Id is the document ID. +func (s *ExplainService) Id(id string) *ExplainService { + s.id = id + return s +} + +// Index is the name of the index. +func (s *ExplainService) Index(index string) *ExplainService { + s.index = index + return s +} + +// Type is the type of the document. +// +// Deprecated: Types are in the process of being removed. +func (s *ExplainService) Type(typ string) *ExplainService { + s.typ = typ + return s +} + +// Source is the URL-encoded query definition (instead of using the request body). +func (s *ExplainService) Source(source string) *ExplainService { + s.source = source + return s +} + +// XSourceExclude is a list of fields to exclude from the returned _source field. +func (s *ExplainService) XSourceExclude(xSourceExclude ...string) *ExplainService { + s.xSourceExclude = append(s.xSourceExclude, xSourceExclude...) + return s +} + +// Lenient specifies whether format-based query failures +// (such as providing text to a numeric field) should be ignored. +func (s *ExplainService) Lenient(lenient bool) *ExplainService { + s.lenient = &lenient + return s +} + +// Query in the Lucene query string syntax. +func (s *ExplainService) Q(q string) *ExplainService { + s.q = q + return s +} + +// Routing sets a specific routing value. +func (s *ExplainService) Routing(routing string) *ExplainService { + s.routing = routing + return s +} + +// AnalyzeWildcard specifies whether wildcards and prefix queries +// in the query string query should be analyzed (default: false). +func (s *ExplainService) AnalyzeWildcard(analyzeWildcard bool) *ExplainService { + s.analyzeWildcard = &analyzeWildcard + return s +} + +// Analyzer is the analyzer for the query string query. +func (s *ExplainService) Analyzer(analyzer string) *ExplainService { + s.analyzer = analyzer + return s +} + +// Df is the default field for query string query (default: _all). +func (s *ExplainService) Df(df string) *ExplainService { + s.df = df + return s +} + +// Fields is a list of fields to return in the response. +func (s *ExplainService) Fields(fields ...string) *ExplainService { + s.fields = append(s.fields, fields...) + return s +} + +// LowercaseExpandedTerms specifies whether query terms should be lowercased. +func (s *ExplainService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *ExplainService { + s.lowercaseExpandedTerms = &lowercaseExpandedTerms + return s +} + +// XSourceInclude is a list of fields to extract and return from the _source field. +func (s *ExplainService) XSourceInclude(xSourceInclude ...string) *ExplainService { + s.xSourceInclude = append(s.xSourceInclude, xSourceInclude...) + return s +} + +// DefaultOperator is the default operator for query string query (AND or OR). +func (s *ExplainService) DefaultOperator(defaultOperator string) *ExplainService { + s.defaultOperator = defaultOperator + return s +} + +// Parent is the ID of the parent document. +func (s *ExplainService) Parent(parent string) *ExplainService { + s.parent = parent + return s +} + +// Preference specifies the node or shard the operation should be performed on (default: random). +func (s *ExplainService) Preference(preference string) *ExplainService { + s.preference = preference + return s +} + +// XSource is true or false to return the _source field or not, or a list of fields to return. +func (s *ExplainService) XSource(xSource ...string) *ExplainService { + s.xSource = append(s.xSource, xSource...) + return s +} + +// Query sets a query definition using the Query DSL. +func (s *ExplainService) Query(query Query) *ExplainService { + src, err := query.Source() + if err != nil { + // Do nothing in case of an error + return s + } + body := make(map[string]interface{}) + body["query"] = src + s.bodyJson = body + return s +} + +// BodyJson sets the query definition using the Query DSL. +func (s *ExplainService) BodyJson(body interface{}) *ExplainService { + s.bodyJson = body + return s +} + +// BodyString sets the query definition using the Query DSL as a string. +func (s *ExplainService) BodyString(body string) *ExplainService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *ExplainService) buildURL() (string, url.Values, error) { + // Build URL + var path string + var err error + + if s.typ == "" || s.typ == "_doc" { + path, err = uritemplates.Expand("/{index}/_explain/{id}", map[string]string{ + "id": s.id, + "index": s.index, + }) + } else { + path, err = uritemplates.Expand("/{index}/{type}/{id}/_explain", map[string]string{ + "id": s.id, + "index": s.index, + "type": s.typ, + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.xSource) > 0 { + params.Set("_source", strings.Join(s.xSource, ",")) + } + if s.defaultOperator != "" { + params.Set("default_operator", s.defaultOperator) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if s.source != "" { + params.Set("source", s.source) + } + if len(s.xSourceExclude) > 0 { + params.Set("_source_excludes", strings.Join(s.xSourceExclude, ",")) + } + if s.lenient != nil { + params.Set("lenient", fmt.Sprintf("%v", *s.lenient)) + } + if s.q != "" { + params.Set("q", s.q) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + if s.lowercaseExpandedTerms != nil { + params.Set("lowercase_expanded_terms", fmt.Sprintf("%v", *s.lowercaseExpandedTerms)) + } + if len(s.xSourceInclude) > 0 { + params.Set("_source_includes", strings.Join(s.xSourceInclude, ",")) + } + if s.analyzeWildcard != nil { + params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard)) + } + if s.analyzer != "" { + params.Set("analyzer", s.analyzer) + } + if s.df != "" { + params.Set("df", s.df) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ExplainService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if s.typ == "" { + invalid = append(invalid, "Type") + } + if s.id == "" { + invalid = append(invalid, "Id") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *ExplainService) Do(ctx context.Context) (*ExplainResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ExplainResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ExplainResponse is the response of ExplainService.Do. +type ExplainResponse struct { + Index string `json:"_index"` + Type string `json:"_type"` + Id string `json:"_id"` + Matched bool `json:"matched"` + Explanation map[string]interface{} `json:"explanation"` +} diff --git a/vendor/github.com/olivere/elastic/v7/fetch_source_context.go b/vendor/github.com/olivere/elastic/v7/fetch_source_context.go new file mode 100644 index 0000000000..7a4a76941c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/fetch_source_context.go @@ -0,0 +1,90 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "net/url" + "strings" +) + +// FetchSourceContext enables source filtering, i.e. it allows control +// over how the _source field is returned with every hit. It is used +// with various endpoints, e.g. when searching for documents, retrieving +// individual documents, or even updating documents. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-source-filtering.html +// for details. +type FetchSourceContext struct { + fetchSource bool + includes []string + excludes []string +} + +// NewFetchSourceContext returns a new FetchSourceContext. +func NewFetchSourceContext(fetchSource bool) *FetchSourceContext { + return &FetchSourceContext{ + fetchSource: fetchSource, + includes: make([]string, 0), + excludes: make([]string, 0), + } +} + +// FetchSource indicates whether to return the _source. +func (fsc *FetchSourceContext) FetchSource() bool { + return fsc.fetchSource +} + +// SetFetchSource specifies whether to return the _source. +func (fsc *FetchSourceContext) SetFetchSource(fetchSource bool) { + fsc.fetchSource = fetchSource +} + +// Include indicates to return specific parts of the _source. +// Wildcards are allowed here. +func (fsc *FetchSourceContext) Include(includes ...string) *FetchSourceContext { + fsc.includes = append(fsc.includes, includes...) + return fsc +} + +// Exclude indicates to exclude specific parts of the _source. +// Wildcards are allowed here. +func (fsc *FetchSourceContext) Exclude(excludes ...string) *FetchSourceContext { + fsc.excludes = append(fsc.excludes, excludes...) + return fsc +} + +// Source returns the JSON-serializable data to be used in a body. +func (fsc *FetchSourceContext) Source() (interface{}, error) { + if !fsc.fetchSource { + return false, nil + } + if len(fsc.includes) == 0 && len(fsc.excludes) == 0 { + return true, nil + } + src := make(map[string]interface{}) + if len(fsc.includes) > 0 { + src["includes"] = fsc.includes + } + if len(fsc.excludes) > 0 { + src["excludes"] = fsc.excludes + } + return src, nil +} + +// Query returns the parameters in a form suitable for a URL query string. +func (fsc *FetchSourceContext) Query() url.Values { + params := url.Values{} + if fsc.fetchSource { + if len(fsc.includes) > 0 { + params.Add("_source_includes", strings.Join(fsc.includes, ",")) + } + if len(fsc.excludes) > 0 { + params.Add("_source_excludes", strings.Join(fsc.excludes, ",")) + } + } else { + params.Add("_source", "false") + } + return params +} diff --git a/vendor/github.com/olivere/elastic/v7/field_caps.go b/vendor/github.com/olivere/elastic/v7/field_caps.go new file mode 100644 index 0000000000..b84a1f13f0 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/field_caps.go @@ -0,0 +1,257 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// FieldCapsService allows retrieving the capabilities of fields among multiple indices. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-field-caps.html +// for details +type FieldCapsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + allowNoIndices *bool + expandWildcards string + fields []string + ignoreUnavailable *bool + bodyJson interface{} + bodyString string +} + +// NewFieldCapsService creates a new FieldCapsService +func NewFieldCapsService(client *Client) *FieldCapsService { + return &FieldCapsService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *FieldCapsService) Pretty(pretty bool) *FieldCapsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *FieldCapsService) Human(human bool) *FieldCapsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *FieldCapsService) ErrorTrace(errorTrace bool) *FieldCapsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *FieldCapsService) FilterPath(filterPath ...string) *FieldCapsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *FieldCapsService) Header(name string, value string) *FieldCapsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *FieldCapsService) Headers(headers http.Header) *FieldCapsService { + s.headers = headers + return s +} + +// Index is a list of index names; use `_all` or empty string to perform +// the operation on all indices. +func (s *FieldCapsService) Index(index ...string) *FieldCapsService { + s.index = append(s.index, index...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. +// (This includes `_all` string or when no indices have been specified). +func (s *FieldCapsService) AllowNoIndices(allowNoIndices bool) *FieldCapsService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *FieldCapsService) ExpandWildcards(expandWildcards string) *FieldCapsService { + s.expandWildcards = expandWildcards + return s +} + +// Fields is a list of fields for to get field capabilities. +func (s *FieldCapsService) Fields(fields ...string) *FieldCapsService { + s.fields = append(s.fields, fields...) + return s +} + +// IgnoreUnavailable is documented as: Whether specified concrete indices should be ignored when unavailable (missing or closed). +func (s *FieldCapsService) IgnoreUnavailable(ignoreUnavailable bool) *FieldCapsService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// BodyJson is documented as: Field json objects containing the name and optionally a range to filter out indices result, that have results outside the defined bounds. +func (s *FieldCapsService) BodyJson(body interface{}) *FieldCapsService { + s.bodyJson = body + return s +} + +// BodyString is documented as: Field json objects containing the name and optionally a range to filter out indices result, that have results outside the defined bounds. +func (s *FieldCapsService) BodyString(body string) *FieldCapsService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *FieldCapsService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_field_caps", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_field_caps" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *FieldCapsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *FieldCapsService) Do(ctx context.Context) (*FieldCapsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + IgnoreErrors: []int{http.StatusNotFound}, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // TODO(oe): Is 404 really a valid response here? + if res.StatusCode == http.StatusNotFound { + return &FieldCapsResponse{}, nil + } + + // Return operation response + ret := new(FieldCapsResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Request -- + +// FieldCapsRequest can be used to set up the body to be used in the +// Field Capabilities API. +type FieldCapsRequest struct { + Fields []string `json:"fields"` +} + +// -- Response -- + +// FieldCapsResponse contains field capabilities. +type FieldCapsResponse struct { + Indices []string `json:"indices,omitempty"` // list of index names + Fields map[string]FieldCapsType `json:"fields,omitempty"` // Name -> type -> caps +} + +// FieldCapsType represents a mapping from type (e.g. keyword) +// to capabilities. +type FieldCapsType map[string]FieldCaps // type -> caps + +// FieldCaps contains capabilities of an individual field. +type FieldCaps struct { + Type string `json:"type"` + Searchable bool `json:"searchable"` + Aggregatable bool `json:"aggregatable"` + Indices []string `json:"indices,omitempty"` + NonSearchableIndices []string `json:"non_searchable_indices,omitempty"` + NonAggregatableIndices []string `json:"non_aggregatable_indices,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/geo_point.go b/vendor/github.com/olivere/elastic/v7/geo_point.go new file mode 100644 index 0000000000..4ef0e1ad47 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/geo_point.go @@ -0,0 +1,54 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "encoding/json" + "fmt" + "strconv" + "strings" +) + +// GeoPoint is a geographic position described via latitude and longitude. +type GeoPoint struct { + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` +} + +// Source returns the object to be serialized in Elasticsearch DSL. +func (pt *GeoPoint) Source() map[string]float64 { + return map[string]float64{ + "lat": pt.Lat, + "lon": pt.Lon, + } +} + +// MarshalJSON encodes the GeoPoint to JSON. +func (pt *GeoPoint) MarshalJSON() ([]byte, error) { + return json.Marshal(pt.Source()) +} + +// GeoPointFromLatLon initializes a new GeoPoint by latitude and longitude. +func GeoPointFromLatLon(lat, lon float64) *GeoPoint { + return &GeoPoint{Lat: lat, Lon: lon} +} + +// GeoPointFromString initializes a new GeoPoint by a string that is +// formatted as "{latitude},{longitude}", e.g. "40.10210,-70.12091". +func GeoPointFromString(latLon string) (*GeoPoint, error) { + latlon := strings.SplitN(latLon, ",", 2) + if len(latlon) != 2 { + return nil, fmt.Errorf("elastic: %s is not a valid geo point string", latLon) + } + lat, err := strconv.ParseFloat(latlon[0], 64) + if err != nil { + return nil, err + } + lon, err := strconv.ParseFloat(latlon[1], 64) + if err != nil { + return nil, err + } + return &GeoPoint{Lat: lat, Lon: lon}, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/get.go b/vendor/github.com/olivere/elastic/v7/get.go new file mode 100644 index 0000000000..769389b53f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/get.go @@ -0,0 +1,317 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// GetService allows to get a typed JSON document from the index based +// on its id. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-get.html +// for details. +type GetService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + typ string + id string + routing string + preference string + storedFields []string + refresh string + realtime *bool + fsc *FetchSourceContext + version interface{} + versionType string + parent string + ignoreErrorsOnGeneratedFields *bool +} + +// NewGetService creates a new GetService. +func NewGetService(client *Client) *GetService { + return &GetService{ + client: client, + typ: "_doc", + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *GetService) Pretty(pretty bool) *GetService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *GetService) Human(human bool) *GetService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *GetService) ErrorTrace(errorTrace bool) *GetService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *GetService) FilterPath(filterPath ...string) *GetService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *GetService) Header(name string, value string) *GetService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *GetService) Headers(headers http.Header) *GetService { + s.headers = headers + return s +} + +// Index is the name of the index. +func (s *GetService) Index(index string) *GetService { + s.index = index + return s +} + +// Type is the type of the document +// +// Deprecated: Types are in the process of being removed. +func (s *GetService) Type(typ string) *GetService { + s.typ = typ + return s +} + +// Id is the document ID. +func (s *GetService) Id(id string) *GetService { + s.id = id + return s +} + +// Parent is the ID of the parent document. +func (s *GetService) Parent(parent string) *GetService { + s.parent = parent + return s +} + +// Routing is the specific routing value. +func (s *GetService) Routing(routing string) *GetService { + s.routing = routing + return s +} + +// Preference specifies the node or shard the operation should be performed on (default: random). +func (s *GetService) Preference(preference string) *GetService { + s.preference = preference + return s +} + +// StoredFields is a list of fields to return in the response. +func (s *GetService) StoredFields(storedFields ...string) *GetService { + s.storedFields = append(s.storedFields, storedFields...) + return s +} + +func (s *GetService) FetchSource(fetchSource bool) *GetService { + if s.fsc == nil { + s.fsc = NewFetchSourceContext(fetchSource) + } else { + s.fsc.SetFetchSource(fetchSource) + } + return s +} + +func (s *GetService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *GetService { + s.fsc = fetchSourceContext + return s +} + +// Refresh the shard containing the document before performing the operation. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *GetService) Refresh(refresh string) *GetService { + s.refresh = refresh + return s +} + +// Realtime specifies whether to perform the operation in realtime or search mode. +func (s *GetService) Realtime(realtime bool) *GetService { + s.realtime = &realtime + return s +} + +// VersionType is the specific version type. +func (s *GetService) VersionType(versionType string) *GetService { + s.versionType = versionType + return s +} + +// Version is an explicit version number for concurrency control. +func (s *GetService) Version(version interface{}) *GetService { + s.version = version + return s +} + +// IgnoreErrorsOnGeneratedFields indicates whether to ignore fields that +// are generated if the transaction log is accessed. +func (s *GetService) IgnoreErrorsOnGeneratedFields(ignore bool) *GetService { + s.ignoreErrorsOnGeneratedFields = &ignore + return s +} + +// Validate checks if the operation is valid. +func (s *GetService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if s.index == "" { + invalid = append(invalid, "Index") + } + if s.typ == "" { + invalid = append(invalid, "Type") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// buildURL builds the URL for the operation. +func (s *GetService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{ + "id": s.id, + "index": s.index, + "type": s.typ, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if len(s.storedFields) > 0 { + params.Set("stored_fields", strings.Join(s.storedFields, ",")) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%v", s.version)) + } + if s.versionType != "" { + params.Set("version_type", s.versionType) + } + if s.realtime != nil { + params.Set("realtime", fmt.Sprintf("%v", *s.realtime)) + } + if s.ignoreErrorsOnGeneratedFields != nil { + params.Add("ignore_errors_on_generated_fields", fmt.Sprintf("%v", *s.ignoreErrorsOnGeneratedFields)) + } + if s.fsc != nil { + for k, values := range s.fsc.Query() { + params.Add(k, strings.Join(values, ",")) + } + } + return path, params, nil +} + +// Do executes the operation. +func (s *GetService) Do(ctx context.Context) (*GetResult, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(GetResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a get request. + +// GetResult is the outcome of GetService.Do. +type GetResult struct { + Index string `json:"_index"` // index meta field + Type string `json:"_type"` // type meta field + Id string `json:"_id"` // id meta field + Uid string `json:"_uid"` // uid meta field (see MapperService.java for all meta fields) + Routing string `json:"_routing"` // routing meta field + Parent string `json:"_parent"` // parent meta field + Version *int64 `json:"_version"` // version number, when Version is set to true in SearchService + SeqNo *int64 `json:"_seq_no"` + PrimaryTerm *int64 `json:"_primary_term"` + Source json.RawMessage `json:"_source,omitempty"` + Found bool `json:"found,omitempty"` + Fields map[string]interface{} `json:"fields,omitempty"` + //Error string `json:"error,omitempty"` // used only in MultiGet + // TODO double-check that MultiGet now returns details error information + Error *ErrorDetails `json:"error,omitempty"` // only used in MultiGet +} diff --git a/vendor/github.com/olivere/elastic/v7/go.mod b/vendor/github.com/olivere/elastic/v7/go.mod new file mode 100644 index 0000000000..165f7e8938 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/go.mod @@ -0,0 +1,18 @@ +module github.com/olivere/elastic/v7 + +go 1.12 + +require ( + github.com/aws/aws-sdk-go v1.25.25 + github.com/fortytw2/leaktest v1.3.0 + github.com/golang/mock v1.2.0 // indirect + github.com/google/go-cmp v0.3.1 + github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e + github.com/opentracing/opentracing-go v1.1.0 + github.com/pkg/errors v0.8.1 + github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9 + go.opencensus.io v0.22.1 + google.golang.org/api v0.3.1 // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect +) diff --git a/vendor/github.com/olivere/elastic/v7/highlight.go b/vendor/github.com/olivere/elastic/v7/highlight.go new file mode 100644 index 0000000000..49e1b4d329 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/highlight.go @@ -0,0 +1,469 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// Highlight allows highlighting search results on one or more fields. +// For details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-highlighting.html +type Highlight struct { + fields []*HighlighterField + tagsSchema *string + highlightFilter *bool + fragmentSize *int + numOfFragments *int + preTags []string + postTags []string + order *string + encoder *string + requireFieldMatch *bool + boundaryMaxScan *int + boundaryChars *string + boundaryScannerType *string + boundaryScannerLocale *string + highlighterType *string + fragmenter *string + highlightQuery Query + noMatchSize *int + phraseLimit *int + options map[string]interface{} + forceSource *bool + useExplicitFieldOrder bool +} + +func NewHighlight() *Highlight { + hl := &Highlight{ + options: make(map[string]interface{}), + } + return hl +} + +func (hl *Highlight) Fields(fields ...*HighlighterField) *Highlight { + hl.fields = append(hl.fields, fields...) + return hl +} + +func (hl *Highlight) Field(name string) *Highlight { + field := NewHighlighterField(name) + hl.fields = append(hl.fields, field) + return hl +} + +func (hl *Highlight) TagsSchema(schemaName string) *Highlight { + hl.tagsSchema = &schemaName + return hl +} + +func (hl *Highlight) HighlightFilter(highlightFilter bool) *Highlight { + hl.highlightFilter = &highlightFilter + return hl +} + +func (hl *Highlight) FragmentSize(fragmentSize int) *Highlight { + hl.fragmentSize = &fragmentSize + return hl +} + +func (hl *Highlight) NumOfFragments(numOfFragments int) *Highlight { + hl.numOfFragments = &numOfFragments + return hl +} + +func (hl *Highlight) Encoder(encoder string) *Highlight { + hl.encoder = &encoder + return hl +} + +func (hl *Highlight) PreTags(preTags ...string) *Highlight { + hl.preTags = append(hl.preTags, preTags...) + return hl +} + +func (hl *Highlight) PostTags(postTags ...string) *Highlight { + hl.postTags = append(hl.postTags, postTags...) + return hl +} + +func (hl *Highlight) Order(order string) *Highlight { + hl.order = &order + return hl +} + +func (hl *Highlight) RequireFieldMatch(requireFieldMatch bool) *Highlight { + hl.requireFieldMatch = &requireFieldMatch + return hl +} + +func (hl *Highlight) BoundaryMaxScan(boundaryMaxScan int) *Highlight { + hl.boundaryMaxScan = &boundaryMaxScan + return hl +} + +func (hl *Highlight) BoundaryChars(boundaryChars string) *Highlight { + hl.boundaryChars = &boundaryChars + return hl +} + +func (hl *Highlight) BoundaryScannerType(boundaryScannerType string) *Highlight { + hl.boundaryScannerType = &boundaryScannerType + return hl +} + +func (hl *Highlight) BoundaryScannerLocale(boundaryScannerLocale string) *Highlight { + hl.boundaryScannerLocale = &boundaryScannerLocale + return hl +} + +func (hl *Highlight) HighlighterType(highlighterType string) *Highlight { + hl.highlighterType = &highlighterType + return hl +} + +func (hl *Highlight) Fragmenter(fragmenter string) *Highlight { + hl.fragmenter = &fragmenter + return hl +} + +func (hl *Highlight) HighlightQuery(highlightQuery Query) *Highlight { + hl.highlightQuery = highlightQuery + return hl +} + +func (hl *Highlight) NoMatchSize(noMatchSize int) *Highlight { + hl.noMatchSize = &noMatchSize + return hl +} + +func (hl *Highlight) Options(options map[string]interface{}) *Highlight { + hl.options = options + return hl +} + +func (hl *Highlight) ForceSource(forceSource bool) *Highlight { + hl.forceSource = &forceSource + return hl +} + +func (hl *Highlight) UseExplicitFieldOrder(useExplicitFieldOrder bool) *Highlight { + hl.useExplicitFieldOrder = useExplicitFieldOrder + return hl +} + +// Creates the query source for the bool query. +func (hl *Highlight) Source() (interface{}, error) { + // Returns the map inside of "highlight": + // "highlight":{ + // ... this ... + // } + source := make(map[string]interface{}) + if hl.tagsSchema != nil { + source["tags_schema"] = *hl.tagsSchema + } + if hl.preTags != nil && len(hl.preTags) > 0 { + source["pre_tags"] = hl.preTags + } + if hl.postTags != nil && len(hl.postTags) > 0 { + source["post_tags"] = hl.postTags + } + if hl.order != nil { + source["order"] = *hl.order + } + if hl.highlightFilter != nil { + source["highlight_filter"] = *hl.highlightFilter + } + if hl.fragmentSize != nil { + source["fragment_size"] = *hl.fragmentSize + } + if hl.numOfFragments != nil { + source["number_of_fragments"] = *hl.numOfFragments + } + if hl.encoder != nil { + source["encoder"] = *hl.encoder + } + if hl.requireFieldMatch != nil { + source["require_field_match"] = *hl.requireFieldMatch + } + if hl.boundaryMaxScan != nil { + source["boundary_max_scan"] = *hl.boundaryMaxScan + } + if hl.boundaryChars != nil { + source["boundary_chars"] = *hl.boundaryChars + } + if hl.boundaryScannerType != nil { + source["boundary_scanner"] = *hl.boundaryScannerType + } + if hl.boundaryScannerLocale != nil { + source["boundary_scanner_locale"] = *hl.boundaryScannerLocale + } + if hl.highlighterType != nil { + source["type"] = *hl.highlighterType + } + if hl.fragmenter != nil { + source["fragmenter"] = *hl.fragmenter + } + if hl.highlightQuery != nil { + src, err := hl.highlightQuery.Source() + if err != nil { + return nil, err + } + source["highlight_query"] = src + } + if hl.noMatchSize != nil { + source["no_match_size"] = *hl.noMatchSize + } + if hl.phraseLimit != nil { + source["phrase_limit"] = *hl.phraseLimit + } + if hl.options != nil && len(hl.options) > 0 { + source["options"] = hl.options + } + if hl.forceSource != nil { + source["force_source"] = *hl.forceSource + } + + if hl.fields != nil && len(hl.fields) > 0 { + if hl.useExplicitFieldOrder { + // Use a slice for the fields + var fields []map[string]interface{} + for _, field := range hl.fields { + src, err := field.Source() + if err != nil { + return nil, err + } + fmap := make(map[string]interface{}) + fmap[field.Name] = src + fields = append(fields, fmap) + } + source["fields"] = fields + } else { + // Use a map for the fields + fields := make(map[string]interface{}) + for _, field := range hl.fields { + src, err := field.Source() + if err != nil { + return nil, err + } + fields[field.Name] = src + } + source["fields"] = fields + } + } + + return source, nil +} + +// HighlighterField specifies a highlighted field. +type HighlighterField struct { + Name string + + preTags []string + postTags []string + fragmentSize int + fragmentOffset int + numOfFragments int + highlightFilter *bool + order *string + requireFieldMatch *bool + boundaryMaxScan int + boundaryChars []rune + highlighterType *string + fragmenter *string + highlightQuery Query + noMatchSize *int + matchedFields []string + phraseLimit *int + options map[string]interface{} + forceSource *bool + + /* + Name string + preTags []string + postTags []string + fragmentSize int + numOfFragments int + fragmentOffset int + highlightFilter *bool + order string + requireFieldMatch *bool + boundaryMaxScan int + boundaryChars []rune + highlighterType string + fragmenter string + highlightQuery Query + noMatchSize *int + matchedFields []string + options map[string]interface{} + forceSource *bool + */ +} + +func NewHighlighterField(name string) *HighlighterField { + return &HighlighterField{ + Name: name, + preTags: make([]string, 0), + postTags: make([]string, 0), + fragmentSize: -1, + fragmentOffset: -1, + numOfFragments: -1, + boundaryMaxScan: -1, + boundaryChars: make([]rune, 0), + matchedFields: make([]string, 0), + options: make(map[string]interface{}), + } +} + +func (f *HighlighterField) PreTags(preTags ...string) *HighlighterField { + f.preTags = append(f.preTags, preTags...) + return f +} + +func (f *HighlighterField) PostTags(postTags ...string) *HighlighterField { + f.postTags = append(f.postTags, postTags...) + return f +} + +func (f *HighlighterField) FragmentSize(fragmentSize int) *HighlighterField { + f.fragmentSize = fragmentSize + return f +} + +func (f *HighlighterField) FragmentOffset(fragmentOffset int) *HighlighterField { + f.fragmentOffset = fragmentOffset + return f +} + +func (f *HighlighterField) NumOfFragments(numOfFragments int) *HighlighterField { + f.numOfFragments = numOfFragments + return f +} + +func (f *HighlighterField) HighlightFilter(highlightFilter bool) *HighlighterField { + f.highlightFilter = &highlightFilter + return f +} + +func (f *HighlighterField) Order(order string) *HighlighterField { + f.order = &order + return f +} + +func (f *HighlighterField) RequireFieldMatch(requireFieldMatch bool) *HighlighterField { + f.requireFieldMatch = &requireFieldMatch + return f +} + +func (f *HighlighterField) BoundaryMaxScan(boundaryMaxScan int) *HighlighterField { + f.boundaryMaxScan = boundaryMaxScan + return f +} + +func (f *HighlighterField) BoundaryChars(boundaryChars ...rune) *HighlighterField { + f.boundaryChars = append(f.boundaryChars, boundaryChars...) + return f +} + +func (f *HighlighterField) HighlighterType(highlighterType string) *HighlighterField { + f.highlighterType = &highlighterType + return f +} + +func (f *HighlighterField) Fragmenter(fragmenter string) *HighlighterField { + f.fragmenter = &fragmenter + return f +} + +func (f *HighlighterField) HighlightQuery(highlightQuery Query) *HighlighterField { + f.highlightQuery = highlightQuery + return f +} + +func (f *HighlighterField) NoMatchSize(noMatchSize int) *HighlighterField { + f.noMatchSize = &noMatchSize + return f +} + +func (f *HighlighterField) Options(options map[string]interface{}) *HighlighterField { + f.options = options + return f +} + +func (f *HighlighterField) MatchedFields(matchedFields ...string) *HighlighterField { + f.matchedFields = append(f.matchedFields, matchedFields...) + return f +} + +func (f *HighlighterField) PhraseLimit(phraseLimit int) *HighlighterField { + f.phraseLimit = &phraseLimit + return f +} + +func (f *HighlighterField) ForceSource(forceSource bool) *HighlighterField { + f.forceSource = &forceSource + return f +} + +func (f *HighlighterField) Source() (interface{}, error) { + source := make(map[string]interface{}) + + if f.preTags != nil && len(f.preTags) > 0 { + source["pre_tags"] = f.preTags + } + if f.postTags != nil && len(f.postTags) > 0 { + source["post_tags"] = f.postTags + } + if f.fragmentSize != -1 { + source["fragment_size"] = f.fragmentSize + } + if f.numOfFragments != -1 { + source["number_of_fragments"] = f.numOfFragments + } + if f.fragmentOffset != -1 { + source["fragment_offset"] = f.fragmentOffset + } + if f.highlightFilter != nil { + source["highlight_filter"] = *f.highlightFilter + } + if f.order != nil { + source["order"] = *f.order + } + if f.requireFieldMatch != nil { + source["require_field_match"] = *f.requireFieldMatch + } + if f.boundaryMaxScan != -1 { + source["boundary_max_scan"] = f.boundaryMaxScan + } + if f.boundaryChars != nil && len(f.boundaryChars) > 0 { + source["boundary_chars"] = f.boundaryChars + } + if f.highlighterType != nil { + source["type"] = *f.highlighterType + } + if f.fragmenter != nil { + source["fragmenter"] = *f.fragmenter + } + if f.highlightQuery != nil { + src, err := f.highlightQuery.Source() + if err != nil { + return nil, err + } + source["highlight_query"] = src + } + if f.noMatchSize != nil { + source["no_match_size"] = *f.noMatchSize + } + if f.matchedFields != nil && len(f.matchedFields) > 0 { + source["matched_fields"] = f.matchedFields + } + if f.phraseLimit != nil { + source["phrase_limit"] = *f.phraseLimit + } + if f.options != nil && len(f.options) > 0 { + source["options"] = f.options + } + if f.forceSource != nil { + source["force_source"] = *f.forceSource + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/index.go b/vendor/github.com/olivere/elastic/v7/index.go new file mode 100644 index 0000000000..2e3d66f231 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/index.go @@ -0,0 +1,377 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndexService adds or updates a typed JSON document in a specified index, +// making it searchable. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-index_.html +// for details. +type IndexService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + index string + typ string + parent string + routing string + timeout string + timestamp string + ttl string + version interface{} + opType string + versionType string + refresh string + waitForActiveShards string + pipeline string + ifSeqNo *int64 + ifPrimaryTerm *int64 + bodyJson interface{} + bodyString string +} + +// NewIndexService creates a new IndexService. +func NewIndexService(client *Client) *IndexService { + return &IndexService{ + client: client, + typ: "_doc", + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndexService) Pretty(pretty bool) *IndexService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndexService) Human(human bool) *IndexService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndexService) ErrorTrace(errorTrace bool) *IndexService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndexService) FilterPath(filterPath ...string) *IndexService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndexService) Header(name string, value string) *IndexService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndexService) Headers(headers http.Header) *IndexService { + s.headers = headers + return s +} + +// Id is the document ID. +func (s *IndexService) Id(id string) *IndexService { + s.id = id + return s +} + +// Index is the name of the index. +func (s *IndexService) Index(index string) *IndexService { + s.index = index + return s +} + +// Type is the type of the document. +// +// Deprecated: Types are in the process of being removed. +func (s *IndexService) Type(typ string) *IndexService { + s.typ = typ + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active +// before proceeding with the index operation. Defaults to 1, meaning the +// primary shard only. Set to `all` for all shard copies, otherwise set to +// any non-negative value less than or equal to the total number of copies +// for the shard (number of replicas + 1). +func (s *IndexService) WaitForActiveShards(waitForActiveShards string) *IndexService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// Pipeline specifies the pipeline id to preprocess incoming documents with. +func (s *IndexService) Pipeline(pipeline string) *IndexService { + s.pipeline = pipeline + return s +} + +// Refresh the index after performing the operation. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *IndexService) Refresh(refresh string) *IndexService { + s.refresh = refresh + return s +} + +// Ttl is an expiration time for the document. +func (s *IndexService) Ttl(ttl string) *IndexService { + s.ttl = ttl + return s +} + +// TTL is an expiration time for the document (alias for Ttl). +func (s *IndexService) TTL(ttl string) *IndexService { + s.ttl = ttl + return s +} + +// Version is an explicit version number for concurrency control. +func (s *IndexService) Version(version interface{}) *IndexService { + s.version = version + return s +} + +// OpType is an explicit operation type, i.e. "create" or "index" (default). +func (s *IndexService) OpType(opType string) *IndexService { + s.opType = opType + return s +} + +// Parent is the ID of the parent document. +func (s *IndexService) Parent(parent string) *IndexService { + s.parent = parent + return s +} + +// Routing is a specific routing value. +func (s *IndexService) Routing(routing string) *IndexService { + s.routing = routing + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndexService) Timeout(timeout string) *IndexService { + s.timeout = timeout + return s +} + +// Timestamp is an explicit timestamp for the document. +func (s *IndexService) Timestamp(timestamp string) *IndexService { + s.timestamp = timestamp + return s +} + +// VersionType is a specific version type. +func (s *IndexService) VersionType(versionType string) *IndexService { + s.versionType = versionType + return s +} + +// IfSeqNo indicates to only perform the index operation if the last +// operation that has changed the document has the specified sequence number. +func (s *IndexService) IfSeqNo(seqNo int64) *IndexService { + s.ifSeqNo = &seqNo + return s +} + +// IfPrimaryTerm indicates to only perform the index operation if the +// last operation that has changed the document has the specified primary term. +func (s *IndexService) IfPrimaryTerm(primaryTerm int64) *IndexService { + s.ifPrimaryTerm = &primaryTerm + return s +} + +// BodyJson is the document as a serializable JSON interface. +func (s *IndexService) BodyJson(body interface{}) *IndexService { + s.bodyJson = body + return s +} + +// BodyString is the document encoded as a string. +func (s *IndexService) BodyString(body string) *IndexService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IndexService) buildURL() (string, string, url.Values, error) { + var err error + var method, path string + + if s.id != "" { + // Create document with manual id + method = "PUT" + path, err = uritemplates.Expand("/{index}/{type}/{id}", map[string]string{ + "id": s.id, + "index": s.index, + "type": s.typ, + }) + } else { + // Automatic ID generation + // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-index_.html#index-creation + method = "POST" + path, err = uritemplates.Expand("/{index}/{type}/", map[string]string{ + "index": s.index, + "type": s.typ, + }) + } + if err != nil { + return "", "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.opType != "" { + params.Set("op_type", s.opType) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if s.pipeline != "" { + params.Set("pipeline", s.pipeline) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.timestamp != "" { + params.Set("timestamp", s.timestamp) + } + if s.ttl != "" { + params.Set("ttl", s.ttl) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%v", s.version)) + } + if s.versionType != "" { + params.Set("version_type", s.versionType) + } + if v := s.ifSeqNo; v != nil { + params.Set("if_seq_no", fmt.Sprintf("%d", *v)) + } + if v := s.ifPrimaryTerm; v != nil { + params.Set("if_primary_term", fmt.Sprintf("%d", *v)) + } + return method, path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndexService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if s.typ == "" { + invalid = append(invalid, "Type") + } + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndexService) Do(ctx context.Context) (*IndexResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + method, path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: method, + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndexResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndexResponse is the result of indexing a document in Elasticsearch. +type IndexResponse struct { + Index string `json:"_index,omitempty"` + Type string `json:"_type,omitempty"` + Id string `json:"_id,omitempty"` + Version int64 `json:"_version,omitempty"` + Result string `json:"result,omitempty"` + Shards *ShardsInfo `json:"_shards,omitempty"` + SeqNo int64 `json:"_seq_no,omitempty"` + PrimaryTerm int64 `json:"_primary_term,omitempty"` + Status int `json:"status,omitempty"` + ForcedRefresh bool `json:"forced_refresh,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_analyze.go b/vendor/github.com/olivere/elastic/v7/indices_analyze.go new file mode 100644 index 0000000000..3f0c282cfa --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_analyze.go @@ -0,0 +1,320 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesAnalyzeService performs the analysis process on a text and returns +// the tokens breakdown of the text. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-analyze.html +// for detail. +type IndicesAnalyzeService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + request *IndicesAnalyzeRequest + format string + preferLocal *bool + bodyJson interface{} + bodyString string +} + +// NewIndicesAnalyzeService creates a new IndicesAnalyzeService. +func NewIndicesAnalyzeService(client *Client) *IndicesAnalyzeService { + return &IndicesAnalyzeService{ + client: client, + request: new(IndicesAnalyzeRequest), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesAnalyzeService) Pretty(pretty bool) *IndicesAnalyzeService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesAnalyzeService) Human(human bool) *IndicesAnalyzeService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesAnalyzeService) ErrorTrace(errorTrace bool) *IndicesAnalyzeService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesAnalyzeService) FilterPath(filterPath ...string) *IndicesAnalyzeService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesAnalyzeService) Header(name string, value string) *IndicesAnalyzeService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesAnalyzeService) Headers(headers http.Header) *IndicesAnalyzeService { + s.headers = headers + return s +} + +// Index is the name of the index to scope the operation. +func (s *IndicesAnalyzeService) Index(index string) *IndicesAnalyzeService { + s.index = index + return s +} + +// Format of the output. +func (s *IndicesAnalyzeService) Format(format string) *IndicesAnalyzeService { + s.format = format + return s +} + +// PreferLocal, when true, specifies that a local shard should be used +// if available. When false, a random shard is used (default: true). +func (s *IndicesAnalyzeService) PreferLocal(preferLocal bool) *IndicesAnalyzeService { + s.preferLocal = &preferLocal + return s +} + +// Request passes the analyze request to use. +func (s *IndicesAnalyzeService) Request(request *IndicesAnalyzeRequest) *IndicesAnalyzeService { + if request == nil { + s.request = new(IndicesAnalyzeRequest) + } else { + s.request = request + } + return s +} + +// Analyzer is the name of the analyzer to use. +func (s *IndicesAnalyzeService) Analyzer(analyzer string) *IndicesAnalyzeService { + s.request.Analyzer = analyzer + return s +} + +// Attributes is a list of token attributes to output; this parameter works +// only with explain=true. +func (s *IndicesAnalyzeService) Attributes(attributes ...string) *IndicesAnalyzeService { + s.request.Attributes = attributes + return s +} + +// CharFilter is a list of character filters to use for the analysis. +func (s *IndicesAnalyzeService) CharFilter(charFilter ...string) *IndicesAnalyzeService { + s.request.CharFilter = charFilter + return s +} + +// Explain, when true, outputs more advanced details (default: false). +func (s *IndicesAnalyzeService) Explain(explain bool) *IndicesAnalyzeService { + s.request.Explain = explain + return s +} + +// Field specifies to use a specific analyzer configured for this field (instead of passing the analyzer name). +func (s *IndicesAnalyzeService) Field(field string) *IndicesAnalyzeService { + s.request.Field = field + return s +} + +// Filter is a list of filters to use for the analysis. +func (s *IndicesAnalyzeService) Filter(filter ...string) *IndicesAnalyzeService { + s.request.Filter = filter + return s +} + +// Text is the text on which the analysis should be performed (when request body is not used). +func (s *IndicesAnalyzeService) Text(text ...string) *IndicesAnalyzeService { + s.request.Text = text + return s +} + +// Tokenizer is the name of the tokenizer to use for the analysis. +func (s *IndicesAnalyzeService) Tokenizer(tokenizer string) *IndicesAnalyzeService { + s.request.Tokenizer = tokenizer + return s +} + +// BodyJson is the text on which the analysis should be performed. +func (s *IndicesAnalyzeService) BodyJson(body interface{}) *IndicesAnalyzeService { + s.bodyJson = body + return s +} + +// BodyString is the text on which the analysis should be performed. +func (s *IndicesAnalyzeService) BodyString(body string) *IndicesAnalyzeService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesAnalyzeService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + + if s.index == "" { + path = "/_analyze" + } else { + path, err = uritemplates.Expand("/{index}/_analyze", map[string]string{ + "index": s.index, + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.format != "" { + params.Set("format", s.format) + } + if s.preferLocal != nil { + params.Set("prefer_local", fmt.Sprintf("%v", *s.preferLocal)) + } + + return path, params, nil +} + +// Do will execute the request with the given context. +func (s *IndicesAnalyzeService) Do(ctx context.Context) (*IndicesAnalyzeResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else if s.bodyString != "" { + body = s.bodyString + } else { + // Request parameters are deprecated in 5.1.1, and we must use a JSON + // structure in the body to pass the parameters. + // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-analyze.html + body = s.request + } + + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + ret := new(IndicesAnalyzeResponse) + if err = s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + + return ret, nil +} + +func (s *IndicesAnalyzeService) Validate() error { + var invalid []string + if s.bodyJson == nil && s.bodyString == "" { + if len(s.request.Text) == 0 { + invalid = append(invalid, "Text") + } + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// IndicesAnalyzeRequest specifies the parameters of the analyze request. +type IndicesAnalyzeRequest struct { + Text []string `json:"text,omitempty"` + Analyzer string `json:"analyzer,omitempty"` + Tokenizer string `json:"tokenizer,omitempty"` + Filter []string `json:"filter,omitempty"` + CharFilter []string `json:"char_filter,omitempty"` + Field string `json:"field,omitempty"` + Explain bool `json:"explain,omitempty"` + Attributes []string `json:"attributes,omitempty"` +} + +type IndicesAnalyzeResponse struct { + Tokens []AnalyzeToken `json:"tokens"` // json part for normal message + Detail IndicesAnalyzeResponseDetail `json:"detail"` // json part for verbose message of explain request +} + +type AnalyzeTokenList struct { + Name string `json:"name"` + Tokens []AnalyzeToken `json:"tokens,omitempty"` +} + +type AnalyzeToken struct { + Token string `json:"token"` + Type string `json:"type"` // e.g. "" + StartOffset int `json:"start_offset"` + EndOffset int `json:"end_offset"` + Bytes string `json:"bytes"` // e.g. "[67 75 79]" + Position int `json:"position"` + PositionLength int `json:"positionLength"` // seems to be wrong in 7.2+ (no snake_case), see https://github.com/elastic/elasticsearch/blob/7.2/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeResponse.java + TermFrequency int `json:"termFrequency"` + Keyword bool `json:"keyword"` +} + +type CharFilteredText struct { + Name string `json:"name"` + FilteredText []string `json:"filtered_text"` +} + +type IndicesAnalyzeResponseDetail struct { + CustomAnalyzer bool `json:"custom_analyzer"` + Analyzer *AnalyzeTokenList `json:"analyzer,omitempty"` + Charfilters []*CharFilteredText `json:"charfilters,omitempty"` + Tokenizer *AnalyzeTokenList `json:"tokenizer,omitempty"` + TokenFilters []*AnalyzeTokenList `json:"tokenfilters,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_close.go b/vendor/github.com/olivere/elastic/v7/indices_close.go new file mode 100644 index 0000000000..36edd60e0b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_close.go @@ -0,0 +1,214 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesCloseService closes an index. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-open-close.html +// for details. +type IndicesCloseService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + timeout string + masterTimeout string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string +} + +// NewIndicesCloseService creates and initializes a new IndicesCloseService. +func NewIndicesCloseService(client *Client) *IndicesCloseService { + return &IndicesCloseService{client: client} +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesCloseService) Pretty(pretty bool) *IndicesCloseService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesCloseService) Human(human bool) *IndicesCloseService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesCloseService) ErrorTrace(errorTrace bool) *IndicesCloseService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesCloseService) FilterPath(filterPath ...string) *IndicesCloseService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesCloseService) Header(name string, value string) *IndicesCloseService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesCloseService) Headers(headers http.Header) *IndicesCloseService { + s.headers = headers + return s +} + +// Index is the name of the index to close. +func (s *IndicesCloseService) Index(index string) *IndicesCloseService { + s.index = index + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesCloseService) Timeout(timeout string) *IndicesCloseService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesCloseService) MasterTimeout(masterTimeout string) *IndicesCloseService { + s.masterTimeout = masterTimeout + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesCloseService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesCloseService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified). +func (s *IndicesCloseService) AllowNoIndices(allowNoIndices bool) *IndicesCloseService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *IndicesCloseService) ExpandWildcards(expandWildcards string) *IndicesCloseService { + s.expandWildcards = expandWildcards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesCloseService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/_close", map[string]string{ + "index": s.index, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesCloseService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesCloseService) Do(ctx context.Context) (*IndicesCloseResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesCloseResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesCloseResponse is the response of IndicesCloseService.Do. +type IndicesCloseResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_create.go b/vendor/github.com/olivere/elastic/v7/indices_create.go new file mode 100644 index 0000000000..4bcd77bedf --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_create.go @@ -0,0 +1,189 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesCreateService creates a new index. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-create-index.html +// for details. +type IndicesCreateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + timeout string + masterTimeout string + bodyJson interface{} + bodyString string +} + +// NewIndicesCreateService returns a new IndicesCreateService. +func NewIndicesCreateService(client *Client) *IndicesCreateService { + return &IndicesCreateService{client: client} +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesCreateService) Pretty(pretty bool) *IndicesCreateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesCreateService) Human(human bool) *IndicesCreateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesCreateService) ErrorTrace(errorTrace bool) *IndicesCreateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesCreateService) FilterPath(filterPath ...string) *IndicesCreateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesCreateService) Header(name string, value string) *IndicesCreateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesCreateService) Headers(headers http.Header) *IndicesCreateService { + s.headers = headers + return s +} + +// Index is the name of the index to create. +func (s *IndicesCreateService) Index(index string) *IndicesCreateService { + s.index = index + return s +} + +// Timeout the explicit operation timeout, e.g. "5s". +func (s *IndicesCreateService) Timeout(timeout string) *IndicesCreateService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesCreateService) MasterTimeout(masterTimeout string) *IndicesCreateService { + s.masterTimeout = masterTimeout + return s +} + +// Body specifies the configuration of the index as a string. +// It is an alias for BodyString. +func (s *IndicesCreateService) Body(body string) *IndicesCreateService { + s.bodyString = body + return s +} + +// BodyString specifies the configuration of the index as a string. +func (s *IndicesCreateService) BodyString(body string) *IndicesCreateService { + s.bodyString = body + return s +} + +// BodyJson specifies the configuration of the index. The interface{} will +// be serializes as a JSON document, so use a map[string]interface{}. +func (s *IndicesCreateService) BodyJson(body interface{}) *IndicesCreateService { + s.bodyJson = body + return s +} + +// Do executes the operation. +func (s *IndicesCreateService) Do(ctx context.Context) (*IndicesCreateResult, error) { + if s.index == "" { + return nil, errors.New("missing index name") + } + + // Build url + path, err := uritemplates.Expand("/{index}", map[string]string{ + "index": s.index, + }) + if err != nil { + return nil, err + } + + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + ret := new(IndicesCreateResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a create index request. + +// IndicesCreateResult is the outcome of creating a new index. +type IndicesCreateResult struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_delete.go b/vendor/github.com/olivere/elastic/v7/indices_delete.go new file mode 100644 index 0000000000..cdf27a5606 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_delete.go @@ -0,0 +1,184 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesDeleteService allows to delete existing indices. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-delete-index.html +// for details. +type IndicesDeleteService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + timeout string + masterTimeout string +} + +// NewIndicesDeleteService creates and initializes a new IndicesDeleteService. +func NewIndicesDeleteService(client *Client) *IndicesDeleteService { + return &IndicesDeleteService{ + client: client, + index: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesDeleteService) Pretty(pretty bool) *IndicesDeleteService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesDeleteService) Human(human bool) *IndicesDeleteService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesDeleteService) ErrorTrace(errorTrace bool) *IndicesDeleteService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesDeleteService) FilterPath(filterPath ...string) *IndicesDeleteService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesDeleteService) Header(name string, value string) *IndicesDeleteService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesDeleteService) Headers(headers http.Header) *IndicesDeleteService { + s.headers = headers + return s +} + +// Index adds the list of indices to delete. +// Use `_all` or `*` string to delete all indices. +func (s *IndicesDeleteService) Index(index []string) *IndicesDeleteService { + s.index = index + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesDeleteService) Timeout(timeout string) *IndicesDeleteService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesDeleteService) MasterTimeout(masterTimeout string) *IndicesDeleteService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesDeleteService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}", map[string]string{ + "index": strings.Join(s.index, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesDeleteService) Validate() error { + var invalid []string + if len(s.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesDeleteService) Do(ctx context.Context) (*IndicesDeleteResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesDeleteResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a delete index request. + +// IndicesDeleteResponse is the response of IndicesDeleteService.Do. +type IndicesDeleteResponse struct { + Acknowledged bool `json:"acknowledged"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_delete_template.go b/vendor/github.com/olivere/elastic/v7/indices_delete_template.go new file mode 100644 index 0000000000..e1ed3e68aa --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_delete_template.go @@ -0,0 +1,180 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesDeleteTemplateService deletes index templates. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-templates.html. +type IndicesDeleteTemplateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string + timeout string + masterTimeout string +} + +// NewIndicesDeleteTemplateService creates a new IndicesDeleteTemplateService. +func NewIndicesDeleteTemplateService(client *Client) *IndicesDeleteTemplateService { + return &IndicesDeleteTemplateService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesDeleteTemplateService) Pretty(pretty bool) *IndicesDeleteTemplateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesDeleteTemplateService) Human(human bool) *IndicesDeleteTemplateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesDeleteTemplateService) ErrorTrace(errorTrace bool) *IndicesDeleteTemplateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesDeleteTemplateService) FilterPath(filterPath ...string) *IndicesDeleteTemplateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesDeleteTemplateService) Header(name string, value string) *IndicesDeleteTemplateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesDeleteTemplateService) Headers(headers http.Header) *IndicesDeleteTemplateService { + s.headers = headers + return s +} + +// Name is the name of the template. +func (s *IndicesDeleteTemplateService) Name(name string) *IndicesDeleteTemplateService { + s.name = name + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesDeleteTemplateService) Timeout(timeout string) *IndicesDeleteTemplateService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesDeleteTemplateService) MasterTimeout(masterTimeout string) *IndicesDeleteTemplateService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesDeleteTemplateService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_template/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesDeleteTemplateService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesDeleteTemplateService) Do(ctx context.Context) (*IndicesDeleteTemplateResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesDeleteTemplateResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesDeleteTemplateResponse is the response of IndicesDeleteTemplateService.Do. +type IndicesDeleteTemplateResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_exists.go b/vendor/github.com/olivere/elastic/v7/indices_exists.go new file mode 100644 index 0000000000..a813f561ac --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_exists.go @@ -0,0 +1,204 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesExistsService checks if an index or indices exist or not. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-exists.html +// for details. +type IndicesExistsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + local *bool +} + +// NewIndicesExistsService creates and initializes a new IndicesExistsService. +func NewIndicesExistsService(client *Client) *IndicesExistsService { + return &IndicesExistsService{ + client: client, + index: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesExistsService) Pretty(pretty bool) *IndicesExistsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesExistsService) Human(human bool) *IndicesExistsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesExistsService) ErrorTrace(errorTrace bool) *IndicesExistsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesExistsService) FilterPath(filterPath ...string) *IndicesExistsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesExistsService) Header(name string, value string) *IndicesExistsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesExistsService) Headers(headers http.Header) *IndicesExistsService { + s.headers = headers + return s +} + +// Index is a list of one or more indices to check. +func (s *IndicesExistsService) Index(index []string) *IndicesExistsService { + s.index = index + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or +// when no indices have been specified). +func (s *IndicesExistsService) AllowNoIndices(allowNoIndices bool) *IndicesExistsService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *IndicesExistsService) ExpandWildcards(expandWildcards string) *IndicesExistsService { + s.expandWildcards = expandWildcards + return s +} + +// Local, when set, returns local information and does not retrieve the state +// from master node (default: false). +func (s *IndicesExistsService) Local(local bool) *IndicesExistsService { + s.local = &local + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesExistsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesExistsService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesExistsService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}", map[string]string{ + "index": strings.Join(s.index, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesExistsService) Validate() error { + var invalid []string + if len(s.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesExistsService) Do(ctx context.Context) (bool, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return false, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return false, err + } + + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "HEAD", + Path: path, + Params: params, + IgnoreErrors: []int{404}, + Headers: s.headers, + }) + if err != nil { + return false, err + } + + // Return operation response + switch res.StatusCode { + case http.StatusOK: + return true, nil + case http.StatusNotFound: + return false, nil + default: + return false, fmt.Errorf("elastic: got HTTP code %d when it should have been either 200 or 404", res.StatusCode) + } +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_exists_template.go b/vendor/github.com/olivere/elastic/v7/indices_exists_template.go new file mode 100644 index 0000000000..c5c9bbdc5b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_exists_template.go @@ -0,0 +1,169 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesExistsTemplateService checks if a given template exists. +// See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-templates.html#indices-templates-exists +// for documentation. +type IndicesExistsTemplateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string + local *bool +} + +// NewIndicesExistsTemplateService creates a new IndicesExistsTemplateService. +func NewIndicesExistsTemplateService(client *Client) *IndicesExistsTemplateService { + return &IndicesExistsTemplateService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesExistsTemplateService) Pretty(pretty bool) *IndicesExistsTemplateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesExistsTemplateService) Human(human bool) *IndicesExistsTemplateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesExistsTemplateService) ErrorTrace(errorTrace bool) *IndicesExistsTemplateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesExistsTemplateService) FilterPath(filterPath ...string) *IndicesExistsTemplateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesExistsTemplateService) Header(name string, value string) *IndicesExistsTemplateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesExistsTemplateService) Headers(headers http.Header) *IndicesExistsTemplateService { + s.headers = headers + return s +} + +// Name is the name of the template. +func (s *IndicesExistsTemplateService) Name(name string) *IndicesExistsTemplateService { + s.name = name + return s +} + +// Local indicates whether to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *IndicesExistsTemplateService) Local(local bool) *IndicesExistsTemplateService { + s.local = &local + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesExistsTemplateService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_template/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesExistsTemplateService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesExistsTemplateService) Do(ctx context.Context) (bool, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return false, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return false, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "HEAD", + Path: path, + Params: params, + IgnoreErrors: []int{404}, + Headers: s.headers, + }) + if err != nil { + return false, err + } + + // Return operation response + switch res.StatusCode { + case http.StatusOK: + return true, nil + case http.StatusNotFound: + return false, nil + default: + return false, fmt.Errorf("elastic: got HTTP code %d when it should have been either 200 or 404", res.StatusCode) + } +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_flush.go b/vendor/github.com/olivere/elastic/v7/indices_flush.go new file mode 100644 index 0000000000..5d4c4e1c61 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_flush.go @@ -0,0 +1,224 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// Flush allows to flush one or more indices. The flush process of an index +// basically frees memory from the index by flushing data to the index +// storage and clearing the internal transaction log. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-flush.html +// for details. +type IndicesFlushService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + force *bool + waitIfOngoing *bool + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string +} + +// NewIndicesFlushService creates a new IndicesFlushService. +func NewIndicesFlushService(client *Client) *IndicesFlushService { + return &IndicesFlushService{ + client: client, + index: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesFlushService) Pretty(pretty bool) *IndicesFlushService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesFlushService) Human(human bool) *IndicesFlushService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesFlushService) ErrorTrace(errorTrace bool) *IndicesFlushService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesFlushService) FilterPath(filterPath ...string) *IndicesFlushService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesFlushService) Header(name string, value string) *IndicesFlushService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesFlushService) Headers(headers http.Header) *IndicesFlushService { + s.headers = headers + return s +} + +// Index is a list of index names; use `_all` or empty string for all indices. +func (s *IndicesFlushService) Index(indices ...string) *IndicesFlushService { + s.index = append(s.index, indices...) + return s +} + +// Force indicates whether a flush should be forced even if it is not +// necessarily needed ie. if no changes will be committed to the index. +// This is useful if transaction log IDs should be incremented even if +// no uncommitted changes are present. (This setting can be considered as internal). +func (s *IndicesFlushService) Force(force bool) *IndicesFlushService { + s.force = &force + return s +} + +// WaitIfOngoing, if set to true, indicates that the flush operation will +// block until the flush can be executed if another flush operation is +// already executing. The default is false and will cause an exception +// to be thrown on the shard level if another flush operation is already running.. +func (s *IndicesFlushService) WaitIfOngoing(waitIfOngoing bool) *IndicesFlushService { + s.waitIfOngoing = &waitIfOngoing + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesFlushService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesFlushService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or when +// no indices have been specified). +func (s *IndicesFlushService) AllowNoIndices(allowNoIndices bool) *IndicesFlushService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards specifies whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesFlushService) ExpandWildcards(expandWildcards string) *IndicesFlushService { + s.expandWildcards = expandWildcards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesFlushService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_flush", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_flush" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.force != nil { + params.Set("force", fmt.Sprintf("%v", *s.force)) + } + if s.waitIfOngoing != nil { + params.Set("wait_if_ongoing", fmt.Sprintf("%v", *s.waitIfOngoing)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesFlushService) Validate() error { + return nil +} + +// Do executes the service. +func (s *IndicesFlushService) Do(ctx context.Context) (*IndicesFlushResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesFlushResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a flush request. + +type IndicesFlushResponse struct { + Shards *ShardsInfo `json:"_shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_flush_synced.go b/vendor/github.com/olivere/elastic/v7/indices_flush_synced.go new file mode 100644 index 0000000000..140ae62e2b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_flush_synced.go @@ -0,0 +1,281 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesSyncedFlushService performs a normal flush, then adds a generated +// unique marked (sync_id) to all shards. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-synced-flush.html +// for details. +type IndicesSyncedFlushService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string +} + +// NewIndicesSyncedFlushService creates a new IndicesSyncedFlushService. +func NewIndicesSyncedFlushService(client *Client) *IndicesSyncedFlushService { + return &IndicesSyncedFlushService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesSyncedFlushService) Pretty(pretty bool) *IndicesSyncedFlushService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesSyncedFlushService) Human(human bool) *IndicesSyncedFlushService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesSyncedFlushService) ErrorTrace(errorTrace bool) *IndicesSyncedFlushService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesSyncedFlushService) FilterPath(filterPath ...string) *IndicesSyncedFlushService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesSyncedFlushService) Header(name string, value string) *IndicesSyncedFlushService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesSyncedFlushService) Headers(headers http.Header) *IndicesSyncedFlushService { + s.headers = headers + return s +} + +// Index is a list of index names; use `_all` or empty string for all indices. +func (s *IndicesSyncedFlushService) Index(indices ...string) *IndicesSyncedFlushService { + s.index = append(s.index, indices...) + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesSyncedFlushService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesSyncedFlushService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or when +// no indices have been specified). +func (s *IndicesSyncedFlushService) AllowNoIndices(allowNoIndices bool) *IndicesSyncedFlushService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards specifies whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesSyncedFlushService) ExpandWildcards(expandWildcards string) *IndicesSyncedFlushService { + s.expandWildcards = expandWildcards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesSyncedFlushService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_flush/synced", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_flush/synced" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesSyncedFlushService) Validate() error { + return nil +} + +// Do executes the service. +func (s *IndicesSyncedFlushService) Do(ctx context.Context) (*IndicesSyncedFlushResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesSyncedFlushResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a flush request. + +// IndicesSyncedFlushResponse is the outcome of a synched flush call. +type IndicesSyncedFlushResponse struct { + Shards *ShardsInfo `json:"_shards"` + Index map[string]*IndicesShardsSyncedFlushResult `json:"-"` + + // TODO Add information about the indices here from the root level + // It looks like this: + // { + // "_shards" : { + // "total" : 4, + // "successful" : 4, + // "failed" : 0 + // }, + // "elastic-test" : { + // "total" : 1, + // "successful" : 1, + // "failed" : 0 + // }, + // "elastic-test2" : { + // "total" : 1, + // "successful" : 1, + // "failed" : 0 + // }, + // "elastic-orders" : { + // "total" : 1, + // "successful" : 1, + // "failed" : 0 + // }, + // "elastic-nosource-test" : { + // "total" : 1, + // "successful" : 1, + // "failed" : 0 + // } + // } +} + +// IndicesShardsSyncedFlushResult represents synced flush information about +// a specific index. +type IndicesShardsSyncedFlushResult struct { + Total int `json:"total"` + Successful int `json:"successful"` + Failed int `json:"failed"` + Failures []IndicesShardsSyncedFlushResultFailure `json:"failures,omitempty"` +} + +// IndicesShardsSyncedFlushResultFailure represents a failure of a synced +// flush operation. +type IndicesShardsSyncedFlushResultFailure struct { + Shard int `json:"shard"` + Reason string `json:"reason"` + Routing struct { + State string `json:"state"` + Primary bool `json:"primary"` + Node string `json:"node"` + RelocatingNode *string `json:"relocating_node"` + Shard int `json:"shard"` + Index string `json:"index"` + ExpectedShardSizeInBytes int64 `json:"expected_shard_size_in_bytes,omitempty"` + // recoverySource + // allocationId + // unassignedInfo + } `json:"routing"` +} + +// UnmarshalJSON parses the output from Synced Flush API. +func (resp *IndicesSyncedFlushResponse) UnmarshalJSON(data []byte) error { + m := make(map[string]json.RawMessage) + err := json.Unmarshal(data, &m) + if err != nil { + return err + } + resp.Index = make(map[string]*IndicesShardsSyncedFlushResult) + for k, v := range m { + if k == "_shards" { + if err := json.Unmarshal(v, &resp.Shards); err != nil { + return err + } + } else { + ix := new(IndicesShardsSyncedFlushResult) + if err := json.Unmarshal(v, &ix); err != nil { + return err + } + resp.Index[k] = ix + } + } + return nil +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_forcemerge.go b/vendor/github.com/olivere/elastic/v7/indices_forcemerge.go new file mode 100644 index 0000000000..809839057b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_forcemerge.go @@ -0,0 +1,235 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesForcemergeService allows to force merging of one or more indices. +// The merge relates to the number of segments a Lucene index holds +// within each shard. The force merge operation allows to reduce the number +// of segments by merging them. +// +// See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-forcemerge.html +// for more information. +type IndicesForcemergeService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + allowNoIndices *bool + expandWildcards string + flush *bool + ignoreUnavailable *bool + maxNumSegments interface{} + onlyExpungeDeletes *bool +} + +// NewIndicesForcemergeService creates a new IndicesForcemergeService. +func NewIndicesForcemergeService(client *Client) *IndicesForcemergeService { + return &IndicesForcemergeService{ + client: client, + index: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesForcemergeService) Pretty(pretty bool) *IndicesForcemergeService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesForcemergeService) Human(human bool) *IndicesForcemergeService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesForcemergeService) ErrorTrace(errorTrace bool) *IndicesForcemergeService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesForcemergeService) FilterPath(filterPath ...string) *IndicesForcemergeService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesForcemergeService) Header(name string, value string) *IndicesForcemergeService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesForcemergeService) Headers(headers http.Header) *IndicesForcemergeService { + s.headers = headers + return s +} + +// Index is a list of index names; use `_all` or empty string to perform +// the operation on all indices. +func (s *IndicesForcemergeService) Index(index ...string) *IndicesForcemergeService { + if s.index == nil { + s.index = make([]string, 0) + } + s.index = append(s.index, index...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// (This includes `_all` string or when no indices have been specified). +func (s *IndicesForcemergeService) AllowNoIndices(allowNoIndices bool) *IndicesForcemergeService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesForcemergeService) ExpandWildcards(expandWildcards string) *IndicesForcemergeService { + s.expandWildcards = expandWildcards + return s +} + +// Flush specifies whether the index should be flushed after performing +// the operation (default: true). +func (s *IndicesForcemergeService) Flush(flush bool) *IndicesForcemergeService { + s.flush = &flush + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should +// be ignored when unavailable (missing or closed). +func (s *IndicesForcemergeService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesForcemergeService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// MaxNumSegments specifies the number of segments the index should be +// merged into (default: dynamic). +func (s *IndicesForcemergeService) MaxNumSegments(maxNumSegments interface{}) *IndicesForcemergeService { + s.maxNumSegments = maxNumSegments + return s +} + +// OnlyExpungeDeletes specifies whether the operation should only expunge +// deleted documents. +func (s *IndicesForcemergeService) OnlyExpungeDeletes(onlyExpungeDeletes bool) *IndicesForcemergeService { + s.onlyExpungeDeletes = &onlyExpungeDeletes + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesForcemergeService) buildURL() (string, url.Values, error) { + var err error + var path string + + // Build URL + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_forcemerge", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_forcemerge" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.flush != nil { + params.Set("flush", fmt.Sprintf("%v", *s.flush)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.maxNumSegments != nil { + params.Set("max_num_segments", fmt.Sprintf("%v", s.maxNumSegments)) + } + if s.onlyExpungeDeletes != nil { + params.Set("only_expunge_deletes", fmt.Sprintf("%v", *s.onlyExpungeDeletes)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesForcemergeService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IndicesForcemergeService) Do(ctx context.Context) (*IndicesForcemergeResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesForcemergeResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesForcemergeResponse is the response of IndicesForcemergeService.Do. +type IndicesForcemergeResponse struct { + Shards *ShardsInfo `json:"_shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_freeze.go b/vendor/github.com/olivere/elastic/v7/indices_freeze.go new file mode 100644 index 0000000000..04c2a3e158 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_freeze.go @@ -0,0 +1,229 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesFreezeService freezes an index. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/freeze-index-api.html +// and https://www.elastic.co/blog/creating-frozen-indices-with-the-elasticsearch-freeze-index-api +// for details. +type IndicesFreezeService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + timeout string + masterTimeout string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + waitForActiveShards string +} + +// NewIndicesFreezeService creates a new IndicesFreezeService. +func NewIndicesFreezeService(client *Client) *IndicesFreezeService { + return &IndicesFreezeService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesFreezeService) Pretty(pretty bool) *IndicesFreezeService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesFreezeService) Human(human bool) *IndicesFreezeService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesFreezeService) ErrorTrace(errorTrace bool) *IndicesFreezeService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesFreezeService) FilterPath(filterPath ...string) *IndicesFreezeService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesFreezeService) Header(name string, value string) *IndicesFreezeService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesFreezeService) Headers(headers http.Header) *IndicesFreezeService { + s.headers = headers + return s +} + +// Index is the name of the index to freeze. +func (s *IndicesFreezeService) Index(index string) *IndicesFreezeService { + s.index = index + return s +} + +// Timeout allows to specify an explicit timeout. +func (s *IndicesFreezeService) Timeout(timeout string) *IndicesFreezeService { + s.timeout = timeout + return s +} + +// MasterTimeout allows to specify a timeout for connection to master. +func (s *IndicesFreezeService) MasterTimeout(masterTimeout string) *IndicesFreezeService { + s.masterTimeout = masterTimeout + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesFreezeService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesFreezeService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or when +// no indices have been specified). +func (s *IndicesFreezeService) AllowNoIndices(allowNoIndices bool) *IndicesFreezeService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards specifies whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesFreezeService) ExpandWildcards(expandWildcards string) *IndicesFreezeService { + s.expandWildcards = expandWildcards + return s +} + +// WaitForActiveShards sets the number of active shards to wait for +// before the operation returns. +func (s *IndicesFreezeService) WaitForActiveShards(numShards string) *IndicesFreezeService { + s.waitForActiveShards = numShards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesFreezeService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/_freeze", map[string]string{ + "index": s.index, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesFreezeService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the service. +func (s *IndicesFreezeService) Do(ctx context.Context) (*IndicesFreezeResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesFreezeResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesFreezeResponse is the outcome of freezing an index. +type IndicesFreezeResponse struct { + Shards *ShardsInfo `json:"_shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_get.go b/vendor/github.com/olivere/elastic/v7/indices_get.go new file mode 100644 index 0000000000..6416c04a9f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_get.go @@ -0,0 +1,237 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesGetService retrieves information about one or more indices. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-get-index.html +// for more details. +type IndicesGetService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + feature []string + local *bool + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + flatSettings *bool +} + +// NewIndicesGetService creates a new IndicesGetService. +func NewIndicesGetService(client *Client) *IndicesGetService { + return &IndicesGetService{ + client: client, + index: make([]string, 0), + feature: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesGetService) Pretty(pretty bool) *IndicesGetService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesGetService) Human(human bool) *IndicesGetService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesGetService) ErrorTrace(errorTrace bool) *IndicesGetService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesGetService) FilterPath(filterPath ...string) *IndicesGetService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesGetService) Header(name string, value string) *IndicesGetService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesGetService) Headers(headers http.Header) *IndicesGetService { + s.headers = headers + return s +} + +// Index is a list of index names. +func (s *IndicesGetService) Index(indices ...string) *IndicesGetService { + s.index = append(s.index, indices...) + return s +} + +// Feature is a list of features. +func (s *IndicesGetService) Feature(features ...string) *IndicesGetService { + s.feature = append(s.feature, features...) + return s +} + +// Local indicates whether to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *IndicesGetService) Local(local bool) *IndicesGetService { + s.local = &local + return s +} + +// IgnoreUnavailable indicates whether to ignore unavailable indexes (default: false). +func (s *IndicesGetService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard expression +// resolves to no concrete indices (default: false). +func (s *IndicesGetService) AllowNoIndices(allowNoIndices bool) *IndicesGetService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether wildcard expressions should get +// expanded to open or closed indices (default: open). +func (s *IndicesGetService) ExpandWildcards(expandWildcards string) *IndicesGetService { + s.expandWildcards = expandWildcards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesGetService) buildURL() (string, url.Values, error) { + var err error + var path string + var index []string + + if len(s.index) > 0 { + index = s.index + } else { + index = []string{"_all"} + } + + if len(s.feature) > 0 { + // Build URL + path, err = uritemplates.Expand("/{index}/{feature}", map[string]string{ + "index": strings.Join(index, ","), + "feature": strings.Join(s.feature, ","), + }) + } else { + // Build URL + path, err = uritemplates.Expand("/{index}", map[string]string{ + "index": strings.Join(index, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesGetService) Validate() error { + var invalid []string + if len(s.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesGetService) Do(ctx context.Context) (map[string]*IndicesGetResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret map[string]*IndicesGetResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesGetResponse is part of the response of IndicesGetService.Do. +type IndicesGetResponse struct { + Aliases map[string]interface{} `json:"aliases"` + Mappings map[string]interface{} `json:"mappings"` + Settings map[string]interface{} `json:"settings"` + Warmers map[string]interface{} `json:"warmers"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_get_aliases.go b/vendor/github.com/olivere/elastic/v7/indices_get_aliases.go new file mode 100644 index 0000000000..596dc606ce --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_get_aliases.go @@ -0,0 +1,230 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// AliasesService returns the aliases associated with one or more indices, or the +// indices associated with one or more aliases, or a combination of those filters. +// See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-aliases.html. +type AliasesService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + alias []string +} + +// NewAliasesService instantiates a new AliasesService. +func NewAliasesService(client *Client) *AliasesService { + builder := &AliasesService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *AliasesService) Pretty(pretty bool) *AliasesService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *AliasesService) Human(human bool) *AliasesService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *AliasesService) ErrorTrace(errorTrace bool) *AliasesService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *AliasesService) FilterPath(filterPath ...string) *AliasesService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *AliasesService) Header(name string, value string) *AliasesService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *AliasesService) Headers(headers http.Header) *AliasesService { + s.headers = headers + return s +} + +// Index adds one or more indices. +func (s *AliasesService) Index(index ...string) *AliasesService { + s.index = append(s.index, index...) + return s +} + +// Alias adds one or more aliases. +func (s *AliasesService) Alias(alias ...string) *AliasesService { + s.alias = append(s.alias, alias...) + return s +} + +// buildURL builds the URL for the operation. +func (s *AliasesService) buildURL() (string, url.Values, error) { + var err error + var path string + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_alias/{alias}", map[string]string{ + "index": strings.Join(s.index, ","), + "alias": strings.Join(s.alias, ","), + }) + } else { + path, err = uritemplates.Expand("/_alias/{alias}", map[string]string{ + "alias": strings.Join(s.alias, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + path = strings.TrimSuffix(path, "/") + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +func (s *AliasesService) Do(ctx context.Context) (*AliasesResult, error) { + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // { + // "indexName" : { + // "aliases" : { + // "alias1" : { }, + // "alias2" : { } + // } + // }, + // "indexName2" : { + // ... + // }, + // } + indexMap := make(map[string]struct { + Aliases map[string]struct { + IsWriteIndex bool `json:"is_write_index"` + } `json:"aliases"` + }) + if err := s.client.decoder.Decode(res.Body, &indexMap); err != nil { + return nil, err + } + + // Each (indexName, _) + ret := &AliasesResult{ + Indices: make(map[string]indexResult), + } + for indexName, indexData := range indexMap { + if indexData.Aliases == nil { + continue + } + + indexOut, found := ret.Indices[indexName] + if !found { + indexOut = indexResult{Aliases: make([]aliasResult, 0)} + } + + // { "aliases" : { ... } } + for aliasName, aliasData := range indexData.Aliases { + aliasRes := aliasResult{AliasName: aliasName, IsWriteIndex: aliasData.IsWriteIndex} + indexOut.Aliases = append(indexOut.Aliases, aliasRes) + } + + ret.Indices[indexName] = indexOut + } + + return ret, nil +} + +// -- Result of an alias request. + +// AliasesResult is the outcome of calling AliasesService.Do. +type AliasesResult struct { + Indices map[string]indexResult +} + +type indexResult struct { + Aliases []aliasResult +} + +type aliasResult struct { + AliasName string + IsWriteIndex bool +} + +// IndicesByAlias returns all indices given a specific alias name. +func (ar AliasesResult) IndicesByAlias(aliasName string) []string { + var indices []string + for indexName, indexInfo := range ar.Indices { + for _, aliasInfo := range indexInfo.Aliases { + if aliasInfo.AliasName == aliasName { + indices = append(indices, indexName) + } + } + } + return indices +} + +// HasAlias returns true if the index has a specific alias. +func (ir indexResult) HasAlias(aliasName string) bool { + for _, alias := range ir.Aliases { + if alias.AliasName == aliasName { + return true + } + } + return false +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_get_field_mapping.go b/vendor/github.com/olivere/elastic/v7/indices_get_field_mapping.go new file mode 100644 index 0000000000..5875af5860 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_get_field_mapping.go @@ -0,0 +1,238 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesGetFieldMappingService retrieves the mapping definitions for the fields in an index +// or index/type. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-get-field-mapping.html +// for details. +type IndicesGetFieldMappingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + typ []string + field []string + local *bool + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string +} + +// NewGetFieldMappingService is an alias for NewIndicesGetFieldMappingService. +// Use NewIndicesGetFieldMappingService. +func NewGetFieldMappingService(client *Client) *IndicesGetFieldMappingService { + return NewIndicesGetFieldMappingService(client) +} + +// NewIndicesGetFieldMappingService creates a new IndicesGetFieldMappingService. +func NewIndicesGetFieldMappingService(client *Client) *IndicesGetFieldMappingService { + return &IndicesGetFieldMappingService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesGetFieldMappingService) Pretty(pretty bool) *IndicesGetFieldMappingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesGetFieldMappingService) Human(human bool) *IndicesGetFieldMappingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesGetFieldMappingService) ErrorTrace(errorTrace bool) *IndicesGetFieldMappingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesGetFieldMappingService) FilterPath(filterPath ...string) *IndicesGetFieldMappingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesGetFieldMappingService) Header(name string, value string) *IndicesGetFieldMappingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesGetFieldMappingService) Headers(headers http.Header) *IndicesGetFieldMappingService { + s.headers = headers + return s +} + +// Index is a list of index names. +func (s *IndicesGetFieldMappingService) Index(indices ...string) *IndicesGetFieldMappingService { + s.index = append(s.index, indices...) + return s +} + +// Type is a list of document types. +func (s *IndicesGetFieldMappingService) Type(types ...string) *IndicesGetFieldMappingService { + s.typ = append(s.typ, types...) + return s +} + +// Field is a list of fields. +func (s *IndicesGetFieldMappingService) Field(fields ...string) *IndicesGetFieldMappingService { + s.field = append(s.field, fields...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// This includes `_all` string or when no indices have been specified. +func (s *IndicesGetFieldMappingService) AllowNoIndices(allowNoIndices bool) *IndicesGetFieldMappingService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesGetFieldMappingService) ExpandWildcards(expandWildcards string) *IndicesGetFieldMappingService { + s.expandWildcards = expandWildcards + return s +} + +// Local indicates whether to return local information, do not retrieve +// the state from master node (default: false). +func (s *IndicesGetFieldMappingService) Local(local bool) *IndicesGetFieldMappingService { + s.local = &local + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesGetFieldMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetFieldMappingService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesGetFieldMappingService) buildURL() (string, url.Values, error) { + var index, typ, field []string + + if len(s.index) > 0 { + index = s.index + } else { + index = []string{"_all"} + } + + if len(s.typ) > 0 { + typ = s.typ + } else { + typ = []string{"_all"} + } + + if len(s.field) > 0 { + field = s.field + } else { + field = []string{"*"} + } + + // Build URL + path, err := uritemplates.Expand("/{index}/_mapping/{type}/field/{field}", map[string]string{ + "index": strings.Join(index, ","), + "type": strings.Join(typ, ","), + "field": strings.Join(field, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesGetFieldMappingService) Validate() error { + return nil +} + +// Do executes the operation. It returns mapping definitions for an index +// or index/type. +func (s *IndicesGetFieldMappingService) Do(ctx context.Context) (map[string]interface{}, error) { + var ret map[string]interface{} + + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_get_mapping.go b/vendor/github.com/olivere/elastic/v7/indices_get_mapping.go new file mode 100644 index 0000000000..352016342d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_get_mapping.go @@ -0,0 +1,225 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesGetMappingService retrieves the mapping definitions for an index or +// index/type. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-get-mapping.html +// for details. +type IndicesGetMappingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + typ []string + local *bool + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string +} + +// NewGetMappingService is an alias for NewIndicesGetMappingService. +// Use NewIndicesGetMappingService. +func NewGetMappingService(client *Client) *IndicesGetMappingService { + return NewIndicesGetMappingService(client) +} + +// NewIndicesGetMappingService creates a new IndicesGetMappingService. +func NewIndicesGetMappingService(client *Client) *IndicesGetMappingService { + return &IndicesGetMappingService{ + client: client, + index: make([]string, 0), + typ: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesGetMappingService) Pretty(pretty bool) *IndicesGetMappingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesGetMappingService) Human(human bool) *IndicesGetMappingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesGetMappingService) ErrorTrace(errorTrace bool) *IndicesGetMappingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesGetMappingService) FilterPath(filterPath ...string) *IndicesGetMappingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesGetMappingService) Header(name string, value string) *IndicesGetMappingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesGetMappingService) Headers(headers http.Header) *IndicesGetMappingService { + s.headers = headers + return s +} + +// Index is a list of index names. +func (s *IndicesGetMappingService) Index(indices ...string) *IndicesGetMappingService { + s.index = append(s.index, indices...) + return s +} + +// Type is a list of document types. +func (s *IndicesGetMappingService) Type(types ...string) *IndicesGetMappingService { + s.typ = append(s.typ, types...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// This includes `_all` string or when no indices have been specified. +func (s *IndicesGetMappingService) AllowNoIndices(allowNoIndices bool) *IndicesGetMappingService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesGetMappingService) ExpandWildcards(expandWildcards string) *IndicesGetMappingService { + s.expandWildcards = expandWildcards + return s +} + +// Local indicates whether to return local information, do not retrieve +// the state from master node (default: false). +func (s *IndicesGetMappingService) Local(local bool) *IndicesGetMappingService { + s.local = &local + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesGetMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetMappingService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesGetMappingService) buildURL() (string, url.Values, error) { + var index, typ []string + + if len(s.index) > 0 { + index = s.index + } else { + index = []string{"_all"} + } + + if len(s.typ) > 0 { + typ = s.typ + } else { + typ = []string{"_all"} + } + + // Build URL + path, err := uritemplates.Expand("/{index}/_mapping/{type}", map[string]string{ + "index": strings.Join(index, ","), + "type": strings.Join(typ, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesGetMappingService) Validate() error { + return nil +} + +// Do executes the operation. It returns mapping definitions for an index +// or index/type. +func (s *IndicesGetMappingService) Do(ctx context.Context) (map[string]interface{}, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret map[string]interface{} + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_get_settings.go b/vendor/github.com/olivere/elastic/v7/indices_get_settings.go new file mode 100644 index 0000000000..2f7e4d49c3 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_get_settings.go @@ -0,0 +1,238 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesGetSettingsService allows to retrieve settings of one +// or more indices. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-get-settings.html +// for more details. +type IndicesGetSettingsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + name []string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + flatSettings *bool + local *bool +} + +// NewIndicesGetSettingsService creates a new IndicesGetSettingsService. +func NewIndicesGetSettingsService(client *Client) *IndicesGetSettingsService { + return &IndicesGetSettingsService{ + client: client, + index: make([]string, 0), + name: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesGetSettingsService) Pretty(pretty bool) *IndicesGetSettingsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesGetSettingsService) Human(human bool) *IndicesGetSettingsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesGetSettingsService) ErrorTrace(errorTrace bool) *IndicesGetSettingsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesGetSettingsService) FilterPath(filterPath ...string) *IndicesGetSettingsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesGetSettingsService) Header(name string, value string) *IndicesGetSettingsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesGetSettingsService) Headers(headers http.Header) *IndicesGetSettingsService { + s.headers = headers + return s +} + +// Index is a list of index names; use `_all` or empty string to perform +// the operation on all indices. +func (s *IndicesGetSettingsService) Index(indices ...string) *IndicesGetSettingsService { + s.index = append(s.index, indices...) + return s +} + +// Name are the names of the settings that should be included. +func (s *IndicesGetSettingsService) Name(name ...string) *IndicesGetSettingsService { + s.name = append(s.name, name...) + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should +// be ignored when unavailable (missing or closed). +func (s *IndicesGetSettingsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetSettingsService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// (This includes `_all` string or when no indices have been specified). +func (s *IndicesGetSettingsService) AllowNoIndices(allowNoIndices bool) *IndicesGetSettingsService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression +// to concrete indices that are open, closed or both. +// Options: open, closed, none, all. Default: open,closed. +func (s *IndicesGetSettingsService) ExpandWildcards(expandWildcards string) *IndicesGetSettingsService { + s.expandWildcards = expandWildcards + return s +} + +// FlatSettings indicates whether to return settings in flat format (default: false). +func (s *IndicesGetSettingsService) FlatSettings(flatSettings bool) *IndicesGetSettingsService { + s.flatSettings = &flatSettings + return s +} + +// Local indicates whether to return local information, do not retrieve +// the state from master node (default: false). +func (s *IndicesGetSettingsService) Local(local bool) *IndicesGetSettingsService { + s.local = &local + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesGetSettingsService) buildURL() (string, url.Values, error) { + var err error + var path string + var index []string + + if len(s.index) > 0 { + index = s.index + } else { + index = []string{"_all"} + } + + if len(s.name) > 0 { + // Build URL + path, err = uritemplates.Expand("/{index}/_settings/{name}", map[string]string{ + "index": strings.Join(index, ","), + "name": strings.Join(s.name, ","), + }) + } else { + // Build URL + path, err = uritemplates.Expand("/{index}/_settings", map[string]string{ + "index": strings.Join(index, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesGetSettingsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IndicesGetSettingsService) Do(ctx context.Context) (map[string]*IndicesGetSettingsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret map[string]*IndicesGetSettingsResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesGetSettingsResponse is the response of IndicesGetSettingsService.Do. +type IndicesGetSettingsResponse struct { + Settings map[string]interface{} `json:"settings"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_get_template.go b/vendor/github.com/olivere/elastic/v7/indices_get_template.go new file mode 100644 index 0000000000..aeafe9128e --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_get_template.go @@ -0,0 +1,184 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesGetTemplateService returns an index template. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-templates.html. +type IndicesGetTemplateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name []string + flatSettings *bool + local *bool +} + +// NewIndicesGetTemplateService creates a new IndicesGetTemplateService. +func NewIndicesGetTemplateService(client *Client) *IndicesGetTemplateService { + return &IndicesGetTemplateService{ + client: client, + name: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesGetTemplateService) Pretty(pretty bool) *IndicesGetTemplateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesGetTemplateService) Human(human bool) *IndicesGetTemplateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesGetTemplateService) ErrorTrace(errorTrace bool) *IndicesGetTemplateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesGetTemplateService) FilterPath(filterPath ...string) *IndicesGetTemplateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesGetTemplateService) Header(name string, value string) *IndicesGetTemplateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesGetTemplateService) Headers(headers http.Header) *IndicesGetTemplateService { + s.headers = headers + return s +} + +// Name is the name of the index template. +func (s *IndicesGetTemplateService) Name(name ...string) *IndicesGetTemplateService { + s.name = append(s.name, name...) + return s +} + +// FlatSettings is returns settings in flat format (default: false). +func (s *IndicesGetTemplateService) FlatSettings(flatSettings bool) *IndicesGetTemplateService { + s.flatSettings = &flatSettings + return s +} + +// Local indicates whether to return local information, i.e. do not retrieve +// the state from master node (default: false). +func (s *IndicesGetTemplateService) Local(local bool) *IndicesGetTemplateService { + s.local = &local + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesGetTemplateService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.name) > 0 { + path, err = uritemplates.Expand("/_template/{name}", map[string]string{ + "name": strings.Join(s.name, ","), + }) + } else { + path = "/_template" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesGetTemplateService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IndicesGetTemplateService) Do(ctx context.Context) (map[string]*IndicesGetTemplateResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret map[string]*IndicesGetTemplateResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesGetTemplateResponse is the response of IndicesGetTemplateService.Do. +type IndicesGetTemplateResponse struct { + Order int `json:"order,omitempty"` + Version int `json:"version,omitempty"` + IndexPatterns []string `json:"index_patterns,omitempty"` + Settings map[string]interface{} `json:"settings,omitempty"` + Mappings map[string]interface{} `json:"mappings,omitempty"` + Aliases map[string]interface{} `json:"aliases,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_open.go b/vendor/github.com/olivere/elastic/v7/indices_open.go new file mode 100644 index 0000000000..81ad90f91b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_open.go @@ -0,0 +1,227 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesOpenService opens an index. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-open-close.html +// for details. +type IndicesOpenService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + timeout string + masterTimeout string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + waitForActiveShards string +} + +// NewIndicesOpenService creates and initializes a new IndicesOpenService. +func NewIndicesOpenService(client *Client) *IndicesOpenService { + return &IndicesOpenService{client: client} +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesOpenService) Pretty(pretty bool) *IndicesOpenService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesOpenService) Human(human bool) *IndicesOpenService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesOpenService) ErrorTrace(errorTrace bool) *IndicesOpenService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesOpenService) FilterPath(filterPath ...string) *IndicesOpenService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesOpenService) Header(name string, value string) *IndicesOpenService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesOpenService) Headers(headers http.Header) *IndicesOpenService { + s.headers = headers + return s +} + +// Index is the name of the index to open. +func (s *IndicesOpenService) Index(index string) *IndicesOpenService { + s.index = index + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesOpenService) Timeout(timeout string) *IndicesOpenService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesOpenService) MasterTimeout(masterTimeout string) *IndicesOpenService { + s.masterTimeout = masterTimeout + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should +// be ignored when unavailable (missing or closed). +func (s *IndicesOpenService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesOpenService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// (This includes `_all` string or when no indices have been specified). +func (s *IndicesOpenService) AllowNoIndices(allowNoIndices bool) *IndicesOpenService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesOpenService) ExpandWildcards(expandWildcards string) *IndicesOpenService { + s.expandWildcards = expandWildcards + return s +} + +// WaitForActiveShards specifies the number of shards that must be allocated +// before the Open operation returns. Valid values are "all" or an integer +// between 0 and number_of_replicas+1 (default: 0) +func (s *IndicesOpenService) WaitForActiveShards(waitForActiveShards string) *IndicesOpenService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesOpenService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/_open", map[string]string{ + "index": s.index, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesOpenService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesOpenService) Do(ctx context.Context) (*IndicesOpenResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesOpenResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesOpenResponse is the response of IndicesOpenService.Do. +type IndicesOpenResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_put_alias.go b/vendor/github.com/olivere/elastic/v7/indices_put_alias.go new file mode 100644 index 0000000000..b723eb908a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_put_alias.go @@ -0,0 +1,399 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" +) + +// -- Actions -- + +// AliasAction is an action to apply to an alias, e.g. "add" or "remove". +type AliasAction interface { + Source() (interface{}, error) +} + +// AliasAddAction is an action to add to an alias. +type AliasAddAction struct { + index []string // index name(s) + alias string // alias name + filter Query + routing string + searchRouting string + indexRouting string + isWriteIndex *bool +} + +// NewAliasAddAction returns an action to add an alias. +func NewAliasAddAction(alias string) *AliasAddAction { + return &AliasAddAction{ + alias: alias, + } +} + +// Index associates one or more indices to the alias. +func (a *AliasAddAction) Index(index ...string) *AliasAddAction { + a.index = append(a.index, index...) + return a +} + +func (a *AliasAddAction) removeBlankIndexNames() { + var indices []string + for _, index := range a.index { + if len(index) > 0 { + indices = append(indices, index) + } + } + a.index = indices +} + +// Filter associates a filter to the alias. +func (a *AliasAddAction) Filter(filter Query) *AliasAddAction { + a.filter = filter + return a +} + +// Routing associates a routing value to the alias. +// This basically sets index and search routing to the same value. +func (a *AliasAddAction) Routing(routing string) *AliasAddAction { + a.routing = routing + return a +} + +// IndexRouting associates an index routing value to the alias. +func (a *AliasAddAction) IndexRouting(routing string) *AliasAddAction { + a.indexRouting = routing + return a +} + +// SearchRouting associates a search routing value to the alias. +func (a *AliasAddAction) SearchRouting(routing ...string) *AliasAddAction { + a.searchRouting = strings.Join(routing, ",") + return a +} + +// IsWriteIndex associates an is_write_index flag to the alias. +func (a *AliasAddAction) IsWriteIndex(flag bool) *AliasAddAction { + a.isWriteIndex = &flag + return a +} + +// Validate checks if the operation is valid. +func (a *AliasAddAction) Validate() error { + var invalid []string + if len(a.alias) == 0 { + invalid = append(invalid, "Alias") + } + if len(a.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + if a.isWriteIndex != nil && len(a.index) > 1 { + return fmt.Errorf("more than 1 target index specified in operation with 'is_write_index' flag present") + } + return nil +} + +// Source returns the JSON-serializable data. +func (a *AliasAddAction) Source() (interface{}, error) { + a.removeBlankIndexNames() + if err := a.Validate(); err != nil { + return nil, err + } + src := make(map[string]interface{}) + act := make(map[string]interface{}) + src["add"] = act + act["alias"] = a.alias + switch len(a.index) { + case 1: + act["index"] = a.index[0] + default: + act["indices"] = a.index + } + if a.filter != nil { + f, err := a.filter.Source() + if err != nil { + return nil, err + } + act["filter"] = f + } + if len(a.routing) > 0 { + act["routing"] = a.routing + } + if len(a.indexRouting) > 0 { + act["index_routing"] = a.indexRouting + } + if len(a.searchRouting) > 0 { + act["search_routing"] = a.searchRouting + } + if a.isWriteIndex != nil { + act["is_write_index"] = *a.isWriteIndex + } + return src, nil +} + +// AliasRemoveAction is an action to remove an alias. +type AliasRemoveAction struct { + index []string // index name(s) + alias string // alias name +} + +// NewAliasRemoveAction returns an action to remove an alias. +func NewAliasRemoveAction(alias string) *AliasRemoveAction { + return &AliasRemoveAction{ + alias: alias, + } +} + +// Index associates one or more indices to the alias. +func (a *AliasRemoveAction) Index(index ...string) *AliasRemoveAction { + a.index = append(a.index, index...) + return a +} + +func (a *AliasRemoveAction) removeBlankIndexNames() { + var indices []string + for _, index := range a.index { + if len(index) > 0 { + indices = append(indices, index) + } + } + a.index = indices +} + +// Validate checks if the operation is valid. +func (a *AliasRemoveAction) Validate() error { + var invalid []string + if len(a.alias) == 0 { + invalid = append(invalid, "Alias") + } + if len(a.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Source returns the JSON-serializable data. +func (a *AliasRemoveAction) Source() (interface{}, error) { + a.removeBlankIndexNames() + if err := a.Validate(); err != nil { + return nil, err + } + src := make(map[string]interface{}) + act := make(map[string]interface{}) + src["remove"] = act + act["alias"] = a.alias + switch len(a.index) { + case 1: + act["index"] = a.index[0] + default: + act["indices"] = a.index + } + return src, nil +} + +// AliasRemoveIndexAction is an action to remove an index during an alias +// operation. +type AliasRemoveIndexAction struct { + index string // index name +} + +// NewAliasRemoveIndexAction returns an action to remove an index. +func NewAliasRemoveIndexAction(index string) *AliasRemoveIndexAction { + return &AliasRemoveIndexAction{ + index: index, + } +} + +// Validate checks if the operation is valid. +func (a *AliasRemoveIndexAction) Validate() error { + if a.index == "" { + return fmt.Errorf("missing required field: index") + } + return nil +} + +// Source returns the JSON-serializable data. +func (a *AliasRemoveIndexAction) Source() (interface{}, error) { + if err := a.Validate(); err != nil { + return nil, err + } + src := make(map[string]interface{}) + act := make(map[string]interface{}) + src["remove_index"] = act + act["index"] = a.index + return src, nil +} + +// -- Service -- + +// AliasService enables users to add or remove an alias. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-aliases.html +// for details. +type AliasService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + actions []AliasAction +} + +// NewAliasService implements a service to manage aliases. +func NewAliasService(client *Client) *AliasService { + builder := &AliasService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *AliasService) Pretty(pretty bool) *AliasService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *AliasService) Human(human bool) *AliasService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *AliasService) ErrorTrace(errorTrace bool) *AliasService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *AliasService) FilterPath(filterPath ...string) *AliasService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *AliasService) Header(name string, value string) *AliasService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *AliasService) Headers(headers http.Header) *AliasService { + s.headers = headers + return s +} + +// Add adds an alias to an index. +func (s *AliasService) Add(indexName string, aliasName string) *AliasService { + action := NewAliasAddAction(aliasName).Index(indexName) + s.actions = append(s.actions, action) + return s +} + +// Add adds an alias to an index and associates a filter to the alias. +func (s *AliasService) AddWithFilter(indexName string, aliasName string, filter Query) *AliasService { + action := NewAliasAddAction(aliasName).Index(indexName).Filter(filter) + s.actions = append(s.actions, action) + return s +} + +// Remove removes an alias. +func (s *AliasService) Remove(indexName string, aliasName string) *AliasService { + action := NewAliasRemoveAction(aliasName).Index(indexName) + s.actions = append(s.actions, action) + return s +} + +// Action accepts one or more AliasAction instances which can be +// of type AliasAddAction or AliasRemoveAction. +func (s *AliasService) Action(action ...AliasAction) *AliasService { + s.actions = append(s.actions, action...) + return s +} + +// buildURL builds the URL for the operation. +func (s *AliasService) buildURL() (string, url.Values, error) { + path := "/_aliases" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Do executes the command. +func (s *AliasService) Do(ctx context.Context) (*AliasResult, error) { + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Body with actions + body := make(map[string]interface{}) + var actions []interface{} + for _, action := range s.actions { + src, err := action.Source() + if err != nil { + return nil, err + } + actions = append(actions, src) + } + body["actions"] = actions + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return results + ret := new(AliasResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of an alias request. + +// AliasResult is the outcome of calling Do on AliasService. +type AliasResult struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_put_mapping.go b/vendor/github.com/olivere/elastic/v7/indices_put_mapping.go new file mode 100644 index 0000000000..eae320ac60 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_put_mapping.go @@ -0,0 +1,261 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesPutMappingService allows to register specific mapping definition +// for a specific type. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-put-mapping.html +// for details. +type IndicesPutMappingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + masterTimeout string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + updateAllTypes *bool + timeout string + bodyJson map[string]interface{} + bodyString string +} + +// NewPutMappingService is an alias for NewIndicesPutMappingService. +// Use NewIndicesPutMappingService. +func NewPutMappingService(client *Client) *IndicesPutMappingService { + return NewIndicesPutMappingService(client) +} + +// NewIndicesPutMappingService creates a new IndicesPutMappingService. +func NewIndicesPutMappingService(client *Client) *IndicesPutMappingService { + return &IndicesPutMappingService{ + client: client, + index: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesPutMappingService) Pretty(pretty bool) *IndicesPutMappingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesPutMappingService) Human(human bool) *IndicesPutMappingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesPutMappingService) ErrorTrace(errorTrace bool) *IndicesPutMappingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesPutMappingService) FilterPath(filterPath ...string) *IndicesPutMappingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesPutMappingService) Header(name string, value string) *IndicesPutMappingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesPutMappingService) Headers(headers http.Header) *IndicesPutMappingService { + s.headers = headers + return s +} + +// Index is a list of index names the mapping should be added to +// (supports wildcards); use `_all` or omit to add the mapping on all indices. +func (s *IndicesPutMappingService) Index(indices ...string) *IndicesPutMappingService { + s.index = append(s.index, indices...) + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesPutMappingService) Timeout(timeout string) *IndicesPutMappingService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesPutMappingService) MasterTimeout(masterTimeout string) *IndicesPutMappingService { + s.masterTimeout = masterTimeout + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesPutMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesPutMappingService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. +// This includes `_all` string or when no indices have been specified. +func (s *IndicesPutMappingService) AllowNoIndices(allowNoIndices bool) *IndicesPutMappingService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *IndicesPutMappingService) ExpandWildcards(expandWildcards string) *IndicesPutMappingService { + s.expandWildcards = expandWildcards + return s +} + +// UpdateAllTypes, if true, indicates that all fields that span multiple indices +// should be updated (default: false). +func (s *IndicesPutMappingService) UpdateAllTypes(updateAllTypes bool) *IndicesPutMappingService { + s.updateAllTypes = &updateAllTypes + return s +} + +// BodyJson contains the mapping definition. +func (s *IndicesPutMappingService) BodyJson(mapping map[string]interface{}) *IndicesPutMappingService { + s.bodyJson = mapping + return s +} + +// BodyString is the mapping definition serialized as a string. +func (s *IndicesPutMappingService) BodyString(mapping string) *IndicesPutMappingService { + s.bodyString = mapping + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesPutMappingService) buildURL() (string, url.Values, error) { + path, err := uritemplates.Expand("/{index}/_mapping", map[string]string{ + "index": strings.Join(s.index, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.updateAllTypes != nil { + params.Set("update_all_types", fmt.Sprintf("%v", *s.updateAllTypes)) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesPutMappingService) Validate() error { + var invalid []string + if len(s.index) == 0 { + invalid = append(invalid, "Index") + } + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesPutMappingService) Do(ctx context.Context) (*PutMappingResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(PutMappingResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// PutMappingResponse is the response of IndicesPutMappingService.Do. +type PutMappingResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_put_settings.go b/vendor/github.com/olivere/elastic/v7/indices_put_settings.go new file mode 100644 index 0000000000..c8c081163a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_put_settings.go @@ -0,0 +1,242 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesPutSettingsService changes specific index level settings in +// real time. +// +// See the documentation at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-update-settings.html. +type IndicesPutSettingsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + allowNoIndices *bool + expandWildcards string + flatSettings *bool + ignoreUnavailable *bool + masterTimeout string + bodyJson interface{} + bodyString string +} + +// NewIndicesPutSettingsService creates a new IndicesPutSettingsService. +func NewIndicesPutSettingsService(client *Client) *IndicesPutSettingsService { + return &IndicesPutSettingsService{ + client: client, + index: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesPutSettingsService) Pretty(pretty bool) *IndicesPutSettingsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesPutSettingsService) Human(human bool) *IndicesPutSettingsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesPutSettingsService) ErrorTrace(errorTrace bool) *IndicesPutSettingsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesPutSettingsService) FilterPath(filterPath ...string) *IndicesPutSettingsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesPutSettingsService) Header(name string, value string) *IndicesPutSettingsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesPutSettingsService) Headers(headers http.Header) *IndicesPutSettingsService { + s.headers = headers + return s +} + +// Index is a list of index names the mapping should be added to +// (supports wildcards); use `_all` or omit to add the mapping on all indices. +func (s *IndicesPutSettingsService) Index(indices ...string) *IndicesPutSettingsService { + s.index = append(s.index, indices...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` +// string or when no indices have been specified). +func (s *IndicesPutSettingsService) AllowNoIndices(allowNoIndices bool) *IndicesPutSettingsService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards specifies whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *IndicesPutSettingsService) ExpandWildcards(expandWildcards string) *IndicesPutSettingsService { + s.expandWildcards = expandWildcards + return s +} + +// FlatSettings indicates whether to return settings in flat format (default: false). +func (s *IndicesPutSettingsService) FlatSettings(flatSettings bool) *IndicesPutSettingsService { + s.flatSettings = &flatSettings + return s +} + +// IgnoreUnavailable specifies whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesPutSettingsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesPutSettingsService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// MasterTimeout is the timeout for connection to master. +func (s *IndicesPutSettingsService) MasterTimeout(masterTimeout string) *IndicesPutSettingsService { + s.masterTimeout = masterTimeout + return s +} + +// BodyJson is documented as: The index settings to be updated. +func (s *IndicesPutSettingsService) BodyJson(body interface{}) *IndicesPutSettingsService { + s.bodyJson = body + return s +} + +// BodyString is documented as: The index settings to be updated. +func (s *IndicesPutSettingsService) BodyString(body string) *IndicesPutSettingsService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesPutSettingsService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_settings", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_settings" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesPutSettingsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IndicesPutSettingsService) Do(ctx context.Context) (*IndicesPutSettingsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesPutSettingsResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesPutSettingsResponse is the response of IndicesPutSettingsService.Do. +type IndicesPutSettingsResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_put_template.go b/vendor/github.com/olivere/elastic/v7/indices_put_template.go new file mode 100644 index 0000000000..4ced8e8f49 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_put_template.go @@ -0,0 +1,259 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesPutTemplateService creates or updates index mappings. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-templates.html. +type IndicesPutTemplateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string + cause string + order interface{} + version *int + create *bool + timeout string + masterTimeout string + flatSettings *bool + bodyJson interface{} + bodyString string +} + +// NewIndicesPutTemplateService creates a new IndicesPutTemplateService. +func NewIndicesPutTemplateService(client *Client) *IndicesPutTemplateService { + return &IndicesPutTemplateService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesPutTemplateService) Pretty(pretty bool) *IndicesPutTemplateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesPutTemplateService) Human(human bool) *IndicesPutTemplateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesPutTemplateService) ErrorTrace(errorTrace bool) *IndicesPutTemplateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesPutTemplateService) FilterPath(filterPath ...string) *IndicesPutTemplateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesPutTemplateService) Header(name string, value string) *IndicesPutTemplateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesPutTemplateService) Headers(headers http.Header) *IndicesPutTemplateService { + s.headers = headers + return s +} + +// Name is the name of the index template. +func (s *IndicesPutTemplateService) Name(name string) *IndicesPutTemplateService { + s.name = name + return s +} + +// Cause describes the cause for this index template creation. This is currently +// undocumented, but part of the Java source. +func (s *IndicesPutTemplateService) Cause(cause string) *IndicesPutTemplateService { + s.cause = cause + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesPutTemplateService) Timeout(timeout string) *IndicesPutTemplateService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesPutTemplateService) MasterTimeout(masterTimeout string) *IndicesPutTemplateService { + s.masterTimeout = masterTimeout + return s +} + +// FlatSettings indicates whether to return settings in flat format (default: false). +func (s *IndicesPutTemplateService) FlatSettings(flatSettings bool) *IndicesPutTemplateService { + s.flatSettings = &flatSettings + return s +} + +// Order is the order for this template when merging multiple matching ones +// (higher numbers are merged later, overriding the lower numbers). +func (s *IndicesPutTemplateService) Order(order interface{}) *IndicesPutTemplateService { + s.order = order + return s +} + +// Version sets the version number for this template. +func (s *IndicesPutTemplateService) Version(version int) *IndicesPutTemplateService { + s.version = &version + return s +} + +// Create indicates whether the index template should only be added if +// new or can also replace an existing one. +func (s *IndicesPutTemplateService) Create(create bool) *IndicesPutTemplateService { + s.create = &create + return s +} + +// BodyJson is documented as: The template definition. +func (s *IndicesPutTemplateService) BodyJson(body interface{}) *IndicesPutTemplateService { + s.bodyJson = body + return s +} + +// BodyString is documented as: The template definition. +func (s *IndicesPutTemplateService) BodyString(body string) *IndicesPutTemplateService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesPutTemplateService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_template/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.order != nil { + params.Set("order", fmt.Sprintf("%v", s.order)) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%v", *s.version)) + } + if s.create != nil { + params.Set("create", fmt.Sprintf("%v", *s.create)) + } + if s.cause != "" { + params.Set("cause", s.cause) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesPutTemplateService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesPutTemplateService) Do(ctx context.Context) (*IndicesPutTemplateResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesPutTemplateResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesPutTemplateResponse is the response of IndicesPutTemplateService.Do. +type IndicesPutTemplateResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_refresh.go b/vendor/github.com/olivere/elastic/v7/indices_refresh.go new file mode 100644 index 0000000000..4dfdbe40fe --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_refresh.go @@ -0,0 +1,149 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// RefreshService explicitly refreshes one or more indices. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-refresh.html. +type RefreshService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string +} + +// NewRefreshService creates a new instance of RefreshService. +func NewRefreshService(client *Client) *RefreshService { + builder := &RefreshService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *RefreshService) Pretty(pretty bool) *RefreshService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *RefreshService) Human(human bool) *RefreshService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *RefreshService) ErrorTrace(errorTrace bool) *RefreshService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *RefreshService) FilterPath(filterPath ...string) *RefreshService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *RefreshService) Header(name string, value string) *RefreshService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *RefreshService) Headers(headers http.Header) *RefreshService { + s.headers = headers + return s +} + +// Index specifies the indices to refresh. +func (s *RefreshService) Index(index ...string) *RefreshService { + s.index = append(s.index, index...) + return s +} + +// buildURL builds the URL for the operation. +func (s *RefreshService) buildURL() (string, url.Values, error) { + var err error + var path string + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_refresh", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_refresh" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Do executes the request. +func (s *RefreshService) Do(ctx context.Context) (*RefreshResult, error) { + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return result + ret := new(RefreshResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Result of a refresh request. + +// RefreshResult is the outcome of RefreshService.Do. +type RefreshResult struct { + Shards *ShardsInfo `json:"_shards,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_rollover.go b/vendor/github.com/olivere/elastic/v7/indices_rollover.go new file mode 100644 index 0000000000..ce72ef1e67 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_rollover.go @@ -0,0 +1,324 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesRolloverService rolls an alias over to a new index when the +// existing index is considered to be too large or too old. +// +// It is documented at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-rollover-index.html. +type IndicesRolloverService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + dryRun bool + newIndex string + alias string + masterTimeout string + timeout string + waitForActiveShards string + conditions map[string]interface{} + settings map[string]interface{} + mappings map[string]interface{} + bodyJson interface{} + bodyString string +} + +// NewIndicesRolloverService creates a new IndicesRolloverService. +func NewIndicesRolloverService(client *Client) *IndicesRolloverService { + return &IndicesRolloverService{ + client: client, + conditions: make(map[string]interface{}), + settings: make(map[string]interface{}), + mappings: make(map[string]interface{}), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesRolloverService) Pretty(pretty bool) *IndicesRolloverService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesRolloverService) Human(human bool) *IndicesRolloverService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesRolloverService) ErrorTrace(errorTrace bool) *IndicesRolloverService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesRolloverService) FilterPath(filterPath ...string) *IndicesRolloverService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesRolloverService) Header(name string, value string) *IndicesRolloverService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesRolloverService) Headers(headers http.Header) *IndicesRolloverService { + s.headers = headers + return s +} + +// Alias is the name of the alias to rollover. +func (s *IndicesRolloverService) Alias(alias string) *IndicesRolloverService { + s.alias = alias + return s +} + +// NewIndex is the name of the rollover index. +func (s *IndicesRolloverService) NewIndex(newIndex string) *IndicesRolloverService { + s.newIndex = newIndex + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesRolloverService) MasterTimeout(masterTimeout string) *IndicesRolloverService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout sets an explicit operation timeout. +func (s *IndicesRolloverService) Timeout(timeout string) *IndicesRolloverService { + s.timeout = timeout + return s +} + +// WaitForActiveShards sets the number of active shards to wait for on the +// newly created rollover index before the operation returns. +func (s *IndicesRolloverService) WaitForActiveShards(waitForActiveShards string) *IndicesRolloverService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// DryRun, when set, specifies that only conditions are checked without +// performing the actual rollover. +func (s *IndicesRolloverService) DryRun(dryRun bool) *IndicesRolloverService { + s.dryRun = dryRun + return s +} + +// Conditions allows to specify all conditions as a dictionary. +func (s *IndicesRolloverService) Conditions(conditions map[string]interface{}) *IndicesRolloverService { + s.conditions = conditions + return s +} + +// AddCondition adds a condition to the rollover decision. +func (s *IndicesRolloverService) AddCondition(name string, value interface{}) *IndicesRolloverService { + s.conditions[name] = value + return s +} + +// AddMaxIndexAgeCondition adds a condition to set the max index age. +func (s *IndicesRolloverService) AddMaxIndexAgeCondition(time string) *IndicesRolloverService { + s.conditions["max_age"] = time + return s +} + +// AddMaxIndexDocsCondition adds a condition to set the max documents in the index. +func (s *IndicesRolloverService) AddMaxIndexDocsCondition(docs int64) *IndicesRolloverService { + s.conditions["max_docs"] = docs + return s +} + +// Settings adds the index settings. +func (s *IndicesRolloverService) Settings(settings map[string]interface{}) *IndicesRolloverService { + s.settings = settings + return s +} + +// AddSetting adds an index setting. +func (s *IndicesRolloverService) AddSetting(name string, value interface{}) *IndicesRolloverService { + s.settings[name] = value + return s +} + +// Mappings adds the index mappings. +func (s *IndicesRolloverService) Mappings(mappings map[string]interface{}) *IndicesRolloverService { + s.mappings = mappings + return s +} + +// AddMapping adds a mapping for the given type. +func (s *IndicesRolloverService) AddMapping(typ string, mapping interface{}) *IndicesRolloverService { + s.mappings[typ] = mapping + return s +} + +// BodyJson sets the conditions that needs to be met for executing rollover, +// specified as a serializable JSON instance which is sent as the body of +// the request. +func (s *IndicesRolloverService) BodyJson(body interface{}) *IndicesRolloverService { + s.bodyJson = body + return s +} + +// BodyString sets the conditions that needs to be met for executing rollover, +// specified as a string which is sent as the body of the request. +func (s *IndicesRolloverService) BodyString(body string) *IndicesRolloverService { + s.bodyString = body + return s +} + +// getBody returns the body of the request, if not explicitly set via +// BodyJson or BodyString. +func (s *IndicesRolloverService) getBody() interface{} { + body := make(map[string]interface{}) + if len(s.conditions) > 0 { + body["conditions"] = s.conditions + } + if len(s.settings) > 0 { + body["settings"] = s.settings + } + if len(s.mappings) > 0 { + body["mappings"] = s.mappings + } + return body +} + +// buildURL builds the URL for the operation. +func (s *IndicesRolloverService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if s.newIndex != "" { + path, err = uritemplates.Expand("/{alias}/_rollover/{new_index}", map[string]string{ + "alias": s.alias, + "new_index": s.newIndex, + }) + } else { + path, err = uritemplates.Expand("/{alias}/_rollover", map[string]string{ + "alias": s.alias, + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.dryRun { + params.Set("dry_run", "true") + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesRolloverService) Validate() error { + var invalid []string + if s.alias == "" { + invalid = append(invalid, "Alias") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesRolloverService) Do(ctx context.Context) (*IndicesRolloverResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else if s.bodyString != "" { + body = s.bodyString + } else { + body = s.getBody() + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesRolloverResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesRolloverResponse is the response of IndicesRolloverService.Do. +type IndicesRolloverResponse struct { + OldIndex string `json:"old_index"` + NewIndex string `json:"new_index"` + RolledOver bool `json:"rolled_over"` + DryRun bool `json:"dry_run"` + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Conditions map[string]bool `json:"conditions"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_segments.go b/vendor/github.com/olivere/elastic/v7/indices_segments.go new file mode 100644 index 0000000000..01c5c8dbd2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_segments.go @@ -0,0 +1,278 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesSegmentsService provides low level segments information that a +// Lucene index (shard level) is built with. Allows to be used to provide +// more information on the state of a shard and an index, possibly +// optimization information, data "wasted" on deletes, and so on. +// +// Find further documentation at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-segments.html. +type IndicesSegmentsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + allowNoIndices *bool + expandWildcards string + ignoreUnavailable *bool + operationThreading interface{} + verbose *bool +} + +// NewIndicesSegmentsService creates a new IndicesSegmentsService. +func NewIndicesSegmentsService(client *Client) *IndicesSegmentsService { + return &IndicesSegmentsService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesSegmentsService) Pretty(pretty bool) *IndicesSegmentsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesSegmentsService) Human(human bool) *IndicesSegmentsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesSegmentsService) ErrorTrace(errorTrace bool) *IndicesSegmentsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesSegmentsService) FilterPath(filterPath ...string) *IndicesSegmentsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesSegmentsService) Header(name string, value string) *IndicesSegmentsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesSegmentsService) Headers(headers http.Header) *IndicesSegmentsService { + s.headers = headers + return s +} + +// Index is a comma-separated list of index names; use `_all` or empty string +// to perform the operation on all indices. +func (s *IndicesSegmentsService) Index(indices ...string) *IndicesSegmentsService { + s.index = append(s.index, indices...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or when +// no indices have been specified). +func (s *IndicesSegmentsService) AllowNoIndices(allowNoIndices bool) *IndicesSegmentsService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to concrete indices +// that are open, closed or both. +func (s *IndicesSegmentsService) ExpandWildcards(expandWildcards string) *IndicesSegmentsService { + s.expandWildcards = expandWildcards + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesSegmentsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesSegmentsService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// OperationThreading is undocumented in Elasticsearch as of now. +func (s *IndicesSegmentsService) OperationThreading(operationThreading interface{}) *IndicesSegmentsService { + s.operationThreading = operationThreading + return s +} + +// Verbose, when set to true, includes detailed memory usage by Lucene. +func (s *IndicesSegmentsService) Verbose(verbose bool) *IndicesSegmentsService { + s.verbose = &verbose + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesSegmentsService) buildURL() (string, url.Values, error) { + var err error + var path string + + if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_segments", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path = "/_segments" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.operationThreading != nil { + params.Set("operation_threading", fmt.Sprintf("%v", s.operationThreading)) + } + if s.verbose != nil { + params.Set("verbose", fmt.Sprintf("%v", *s.verbose)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesSegmentsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IndicesSegmentsService) Do(ctx context.Context) (*IndicesSegmentsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesSegmentsResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesSegmentsResponse is the response of IndicesSegmentsService.Do. +type IndicesSegmentsResponse struct { + // Shards provides information returned from shards. + Shards *ShardsInfo `json:"_shards"` + + // Indices provides a map into the stats of an index. + // The key of the map is the index name. + Indices map[string]*IndexSegments `json:"indices,omitempty"` +} + +type IndexSegments struct { + // Shards provides a map into the shard related information of an index. + // The key of the map is the number of a specific shard. + Shards map[string][]*IndexSegmentsShards `json:"shards,omitempty"` +} + +type IndexSegmentsShards struct { + Routing *IndexSegmentsRouting `json:"routing,omitempty"` + NumCommittedSegments int64 `json:"num_committed_segments,omitempty"` + NumSearchSegments int64 `json:"num_search_segments"` + + // Segments provides a map into the segment related information of a shard. + // The key of the map is the specific lucene segment id. + Segments map[string]*IndexSegmentsDetails `json:"segments,omitempty"` +} + +type IndexSegmentsRouting struct { + State string `json:"state,omitempty"` + Primary bool `json:"primary,omitempty"` + Node string `json:"node,omitempty"` + RelocatingNode string `json:"relocating_node,omitempty"` +} + +type IndexSegmentsDetails struct { + Generation int64 `json:"generation,omitempty"` + NumDocs int64 `json:"num_docs,omitempty"` + DeletedDocs int64 `json:"deleted_docs,omitempty"` + Size string `json:"size,omitempty"` + SizeInBytes int64 `json:"size_in_bytes,omitempty"` + Memory string `json:"memory,omitempty"` + MemoryInBytes int64 `json:"memory_in_bytes,omitempty"` + Committed bool `json:"committed,omitempty"` + Search bool `json:"search,omitempty"` + Version string `json:"version,omitempty"` + Compound bool `json:"compound,omitempty"` + MergeId string `json:"merge_id,omitempty"` + Sort []*IndexSegmentsSort `json:"sort,omitempty"` + RAMTree []*IndexSegmentsRamTree `json:"ram_tree,omitempty"` + Attributes map[string]string `json:"attributes,omitempty"` +} + +type IndexSegmentsSort struct { + Field string `json:"field,omitempty"` + Mode string `json:"mode,omitempty"` + Missing interface{} `json:"missing,omitempty"` + Reverse bool `json:"reverse,omitempty"` +} + +type IndexSegmentsRamTree struct { + Description string `json:"description,omitempty"` + Size string `json:"size,omitempty"` + SizeInBytes int64 `json:"size_in_bytes,omitempty"` + Children []*IndexSegmentsRamTree `json:"children,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_shrink.go b/vendor/github.com/olivere/elastic/v7/indices_shrink.go new file mode 100644 index 0000000000..76cec613b4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_shrink.go @@ -0,0 +1,231 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesShrinkService allows you to shrink an existing index into a +// new index with fewer primary shards. +// +// For further details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-shrink-index.html. +type IndicesShrinkService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + source string + target string + masterTimeout string + timeout string + waitForActiveShards string + bodyJson interface{} + bodyString string +} + +// NewIndicesShrinkService creates a new IndicesShrinkService. +func NewIndicesShrinkService(client *Client) *IndicesShrinkService { + return &IndicesShrinkService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesShrinkService) Pretty(pretty bool) *IndicesShrinkService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesShrinkService) Human(human bool) *IndicesShrinkService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesShrinkService) ErrorTrace(errorTrace bool) *IndicesShrinkService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesShrinkService) FilterPath(filterPath ...string) *IndicesShrinkService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesShrinkService) Header(name string, value string) *IndicesShrinkService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesShrinkService) Headers(headers http.Header) *IndicesShrinkService { + s.headers = headers + return s +} + +// Source is the name of the source index to shrink. +func (s *IndicesShrinkService) Source(source string) *IndicesShrinkService { + s.source = source + return s +} + +// Target is the name of the target index to shrink into. +func (s *IndicesShrinkService) Target(target string) *IndicesShrinkService { + s.target = target + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *IndicesShrinkService) MasterTimeout(masterTimeout string) *IndicesShrinkService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout is an explicit operation timeout. +func (s *IndicesShrinkService) Timeout(timeout string) *IndicesShrinkService { + s.timeout = timeout + return s +} + +// WaitForActiveShards sets the number of active shards to wait for on +// the shrunken index before the operation returns. +func (s *IndicesShrinkService) WaitForActiveShards(waitForActiveShards string) *IndicesShrinkService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// BodyJson is the configuration for the target index (`settings` and `aliases`) +// defined as a JSON-serializable instance to be sent as the request body. +func (s *IndicesShrinkService) BodyJson(body interface{}) *IndicesShrinkService { + s.bodyJson = body + return s +} + +// BodyString is the configuration for the target index (`settings` and `aliases`) +// defined as a string to send as the request body. +func (s *IndicesShrinkService) BodyString(body string) *IndicesShrinkService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesShrinkService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{source}/_shrink/{target}", map[string]string{ + "source": s.source, + "target": s.target, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesShrinkService) Validate() error { + var invalid []string + if s.source == "" { + invalid = append(invalid, "Source") + } + if s.target == "" { + invalid = append(invalid, "Target") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IndicesShrinkService) Do(ctx context.Context) (*IndicesShrinkResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else if s.bodyString != "" { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesShrinkResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesShrinkResponse is the response of IndicesShrinkService.Do. +type IndicesShrinkResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_stats.go b/vendor/github.com/olivere/elastic/v7/indices_stats.go new file mode 100644 index 0000000000..397d638611 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_stats.go @@ -0,0 +1,530 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesStatsService provides stats on various metrics of one or more +// indices. See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-stats.html. +type IndicesStatsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + metric []string + index []string + level string + types []string + completionFields []string + fielddataFields []string + fields []string + groups []string +} + +// NewIndicesStatsService creates a new IndicesStatsService. +func NewIndicesStatsService(client *Client) *IndicesStatsService { + return &IndicesStatsService{ + client: client, + index: make([]string, 0), + metric: make([]string, 0), + completionFields: make([]string, 0), + fielddataFields: make([]string, 0), + fields: make([]string, 0), + groups: make([]string, 0), + types: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesStatsService) Pretty(pretty bool) *IndicesStatsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesStatsService) Human(human bool) *IndicesStatsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesStatsService) ErrorTrace(errorTrace bool) *IndicesStatsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesStatsService) FilterPath(filterPath ...string) *IndicesStatsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesStatsService) Header(name string, value string) *IndicesStatsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesStatsService) Headers(headers http.Header) *IndicesStatsService { + s.headers = headers + return s +} + +// Metric limits the information returned the specific metrics. Options are: +// docs, store, indexing, get, search, completion, fielddata, flush, merge, +// query_cache, refresh, suggest, and warmer. +func (s *IndicesStatsService) Metric(metric ...string) *IndicesStatsService { + s.metric = append(s.metric, metric...) + return s +} + +// Index is the list of index names; use `_all` or empty string to perform +// the operation on all indices. +func (s *IndicesStatsService) Index(indices ...string) *IndicesStatsService { + s.index = append(s.index, indices...) + return s +} + +// Type is a list of document types for the `indexing` index metric. +func (s *IndicesStatsService) Type(types ...string) *IndicesStatsService { + s.types = append(s.types, types...) + return s +} + +// Level returns stats aggregated at cluster, index or shard level. +func (s *IndicesStatsService) Level(level string) *IndicesStatsService { + s.level = level + return s +} + +// CompletionFields is a list of fields for `fielddata` and `suggest` +// index metric (supports wildcards). +func (s *IndicesStatsService) CompletionFields(completionFields ...string) *IndicesStatsService { + s.completionFields = append(s.completionFields, completionFields...) + return s +} + +// FielddataFields is a list of fields for `fielddata` index metric (supports wildcards). +func (s *IndicesStatsService) FielddataFields(fielddataFields ...string) *IndicesStatsService { + s.fielddataFields = append(s.fielddataFields, fielddataFields...) + return s +} + +// Fields is a list of fields for `fielddata` and `completion` index metric +// (supports wildcards). +func (s *IndicesStatsService) Fields(fields ...string) *IndicesStatsService { + s.fields = append(s.fields, fields...) + return s +} + +// Groups is a list of search groups for `search` index metric. +func (s *IndicesStatsService) Groups(groups ...string) *IndicesStatsService { + s.groups = append(s.groups, groups...) + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesStatsService) buildURL() (string, url.Values, error) { + var err error + var path string + if len(s.index) > 0 && len(s.metric) > 0 { + path, err = uritemplates.Expand("/{index}/_stats/{metric}", map[string]string{ + "index": strings.Join(s.index, ","), + "metric": strings.Join(s.metric, ","), + }) + } else if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_stats", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else if len(s.metric) > 0 { + path, err = uritemplates.Expand("/_stats/{metric}", map[string]string{ + "metric": strings.Join(s.metric, ","), + }) + } else { + path = "/_stats" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.groups) > 0 { + params.Set("groups", strings.Join(s.groups, ",")) + } + if s.level != "" { + params.Set("level", s.level) + } + if len(s.types) > 0 { + params.Set("types", strings.Join(s.types, ",")) + } + if len(s.completionFields) > 0 { + params.Set("completion_fields", strings.Join(s.completionFields, ",")) + } + if len(s.fielddataFields) > 0 { + params.Set("fielddata_fields", strings.Join(s.fielddataFields, ",")) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesStatsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IndicesStatsService) Do(ctx context.Context) (*IndicesStatsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesStatsResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesStatsResponse is the response of IndicesStatsService.Do. +type IndicesStatsResponse struct { + // Shards provides information returned from shards. + Shards *ShardsInfo `json:"_shards"` + + // All provides summary stats about all indices. + All *IndexStats `json:"_all,omitempty"` + + // Indices provides a map into the stats of an index. The key of the + // map is the index name. + Indices map[string]*IndexStats `json:"indices,omitempty"` +} + +// IndexStats is index stats for a specific index. +type IndexStats struct { + UUID string `json:"uuid,omitempty"` + Primaries *IndexStatsDetails `json:"primaries,omitempty"` + Total *IndexStatsDetails `json:"total,omitempty"` + Shards map[string][]*IndexStatsDetails `json:"shards,omitempty"` +} + +type IndexStatsDetails struct { + Routing *IndexStatsRouting `json:"routing,omitempty"` + Docs *IndexStatsDocs `json:"docs,omitempty"` + Store *IndexStatsStore `json:"store,omitempty"` + Indexing *IndexStatsIndexing `json:"indexing,omitempty"` + Get *IndexStatsGet `json:"get,omitempty"` + Search *IndexStatsSearch `json:"search,omitempty"` + Merges *IndexStatsMerges `json:"merges,omitempty"` + Refresh *IndexStatsRefresh `json:"refresh,omitempty"` + Recovery *IndexStatsRecovery `json:"recovery,omitempty"` + Flush *IndexStatsFlush `json:"flush,omitempty"` + Warmer *IndexStatsWarmer `json:"warmer,omitempty"` + FilterCache *IndexStatsFilterCache `json:"filter_cache,omitempty"` + IdCache *IndexStatsIdCache `json:"id_cache,omitempty"` + Fielddata *IndexStatsFielddata `json:"fielddata,omitempty"` + Percolate *IndexStatsPercolate `json:"percolate,omitempty"` + Completion *IndexStatsCompletion `json:"completion,omitempty"` + Segments *IndexStatsSegments `json:"segments,omitempty"` + Translog *IndexStatsTranslog `json:"translog,omitempty"` + Suggest *IndexStatsSuggest `json:"suggest,omitempty"` + QueryCache *IndexStatsQueryCache `json:"query_cache,omitempty"` + RequestCache *IndexStatsRequestCache `json:"request_cache,omitempty"` + Commit *IndexStatsCommit `json:"commit,omitempty"` + SeqNo *IndexStatsSeqNo `json:"seq_no,omitempty"` + RetentionLeases *IndexStatsRetentionLeases `json:"retention_leases,omitempty"` + ShardPath *IndexStatsShardPath `json:"shard_path,omitempty"` +} + +type IndexStatsRouting struct { + State string `json:"state"` // e.g. "STARTED" + Primary bool `json:"primary"` + Node string `json:"node"` // e.g. "-aXnGv4oTW6bIIl0db3eCg" + RelocatingNode *string `json:"relocating_node"` +} + +type IndexStatsShardPath struct { + StatePath string `json:"state_path"` // e.g. "/usr/share/elasticsearch/data/nodes/0" + DataPath string `json:"data_path"` // e.g. "/usr/share/elasticsearch/data/nodes/0" + IsCustomDataPath bool `json:"is_custom_data_path"` +} + +type IndexStatsDocs struct { + Count int64 `json:"count,omitempty"` + Deleted int64 `json:"deleted,omitempty"` +} + +type IndexStatsStore struct { + Size string `json:"size,omitempty"` // human size, e.g. 119.3mb + SizeInBytes int64 `json:"size_in_bytes,omitempty"` +} + +type IndexStatsIndexing struct { + IndexTotal int64 `json:"index_total,omitempty"` + IndexTime string `json:"index_time,omitempty"` + IndexTimeInMillis int64 `json:"index_time_in_millis,omitempty"` + IndexCurrent int64 `json:"index_current,omitempty"` + IndexFailed int64 `json:"index_failed,omitempty"` + DeleteTotal int64 `json:"delete_total,omitempty"` + DeleteTime string `json:"delete_time,omitempty"` + DeleteTimeInMillis int64 `json:"delete_time_in_millis,omitempty"` + DeleteCurrent int64 `json:"delete_current,omitempty"` + NoopUpdateTotal int64 `json:"noop_update_total,omitempty"` + IsThrottled bool `json:"is_throttled,omitempty"` + ThrottleTime string `json:"throttle_time,omitempty"` + ThrottleTimeInMillis int64 `json:"throttle_time_in_millis,omitempty"` +} + +type IndexStatsGet struct { + Total int64 `json:"total,omitempty"` + GetTime string `json:"getTime,omitempty"` // 7.4.0 uses "getTime", earlier versions used "get_time" + TimeInMillis int64 `json:"time_in_millis,omitempty"` + ExistsTotal int64 `json:"exists_total,omitempty"` + ExistsTime string `json:"exists_time,omitempty"` + ExistsTimeInMillis int64 `json:"exists_time_in_millis,omitempty"` + MissingTotal int64 `json:"missing_total,omitempty"` + MissingTime string `json:"missing_time,omitempty"` + MissingTimeInMillis int64 `json:"missing_time_in_millis,omitempty"` + Current int64 `json:"current,omitempty"` +} + +type IndexStatsSearch struct { + OpenContexts int64 `json:"open_contexts,omitempty"` + QueryTotal int64 `json:"query_total,omitempty"` + QueryTime string `json:"query_time,omitempty"` + QueryTimeInMillis int64 `json:"query_time_in_millis,omitempty"` + QueryCurrent int64 `json:"query_current,omitempty"` + FetchTotal int64 `json:"fetch_total,omitempty"` + FetchTime string `json:"fetch_time,omitempty"` + FetchTimeInMillis int64 `json:"fetch_time_in_millis,omitempty"` + FetchCurrent int64 `json:"fetch_current,omitempty"` + ScrollTotal int64 `json:"scroll_total,omitempty"` + ScrollTime string `json:"scroll_time,omitempty"` + ScrollTimeInMillis int64 `json:"scroll_time_in_millis,omitempty"` + ScrollCurrent int64 `json:"scroll_current,omitempty"` + SuggestTotal int64 `json:"suggest_total,omitempty"` + SuggestTime string `json:"suggest_time,omitempty"` + SuggestTimeInMillis int64 `json:"suggest_time_in_millis,omitempty"` + SuggestCurrent int64 `json:"suggest_current,omitempty"` +} + +type IndexStatsMerges struct { + Current int64 `json:"current,omitempty"` + CurrentDocs int64 `json:"current_docs,omitempty"` + CurrentSize string `json:"current_size,omitempty"` + CurrentSizeInBytes int64 `json:"current_size_in_bytes,omitempty"` + Total int64 `json:"total,omitempty"` + TotalTime string `json:"total_time,omitempty"` + TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"` + TotalDocs int64 `json:"total_docs,omitempty"` + TotalSize string `json:"total_size,omitempty"` + TotalSizeInBytes int64 `json:"total_size_in_bytes,omitempty"` + TotalStoppedTime string `json:"total_stopped_time,omitempty"` + TotalStoppedTimeInMillis int64 `json:"total_stopped_time_in_millis,omitempty"` + TotalThrottledTime string `json:"total_throttled_time,omitempty"` + TotalThrottledTimeInMillis int64 `json:"total_throttled_time_in_millis,omitempty"` + TotalAutoThrottle string `json:"total_auto_throttle,omitempty"` + TotalAutoThrottleInBytes int64 `json:"total_auto_throttle_in_bytes,omitempty"` +} + +type IndexStatsRefresh struct { + Total int64 `json:"total,omitempty"` + TotalTime string `json:"total_time,omitempty"` + TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"` + ExternalTotal int64 `json:"external_total,omitempty"` + ExternalTotalTime string `json:"external_total_time,omitempty"` + ExternalTotalTimeInMillis int64 `json:"external_total_time_in_millis,omitempty"` + Listeners int64 `json:"listeners,omitempty"` +} + +type IndexStatsRecovery struct { + CurrentAsSource int64 `json:"current_as_source,omitempty"` + CurrentAsTarget int64 `json:"current_as_target,omitempty"` + ThrottleTime string `json:"throttle_time,omitempty"` + ThrottleTimeInMillis int64 `json:"throttle_time_in_millis,omitempty"` +} + +type IndexStatsFlush struct { + Total int64 `json:"total,omitempty"` + TotalTime string `json:"total_time,omitempty"` + TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"` + Periodic int64 `json:"periodic,omitempty"` +} + +type IndexStatsWarmer struct { + Current int64 `json:"current,omitempty"` + Total int64 `json:"total,omitempty"` + TotalTime string `json:"total_time,omitempty"` + TotalTimeInMillis int64 `json:"total_time_in_millis,omitempty"` +} + +type IndexStatsRequestCache struct { + MemorySize string `json:"memory_size,omitempty"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"` + Evictions int64 `json:"evictions,omitempty"` + HitCount int64 `json:"hit_count,omitempty"` + MissCount int64 `json:"miss_count,omitempty"` +} + +type IndexStatsCommit struct { + ID string `json:"id,omitempty"` // lucene commit ID in base64, e.g. "m2tDMYHzSpSV6zJH0lIAnA==" + Generation int64 `json:"generation,omitempty"` + UserData map[string]string `json:"user_data,omitempty"` + NumDocs int64 `json:"num_docs,omitempty"` +} + +type IndexStatsFilterCache struct { + MemorySize string `json:"memory_size,omitempty"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"` + Evictions int64 `json:"evictions,omitempty"` +} + +type IndexStatsIdCache struct { + MemorySize string `json:"memory_size,omitempty"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"` +} + +type IndexStatsFielddata struct { + MemorySize string `json:"memory_size,omitempty"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"` + Evictions int64 `json:"evictions,omitempty"` +} + +type IndexStatsPercolate struct { + Total int64 `json:"total,omitempty"` + GetTime string `json:"get_time,omitempty"` + TimeInMillis int64 `json:"time_in_millis,omitempty"` + Current int64 `json:"current,omitempty"` + MemorySize string `json:"memory_size,omitempty"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"` + Queries int64 `json:"queries,omitempty"` +} + +type IndexStatsCompletion struct { + Size string `json:"size,omitempty"` + SizeInBytes int64 `json:"size_in_bytes,omitempty"` +} + +type IndexStatsSegments struct { + Count int64 `json:"count"` + Memory string `json:"memory"` // e.g. "61.3kb" + MemoryInBytes int64 `json:"memory_in_bytes"` + TermsMemory string `json:"terms_memory"` // e.g. "61.3kb" + TermsMemoryInBytes int64 `json:"terms_memory_in_bytes"` + StoredFieldsMemory string `json:"stored_fields_memory"` // e.g. "61.3kb" + StoredFieldsMemoryInBytes int64 `json:"stored_fields_memory_in_bytes"` + TermVectorsMemory string `json:"term_vectors_memory"` // e.g. "61.3kb" + TermVectorsMemoryInBytes int64 `json:"term_vectors_memory_in_bytes"` + NormsMemory string `json:"norms_memory"` // e.g. "61.3kb" + NormsMemoryInBytes int64 `json:"norms_memory_in_bytes"` + PointsMemory string `json:"points_memory"` // e.g. "61.3kb" + PointsMemoryInBytes int64 `json:"points_memory_in_bytes"` + DocValuesMemory string `json:"doc_values_memory"` // e.g. "61.3kb" + DocValuesMemoryInBytes int64 `json:"doc_values_memory_in_bytes"` + IndexWriterMemory string `json:"index_writer_memory"` // e.g. "61.3kb" + IndexWriterMemoryInBytes int64 `json:"index_writer_memory_in_bytes"` + VersionMapMemory string `json:"version_map_memory"` // e.g. "61.3kb" + VersionMapMemoryInBytes int64 `json:"version_map_memory_in_bytes"` + FixedBitSet string `json:"fixed_bit_set"` // e.g. "61.3kb" + FixedBitSetInBytes int64 `json:"fixed_bit_set_memory_in_bytes"` + MaxUnsafeAutoIDTimestamp int64 `json:"max_unsafe_auto_id_timestamp"` + FileSizes map[string]*ClusterStatsIndicesSegmentsFile `json:"file_sizes"` +} + +type IndexStatsTranslog struct { + Operations int64 `json:"operations,omitempty"` + Size string `json:"size,omitempty"` + SizeInBytes int64 `json:"size_in_bytes,omitempty"` + UncommittedOperations int64 `json:"uncommitted_operations,omitempty"` + UncommittedSize string `json:"uncommitted_size,omitempty"` + UncommittedSizeInBytes int64 `json:"uncommitted_size_in_bytes,omitempty"` + EarliestLastModifiedAge int64 `json:"earliest_last_modified_age,omitempty"` +} + +type IndexStatsSuggest struct { + Total int64 `json:"total,omitempty"` + Time string `json:"time,omitempty"` + TimeInMillis int64 `json:"time_in_millis,omitempty"` + Current int64 `json:"current,omitempty"` +} + +type IndexStatsQueryCache struct { + MemorySize string `json:"memory_size,omitempty"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes,omitempty"` + TotalCount int64 `json:"total_count,omitempty"` + HitCount int64 `json:"hit_count,omitempty"` + MissCount int64 `json:"miss_count,omitempty"` + CacheSize int64 `json:"cache_size,omitempty"` + CacheCount int64 `json:"cache_count,omitempty"` + Evictions int64 `json:"evictions,omitempty"` +} + +type IndexStatsSeqNo struct { + MaxSeqNo int64 `json:"max_seq_no,omitempty"` + LocalCheckpoint int64 `json:"local_checkpoint,omitempty"` + GlobalCheckpoint int64 `json:"global_checkpoint,omitempty"` +} + +type IndexStatsRetentionLeases struct { + PrimaryTerm int64 `json:"primary_term,omitempty"` + Version int64 `json:"version,omitempty"` + Leases []*IndexStatsRetentionLease `json:"leases,omitempty"` +} + +type IndexStatsRetentionLease struct { + Id string `json:"id,omitempty"` + RetainingSeqNo int64 `json:"retaining_seq_no,omitempty"` + Timestamp int64 `json:"timestamp,omitempty"` + Source string `json:"source,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/indices_unfreeze.go b/vendor/github.com/olivere/elastic/v7/indices_unfreeze.go new file mode 100644 index 0000000000..3da75d4fd7 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/indices_unfreeze.go @@ -0,0 +1,229 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IndicesUnfreezeService unfreezes an index. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/unfreeze-index-api.html +// and https://www.elastic.co/blog/creating-frozen-indices-with-the-elasticsearch-freeze-index-api +// for details. +type IndicesUnfreezeService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + timeout string + masterTimeout string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + waitForActiveShards string +} + +// NewIndicesUnfreezeService creates a new IndicesUnfreezeService. +func NewIndicesUnfreezeService(client *Client) *IndicesUnfreezeService { + return &IndicesUnfreezeService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IndicesUnfreezeService) Pretty(pretty bool) *IndicesUnfreezeService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IndicesUnfreezeService) Human(human bool) *IndicesUnfreezeService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IndicesUnfreezeService) ErrorTrace(errorTrace bool) *IndicesUnfreezeService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IndicesUnfreezeService) FilterPath(filterPath ...string) *IndicesUnfreezeService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IndicesUnfreezeService) Header(name string, value string) *IndicesUnfreezeService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IndicesUnfreezeService) Headers(headers http.Header) *IndicesUnfreezeService { + s.headers = headers + return s +} + +// Index is the name of the index to unfreeze. +func (s *IndicesUnfreezeService) Index(index string) *IndicesUnfreezeService { + s.index = index + return s +} + +// Timeout allows to specify an explicit timeout. +func (s *IndicesUnfreezeService) Timeout(timeout string) *IndicesUnfreezeService { + s.timeout = timeout + return s +} + +// MasterTimeout allows to specify a timeout for connection to master. +func (s *IndicesUnfreezeService) MasterTimeout(masterTimeout string) *IndicesUnfreezeService { + s.masterTimeout = masterTimeout + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *IndicesUnfreezeService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesUnfreezeService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or when +// no indices have been specified). +func (s *IndicesUnfreezeService) AllowNoIndices(allowNoIndices bool) *IndicesUnfreezeService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards specifies whether to expand wildcard expression to +// concrete indices that are open, closed or both.. +func (s *IndicesUnfreezeService) ExpandWildcards(expandWildcards string) *IndicesUnfreezeService { + s.expandWildcards = expandWildcards + return s +} + +// WaitForActiveShards sets the number of active shards to wait for +// before the operation returns. +func (s *IndicesUnfreezeService) WaitForActiveShards(numShards string) *IndicesUnfreezeService { + s.waitForActiveShards = numShards + return s +} + +// buildURL builds the URL for the operation. +func (s *IndicesUnfreezeService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/_unfreeze", map[string]string{ + "index": s.index, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IndicesUnfreezeService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the service. +func (s *IndicesUnfreezeService) Do(ctx context.Context) (*IndicesUnfreezeResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IndicesUnfreezeResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IndicesUnfreezeResponse is the outcome of freezing an index. +type IndicesUnfreezeResponse struct { + Shards *ShardsInfo `json:"_shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/ingest_delete_pipeline.go b/vendor/github.com/olivere/elastic/v7/ingest_delete_pipeline.go new file mode 100644 index 0000000000..fed06d6070 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/ingest_delete_pipeline.go @@ -0,0 +1,181 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IngestDeletePipelineService deletes pipelines by ID. +// It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/delete-pipeline-api.html. +type IngestDeletePipelineService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + masterTimeout string + timeout string +} + +// NewIngestDeletePipelineService creates a new IngestDeletePipelineService. +func NewIngestDeletePipelineService(client *Client) *IngestDeletePipelineService { + return &IngestDeletePipelineService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IngestDeletePipelineService) Pretty(pretty bool) *IngestDeletePipelineService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IngestDeletePipelineService) Human(human bool) *IngestDeletePipelineService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IngestDeletePipelineService) ErrorTrace(errorTrace bool) *IngestDeletePipelineService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IngestDeletePipelineService) FilterPath(filterPath ...string) *IngestDeletePipelineService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IngestDeletePipelineService) Header(name string, value string) *IngestDeletePipelineService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IngestDeletePipelineService) Headers(headers http.Header) *IngestDeletePipelineService { + s.headers = headers + return s +} + +// Id is documented as: Pipeline ID. +func (s *IngestDeletePipelineService) Id(id string) *IngestDeletePipelineService { + s.id = id + return s +} + +// MasterTimeout is documented as: Explicit operation timeout for connection to master node. +func (s *IngestDeletePipelineService) MasterTimeout(masterTimeout string) *IngestDeletePipelineService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout is documented as: Explicit operation timeout. +func (s *IngestDeletePipelineService) Timeout(timeout string) *IngestDeletePipelineService { + s.timeout = timeout + return s +} + +// buildURL builds the URL for the operation. +func (s *IngestDeletePipelineService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_ingest/pipeline/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IngestDeletePipelineService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IngestDeletePipelineService) Do(ctx context.Context) (*IngestDeletePipelineResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IngestDeletePipelineResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IngestDeletePipelineResponse is the response of IngestDeletePipelineService.Do. +type IngestDeletePipelineResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/ingest_get_pipeline.go b/vendor/github.com/olivere/elastic/v7/ingest_get_pipeline.go new file mode 100644 index 0000000000..5f7e0279ce --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/ingest_get_pipeline.go @@ -0,0 +1,177 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IngestGetPipelineService returns pipelines based on ID. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/get-pipeline-api.html +// for documentation. +type IngestGetPipelineService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id []string + masterTimeout string +} + +// NewIngestGetPipelineService creates a new IngestGetPipelineService. +func NewIngestGetPipelineService(client *Client) *IngestGetPipelineService { + return &IngestGetPipelineService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IngestGetPipelineService) Pretty(pretty bool) *IngestGetPipelineService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IngestGetPipelineService) Human(human bool) *IngestGetPipelineService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IngestGetPipelineService) ErrorTrace(errorTrace bool) *IngestGetPipelineService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IngestGetPipelineService) FilterPath(filterPath ...string) *IngestGetPipelineService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IngestGetPipelineService) Header(name string, value string) *IngestGetPipelineService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IngestGetPipelineService) Headers(headers http.Header) *IngestGetPipelineService { + s.headers = headers + return s +} + +// Id is a list of pipeline ids. Wildcards supported. +func (s *IngestGetPipelineService) Id(id ...string) *IngestGetPipelineService { + s.id = append(s.id, id...) + return s +} + +// MasterTimeout is an explicit operation timeout for connection to master node. +func (s *IngestGetPipelineService) MasterTimeout(masterTimeout string) *IngestGetPipelineService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *IngestGetPipelineService) buildURL() (string, url.Values, error) { + var err error + var path string + + // Build URL + if len(s.id) > 0 { + path, err = uritemplates.Expand("/_ingest/pipeline/{id}", map[string]string{ + "id": strings.Join(s.id, ","), + }) + } else { + path = "/_ingest/pipeline" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IngestGetPipelineService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *IngestGetPipelineService) Do(ctx context.Context) (IngestGetPipelineResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret IngestGetPipelineResponse + if err := json.Unmarshal(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// IngestGetPipelineResponse is the response of IngestGetPipelineService.Do. +type IngestGetPipelineResponse map[string]*IngestGetPipeline + +// IngestGetPipeline describes a specific ingest pipeline, its +// processors etc. +type IngestGetPipeline struct { + Description string `json:"description"` + Processors []map[string]interface{} `json:"processors"` + Version int64 `json:"version,omitempty"` + OnFailure []map[string]interface{} `json:"on_failure,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/ingest_put_pipeline.go b/vendor/github.com/olivere/elastic/v7/ingest_put_pipeline.go new file mode 100644 index 0000000000..5f780f6bd7 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/ingest_put_pipeline.go @@ -0,0 +1,210 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IngestPutPipelineService adds pipelines and updates existing pipelines in +// the cluster. +// +// It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/put-pipeline-api.html. +type IngestPutPipelineService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + masterTimeout string + timeout string + bodyJson interface{} + bodyString string +} + +// NewIngestPutPipelineService creates a new IngestPutPipelineService. +func NewIngestPutPipelineService(client *Client) *IngestPutPipelineService { + return &IngestPutPipelineService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IngestPutPipelineService) Pretty(pretty bool) *IngestPutPipelineService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IngestPutPipelineService) Human(human bool) *IngestPutPipelineService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IngestPutPipelineService) ErrorTrace(errorTrace bool) *IngestPutPipelineService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IngestPutPipelineService) FilterPath(filterPath ...string) *IngestPutPipelineService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IngestPutPipelineService) Header(name string, value string) *IngestPutPipelineService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IngestPutPipelineService) Headers(headers http.Header) *IngestPutPipelineService { + s.headers = headers + return s +} + +// Id is the pipeline ID. +func (s *IngestPutPipelineService) Id(id string) *IngestPutPipelineService { + s.id = id + return s +} + +// MasterTimeout is an explicit operation timeout for connection to master node. +func (s *IngestPutPipelineService) MasterTimeout(masterTimeout string) *IngestPutPipelineService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout specifies an explicit operation timeout. +func (s *IngestPutPipelineService) Timeout(timeout string) *IngestPutPipelineService { + s.timeout = timeout + return s +} + +// BodyJson is the ingest definition, defined as a JSON-serializable document. +// Use e.g. a map[string]interface{} here. +func (s *IngestPutPipelineService) BodyJson(body interface{}) *IngestPutPipelineService { + s.bodyJson = body + return s +} + +// BodyString is the ingest definition, specified as a string. +func (s *IngestPutPipelineService) BodyString(body string) *IngestPutPipelineService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IngestPutPipelineService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_ingest/pipeline/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IngestPutPipelineService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IngestPutPipelineService) Do(ctx context.Context) (*IngestPutPipelineResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IngestPutPipelineResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IngestPutPipelineResponse is the response of IngestPutPipelineService.Do. +type IngestPutPipelineResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/ingest_simulate_pipeline.go b/vendor/github.com/olivere/elastic/v7/ingest_simulate_pipeline.go new file mode 100644 index 0000000000..70de000d0c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/ingest_simulate_pipeline.go @@ -0,0 +1,213 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// IngestSimulatePipelineService executes a specific pipeline against the set of +// documents provided in the body of the request. +// +// The API is documented at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/simulate-pipeline-api.html. +type IngestSimulatePipelineService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + verbose *bool + bodyJson interface{} + bodyString string +} + +// NewIngestSimulatePipelineService creates a new IngestSimulatePipeline. +func NewIngestSimulatePipelineService(client *Client) *IngestSimulatePipelineService { + return &IngestSimulatePipelineService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *IngestSimulatePipelineService) Pretty(pretty bool) *IngestSimulatePipelineService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *IngestSimulatePipelineService) Human(human bool) *IngestSimulatePipelineService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *IngestSimulatePipelineService) ErrorTrace(errorTrace bool) *IngestSimulatePipelineService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *IngestSimulatePipelineService) FilterPath(filterPath ...string) *IngestSimulatePipelineService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *IngestSimulatePipelineService) Header(name string, value string) *IngestSimulatePipelineService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *IngestSimulatePipelineService) Headers(headers http.Header) *IngestSimulatePipelineService { + s.headers = headers + return s +} + +// Id specifies the pipeline ID. +func (s *IngestSimulatePipelineService) Id(id string) *IngestSimulatePipelineService { + s.id = id + return s +} + +// Verbose mode. Display data output for each processor in executed pipeline. +func (s *IngestSimulatePipelineService) Verbose(verbose bool) *IngestSimulatePipelineService { + s.verbose = &verbose + return s +} + +// BodyJson is the ingest definition, defined as a JSON-serializable simulate +// definition. Use e.g. a map[string]interface{} here. +func (s *IngestSimulatePipelineService) BodyJson(body interface{}) *IngestSimulatePipelineService { + s.bodyJson = body + return s +} + +// BodyString is the simulate definition, defined as a string. +func (s *IngestSimulatePipelineService) BodyString(body string) *IngestSimulatePipelineService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *IngestSimulatePipelineService) buildURL() (string, url.Values, error) { + var err error + var path string + + // Build URL + if s.id != "" { + path, err = uritemplates.Expand("/_ingest/pipeline/{id}/_simulate", map[string]string{ + "id": s.id, + }) + } else { + path = "/_ingest/pipeline/_simulate" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.verbose; v != nil { + params.Set("verbose", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *IngestSimulatePipelineService) Validate() error { + var invalid []string + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *IngestSimulatePipelineService) Do(ctx context.Context) (*IngestSimulatePipelineResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(IngestSimulatePipelineResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// IngestSimulatePipelineResponse is the response of IngestSimulatePipeline.Do. +type IngestSimulatePipelineResponse struct { + Docs []*IngestSimulateDocumentResult `json:"docs"` +} + +type IngestSimulateDocumentResult struct { + Doc map[string]interface{} `json:"doc"` + ProcessorResults []*IngestSimulateProcessorResult `json:"processor_results"` +} + +type IngestSimulateProcessorResult struct { + ProcessorTag string `json:"tag"` + Doc map[string]interface{} `json:"doc"` +} diff --git a/vendor/github.com/olivere/elastic/v7/inner_hit.go b/vendor/github.com/olivere/elastic/v7/inner_hit.go new file mode 100644 index 0000000000..803662646c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/inner_hit.go @@ -0,0 +1,170 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// InnerHit implements a simple join for parent/child, nested, and even +// top-level documents in Elasticsearch. +// It is an experimental feature for Elasticsearch versions 1.5 (or greater). +// See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-inner-hits.html +// for documentation. +// +// See the tests for SearchSource, HasChildFilter, HasChildQuery, +// HasParentFilter, HasParentQuery, NestedFilter, and NestedQuery +// for usage examples. +type InnerHit struct { + source *SearchSource + path string + typ string + + name string +} + +// NewInnerHit creates a new InnerHit. +func NewInnerHit() *InnerHit { + return &InnerHit{source: NewSearchSource()} +} + +func (hit *InnerHit) Path(path string) *InnerHit { + hit.path = path + return hit +} + +func (hit *InnerHit) Type(typ string) *InnerHit { + hit.typ = typ + return hit +} + +func (hit *InnerHit) Query(query Query) *InnerHit { + hit.source.Query(query) + return hit +} + +func (hit *InnerHit) From(from int) *InnerHit { + hit.source.From(from) + return hit +} + +func (hit *InnerHit) Size(size int) *InnerHit { + hit.source.Size(size) + return hit +} + +func (hit *InnerHit) TrackScores(trackScores bool) *InnerHit { + hit.source.TrackScores(trackScores) + return hit +} + +func (hit *InnerHit) Explain(explain bool) *InnerHit { + hit.source.Explain(explain) + return hit +} + +func (hit *InnerHit) Version(version bool) *InnerHit { + hit.source.Version(version) + return hit +} + +func (hit *InnerHit) StoredField(storedFieldName string) *InnerHit { + hit.source.StoredField(storedFieldName) + return hit +} + +func (hit *InnerHit) StoredFields(storedFieldNames ...string) *InnerHit { + hit.source.StoredFields(storedFieldNames...) + return hit +} + +func (hit *InnerHit) NoStoredFields() *InnerHit { + hit.source.NoStoredFields() + return hit +} + +func (hit *InnerHit) FetchSource(fetchSource bool) *InnerHit { + hit.source.FetchSource(fetchSource) + return hit +} + +func (hit *InnerHit) FetchSourceContext(fetchSourceContext *FetchSourceContext) *InnerHit { + hit.source.FetchSourceContext(fetchSourceContext) + return hit +} + +func (hit *InnerHit) DocvalueFields(docvalueFields ...string) *InnerHit { + hit.source.DocvalueFields(docvalueFields...) + return hit +} + +func (hit *InnerHit) DocvalueFieldsWithFormat(docvalueFields ...DocvalueField) *InnerHit { + hit.source.DocvalueFieldsWithFormat(docvalueFields...) + return hit +} + +func (hit *InnerHit) DocvalueField(docvalueField string) *InnerHit { + hit.source.DocvalueField(docvalueField) + return hit +} + +func (hit *InnerHit) DocvalueFieldWithFormat(docvalueField DocvalueField) *InnerHit { + hit.source.DocvalueFieldWithFormat(docvalueField) + return hit +} + +func (hit *InnerHit) ScriptFields(scriptFields ...*ScriptField) *InnerHit { + hit.source.ScriptFields(scriptFields...) + return hit +} + +func (hit *InnerHit) ScriptField(scriptField *ScriptField) *InnerHit { + hit.source.ScriptField(scriptField) + return hit +} + +func (hit *InnerHit) Sort(field string, ascending bool) *InnerHit { + hit.source.Sort(field, ascending) + return hit +} + +func (hit *InnerHit) SortWithInfo(info SortInfo) *InnerHit { + hit.source.SortWithInfo(info) + return hit +} + +func (hit *InnerHit) SortBy(sorter ...Sorter) *InnerHit { + hit.source.SortBy(sorter...) + return hit +} + +func (hit *InnerHit) Highlight(highlight *Highlight) *InnerHit { + hit.source.Highlight(highlight) + return hit +} + +func (hit *InnerHit) Highlighter() *Highlight { + return hit.source.Highlighter() +} + +func (hit *InnerHit) Name(name string) *InnerHit { + hit.name = name + return hit +} + +func (hit *InnerHit) Source() (interface{}, error) { + src, err := hit.source.Source() + if err != nil { + return nil, err + } + source, ok := src.(map[string]interface{}) + if !ok { + return nil, nil + } + + // Notice that hit.typ and hit.path are not exported here. + // They are only used with SearchSource and serialized there. + + if hit.name != "" { + source["name"] = hit.name + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/logger.go b/vendor/github.com/olivere/elastic/v7/logger.go new file mode 100644 index 0000000000..095eb4cd4e --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/logger.go @@ -0,0 +1,10 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// Logger specifies the interface for all log operations. +type Logger interface { + Printf(format string, v ...interface{}) +} diff --git a/vendor/github.com/olivere/elastic/v7/mget.go b/vendor/github.com/olivere/elastic/v7/mget.go new file mode 100644 index 0000000000..7e30af5f67 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/mget.go @@ -0,0 +1,314 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" +) + +// MgetService allows to get multiple documents based on an index, +// type (optional) and id (possibly routing). The response includes +// a docs array with all the fetched documents, each element similar +// in structure to a document provided by the Get API. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-multi-get.html +// for details. +type MgetService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + preference string + realtime *bool + refresh string + routing string + storedFields []string + items []*MultiGetItem +} + +// NewMgetService initializes a new Multi GET API request call. +func NewMgetService(client *Client) *MgetService { + builder := &MgetService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *MgetService) Pretty(pretty bool) *MgetService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *MgetService) Human(human bool) *MgetService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *MgetService) ErrorTrace(errorTrace bool) *MgetService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *MgetService) FilterPath(filterPath ...string) *MgetService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *MgetService) Header(name string, value string) *MgetService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *MgetService) Headers(headers http.Header) *MgetService { + s.headers = headers + return s +} + +// Preference specifies the node or shard the operation should be performed +// on (default: random). +func (s *MgetService) Preference(preference string) *MgetService { + s.preference = preference + return s +} + +// Refresh the shard containing the document before performing the operation. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *MgetService) Refresh(refresh string) *MgetService { + s.refresh = refresh + return s +} + +// Realtime specifies whether to perform the operation in realtime or search mode. +func (s *MgetService) Realtime(realtime bool) *MgetService { + s.realtime = &realtime + return s +} + +// Routing is the specific routing value. +func (s *MgetService) Routing(routing string) *MgetService { + s.routing = routing + return s +} + +// StoredFields is a list of fields to return in the response. +func (s *MgetService) StoredFields(storedFields ...string) *MgetService { + s.storedFields = append(s.storedFields, storedFields...) + return s +} + +// Add an item to the request. +func (s *MgetService) Add(items ...*MultiGetItem) *MgetService { + s.items = append(s.items, items...) + return s +} + +// Source returns the request body, which will be serialized into JSON. +func (s *MgetService) Source() (interface{}, error) { + source := make(map[string]interface{}) + items := make([]interface{}, len(s.items)) + for i, item := range s.items { + src, err := item.Source() + if err != nil { + return nil, err + } + items[i] = src + } + source["docs"] = items + return source, nil +} + +// Do executes the request. +func (s *MgetService) Do(ctx context.Context) (*MgetResponse, error) { + // Build url + path := "/_mget" + + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.realtime != nil { + params.Add("realtime", fmt.Sprintf("%v", *s.realtime)) + } + if s.preference != "" { + params.Add("preference", s.preference) + } + if s.refresh != "" { + params.Add("refresh", s.refresh) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if len(s.storedFields) > 0 { + params.Set("stored_fields", strings.Join(s.storedFields, ",")) + } + + // Set body + body, err := s.Source() + if err != nil { + return nil, err + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return result + ret := new(MgetResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Multi Get Item -- + +// MultiGetItem is a single document to retrieve via the MgetService. +type MultiGetItem struct { + index string + typ string + id string + routing string + storedFields []string + version *int64 // see org.elasticsearch.common.lucene.uid.Versions + versionType string // see org.elasticsearch.index.VersionType + fsc *FetchSourceContext +} + +// NewMultiGetItem initializes a new, single item for a Multi GET request. +func NewMultiGetItem() *MultiGetItem { + return &MultiGetItem{} +} + +// Index specifies the index name. +func (item *MultiGetItem) Index(index string) *MultiGetItem { + item.index = index + return item +} + +// Type specifies the type name. +func (item *MultiGetItem) Type(typ string) *MultiGetItem { + item.typ = typ + return item +} + +// Id specifies the identifier of the document. +func (item *MultiGetItem) Id(id string) *MultiGetItem { + item.id = id + return item +} + +// Routing is the specific routing value. +func (item *MultiGetItem) Routing(routing string) *MultiGetItem { + item.routing = routing + return item +} + +// StoredFields is a list of fields to return in the response. +func (item *MultiGetItem) StoredFields(storedFields ...string) *MultiGetItem { + item.storedFields = append(item.storedFields, storedFields...) + return item +} + +// Version can be MatchAny (-3), MatchAnyPre120 (0), NotFound (-1), +// or NotSet (-2). These are specified in org.elasticsearch.common.lucene.uid.Versions. +// The default in Elasticsearch is MatchAny (-3). +func (item *MultiGetItem) Version(version int64) *MultiGetItem { + item.version = &version + return item +} + +// VersionType can be "internal", "external", "external_gt", or "external_gte". +// See org.elasticsearch.index.VersionType in Elasticsearch source. +// It is "internal" by default. +func (item *MultiGetItem) VersionType(versionType string) *MultiGetItem { + item.versionType = versionType + return item +} + +// FetchSource allows to specify source filtering. +func (item *MultiGetItem) FetchSource(fetchSourceContext *FetchSourceContext) *MultiGetItem { + item.fsc = fetchSourceContext + return item +} + +// Source returns the serialized JSON to be sent to Elasticsearch as +// part of a MultiGet search. +func (item *MultiGetItem) Source() (interface{}, error) { + source := make(map[string]interface{}) + + source["_id"] = item.id + + if item.index != "" { + source["_index"] = item.index + } + if item.typ != "" { + source["_type"] = item.typ + } + if item.fsc != nil { + src, err := item.fsc.Source() + if err != nil { + return nil, err + } + source["_source"] = src + } + if item.routing != "" { + source["_routing"] = item.routing + } + if len(item.storedFields) > 0 { + source["stored_fields"] = strings.Join(item.storedFields, ",") + } + if item.version != nil { + source["version"] = fmt.Sprintf("%d", *item.version) + } + if item.versionType != "" { + source["version_type"] = item.versionType + } + + return source, nil +} + +// -- Result of a Multi Get request. + +// MgetResponse is the outcome of a Multi GET API request. +type MgetResponse struct { + Docs []*GetResult `json:"docs,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/msearch.go b/vendor/github.com/olivere/elastic/v7/msearch.go new file mode 100644 index 0000000000..1f6aed0e55 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/msearch.go @@ -0,0 +1,169 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" +) + +// MultiSearch executes one or more searches in one roundtrip. +type MultiSearchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + requests []*SearchRequest + indices []string + maxConcurrentRequests *int + preFilterShardSize *int +} + +func NewMultiSearchService(client *Client) *MultiSearchService { + builder := &MultiSearchService{ + client: client, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *MultiSearchService) Pretty(pretty bool) *MultiSearchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *MultiSearchService) Human(human bool) *MultiSearchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *MultiSearchService) ErrorTrace(errorTrace bool) *MultiSearchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *MultiSearchService) FilterPath(filterPath ...string) *MultiSearchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *MultiSearchService) Header(name string, value string) *MultiSearchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *MultiSearchService) Headers(headers http.Header) *MultiSearchService { + s.headers = headers + return s +} + +func (s *MultiSearchService) Add(requests ...*SearchRequest) *MultiSearchService { + s.requests = append(s.requests, requests...) + return s +} + +func (s *MultiSearchService) Index(indices ...string) *MultiSearchService { + s.indices = append(s.indices, indices...) + return s +} + +func (s *MultiSearchService) MaxConcurrentSearches(max int) *MultiSearchService { + s.maxConcurrentRequests = &max + return s +} + +func (s *MultiSearchService) PreFilterShardSize(size int) *MultiSearchService { + s.preFilterShardSize = &size + return s +} + +func (s *MultiSearchService) Do(ctx context.Context) (*MultiSearchResult, error) { + // Build url + path := "/_msearch" + + // Parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.maxConcurrentRequests; v != nil { + params.Set("max_concurrent_searches", fmt.Sprintf("%v", *v)) + } + if v := s.preFilterShardSize; v != nil { + params.Set("pre_filter_shard_size", fmt.Sprintf("%v", *v)) + } + + // Set body + var lines []string + for _, sr := range s.requests { + // Set default indices if not specified in the request + if !sr.HasIndices() && len(s.indices) > 0 { + sr = sr.Index(s.indices...) + } + + header, err := json.Marshal(sr.header()) + if err != nil { + return nil, err + } + body, err := sr.Body() + if err != nil { + return nil, err + } + lines = append(lines, string(header)) + lines = append(lines, body) + } + body := strings.Join(lines, "\n") + "\n" // add trailing \n + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return result + ret := new(MultiSearchResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// MultiSearchResult is the outcome of running a multi-search operation. +type MultiSearchResult struct { + TookInMillis int64 `json:"took,omitempty"` // search time in milliseconds + Responses []*SearchResult `json:"responses,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/mtermvectors.go b/vendor/github.com/olivere/elastic/v7/mtermvectors.go new file mode 100644 index 0000000000..59284e60ad --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/mtermvectors.go @@ -0,0 +1,526 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// MultiTermvectorService returns information and statistics on terms in the +// fields of a particular document. The document could be stored in the +// index or artificially provided by the user. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-multi-termvectors.html +// for documentation. +type MultiTermvectorService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + typ string + fieldStatistics *bool + fields []string + ids []string + offsets *bool + parent string + payloads *bool + positions *bool + preference string + realtime *bool + routing string + termStatistics *bool + version interface{} + versionType string + bodyJson interface{} + bodyString string + docs []*MultiTermvectorItem +} + +// NewMultiTermvectorService creates a new MultiTermvectorService. +func NewMultiTermvectorService(client *Client) *MultiTermvectorService { + return &MultiTermvectorService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *MultiTermvectorService) Pretty(pretty bool) *MultiTermvectorService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *MultiTermvectorService) Human(human bool) *MultiTermvectorService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *MultiTermvectorService) ErrorTrace(errorTrace bool) *MultiTermvectorService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *MultiTermvectorService) FilterPath(filterPath ...string) *MultiTermvectorService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *MultiTermvectorService) Header(name string, value string) *MultiTermvectorService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *MultiTermvectorService) Headers(headers http.Header) *MultiTermvectorService { + s.headers = headers + return s +} + +// Add adds documents to MultiTermvectors service. +func (s *MultiTermvectorService) Add(docs ...*MultiTermvectorItem) *MultiTermvectorService { + s.docs = append(s.docs, docs...) + return s +} + +// Index in which the document resides. +func (s *MultiTermvectorService) Index(index string) *MultiTermvectorService { + s.index = index + return s +} + +// Type of the document. +func (s *MultiTermvectorService) Type(typ string) *MultiTermvectorService { + s.typ = typ + return s +} + +// FieldStatistics specifies if document count, sum of document frequencies and sum of total term frequencies should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) FieldStatistics(fieldStatistics bool) *MultiTermvectorService { + s.fieldStatistics = &fieldStatistics + return s +} + +// Fields is a comma-separated list of fields to return. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Fields(fields []string) *MultiTermvectorService { + s.fields = fields + return s +} + +// Ids is a comma-separated list of documents ids. You must define ids as parameter or set "ids" or "docs" in the request body. +func (s *MultiTermvectorService) Ids(ids []string) *MultiTermvectorService { + s.ids = ids + return s +} + +// Offsets specifies if term offsets should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Offsets(offsets bool) *MultiTermvectorService { + s.offsets = &offsets + return s +} + +// Parent id of documents. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Parent(parent string) *MultiTermvectorService { + s.parent = parent + return s +} + +// Payloads specifies if term payloads should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Payloads(payloads bool) *MultiTermvectorService { + s.payloads = &payloads + return s +} + +// Positions specifies if term positions should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Positions(positions bool) *MultiTermvectorService { + s.positions = &positions + return s +} + +// Preference specifies the node or shard the operation should be performed on (default: random). Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Preference(preference string) *MultiTermvectorService { + s.preference = preference + return s +} + +// Realtime specifies if requests are real-time as opposed to near-real-time (default: true). +func (s *MultiTermvectorService) Realtime(realtime bool) *MultiTermvectorService { + s.realtime = &realtime + return s +} + +// Routing specific routing value. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) Routing(routing string) *MultiTermvectorService { + s.routing = routing + return s +} + +// TermStatistics specifies if total term frequency and document frequency should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs". +func (s *MultiTermvectorService) TermStatistics(termStatistics bool) *MultiTermvectorService { + s.termStatistics = &termStatistics + return s +} + +// Version is explicit version number for concurrency control. +func (s *MultiTermvectorService) Version(version interface{}) *MultiTermvectorService { + s.version = version + return s +} + +// VersionType is specific version type. +func (s *MultiTermvectorService) VersionType(versionType string) *MultiTermvectorService { + s.versionType = versionType + return s +} + +// BodyJson is documented as: Define ids, documents, parameters or a list of parameters per document here. You must at least provide a list of document ids. See documentation.. +func (s *MultiTermvectorService) BodyJson(body interface{}) *MultiTermvectorService { + s.bodyJson = body + return s +} + +// BodyString is documented as: Define ids, documents, parameters or a list of parameters per document here. You must at least provide a list of document ids. See documentation.. +func (s *MultiTermvectorService) BodyString(body string) *MultiTermvectorService { + s.bodyString = body + return s +} + +func (s *MultiTermvectorService) Source() interface{} { + source := make(map[string]interface{}) + docs := make([]interface{}, len(s.docs)) + for i, doc := range s.docs { + docs[i] = doc.Source() + } + source["docs"] = docs + return source +} + +// buildURL builds the URL for the operation. +func (s *MultiTermvectorService) buildURL() (string, url.Values, error) { + var path string + var err error + + if s.index != "" && s.typ != "" { + path, err = uritemplates.Expand("/{index}/{type}/_mtermvectors", map[string]string{ + "index": s.index, + "type": s.typ, + }) + } else if s.index != "" && s.typ == "" { + path, err = uritemplates.Expand("/{index}/_mtermvectors", map[string]string{ + "index": s.index, + }) + } else { + path = "/_mtermvectors" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.fieldStatistics != nil { + params.Set("field_statistics", fmt.Sprintf("%v", *s.fieldStatistics)) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + if len(s.ids) > 0 { + params.Set("ids", strings.Join(s.ids, ",")) + } + if s.offsets != nil { + params.Set("offsets", fmt.Sprintf("%v", *s.offsets)) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if s.payloads != nil { + params.Set("payloads", fmt.Sprintf("%v", *s.payloads)) + } + if s.positions != nil { + params.Set("positions", fmt.Sprintf("%v", *s.positions)) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if s.realtime != nil { + params.Set("realtime", fmt.Sprintf("%v", *s.realtime)) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.termStatistics != nil { + params.Set("term_statistics", fmt.Sprintf("%v", *s.termStatistics)) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%v", s.version)) + } + if s.versionType != "" { + params.Set("version_type", s.versionType) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *MultiTermvectorService) Validate() error { + var invalid []string + if s.index == "" && s.typ != "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *MultiTermvectorService) Do(ctx context.Context) (*MultiTermvectorResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else if len(s.bodyString) > 0 { + body = s.bodyString + } else { + body = s.Source() + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(MultiTermvectorResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// MultiTermvectorResponse is the response of MultiTermvectorService.Do. +type MultiTermvectorResponse struct { + Docs []*TermvectorsResponse `json:"docs"` +} + +// -- MultiTermvectorItem -- + +// MultiTermvectorItem is a single document to retrieve via MultiTermvectorService. +type MultiTermvectorItem struct { + index string + typ string + id string + doc interface{} + fieldStatistics *bool + fields []string + perFieldAnalyzer map[string]string + offsets *bool + parent string + payloads *bool + positions *bool + preference string + realtime *bool + routing string + termStatistics *bool +} + +func NewMultiTermvectorItem() *MultiTermvectorItem { + return &MultiTermvectorItem{} +} + +func (s *MultiTermvectorItem) Index(index string) *MultiTermvectorItem { + s.index = index + return s +} + +func (s *MultiTermvectorItem) Type(typ string) *MultiTermvectorItem { + s.typ = typ + return s +} + +func (s *MultiTermvectorItem) Id(id string) *MultiTermvectorItem { + s.id = id + return s +} + +// Doc is the document to analyze. +func (s *MultiTermvectorItem) Doc(doc interface{}) *MultiTermvectorItem { + s.doc = doc + return s +} + +// FieldStatistics specifies if document count, sum of document frequencies +// and sum of total term frequencies should be returned. +func (s *MultiTermvectorItem) FieldStatistics(fieldStatistics bool) *MultiTermvectorItem { + s.fieldStatistics = &fieldStatistics + return s +} + +// Fields a list of fields to return. +func (s *MultiTermvectorItem) Fields(fields ...string) *MultiTermvectorItem { + if s.fields == nil { + s.fields = make([]string, 0) + } + s.fields = append(s.fields, fields...) + return s +} + +// PerFieldAnalyzer allows to specify a different analyzer than the one +// at the field. +func (s *MultiTermvectorItem) PerFieldAnalyzer(perFieldAnalyzer map[string]string) *MultiTermvectorItem { + s.perFieldAnalyzer = perFieldAnalyzer + return s +} + +// Offsets specifies if term offsets should be returned. +func (s *MultiTermvectorItem) Offsets(offsets bool) *MultiTermvectorItem { + s.offsets = &offsets + return s +} + +// Parent id of documents. +func (s *MultiTermvectorItem) Parent(parent string) *MultiTermvectorItem { + s.parent = parent + return s +} + +// Payloads specifies if term payloads should be returned. +func (s *MultiTermvectorItem) Payloads(payloads bool) *MultiTermvectorItem { + s.payloads = &payloads + return s +} + +// Positions specifies if term positions should be returned. +func (s *MultiTermvectorItem) Positions(positions bool) *MultiTermvectorItem { + s.positions = &positions + return s +} + +// Preference specify the node or shard the operation +// should be performed on (default: random). +func (s *MultiTermvectorItem) Preference(preference string) *MultiTermvectorItem { + s.preference = preference + return s +} + +// Realtime specifies if request is real-time as opposed to +// near-real-time (default: true). +func (s *MultiTermvectorItem) Realtime(realtime bool) *MultiTermvectorItem { + s.realtime = &realtime + return s +} + +// Routing is a specific routing value. +func (s *MultiTermvectorItem) Routing(routing string) *MultiTermvectorItem { + s.routing = routing + return s +} + +// TermStatistics specifies if total term frequency and document frequency +// should be returned. +func (s *MultiTermvectorItem) TermStatistics(termStatistics bool) *MultiTermvectorItem { + s.termStatistics = &termStatistics + return s +} + +// Source returns the serialized JSON to be sent to Elasticsearch as +// part of a MultiTermvector. +func (s *MultiTermvectorItem) Source() interface{} { + source := make(map[string]interface{}) + + source["_id"] = s.id + + if s.index != "" { + source["_index"] = s.index + } + if s.typ != "" { + source["_type"] = s.typ + } + if s.fields != nil { + source["fields"] = s.fields + } + if s.fieldStatistics != nil { + source["field_statistics"] = fmt.Sprintf("%v", *s.fieldStatistics) + } + if s.offsets != nil { + source["offsets"] = s.offsets + } + if s.parent != "" { + source["parent"] = s.parent + } + if s.payloads != nil { + source["payloads"] = fmt.Sprintf("%v", *s.payloads) + } + if s.positions != nil { + source["positions"] = fmt.Sprintf("%v", *s.positions) + } + if s.preference != "" { + source["preference"] = s.preference + } + if s.realtime != nil { + source["realtime"] = fmt.Sprintf("%v", *s.realtime) + } + if s.routing != "" { + source["routing"] = s.routing + } + if s.termStatistics != nil { + source["term_statistics"] = fmt.Sprintf("%v", *s.termStatistics) + } + if s.doc != nil { + source["doc"] = s.doc + } + if s.perFieldAnalyzer != nil && len(s.perFieldAnalyzer) > 0 { + source["per_field_analyzer"] = s.perFieldAnalyzer + } + + return source +} diff --git a/vendor/github.com/olivere/elastic/v7/nodes_info.go b/vendor/github.com/olivere/elastic/v7/nodes_info.go new file mode 100644 index 0000000000..3037df4f62 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/nodes_info.go @@ -0,0 +1,419 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// NodesInfoService allows to retrieve one or more or all of the +// cluster nodes information. +// It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-nodes-info.html. +type NodesInfoService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + nodeId []string + metric []string + flatSettings *bool +} + +// NewNodesInfoService creates a new NodesInfoService. +func NewNodesInfoService(client *Client) *NodesInfoService { + return &NodesInfoService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *NodesInfoService) Pretty(pretty bool) *NodesInfoService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *NodesInfoService) Human(human bool) *NodesInfoService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *NodesInfoService) ErrorTrace(errorTrace bool) *NodesInfoService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *NodesInfoService) FilterPath(filterPath ...string) *NodesInfoService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *NodesInfoService) Header(name string, value string) *NodesInfoService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *NodesInfoService) Headers(headers http.Header) *NodesInfoService { + s.headers = headers + return s +} + +// NodeId is a list of node IDs or names to limit the returned information. +// Use "_local" to return information from the node you're connecting to, +// leave empty to get information from all nodes. +func (s *NodesInfoService) NodeId(nodeId ...string) *NodesInfoService { + s.nodeId = append(s.nodeId, nodeId...) + return s +} + +// Metric is a list of metrics you wish returned. Leave empty to return all. +// Valid metrics are: settings, os, process, jvm, thread_pool, network, +// transport, http, and plugins. +func (s *NodesInfoService) Metric(metric ...string) *NodesInfoService { + s.metric = append(s.metric, metric...) + return s +} + +// FlatSettings returns settings in flat format (default: false). +func (s *NodesInfoService) FlatSettings(flatSettings bool) *NodesInfoService { + s.flatSettings = &flatSettings + return s +} + +// buildURL builds the URL for the operation. +func (s *NodesInfoService) buildURL() (string, url.Values, error) { + var nodeId, metric string + + if len(s.nodeId) > 0 { + nodeId = strings.Join(s.nodeId, ",") + } else { + nodeId = "_all" + } + + if len(s.metric) > 0 { + metric = strings.Join(s.metric, ",") + } else { + metric = "_all" + } + + // Build URL + path, err := uritemplates.Expand("/_nodes/{node_id}/{metric}", map[string]string{ + "node_id": nodeId, + "metric": metric, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.flatSettings != nil { + params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *NodesInfoService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *NodesInfoService) Do(ctx context.Context) (*NodesInfoResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(NodesInfoResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// NodesInfoResponse is the response of NodesInfoService.Do. +type NodesInfoResponse struct { + ClusterName string `json:"cluster_name"` + Nodes map[string]*NodesInfoNode `json:"nodes"` +} + +// NodesInfoNode represents information about a node in the cluster. +type NodesInfoNode struct { + // Name of the node, e.g. "Mister Fear" + Name string `json:"name"` + // TransportAddress, e.g. "127.0.0.1:9300" + TransportAddress string `json:"transport_address"` + // Host is the host name, e.g. "macbookair" + Host string `json:"host"` + // IP is the IP address, e.g. "192.168.1.2" + IP string `json:"ip"` + // Version is the Elasticsearch version running on the node, e.g. "1.4.3" + Version string `json:"version"` + // BuildHash is the Elasticsearch build bash, e.g. "36a29a7" + BuildHash string `json:"build_hash"` + + // TotalIndexingBuffer represents the total heap allowed to be used to + // hold recently indexed documents before they must be written to disk. + TotalIndexingBuffer int64 `json:"total_indexing_buffer"` // e.g. 16gb + // TotalIndexingBufferInBytes is the same as TotalIndexingBuffer, but + // expressed in bytes. + TotalIndexingBufferInBytes string `json:"total_indexing_buffer_in_bytes"` + + // Roles of the node, e.g. [master, ingest, data] + Roles []string `json:"roles"` + + // Attributes of the node. + Attributes map[string]string `json:"attributes"` + + // Settings of the node, e.g. paths and pidfile. + Settings map[string]interface{} `json:"settings"` + + // OS information, e.g. CPU and memory. + OS *NodesInfoNodeOS `json:"os"` + + // Process information, e.g. max file descriptors. + Process *NodesInfoNodeProcess `json:"process"` + + // JVM information, e.g. VM version. + JVM *NodesInfoNodeJVM `json:"jvm"` + + // ThreadPool information. + ThreadPool *NodesInfoNodeThreadPool `json:"thread_pool"` + + // Network information. + Transport *NodesInfoNodeTransport `json:"transport"` + + // HTTP information. + HTTP *NodesInfoNodeHTTP `json:"http"` + + // Plugins information. + Plugins []*NodesInfoNodePlugin `json:"plugins"` + + // Modules information. + Modules []*NodesInfoNodeModule `json:"modules"` + + // Ingest information. + Ingest *NodesInfoNodeIngest `json:"ingest"` +} + +// HasRole returns true if the node fulfills the given role. +func (n *NodesInfoNode) HasRole(role string) bool { + for _, r := range n.Roles { + if r == role { + return true + } + } + return false +} + +// IsMaster returns true if the node is a master node. +func (n *NodesInfoNode) IsMaster() bool { + return n.HasRole("master") +} + +// IsData returns true if the node is a data node. +func (n *NodesInfoNode) IsData() bool { + return n.HasRole("data") +} + +// IsIngest returns true if the node is an ingest node. +func (n *NodesInfoNode) IsIngest() bool { + return n.HasRole("ingest") +} + +// NodesInfoNodeOS represents OS-specific details about a node. +type NodesInfoNodeOS struct { + RefreshInterval string `json:"refresh_interval"` // e.g. 1s + RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000 + Name string `json:"name"` // e.g. Linux + Arch string `json:"arch"` // e.g. amd64 + Version string `json:"version"` // e.g. 4.9.87-linuxkit-aufs + AvailableProcessors int `json:"available_processors"` // e.g. 4 + AllocatedProcessors int `json:"allocated_processors"` // e.g. 4 +} + +// NodesInfoNodeProcess represents process-related information. +type NodesInfoNodeProcess struct { + RefreshInterval string `json:"refresh_interval"` // e.g. 1s + RefreshIntervalInMillis int64 `json:"refresh_interval_in_millis"` // e.g. 1000 + ID int `json:"id"` // process id, e.g. 87079 + Mlockall bool `json:"mlockall"` // e.g. false +} + +// NodesInfoNodeJVM represents JVM-related information. +type NodesInfoNodeJVM struct { + PID int `json:"pid"` // process id, e.g. 87079 + Version string `json:"version"` // e.g. "1.8.0_161" + VMName string `json:"vm_name"` // e.g. "OpenJDK 64-Bit Server VM" + VMVersion string `json:"vm_version"` // e.g. "25.161-b14" + VMVendor string `json:"vm_vendor"` // e.g. "Oracle Corporation" + StartTime time.Time `json:"start_time"` // e.g. "2018-03-30T11:06:36.644Z" + StartTimeInMillis int64 `json:"start_time_in_millis"` // e.g. 1522407996644 + + // Mem information + Mem struct { + HeapInit string `json:"heap_init"` // e.g. "1gb" + HeapInitInBytes int `json:"heap_init_in_bytes"` // e.g. 1073741824 + HeapMax string `json:"heap_max"` // e.g. "1007.3mb" + HeapMaxInBytes int `json:"heap_max_in_bytes"` // e.g. 1056309248 + NonHeapInit string `json:"non_heap_init"` // e.g. "2.4mb" + NonHeapInitInBytes int `json:"non_heap_init_in_bytes"` // e.g. 2555904 + NonHeapMax string `json:"non_heap_max"` // e.g. "0b" + NonHeapMaxInBytes int `json:"non_heap_max_in_bytes"` // e.g. 0 + DirectMax string `json:"direct_max"` // e.g. "1007.3mb" + DirectMaxInBytes int `json:"direct_max_in_bytes"` // e.g. 1056309248 + } `json:"mem"` + + GCCollectors []string `json:"gc_collectors"` // e.g. ["ParNew", "ConcurrentMarkSweep"] + MemoryPools []string `json:"memory_pools"` // e.g. ["Code Cache", "Metaspace", "Compressed Class Space", "Par Eden Space", "Par Survivor Space", "CMS Old Gen"] + + // UsingCompressedOrdinaryObjectPointers should be a bool, but is a + // string in 6.2.3. We use an interface{} for now so that it won't break + // when this will be fixed in later versions of Elasticsearch. + UsingCompressedOrdinaryObjectPointers interface{} `json:"using_compressed_ordinary_object_pointers"` + + InputArguments []string `json:"input_arguments"` // e.g. ["-Xms1g", "-Xmx1g" ...] +} + +// NodesInfoNodeThreadPool represents information about the thread pool. +type NodesInfoNodeThreadPool struct { + ForceMerge *NodesInfoNodeThreadPoolSection `json:"force_merge"` + FetchShardStarted *NodesInfoNodeThreadPoolSection `json:"fetch_shard_started"` + Listener *NodesInfoNodeThreadPoolSection `json:"listener"` + Index *NodesInfoNodeThreadPoolSection `json:"index"` + Refresh *NodesInfoNodeThreadPoolSection `json:"refresh"` + Generic *NodesInfoNodeThreadPoolSection `json:"generic"` + Warmer *NodesInfoNodeThreadPoolSection `json:"warmer"` + Search *NodesInfoNodeThreadPoolSection `json:"search"` + Flush *NodesInfoNodeThreadPoolSection `json:"flush"` + FetchShardStore *NodesInfoNodeThreadPoolSection `json:"fetch_shard_store"` + Management *NodesInfoNodeThreadPoolSection `json:"management"` + Get *NodesInfoNodeThreadPoolSection `json:"get"` + Bulk *NodesInfoNodeThreadPoolSection `json:"bulk"` + Snapshot *NodesInfoNodeThreadPoolSection `json:"snapshot"` + + Percolate *NodesInfoNodeThreadPoolSection `json:"percolate"` // check + Bench *NodesInfoNodeThreadPoolSection `json:"bench"` // check + Suggest *NodesInfoNodeThreadPoolSection `json:"suggest"` // deprecated + Optimize *NodesInfoNodeThreadPoolSection `json:"optimize"` // deprecated + Merge *NodesInfoNodeThreadPoolSection `json:"merge"` // deprecated +} + +// NodesInfoNodeThreadPoolSection represents information about a certain +// type of thread pool, e.g. for indexing or searching. +type NodesInfoNodeThreadPoolSection struct { + Type string `json:"type"` // e.g. fixed, scaling, or fixed_auto_queue_size + Min int `json:"min"` // e.g. 4 + Max int `json:"max"` // e.g. 4 + KeepAlive string `json:"keep_alive"` // e.g. "5m" + QueueSize interface{} `json:"queue_size"` // e.g. "1k" or -1 +} + +// NodesInfoNodeTransport represents transport-related information. +type NodesInfoNodeTransport struct { + BoundAddress []string `json:"bound_address"` + PublishAddress string `json:"publish_address"` + Profiles map[string]*NodesInfoNodeTransportProfile `json:"profiles"` +} + +// NodesInfoNodeTransportProfile represents a transport profile. +type NodesInfoNodeTransportProfile struct { + BoundAddress []string `json:"bound_address"` + PublishAddress string `json:"publish_address"` +} + +// NodesInfoNodeHTTP represents HTTP-related information. +type NodesInfoNodeHTTP struct { + BoundAddress []string `json:"bound_address"` // e.g. ["127.0.0.1:9200", "[fe80::1]:9200", "[::1]:9200"] + PublishAddress string `json:"publish_address"` // e.g. "127.0.0.1:9300" + MaxContentLength string `json:"max_content_length"` // e.g. "100mb" + MaxContentLengthInBytes int64 `json:"max_content_length_in_bytes"` +} + +// NodesInfoNodePlugin represents information about a plugin. +type NodesInfoNodePlugin struct { + Name string `json:"name"` // e.g. "ingest-geoip" + Version string `json:"version"` // e.g. "6.2.3" + ElasticsearchVersion string `json:"elasticsearch_version"` + JavaVersion string `json:"java_version"` + Description string `json:"description"` // e.g. "Ingest processor ..." + Classname string `json:"classname"` // e.g. "org.elasticsearch.ingest.geoip.IngestGeoIpPlugin" + ExtendedPlugins []string `json:"extended_plugins"` + HasNativeController bool `json:"has_native_controller"` + RequiresKeystore bool `json:"requires_keystore"` +} + +// NodesInfoNodeModule represents information about a module. +type NodesInfoNodeModule struct { + Name string `json:"name"` // e.g. "ingest-geoip" + Version string `json:"version"` // e.g. "6.2.3" + ElasticsearchVersion string `json:"elasticsearch_version"` + JavaVersion string `json:"java_version"` + Description string `json:"description"` // e.g. "Ingest processor ..." + Classname string `json:"classname"` // e.g. "org.elasticsearch.ingest.geoip.IngestGeoIpPlugin" + ExtendedPlugins []string `json:"extended_plugins"` + HasNativeController bool `json:"has_native_controller"` + RequiresKeystore bool `json:"requires_keystore"` +} + +// NodesInfoNodeIngest represents information about the ingester. +type NodesInfoNodeIngest struct { + Processors []*NodesInfoNodeIngestProcessorInfo `json:"processors"` +} + +// NodesInfoNodeIngestProcessorInfo represents ingest processor info. +type NodesInfoNodeIngestProcessorInfo struct { + Type string `json:"type"` // e.g. append, convert, date etc. +} diff --git a/vendor/github.com/olivere/elastic/v7/nodes_stats.go b/vendor/github.com/olivere/elastic/v7/nodes_stats.go new file mode 100644 index 0000000000..a57651e3be --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/nodes_stats.go @@ -0,0 +1,747 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// NodesStatsService returns node statistics. +// See http://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-nodes-stats.html +// for details. +type NodesStatsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + metric []string + indexMetric []string + nodeId []string + completionFields []string + fielddataFields []string + fields []string + groups *bool + level string + timeout string + types []string +} + +// NewNodesStatsService creates a new NodesStatsService. +func NewNodesStatsService(client *Client) *NodesStatsService { + return &NodesStatsService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *NodesStatsService) Pretty(pretty bool) *NodesStatsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *NodesStatsService) Human(human bool) *NodesStatsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *NodesStatsService) ErrorTrace(errorTrace bool) *NodesStatsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *NodesStatsService) FilterPath(filterPath ...string) *NodesStatsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *NodesStatsService) Header(name string, value string) *NodesStatsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *NodesStatsService) Headers(headers http.Header) *NodesStatsService { + s.headers = headers + return s +} + +// Metric limits the information returned to the specified metrics. +func (s *NodesStatsService) Metric(metric ...string) *NodesStatsService { + s.metric = append(s.metric, metric...) + return s +} + +// IndexMetric limits the information returned for `indices` metric +// to the specific index metrics. Isn't used if `indices` (or `all`) +// metric isn't specified.. +func (s *NodesStatsService) IndexMetric(indexMetric ...string) *NodesStatsService { + s.indexMetric = append(s.indexMetric, indexMetric...) + return s +} + +// NodeId is a list of node IDs or names to limit the returned information; +// use `_local` to return information from the node you're connecting to, +// leave empty to get information from all nodes. +func (s *NodesStatsService) NodeId(nodeId ...string) *NodesStatsService { + s.nodeId = append(s.nodeId, nodeId...) + return s +} + +// CompletionFields is a list of fields for `fielddata` and `suggest` +// index metric (supports wildcards). +func (s *NodesStatsService) CompletionFields(completionFields ...string) *NodesStatsService { + s.completionFields = append(s.completionFields, completionFields...) + return s +} + +// FielddataFields is a list of fields for `fielddata` index metric (supports wildcards). +func (s *NodesStatsService) FielddataFields(fielddataFields ...string) *NodesStatsService { + s.fielddataFields = append(s.fielddataFields, fielddataFields...) + return s +} + +// Fields is a list of fields for `fielddata` and `completion` index metric (supports wildcards). +func (s *NodesStatsService) Fields(fields ...string) *NodesStatsService { + s.fields = append(s.fields, fields...) + return s +} + +// Groups is a list of search groups for `search` index metric. +func (s *NodesStatsService) Groups(groups bool) *NodesStatsService { + s.groups = &groups + return s +} + +// Level specifies whether to return indices stats aggregated at node, index or shard level. +func (s *NodesStatsService) Level(level string) *NodesStatsService { + s.level = level + return s +} + +// Timeout specifies an explicit operation timeout. +func (s *NodesStatsService) Timeout(timeout string) *NodesStatsService { + s.timeout = timeout + return s +} + +// Types a list of document types for the `indexing` index metric. +func (s *NodesStatsService) Types(types ...string) *NodesStatsService { + s.types = append(s.types, types...) + return s +} + +// buildURL builds the URL for the operation. +func (s *NodesStatsService) buildURL() (string, url.Values, error) { + var err error + var path string + + if len(s.nodeId) > 0 && len(s.metric) > 0 && len(s.indexMetric) > 0 { + path, err = uritemplates.Expand("/_nodes/{node_id}/stats/{metric}/{index_metric}", map[string]string{ + "index_metric": strings.Join(s.indexMetric, ","), + "node_id": strings.Join(s.nodeId, ","), + "metric": strings.Join(s.metric, ","), + }) + } else if len(s.nodeId) > 0 && len(s.metric) > 0 && len(s.indexMetric) == 0 { + path, err = uritemplates.Expand("/_nodes/{node_id}/stats/{metric}", map[string]string{ + "node_id": strings.Join(s.nodeId, ","), + "metric": strings.Join(s.metric, ","), + }) + } else if len(s.nodeId) > 0 && len(s.metric) == 0 && len(s.indexMetric) > 0 { + path, err = uritemplates.Expand("/_nodes/{node_id}/stats/_all/{index_metric}", map[string]string{ + "index_metric": strings.Join(s.indexMetric, ","), + "node_id": strings.Join(s.nodeId, ","), + }) + } else if len(s.nodeId) > 0 && len(s.metric) == 0 && len(s.indexMetric) == 0 { + path, err = uritemplates.Expand("/_nodes/{node_id}/stats", map[string]string{ + "node_id": strings.Join(s.nodeId, ","), + }) + } else if len(s.nodeId) == 0 && len(s.metric) > 0 && len(s.indexMetric) > 0 { + path, err = uritemplates.Expand("/_nodes/stats/{metric}/{index_metric}", map[string]string{ + "index_metric": strings.Join(s.indexMetric, ","), + "metric": strings.Join(s.metric, ","), + }) + } else if len(s.nodeId) == 0 && len(s.metric) > 0 && len(s.indexMetric) == 0 { + path, err = uritemplates.Expand("/_nodes/stats/{metric}", map[string]string{ + "metric": strings.Join(s.metric, ","), + }) + } else if len(s.nodeId) == 0 && len(s.metric) == 0 && len(s.indexMetric) > 0 { + path, err = uritemplates.Expand("/_nodes/stats/_all/{index_metric}", map[string]string{ + "index_metric": strings.Join(s.indexMetric, ","), + }) + } else { // if len(s.nodeId) == 0 && len(s.metric) == 0 && len(s.indexMetric) == 0 { + path = "/_nodes/stats" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.completionFields) > 0 { + params.Set("completion_fields", strings.Join(s.completionFields, ",")) + } + if len(s.fielddataFields) > 0 { + params.Set("fielddata_fields", strings.Join(s.fielddataFields, ",")) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + if s.groups != nil { + params.Set("groups", fmt.Sprintf("%v", *s.groups)) + } + if s.level != "" { + params.Set("level", s.level) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if len(s.types) > 0 { + params.Set("types", strings.Join(s.types, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *NodesStatsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *NodesStatsService) Do(ctx context.Context) (*NodesStatsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(NodesStatsResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// NodesStatsResponse is the response of NodesStatsService.Do. +type NodesStatsResponse struct { + ClusterName string `json:"cluster_name"` + Nodes map[string]*NodesStatsNode `json:"nodes"` +} + +type NodesStatsNode struct { + // Timestamp when these stats we're gathered. + Timestamp int64 `json:"timestamp"` + // Name of the node, e.g. "Mister Fear" + Name string `json:"name"` + // TransportAddress, e.g. "127.0.0.1:9300" + TransportAddress string `json:"transport_address"` + // Host is the host name, e.g. "macbookair" + Host string `json:"host"` + // IP is an IP address, e.g. "192.168.1.2" + IP string `json:"ip"` + // Roles is a list of the roles of the node, e.g. master, data, ingest. + Roles []string `json:"roles"` + + // Attributes of the node. + Attributes map[string]interface{} `json:"attributes"` + + // Indices returns index information. + Indices *NodesStatsIndex `json:"indices"` + + // OS information, e.g. CPU and memory. + OS *NodesStatsNodeOS `json:"os"` + + // Process information, e.g. max file descriptors. + Process *NodesStatsNodeProcess `json:"process"` + + // JVM information, e.g. VM version. + JVM *NodesStatsNodeJVM `json:"jvm"` + + // ThreadPool information. + ThreadPool map[string]*NodesStatsNodeThreadPool `json:"thread_pool"` + + // FS returns information about the filesystem. + FS *NodesStatsNodeFS `json:"fs"` + + // Network information. + Transport *NodesStatsNodeTransport `json:"transport"` + + // HTTP information. + HTTP *NodesStatsNodeHTTP `json:"http"` + + // Breaker contains information about circuit breakers. + Breaker map[string]*NodesStatsBreaker `json:"breakers"` + + // ScriptStats information. + ScriptStats *NodesStatsScriptStats `json:"script"` + + // Discovery information. + Discovery *NodesStatsDiscovery `json:"discovery"` + + // Ingest information + Ingest *NodesStatsIngest `json:"ingest"` +} + +type NodesStatsIndex struct { + Docs *NodesStatsDocsStats `json:"docs"` + Store *NodesStatsStoreStats `json:"store"` + Indexing *NodesStatsIndexingStats `json:"indexing"` + Get *NodesStatsGetStats `json:"get"` + Search *NodesStatsSearchStats `json:"search"` + Merges *NodesStatsMergeStats `json:"merges"` + Refresh *NodesStatsRefreshStats `json:"refresh"` + Flush *NodesStatsFlushStats `json:"flush"` + Warmer *NodesStatsWarmerStats `json:"warmer"` + QueryCache *NodesStatsQueryCacheStats `json:"query_cache"` + Fielddata *NodesStatsFielddataStats `json:"fielddata"` + Percolate *NodesStatsPercolateStats `json:"percolate"` + Completion *NodesStatsCompletionStats `json:"completion"` + Segments *NodesStatsSegmentsStats `json:"segments"` + Translog *NodesStatsTranslogStats `json:"translog"` + Suggest *NodesStatsSuggestStats `json:"suggest"` + RequestCache *NodesStatsRequestCacheStats `json:"request_cache"` + Recovery NodesStatsRecoveryStats `json:"recovery"` + + Indices map[string]*NodesStatsIndex `json:"indices"` // for level=indices + Shards map[string]*NodesStatsIndex `json:"shards"` // for level=shards +} + +type NodesStatsDocsStats struct { + Count int64 `json:"count"` + Deleted int64 `json:"deleted"` +} + +type NodesStatsStoreStats struct { + Size string `json:"size"` + SizeInBytes int64 `json:"size_in_bytes"` +} + +type NodesStatsIndexingStats struct { + IndexTotal int64 `json:"index_total"` + IndexTime string `json:"index_time"` + IndexTimeInMillis int64 `json:"index_time_in_millis"` + IndexCurrent int64 `json:"index_current"` + IndexFailed int64 `json:"index_failed"` + DeleteTotal int64 `json:"delete_total"` + DeleteTime string `json:"delete_time"` + DeleteTimeInMillis int64 `json:"delete_time_in_millis"` + DeleteCurrent int64 `json:"delete_current"` + NoopUpdateTotal int64 `json:"noop_update_total"` + IsThrottled bool `json:"is_throttled"` + ThrottledTime string `json:"throttle_time"` // no typo, see https://github.com/elastic/elasticsearch/blob/ff99bc1d3f8a7ea72718872d214ec2097dfca276/server/src/main/java/org/elasticsearch/index/shard/IndexingStats.java#L244 + ThrottledTimeInMillis int64 `json:"throttle_time_in_millis"` + + Types map[string]*NodesStatsIndexingStats `json:"types"` // stats for individual types +} + +type NodesStatsGetStats struct { + Total int64 `json:"total"` + Time string `json:"get_time"` + TimeInMillis int64 `json:"time_in_millis"` + Exists int64 `json:"exists"` + ExistsTime string `json:"exists_time"` + ExistsTimeInMillis int64 `json:"exists_in_millis"` + Missing int64 `json:"missing"` + MissingTime string `json:"missing_time"` + MissingTimeInMillis int64 `json:"missing_in_millis"` + Current int64 `json:"current"` +} + +type NodesStatsSearchStats struct { + OpenContexts int64 `json:"open_contexts"` + QueryTotal int64 `json:"query_total"` + QueryTime string `json:"query_time"` + QueryTimeInMillis int64 `json:"query_time_in_millis"` + QueryCurrent int64 `json:"query_current"` + FetchTotal int64 `json:"fetch_total"` + FetchTime string `json:"fetch_time"` + FetchTimeInMillis int64 `json:"fetch_time_in_millis"` + FetchCurrent int64 `json:"fetch_current"` + ScrollTotal int64 `json:"scroll_total"` + ScrollTime string `json:"scroll_time"` + ScrollTimeInMillis int64 `json:"scroll_time_in_millis"` + ScrollCurrent int64 `json:"scroll_current"` + + Groups map[string]*NodesStatsSearchStats `json:"groups"` // stats for individual groups +} + +type NodesStatsMergeStats struct { + Current int64 `json:"current"` + CurrentDocs int64 `json:"current_docs"` + CurrentSize string `json:"current_size"` + CurrentSizeInBytes int64 `json:"current_size_in_bytes"` + Total int64 `json:"total"` + TotalTime string `json:"total_time"` + TotalTimeInMillis int64 `json:"total_time_in_millis"` + TotalDocs int64 `json:"total_docs"` + TotalSize string `json:"total_size"` + TotalSizeInBytes int64 `json:"total_size_in_bytes"` + TotalStoppedTime string `json:"total_stopped_time"` + TotalStoppedTimeInMillis int64 `json:"total_stopped_time_in_millis"` + TotalThrottledTime string `json:"total_throttled_time"` + TotalThrottledTimeInMillis int64 `json:"total_throttled_time_in_millis"` + TotalThrottleBytes string `json:"total_auto_throttle"` + TotalThrottleBytesInBytes int64 `json:"total_auto_throttle_in_bytes"` +} + +type NodesStatsRefreshStats struct { + Total int64 `json:"total"` + TotalTime string `json:"total_time"` + TotalTimeInMillis int64 `json:"total_time_in_millis"` +} + +type NodesStatsFlushStats struct { + Total int64 `json:"total"` + TotalTime string `json:"total_time"` + TotalTimeInMillis int64 `json:"total_time_in_millis"` +} + +type NodesStatsWarmerStats struct { + Current int64 `json:"current"` + Total int64 `json:"total"` + TotalTime string `json:"total_time"` + TotalTimeInMillis int64 `json:"total_time_in_millis"` +} + +type NodesStatsQueryCacheStats struct { + MemorySize string `json:"memory_size"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + TotalCount int64 `json:"total_count"` + HitCount int64 `json:"hit_count"` + MissCount int64 `json:"miss_count"` + CacheSize int64 `json:"cache_size"` + CacheCount int64 `json:"cache_count"` + Evictions int64 `json:"evictions"` +} + +type NodesStatsFielddataStats struct { + MemorySize string `json:"memory_size"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + Evictions int64 `json:"evictions"` + Fields map[string]struct { + MemorySize string `json:"memory_size"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + } `json:"fields"` +} + +type NodesStatsPercolateStats struct { + Total int64 `json:"total"` + Time string `json:"time"` + TimeInMillis int64 `json:"time_in_millis"` + Current int64 `json:"current"` + MemorySize string `json:"memory_size"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + Queries int64 `json:"queries"` +} + +type NodesStatsCompletionStats struct { + Size string `json:"size"` + SizeInBytes int64 `json:"size_in_bytes"` + Fields map[string]struct { + Size string `json:"size"` + SizeInBytes int64 `json:"size_in_bytes"` + } `json:"fields"` +} + +type NodesStatsSegmentsStats struct { + Count int64 `json:"count"` + Memory string `json:"memory"` + MemoryInBytes int64 `json:"memory_in_bytes"` + TermsMemory string `json:"terms_memory"` + TermsMemoryInBytes int64 `json:"terms_memory_in_bytes"` + StoredFieldsMemory string `json:"stored_fields_memory"` + StoredFieldsMemoryInBytes int64 `json:"stored_fields_memory_in_bytes"` + TermVectorsMemory string `json:"term_vectors_memory"` + TermVectorsMemoryInBytes int64 `json:"term_vectors_memory_in_bytes"` + NormsMemory string `json:"norms_memory"` + NormsMemoryInBytes int64 `json:"norms_memory_in_bytes"` + DocValuesMemory string `json:"doc_values_memory"` + DocValuesMemoryInBytes int64 `json:"doc_values_memory_in_bytes"` + IndexWriterMemory string `json:"index_writer_memory"` + IndexWriterMemoryInBytes int64 `json:"index_writer_memory_in_bytes"` + IndexWriterMaxMemory string `json:"index_writer_max_memory"` + IndexWriterMaxMemoryInBytes int64 `json:"index_writer_max_memory_in_bytes"` + VersionMapMemory string `json:"version_map_memory"` + VersionMapMemoryInBytes int64 `json:"version_map_memory_in_bytes"` + FixedBitSetMemory string `json:"fixed_bit_set"` // not a typo + FixedBitSetMemoryInBytes int64 `json:"fixed_bit_set_memory_in_bytes"` +} + +type NodesStatsTranslogStats struct { + Operations int64 `json:"operations"` + Size string `json:"size"` + SizeInBytes int64 `json:"size_in_bytes"` +} + +type NodesStatsSuggestStats struct { + Total int64 `json:"total"` + TotalTime string `json:"total_time"` + TotalTimeInMillis int64 `json:"total_time_in_millis"` + Current int64 `json:"current"` +} + +type NodesStatsRequestCacheStats struct { + MemorySize string `json:"memory_size"` + MemorySizeInBytes int64 `json:"memory_size_in_bytes"` + Evictions int64 `json:"evictions"` + HitCount int64 `json:"hit_count"` + MissCount int64 `json:"miss_count"` +} + +type NodesStatsRecoveryStats struct { + CurrentAsSource int `json:"current_as_source"` + CurrentAsTarget int `json:"current_as_target"` +} + +type NodesStatsNodeOS struct { + Timestamp int64 `json:"timestamp"` + CPU *NodesStatsNodeOSCPU `json:"cpu"` + Mem *NodesStatsNodeOSMem `json:"mem"` + Swap *NodesStatsNodeOSSwap `json:"swap"` +} + +type NodesStatsNodeOSCPU struct { + Percent int `json:"percent"` + LoadAverage map[string]float64 `json:"load_average"` // keys are: 1m, 5m, and 15m +} + +type NodesStatsNodeOSMem struct { + Total string `json:"total"` + TotalInBytes int64 `json:"total_in_bytes"` + Free string `json:"free"` + FreeInBytes int64 `json:"free_in_bytes"` + Used string `json:"used"` + UsedInBytes int64 `json:"used_in_bytes"` + FreePercent int `json:"free_percent"` + UsedPercent int `json:"used_percent"` +} + +type NodesStatsNodeOSSwap struct { + Total string `json:"total"` + TotalInBytes int64 `json:"total_in_bytes"` + Free string `json:"free"` + FreeInBytes int64 `json:"free_in_bytes"` + Used string `json:"used"` + UsedInBytes int64 `json:"used_in_bytes"` +} + +type NodesStatsNodeProcess struct { + Timestamp int64 `json:"timestamp"` + OpenFileDescriptors int64 `json:"open_file_descriptors"` + MaxFileDescriptors int64 `json:"max_file_descriptors"` + CPU struct { + Percent int `json:"percent"` + Total string `json:"total"` + TotalInMillis int64 `json:"total_in_millis"` + } `json:"cpu"` + Mem struct { + TotalVirtual string `json:"total_virtual"` + TotalVirtualInBytes int64 `json:"total_virtual_in_bytes"` + } `json:"mem"` +} + +type NodesStatsNodeJVM struct { + Timestamp int64 `json:"timestamp"` + Uptime string `json:"uptime"` + UptimeInMillis int64 `json:"uptime_in_millis"` + Mem *NodesStatsNodeJVMMem `json:"mem"` + Threads *NodesStatsNodeJVMThreads `json:"threads"` + GC *NodesStatsNodeJVMGC `json:"gc"` + BufferPools map[string]*NodesStatsNodeJVMBufferPool `json:"buffer_pools"` + Classes *NodesStatsNodeJVMClasses `json:"classes"` +} + +type NodesStatsNodeJVMMem struct { + HeapUsed string `json:"heap_used"` + HeapUsedInBytes int64 `json:"heap_used_in_bytes"` + HeapUsedPercent int `json:"heap_used_percent"` + HeapCommitted string `json:"heap_committed"` + HeapCommittedInBytes int64 `json:"heap_committed_in_bytes"` + HeapMax string `json:"heap_max"` + HeapMaxInBytes int64 `json:"heap_max_in_bytes"` + NonHeapUsed string `json:"non_heap_used"` + NonHeapUsedInBytes int64 `json:"non_heap_used_in_bytes"` + NonHeapCommitted string `json:"non_heap_committed"` + NonHeapCommittedInBytes int64 `json:"non_heap_committed_in_bytes"` + Pools map[string]struct { + Used string `json:"used"` + UsedInBytes int64 `json:"used_in_bytes"` + Max string `json:"max"` + MaxInBytes int64 `json:"max_in_bytes"` + PeakUsed string `json:"peak_used"` + PeakUsedInBytes int64 `json:"peak_used_in_bytes"` + PeakMax string `json:"peak_max"` + PeakMaxInBytes int64 `json:"peak_max_in_bytes"` + } `json:"pools"` +} + +type NodesStatsNodeJVMThreads struct { + Count int64 `json:"count"` + PeakCount int64 `json:"peak_count"` +} + +type NodesStatsNodeJVMGC struct { + Collectors map[string]*NodesStatsNodeJVMGCCollector `json:"collectors"` +} + +type NodesStatsNodeJVMGCCollector struct { + CollectionCount int64 `json:"collection_count"` + CollectionTime string `json:"collection_time"` + CollectionTimeInMillis int64 `json:"collection_time_in_millis"` +} + +type NodesStatsNodeJVMBufferPool struct { + Count int64 `json:"count"` + TotalCapacity string `json:"total_capacity"` + TotalCapacityInBytes int64 `json:"total_capacity_in_bytes"` +} + +type NodesStatsNodeJVMClasses struct { + CurrentLoadedCount int64 `json:"current_loaded_count"` + TotalLoadedCount int64 `json:"total_loaded_count"` + TotalUnloadedCount int64 `json:"total_unloaded_count"` +} + +type NodesStatsNodeThreadPool struct { + Threads int `json:"threads"` + Queue int `json:"queue"` + Active int `json:"active"` + Rejected int64 `json:"rejected"` + Largest int `json:"largest"` + Completed int64 `json:"completed"` +} + +type NodesStatsNodeFS struct { + Timestamp int64 `json:"timestamp"` + Total *NodesStatsNodeFSEntry `json:"total"` + Data []*NodesStatsNodeFSEntry `json:"data"` + IOStats *NodesStatsNodeFSIOStats `json:"io_stats"` +} + +type NodesStatsNodeFSEntry struct { + Path string `json:"path"` + Mount string `json:"mount"` + Type string `json:"type"` + Total string `json:"total"` + TotalInBytes int64 `json:"total_in_bytes"` + Free string `json:"free"` + FreeInBytes int64 `json:"free_in_bytes"` + Available string `json:"available"` + AvailableInBytes int64 `json:"available_in_bytes"` + Spins string `json:"spins"` +} + +type NodesStatsNodeFSIOStats struct { + Devices []*NodesStatsNodeFSIOStatsEntry `json:"devices"` + Total *NodesStatsNodeFSIOStatsEntry `json:"total"` +} + +type NodesStatsNodeFSIOStatsEntry struct { + DeviceName string `json:"device_name"` + Operations int64 `json:"operations"` + ReadOperations int64 `json:"read_operations"` + WriteOperations int64 `json:"write_operations"` + ReadKilobytes int64 `json:"read_kilobytes"` + WriteKilobytes int64 `json:"write_kilobytes"` +} + +type NodesStatsNodeTransport struct { + ServerOpen int `json:"server_open"` + RxCount int64 `json:"rx_count"` + RxSize string `json:"rx_size"` + RxSizeInBytes int64 `json:"rx_size_in_bytes"` + TxCount int64 `json:"tx_count"` + TxSize string `json:"tx_size"` + TxSizeInBytes int64 `json:"tx_size_in_bytes"` +} + +type NodesStatsNodeHTTP struct { + CurrentOpen int `json:"current_open"` + TotalOpened int `json:"total_opened"` +} + +type NodesStatsBreaker struct { + LimitSize string `json:"limit_size"` + LimitSizeInBytes int64 `json:"limit_size_in_bytes"` + EstimatedSize string `json:"estimated_size"` + EstimatedSizeInBytes int64 `json:"estimated_size_in_bytes"` + Overhead float64 `json:"overhead"` + Tripped int64 `json:"tripped"` +} + +type NodesStatsScriptStats struct { + Compilations int64 `json:"compilations"` + CacheEvictions int64 `json:"cache_evictions"` +} + +type NodesStatsDiscovery struct { + ClusterStateQueue *NodesStatsDiscoveryStats `json:"cluster_state_queue"` +} + +type NodesStatsDiscoveryStats struct { + Total int64 `json:"total"` + Pending int64 `json:"pending"` + Committed int64 `json:"committed"` +} + +type NodesStatsIngest struct { + Total *NodesStatsIngestStats `json:"total"` + Pipelines interface{} `json:"pipelines"` +} + +type NodesStatsIngestStats struct { + Count int64 `json:"count"` + Time string `json:"time"` + TimeInMillis int64 `json:"time_in_millis"` + Current int64 `json:"current"` + Failed int64 `json:"failed"` +} diff --git a/vendor/github.com/olivere/elastic/v7/ping.go b/vendor/github.com/olivere/elastic/v7/ping.go new file mode 100644 index 0000000000..1ad7861740 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/ping.go @@ -0,0 +1,197 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" +) + +// PingService checks if an Elasticsearch server on a given URL is alive. +// When asked for, it can also return various information about the +// Elasticsearch server, e.g. the Elasticsearch version number. +// +// Ping simply starts a HTTP GET request to the URL of the server. +// If the server responds with HTTP Status code 200 OK, the server is alive. +type PingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + url string + timeout string + httpHeadOnly bool +} + +// PingResult is the result returned from querying the Elasticsearch server. +type PingResult struct { + Name string `json:"name"` + ClusterName string `json:"cluster_name"` + Version struct { + Number string `json:"number"` // e.g. "7.0.0" + BuildFlavor string `json:"build_flavor"` // e.g. "oss" or "default" + BuildType string `json:"build_type"` // e.g. "docker" + BuildHash string `json:"build_hash"` // e.g. "b7e28a7" + BuildDate string `json:"build_date"` // e.g. "2019-04-05T22:55:32.697037Z" + BuildSnapshot bool `json:"build_snapshot"` // e.g. false + LuceneVersion string `json:"lucene_version"` // e.g. "8.0.0" + MinimumWireCompatibilityVersion string `json:"minimum_wire_compatibility_version"` // e.g. "6.7.0" + MinimumIndexCompatibilityVersion string `json:"minimum_index_compatibility_version"` // e.g. "6.0.0-beta1" + } `json:"version"` + TagLine string `json:"tagline"` +} + +func NewPingService(client *Client) *PingService { + return &PingService{ + client: client, + url: DefaultURL, + httpHeadOnly: false, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *PingService) Pretty(pretty bool) *PingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *PingService) Human(human bool) *PingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *PingService) ErrorTrace(errorTrace bool) *PingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *PingService) FilterPath(filterPath ...string) *PingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *PingService) Header(name string, value string) *PingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *PingService) Headers(headers http.Header) *PingService { + s.headers = headers + return s +} + +func (s *PingService) URL(url string) *PingService { + s.url = url + return s +} + +func (s *PingService) Timeout(timeout string) *PingService { + s.timeout = timeout + return s +} + +// HeadOnly makes the service to only return the status code in Do; +// the PingResult will be nil. +func (s *PingService) HttpHeadOnly(httpHeadOnly bool) *PingService { + s.httpHeadOnly = httpHeadOnly + return s +} + +// Do returns the PingResult, the HTTP status code of the Elasticsearch +// server, and an error. +func (s *PingService) Do(ctx context.Context) (*PingResult, int, error) { + s.client.mu.RLock() + basicAuth := s.client.basicAuth + basicAuthUsername := s.client.basicAuthUsername + basicAuthPassword := s.client.basicAuthPassword + defaultHeaders := s.client.headers + s.client.mu.RUnlock() + + url_ := strings.TrimSuffix(s.url, "/") + "/" + + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if len(params) > 0 { + url_ += "?" + params.Encode() + } + + var method string + if s.httpHeadOnly { + method = "HEAD" + } else { + method = "GET" + } + + // Notice: This service must NOT use PerformRequest! + req, err := NewRequest(method, url_) + if err != nil { + return nil, 0, err + } + if len(s.headers) > 0 { + for key, values := range s.headers { + for _, v := range values { + req.Header.Add(key, v) + } + } + } + if len(defaultHeaders) > 0 { + for key, values := range defaultHeaders { + for _, v := range values { + req.Header.Add(key, v) + } + } + } + + if basicAuth { + req.SetBasicAuth(basicAuthUsername, basicAuthPassword) + } + + res, err := s.client.c.Do((*http.Request)(req).WithContext(ctx)) + if err != nil { + return nil, 0, err + } + defer res.Body.Close() + + var ret *PingResult + if !s.httpHeadOnly { + ret = new(PingResult) + if err := json.NewDecoder(res.Body).Decode(ret); err != nil { + return nil, res.StatusCode, err + } + } + + return ret, res.StatusCode, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/plugins.go b/vendor/github.com/olivere/elastic/v7/plugins.go new file mode 100644 index 0000000000..60bda7552d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/plugins.go @@ -0,0 +1,40 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "context" + +// HasPlugin indicates whether the cluster has the named plugin. +func (c *Client) HasPlugin(name string) (bool, error) { + plugins, err := c.Plugins() + if err != nil { + return false, nil + } + for _, plugin := range plugins { + if plugin == name { + return true, nil + } + } + return false, nil +} + +// Plugins returns the list of all registered plugins. +func (c *Client) Plugins() ([]string, error) { + stats, err := c.ClusterStats().Do(context.Background()) + if err != nil { + return nil, err + } + if stats == nil { + return nil, err + } + if stats.Nodes == nil { + return nil, err + } + var plugins []string + for _, plugin := range stats.Nodes.Plugins { + plugins = append(plugins, plugin.Name) + } + return plugins, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/query.go b/vendor/github.com/olivere/elastic/v7/query.go new file mode 100644 index 0000000000..ad01354a08 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/query.go @@ -0,0 +1,13 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// Query represents the generic query interface. A query's sole purpose +// is to return the source of the query as a JSON-serializable object. +// Returning map[string]interface{} is the norm for queries. +type Query interface { + // Source returns the JSON-serializable query request. + Source() (interface{}, error) +} diff --git a/vendor/github.com/olivere/elastic/v7/reindex.go b/vendor/github.com/olivere/elastic/v7/reindex.go new file mode 100644 index 0000000000..589fb0e3b1 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/reindex.go @@ -0,0 +1,745 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/url" + "strings" +) + +// ReindexService is a method to copy documents from one index to another. +// It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-reindex.html. +type ReindexService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + refresh string + timeout string + waitForActiveShards string + waitForCompletion *bool + requestsPerSecond *int + slices interface{} + body interface{} + source *ReindexSource + destination *ReindexDestination + conflicts string + size *int + script *Script +} + +// NewReindexService creates a new ReindexService. +func NewReindexService(client *Client) *ReindexService { + return &ReindexService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ReindexService) Pretty(pretty bool) *ReindexService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ReindexService) Human(human bool) *ReindexService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ReindexService) ErrorTrace(errorTrace bool) *ReindexService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ReindexService) FilterPath(filterPath ...string) *ReindexService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ReindexService) Header(name string, value string) *ReindexService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ReindexService) Headers(headers http.Header) *ReindexService { + s.headers = headers + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active before +// proceeding with the reindex operation. Defaults to 1, meaning the primary shard only. +// Set to `all` for all shard copies, otherwise set to any non-negative value less than or +// equal to the total number of copies for the shard (number of replicas + 1). +func (s *ReindexService) WaitForActiveShards(waitForActiveShards string) *ReindexService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// RequestsPerSecond specifies the throttle to set on this request in sub-requests per second. +// -1 means set no throttle as does "unlimited" which is the only non-float this accepts. +func (s *ReindexService) RequestsPerSecond(requestsPerSecond int) *ReindexService { + s.requestsPerSecond = &requestsPerSecond + return s +} + +// Slices specifies the number of slices this task should be divided into. Defaults to 1. +// It used to be a number, but can be set to "auto" as of 6.7. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-reindex.html#docs-reindex-slice +// for details. +func (s *ReindexService) Slices(slices interface{}) *ReindexService { + s.slices = slices + return s +} + +// Refresh indicates whether Elasticsearch should refresh the effected indexes +// immediately. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *ReindexService) Refresh(refresh string) *ReindexService { + s.refresh = refresh + return s +} + +// Timeout is the time each individual bulk request should wait for shards +// that are unavailable. +func (s *ReindexService) Timeout(timeout string) *ReindexService { + s.timeout = timeout + return s +} + +// WaitForCompletion indicates whether Elasticsearch should block until the +// reindex is complete. +func (s *ReindexService) WaitForCompletion(waitForCompletion bool) *ReindexService { + s.waitForCompletion = &waitForCompletion + return s +} + +// Source specifies the source of the reindexing process. +func (s *ReindexService) Source(source *ReindexSource) *ReindexService { + s.source = source + return s +} + +// SourceIndex specifies the source index of the reindexing process. +func (s *ReindexService) SourceIndex(index string) *ReindexService { + if s.source == nil { + s.source = NewReindexSource() + } + s.source = s.source.Index(index) + return s +} + +// Destination specifies the destination of the reindexing process. +func (s *ReindexService) Destination(destination *ReindexDestination) *ReindexService { + s.destination = destination + return s +} + +// DestinationIndex specifies the destination index of the reindexing process. +func (s *ReindexService) DestinationIndex(index string) *ReindexService { + if s.destination == nil { + s.destination = NewReindexDestination() + } + s.destination = s.destination.Index(index) + return s +} + +// DestinationIndexAndType specifies both the destination index and type +// of the reindexing process. +func (s *ReindexService) DestinationIndexAndType(index, typ string) *ReindexService { + if s.destination == nil { + s.destination = NewReindexDestination() + } + s.destination = s.destination.Index(index) + s.destination = s.destination.Type(typ) + return s +} + +// Conflicts indicates what to do when the process detects version conflicts. +// Possible values are "proceed" and "abort". +func (s *ReindexService) Conflicts(conflicts string) *ReindexService { + s.conflicts = conflicts + return s +} + +// AbortOnVersionConflict aborts the request on version conflicts. +// It is an alias to setting Conflicts("abort"). +func (s *ReindexService) AbortOnVersionConflict() *ReindexService { + s.conflicts = "abort" + return s +} + +// ProceedOnVersionConflict aborts the request on version conflicts. +// It is an alias to setting Conflicts("proceed"). +func (s *ReindexService) ProceedOnVersionConflict() *ReindexService { + s.conflicts = "proceed" + return s +} + +// Size sets an upper limit for the number of processed documents. +func (s *ReindexService) Size(size int) *ReindexService { + s.size = &size + return s +} + +// Script allows for modification of the documents as they are reindexed +// from source to destination. +func (s *ReindexService) Script(script *Script) *ReindexService { + s.script = script + return s +} + +// Body specifies the body of the request to send to Elasticsearch. +// It overrides settings specified with other setters, e.g. Query. +func (s *ReindexService) Body(body interface{}) *ReindexService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *ReindexService) buildURL() (string, url.Values, error) { + // Build URL path + path := "/_reindex" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.requestsPerSecond != nil { + params.Set("requests_per_second", fmt.Sprintf("%v", *s.requestsPerSecond)) + } + if s.slices != nil { + params.Set("slices", fmt.Sprintf("%v", s.slices)) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + if s.waitForCompletion != nil { + params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ReindexService) Validate() error { + var invalid []string + if s.body != nil { + return nil + } + if s.source == nil { + invalid = append(invalid, "Source") + } else { + if len(s.source.request.indices) == 0 { + invalid = append(invalid, "Source.Index") + } + } + if s.destination == nil { + invalid = append(invalid, "Destination") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// getBody returns the body part of the document request. +func (s *ReindexService) getBody() (interface{}, error) { + if s.body != nil { + return s.body, nil + } + + body := make(map[string]interface{}) + + if s.conflicts != "" { + body["conflicts"] = s.conflicts + } + if s.size != nil { + body["size"] = *s.size + } + if s.script != nil { + out, err := s.script.Source() + if err != nil { + return nil, err + } + body["script"] = out + } + + src, err := s.source.Source() + if err != nil { + return nil, err + } + body["source"] = src + + dst, err := s.destination.Source() + if err != nil { + return nil, err + } + body["dest"] = dst + + return body, nil +} + +// Do executes the operation. +func (s *ReindexService) Do(ctx context.Context) (*BulkIndexByScrollResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body, err := s.getBody() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(BulkIndexByScrollResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + ret.Header = res.Header + return ret, nil +} + +// DoAsync executes the reindexing operation asynchronously by starting a new task. +// Callers need to use the Task Management API to watch the outcome of the reindexing +// operation. +func (s *ReindexService) DoAsync(ctx context.Context) (*StartTaskResult, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // DoAsync only makes sense with WaitForCompletion set to true + if s.waitForCompletion != nil && *s.waitForCompletion { + return nil, fmt.Errorf("cannot start a task with WaitForCompletion set to true") + } + f := false + s.waitForCompletion = &f + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body, err := s.getBody() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(StartTaskResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + ret.Header = res.Header + return ret, nil +} + +// -- Source of Reindex -- + +// ReindexSource specifies the source of a Reindex process. +type ReindexSource struct { + request *SearchRequest + remoteInfo *ReindexRemoteInfo +} + +// NewReindexSource creates a new ReindexSource. +func NewReindexSource() *ReindexSource { + return &ReindexSource{ + request: NewSearchRequest(), + } +} + +// Request specifies the search request used for source. +func (r *ReindexSource) Request(request *SearchRequest) *ReindexSource { + if request == nil { + r.request = NewSearchRequest() + } else { + r.request = request + } + return r +} + +// SearchType is the search operation type. Possible values are +// "query_then_fetch" and "dfs_query_then_fetch". +func (r *ReindexSource) SearchType(searchType string) *ReindexSource { + r.request = r.request.SearchType(searchType) + return r +} + +func (r *ReindexSource) SearchTypeDfsQueryThenFetch() *ReindexSource { + r.request = r.request.SearchType("dfs_query_then_fetch") + return r +} + +func (r *ReindexSource) SearchTypeQueryThenFetch() *ReindexSource { + r.request = r.request.SearchType("query_then_fetch") + return r +} + +func (r *ReindexSource) Index(indices ...string) *ReindexSource { + r.request = r.request.Index(indices...) + return r +} + +func (r *ReindexSource) Type(types ...string) *ReindexSource { + r.request = r.request.Type(types...) + return r +} + +func (r *ReindexSource) Preference(preference string) *ReindexSource { + r.request = r.request.Preference(preference) + return r +} + +func (r *ReindexSource) RequestCache(requestCache bool) *ReindexSource { + r.request = r.request.RequestCache(requestCache) + return r +} + +func (r *ReindexSource) Scroll(scroll string) *ReindexSource { + r.request = r.request.Scroll(scroll) + return r +} + +func (r *ReindexSource) Query(query Query) *ReindexSource { + r.request = r.request.Query(query) + return r +} + +// Sort adds a sort order. +func (r *ReindexSource) Sort(field string, ascending bool) *ReindexSource { + r.request = r.request.Sort(field, ascending) + return r +} + +// SortWithInfo adds a sort order. +func (r *ReindexSource) SortWithInfo(info SortInfo) *ReindexSource { + r.request = r.request.SortWithInfo(info) + return r +} + +// SortBy adds a sort order. +func (r *ReindexSource) SortBy(sorter ...Sorter) *ReindexSource { + r.request = r.request.SortBy(sorter...) + return r +} + +// FetchSource indicates whether the response should contain the stored +// _source for every hit. +func (r *ReindexSource) FetchSource(fetchSource bool) *ReindexSource { + r.request = r.request.FetchSource(fetchSource) + return r +} + +// FetchSourceIncludeExclude specifies that _source should be returned +// with each hit, where "include" and "exclude" serve as a simple wildcard +// matcher that gets applied to its fields +// (e.g. include := []string{"obj1.*","obj2.*"}, exclude := []string{"description.*"}). +func (r *ReindexSource) FetchSourceIncludeExclude(include, exclude []string) *ReindexSource { + r.request = r.request.FetchSourceIncludeExclude(include, exclude) + return r +} + +// FetchSourceContext indicates how the _source should be fetched. +func (r *ReindexSource) FetchSourceContext(fsc *FetchSourceContext) *ReindexSource { + r.request = r.request.FetchSourceContext(fsc) + return r +} + +// RemoteInfo sets up reindexing from a remote cluster. +func (r *ReindexSource) RemoteInfo(ri *ReindexRemoteInfo) *ReindexSource { + r.remoteInfo = ri + return r +} + +// Source returns a serializable JSON request for the request. +func (r *ReindexSource) Source() (interface{}, error) { + src, err := r.request.sourceAsMap() + if err != nil { + return nil, err + } + source, ok := src.(map[string]interface{}) + if !ok { + return nil, errors.New("unable to use SearchRequest as map[string]interface{}") + } + + switch len(r.request.indices) { + case 1: + source["index"] = r.request.indices[0] + default: + source["index"] = r.request.indices + } + switch len(r.request.types) { + case 0: + case 1: + source["type"] = r.request.types[0] + default: + source["type"] = r.request.types + } + if r.remoteInfo != nil { + src, err := r.remoteInfo.Source() + if err != nil { + return nil, err + } + source["remote"] = src + } + return source, nil +} + +// ReindexRemoteInfo contains information for reindexing from a remote cluster. +type ReindexRemoteInfo struct { + host string + username string + password string + socketTimeout string // e.g. "1m" or "30s" + connectTimeout string // e.g. "1m" or "30s" +} + +// NewReindexRemoteInfo creates a new ReindexRemoteInfo. +func NewReindexRemoteInfo() *ReindexRemoteInfo { + return &ReindexRemoteInfo{} +} + +// Host sets the host information of the remote cluster. +// It must be of the form "http(s)://:" +func (ri *ReindexRemoteInfo) Host(host string) *ReindexRemoteInfo { + ri.host = host + return ri +} + +// Username sets the username to authenticate with the remote cluster. +func (ri *ReindexRemoteInfo) Username(username string) *ReindexRemoteInfo { + ri.username = username + return ri +} + +// Password sets the password to authenticate with the remote cluster. +func (ri *ReindexRemoteInfo) Password(password string) *ReindexRemoteInfo { + ri.password = password + return ri +} + +// SocketTimeout sets the socket timeout to connect with the remote cluster. +// Use ES compatible values like e.g. "30s" or "1m". +func (ri *ReindexRemoteInfo) SocketTimeout(timeout string) *ReindexRemoteInfo { + ri.socketTimeout = timeout + return ri +} + +// ConnectTimeout sets the connection timeout to connect with the remote cluster. +// Use ES compatible values like e.g. "30s" or "1m". +func (ri *ReindexRemoteInfo) ConnectTimeout(timeout string) *ReindexRemoteInfo { + ri.connectTimeout = timeout + return ri +} + +// Source returns the serializable JSON data for the request. +func (ri *ReindexRemoteInfo) Source() (interface{}, error) { + res := make(map[string]interface{}) + res["host"] = ri.host + if len(ri.username) > 0 { + res["username"] = ri.username + } + if len(ri.password) > 0 { + res["password"] = ri.password + } + if len(ri.socketTimeout) > 0 { + res["socket_timeout"] = ri.socketTimeout + } + if len(ri.connectTimeout) > 0 { + res["connect_timeout"] = ri.connectTimeout + } + return res, nil +} + +// -source Destination of Reindex -- + +// ReindexDestination is the destination of a Reindex API call. +// It is basically the meta data of a BulkIndexRequest. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-reindex.html +// fsourcer details. +type ReindexDestination struct { + index string + typ string + routing string + parent string + opType string + version int64 // default is MATCH_ANY + versionType string // default is "internal" + pipeline string +} + +// NewReindexDestination returns a new ReindexDestination. +func NewReindexDestination() *ReindexDestination { + return &ReindexDestination{} +} + +// Index specifies name of the Elasticsearch index to use as the destination +// of a reindexing process. +func (r *ReindexDestination) Index(index string) *ReindexDestination { + r.index = index + return r +} + +// Type specifies the Elasticsearch type to use for reindexing. +func (r *ReindexDestination) Type(typ string) *ReindexDestination { + r.typ = typ + return r +} + +// Routing specifies a routing value for the reindexing request. +// It can be "keep", "discard", or start with "=". The latter specifies +// the routing on the bulk request. +func (r *ReindexDestination) Routing(routing string) *ReindexDestination { + r.routing = routing + return r +} + +// Keep sets the routing on the bulk request sent for each match to the routing +// of the match (the default). +func (r *ReindexDestination) Keep() *ReindexDestination { + r.routing = "keep" + return r +} + +// Discard sets the routing on the bulk request sent for each match to null. +func (r *ReindexDestination) Discard() *ReindexDestination { + r.routing = "discard" + return r +} + +// Parent specifies the identifier of the parent document (if available). +func (r *ReindexDestination) Parent(parent string) *ReindexDestination { + r.parent = parent + return r +} + +// OpType specifies if this request should follow create-only or upsert +// behavior. This follows the OpType of the standard document index API. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-index_.html#operation-type +// for details. +func (r *ReindexDestination) OpType(opType string) *ReindexDestination { + r.opType = opType + return r +} + +// Version indicates the version of the document as part of an optimistic +// concurrency model. +func (r *ReindexDestination) Version(version int64) *ReindexDestination { + r.version = version + return r +} + +// VersionType specifies how versions are created. +func (r *ReindexDestination) VersionType(versionType string) *ReindexDestination { + r.versionType = versionType + return r +} + +// Pipeline specifies the pipeline to use for reindexing. +func (r *ReindexDestination) Pipeline(pipeline string) *ReindexDestination { + r.pipeline = pipeline + return r +} + +// Source returns a serializable JSON request for the request. +func (r *ReindexDestination) Source() (interface{}, error) { + source := make(map[string]interface{}) + if r.index != "" { + source["index"] = r.index + } + if r.typ != "" { + source["type"] = r.typ + } + if r.routing != "" { + source["routing"] = r.routing + } + if r.opType != "" { + source["op_type"] = r.opType + } + if r.parent != "" { + source["parent"] = r.parent + } + if r.version > 0 { + source["version"] = r.version + } + if r.versionType != "" { + source["version_type"] = r.versionType + } + if r.pipeline != "" { + source["pipeline"] = r.pipeline + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/request.go b/vendor/github.com/olivere/elastic/v7/request.go new file mode 100644 index 0000000000..288c837aba --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/request.go @@ -0,0 +1,123 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "bytes" + "compress/gzip" + "encoding/json" + "io" + "io/ioutil" + "net/http" + "runtime" + "strings" +) + +// Elasticsearch-specific HTTP request +type Request http.Request + +// NewRequest is a http.Request and adds features such as encoding the body. +func NewRequest(method, url string) (*Request, error) { + req, err := http.NewRequest(method, url, nil) + if err != nil { + return nil, err + } + req.Header.Add("User-Agent", "elastic/"+Version+" ("+runtime.GOOS+"-"+runtime.GOARCH+")") + req.Header.Add("Accept", "application/json") + req.Header.Set("Content-Type", "application/json") + return (*Request)(req), nil +} + +// SetBasicAuth wraps http.Request's SetBasicAuth. +func (r *Request) SetBasicAuth(username, password string) { + ((*http.Request)(r)).SetBasicAuth(username, password) +} + +// SetBody encodes the body in the request. You may pass a flag to +// compress the request via gzip. +func (r *Request) SetBody(body interface{}, gzipCompress bool) error { + switch b := body.(type) { + case string: + if gzipCompress { + return r.setBodyGzip(b) + } + return r.setBodyString(b) + default: + if gzipCompress { + return r.setBodyGzip(body) + } + return r.setBodyJson(body) + } +} + +// setBodyJson encodes the body as a struct to be marshaled via json.Marshal. +func (r *Request) setBodyJson(data interface{}) error { + body, err := json.Marshal(data) + if err != nil { + return err + } + r.Header.Set("Content-Type", "application/json") + r.setBodyReader(bytes.NewReader(body)) + return nil +} + +// setBodyString encodes the body as a string. +func (r *Request) setBodyString(body string) error { + return r.setBodyReader(strings.NewReader(body)) +} + +// setBodyGzip gzip's the body. It accepts both strings and structs as body. +// The latter will be encoded via json.Marshal. +func (r *Request) setBodyGzip(body interface{}) error { + switch b := body.(type) { + case string: + buf := new(bytes.Buffer) + w := gzip.NewWriter(buf) + if _, err := w.Write([]byte(b)); err != nil { + return err + } + if err := w.Close(); err != nil { + return err + } + r.Header.Add("Content-Encoding", "gzip") + r.Header.Add("Vary", "Accept-Encoding") + return r.setBodyReader(bytes.NewReader(buf.Bytes())) + default: + data, err := json.Marshal(b) + if err != nil { + return err + } + buf := new(bytes.Buffer) + w := gzip.NewWriter(buf) + if _, err := w.Write(data); err != nil { + return err + } + if err := w.Close(); err != nil { + return err + } + r.Header.Add("Content-Encoding", "gzip") + r.Header.Add("Vary", "Accept-Encoding") + r.Header.Set("Content-Type", "application/json") + return r.setBodyReader(bytes.NewReader(buf.Bytes())) + } +} + +// setBodyReader writes the body from an io.Reader. +func (r *Request) setBodyReader(body io.Reader) error { + rc, ok := body.(io.ReadCloser) + if !ok && body != nil { + rc = ioutil.NopCloser(body) + } + r.Body = rc + if body != nil { + switch v := body.(type) { + case *strings.Reader: + r.ContentLength = int64(v.Len()) + case *bytes.Buffer: + r.ContentLength = int64(v.Len()) + } + } + return nil +} diff --git a/vendor/github.com/olivere/elastic/v7/rescore.go b/vendor/github.com/olivere/elastic/v7/rescore.go new file mode 100644 index 0000000000..9b7eaee1d8 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/rescore.go @@ -0,0 +1,44 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +type Rescore struct { + rescorer Rescorer + windowSize *int + defaultRescoreWindowSize *int +} + +func NewRescore() *Rescore { + return &Rescore{} +} + +func (r *Rescore) WindowSize(windowSize int) *Rescore { + r.windowSize = &windowSize + return r +} + +func (r *Rescore) IsEmpty() bool { + return r.rescorer == nil +} + +func (r *Rescore) Rescorer(rescorer Rescorer) *Rescore { + r.rescorer = rescorer + return r +} + +func (r *Rescore) Source() (interface{}, error) { + source := make(map[string]interface{}) + if r.windowSize != nil { + source["window_size"] = *r.windowSize + } else if r.defaultRescoreWindowSize != nil { + source["window_size"] = *r.defaultRescoreWindowSize + } + rescorerSrc, err := r.rescorer.Source() + if err != nil { + return nil, err + } + source[r.rescorer.Name()] = rescorerSrc + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/rescorer.go b/vendor/github.com/olivere/elastic/v7/rescorer.go new file mode 100644 index 0000000000..ccd4bb854b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/rescorer.go @@ -0,0 +1,64 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +type Rescorer interface { + Name() string + Source() (interface{}, error) +} + +// -- Query Rescorer -- + +type QueryRescorer struct { + query Query + rescoreQueryWeight *float64 + queryWeight *float64 + scoreMode string +} + +func NewQueryRescorer(query Query) *QueryRescorer { + return &QueryRescorer{ + query: query, + } +} + +func (r *QueryRescorer) Name() string { + return "query" +} + +func (r *QueryRescorer) RescoreQueryWeight(rescoreQueryWeight float64) *QueryRescorer { + r.rescoreQueryWeight = &rescoreQueryWeight + return r +} + +func (r *QueryRescorer) QueryWeight(queryWeight float64) *QueryRescorer { + r.queryWeight = &queryWeight + return r +} + +func (r *QueryRescorer) ScoreMode(scoreMode string) *QueryRescorer { + r.scoreMode = scoreMode + return r +} + +func (r *QueryRescorer) Source() (interface{}, error) { + rescoreQuery, err := r.query.Source() + if err != nil { + return nil, err + } + + source := make(map[string]interface{}) + source["rescore_query"] = rescoreQuery + if r.queryWeight != nil { + source["query_weight"] = *r.queryWeight + } + if r.rescoreQueryWeight != nil { + source["rescore_query_weight"] = *r.rescoreQueryWeight + } + if r.scoreMode != "" { + source["score_mode"] = r.scoreMode + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/response.go b/vendor/github.com/olivere/elastic/v7/response.go new file mode 100644 index 0000000000..200ed3d920 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/response.go @@ -0,0 +1,62 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "encoding/json" + "errors" + "io" + "io/ioutil" + "net/http" +) + +var ( + // ErrResponseSize is raised if a response body exceeds the given max body size. + ErrResponseSize = errors.New("elastic: response size too large") +) + +// Response represents a response from Elasticsearch. +type Response struct { + // StatusCode is the HTTP status code, e.g. 200. + StatusCode int + // Header is the HTTP header from the HTTP response. + // Keys in the map are canonicalized (see http.CanonicalHeaderKey). + Header http.Header + // Body is the deserialized response body. + Body json.RawMessage + // DeprecationWarnings lists all deprecation warnings returned from + // Elasticsearch. + DeprecationWarnings []string +} + +// newResponse creates a new response from the HTTP response. +func (c *Client) newResponse(res *http.Response, maxBodySize int64) (*Response, error) { + r := &Response{ + StatusCode: res.StatusCode, + Header: res.Header, + DeprecationWarnings: res.Header["Warning"], + } + if res.Body != nil { + body := io.Reader(res.Body) + if maxBodySize > 0 { + if res.ContentLength > maxBodySize { + return nil, ErrResponseSize + } + body = io.LimitReader(body, maxBodySize+1) + } + slurp, err := ioutil.ReadAll(body) + if err != nil { + return nil, err + } + if maxBodySize > 0 && int64(len(slurp)) > maxBodySize { + return nil, ErrResponseSize + } + // HEAD requests return a body but no content + if len(slurp) > 0 { + r.Body = json.RawMessage(slurp) + } + } + return r, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/retrier.go b/vendor/github.com/olivere/elastic/v7/retrier.go new file mode 100644 index 0000000000..e05ca66c4f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/retrier.go @@ -0,0 +1,68 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "net/http" + "time" +) + +// RetrierFunc specifies the signature of a Retry function, and is an adapter +// to allow the use of ordinary Retry functions. If f is a function with the +// appropriate signature, RetrierFunc(f) is a Retrier that calls f. +type RetrierFunc func(context.Context, int, *http.Request, *http.Response, error) (time.Duration, bool, error) + +// Retry calls f. +func (f RetrierFunc) Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error) { + return f(ctx, retry, req, resp, err) +} + +// Retrier decides whether to retry a failed HTTP request with Elasticsearch. +type Retrier interface { + // Retry is called when a request has failed. It decides whether to retry + // the call, how long to wait for the next call, or whether to return an + // error (which will be returned to the service that started the HTTP + // request in the first place). + // + // Callers may also use this to inspect the HTTP request/response and + // the error that happened. Additional data can be passed through via + // the context. + Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error) +} + +// -- StopRetrier -- + +// StopRetrier is an implementation that does no retries. +type StopRetrier struct { +} + +// NewStopRetrier returns a retrier that does no retries. +func NewStopRetrier() *StopRetrier { + return &StopRetrier{} +} + +// Retry does not retry. +func (r *StopRetrier) Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error) { + return 0, false, nil +} + +// -- BackoffRetrier -- + +// BackoffRetrier is an implementation that does nothing but return nil on Retry. +type BackoffRetrier struct { + backoff Backoff +} + +// NewBackoffRetrier returns a retrier that uses the given backoff strategy. +func NewBackoffRetrier(backoff Backoff) *BackoffRetrier { + return &BackoffRetrier{backoff: backoff} +} + +// Retry calls into the backoff strategy and its wait interval. +func (r *BackoffRetrier) Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error) { + wait, goahead := r.backoff.Next(retry) + return wait, goahead, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/retry.go b/vendor/github.com/olivere/elastic/v7/retry.go new file mode 100644 index 0000000000..3571a3b7a6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/retry.go @@ -0,0 +1,56 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +// This file is based on code (c) 2014 Cenk Altı and governed by the MIT license. +// See https://github.com/cenkalti/backoff for original source. + +package elastic + +import "time" + +// An Operation is executing by Retry() or RetryNotify(). +// The operation will be retried using a backoff policy if it returns an error. +type Operation func() error + +// Notify is a notify-on-error function. It receives error returned +// from an operation. +// +// Notice that if the backoff policy stated to stop retrying, +// the notify function isn't called. +type Notify func(error) + +// Retry the function f until it does not return error or BackOff stops. +// f is guaranteed to be run at least once. +// It is the caller's responsibility to reset b after Retry returns. +// +// Retry sleeps the goroutine for the duration returned by BackOff after a +// failed operation returns. +func Retry(o Operation, b Backoff) error { return RetryNotify(o, b, nil) } + +// RetryNotify calls notify function with the error and wait duration +// for each failed attempt before sleep. +func RetryNotify(operation Operation, b Backoff, notify Notify) error { + var err error + var wait time.Duration + var retry bool + var n int + + for { + if err = operation(); err == nil { + return nil + } + + n++ + wait, retry = b.Next(n) + if !retry { + return err + } + + if notify != nil { + notify(err) + } + + time.Sleep(wait) + } +} diff --git a/vendor/github.com/olivere/elastic/v7/run-es.sh b/vendor/github.com/olivere/elastic/v7/run-es.sh new file mode 100644 index 0000000000..8b60fbc95d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/run-es.sh @@ -0,0 +1,3 @@ +#!/bin/sh +VERSION=${VERSION:=6.4.0} +docker run --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch-oss:$VERSION elasticsearch -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_ diff --git a/vendor/github.com/olivere/elastic/v7/run-tests.sh b/vendor/github.com/olivere/elastic/v7/run-tests.sh new file mode 100644 index 0000000000..1204ad367a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/run-tests.sh @@ -0,0 +1,2 @@ +#!/bin/sh +go test . ./aws/... ./config/... ./trace/... ./uritemplates/... diff --git a/vendor/github.com/olivere/elastic/v7/script.go b/vendor/github.com/olivere/elastic/v7/script.go new file mode 100644 index 0000000000..ce3bd83d87 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/script.go @@ -0,0 +1,160 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "encoding/json" + "errors" + "fmt" + "strings" +) + +// Script holds all the parameters necessary to compile or find in cache +// and then execute a script. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html +// for details of scripting. +type Script struct { + script string + typ string + lang string + params map[string]interface{} +} + +// NewScript creates and initializes a new Script. By default, it is of +// type "inline". Use NewScriptStored for a stored script (where type is "id"). +func NewScript(script string) *Script { + return &Script{ + script: script, + typ: "inline", + params: make(map[string]interface{}), + } +} + +// NewScriptInline creates and initializes a new inline script, i.e. code. +func NewScriptInline(script string) *Script { + return NewScript(script).Type("inline") +} + +// NewScriptStored creates and initializes a new stored script. +func NewScriptStored(script string) *Script { + return NewScript(script).Type("id") +} + +// Script is either the cache key of the script to be compiled/executed +// or the actual script source code for inline scripts. For indexed +// scripts this is the id used in the request. For file scripts this is +// the file name. +func (s *Script) Script(script string) *Script { + s.script = script + return s +} + +// Type sets the type of script: "inline" or "id". +func (s *Script) Type(typ string) *Script { + s.typ = typ + return s +} + +// Lang sets the language of the script. The default scripting language +// is Painless ("painless"). +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html +// for details. +func (s *Script) Lang(lang string) *Script { + s.lang = lang + return s +} + +// Param adds a key/value pair to the parameters that this script will be executed with. +func (s *Script) Param(name string, value interface{}) *Script { + if s.params == nil { + s.params = make(map[string]interface{}) + } + s.params[name] = value + return s +} + +// Params sets the map of parameters this script will be executed with. +func (s *Script) Params(params map[string]interface{}) *Script { + s.params = params + return s +} + +// Source returns the JSON serializable data for this Script. +func (s *Script) Source() (interface{}, error) { + if s.typ == "" && s.lang == "" && len(s.params) == 0 { + return s.script, nil + } + source := make(map[string]interface{}) + // Beginning with 6.0, the type can only be "source" or "id" + if s.typ == "" || s.typ == "inline" { + src, err := s.rawScriptSource(s.script) + if err != nil { + return nil, err + } + source["source"] = src + } else { + source["id"] = s.script + } + if s.lang != "" { + source["lang"] = s.lang + } + if len(s.params) > 0 { + source["params"] = s.params + } + return source, nil +} + +// rawScriptSource returns an embeddable script. If it uses a short +// script form, e.g. "ctx._source.likes++" (without the quotes), it +// is quoted. Otherwise it returns the raw script that will be directly +// embedded into the JSON data. +func (s *Script) rawScriptSource(script string) (interface{}, error) { + v := strings.TrimSpace(script) + if !strings.HasPrefix(v, "{") && !strings.HasPrefix(v, `"`) { + v = fmt.Sprintf("%q", v) + } + raw := json.RawMessage(v) + return &raw, nil +} + +// -- Script Field -- + +// ScriptField is a single script field. +type ScriptField struct { + FieldName string // name of the field + + script *Script + ignoreFailure *bool // used in e.g. ScriptSource +} + +// NewScriptField creates and initializes a new ScriptField. +func NewScriptField(fieldName string, script *Script) *ScriptField { + return &ScriptField{FieldName: fieldName, script: script} +} + +// IgnoreFailure indicates whether to ignore failures. It is used +// in e.g. ScriptSource. +func (f *ScriptField) IgnoreFailure(ignore bool) *ScriptField { + f.ignoreFailure = &ignore + return f +} + +// Source returns the serializable JSON for the ScriptField. +func (f *ScriptField) Source() (interface{}, error) { + if f.script == nil { + return nil, errors.New("ScriptField expects script") + } + source := make(map[string]interface{}) + src, err := f.script.Source() + if err != nil { + return nil, err + } + source["script"] = src + if v := f.ignoreFailure; v != nil { + source["ignore_failure"] = *v + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/script_delete.go b/vendor/github.com/olivere/elastic/v7/script_delete.go new file mode 100644 index 0000000000..dc6833606c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/script_delete.go @@ -0,0 +1,186 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// DeleteScriptService removes a stored script in Elasticsearch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html +// for details. +type DeleteScriptService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + timeout string + masterTimeout string +} + +// NewDeleteScriptService creates a new DeleteScriptService. +func NewDeleteScriptService(client *Client) *DeleteScriptService { + return &DeleteScriptService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *DeleteScriptService) Pretty(pretty bool) *DeleteScriptService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *DeleteScriptService) Human(human bool) *DeleteScriptService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *DeleteScriptService) ErrorTrace(errorTrace bool) *DeleteScriptService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *DeleteScriptService) FilterPath(filterPath ...string) *DeleteScriptService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *DeleteScriptService) Header(name string, value string) *DeleteScriptService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *DeleteScriptService) Headers(headers http.Header) *DeleteScriptService { + s.headers = headers + return s +} + +// Id is the script ID. +func (s *DeleteScriptService) Id(id string) *DeleteScriptService { + s.id = id + return s +} + +// Timeout is an explicit operation timeout. +func (s *DeleteScriptService) Timeout(timeout string) *DeleteScriptService { + s.timeout = timeout + return s +} + +// MasterTimeout is the timeout for connecting to master. +func (s *DeleteScriptService) MasterTimeout(masterTimeout string) *DeleteScriptService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *DeleteScriptService) buildURL() (string, string, url.Values, error) { + var ( + err error + method = "DELETE" + path string + ) + + path, err = uritemplates.Expand("/_scripts/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timestamp", s.masterTimeout) + } + return method, path, params, nil +} + +// Validate checks if the operation is valid. +func (s *DeleteScriptService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *DeleteScriptService) Do(ctx context.Context) (*DeleteScriptResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + method, path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: method, + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(DeleteScriptResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// DeleteScriptResponse is the result of deleting a stored script +// in Elasticsearch. +type DeleteScriptResponse struct { + AcknowledgedResponse +} diff --git a/vendor/github.com/olivere/elastic/v7/script_get.go b/vendor/github.com/olivere/elastic/v7/script_get.go new file mode 100644 index 0000000000..81cedac710 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/script_get.go @@ -0,0 +1,169 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// GetScriptService reads a stored script in Elasticsearch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html +// for details. +type GetScriptService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string +} + +// NewGetScriptService creates a new GetScriptService. +func NewGetScriptService(client *Client) *GetScriptService { + return &GetScriptService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *GetScriptService) Pretty(pretty bool) *GetScriptService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *GetScriptService) Human(human bool) *GetScriptService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *GetScriptService) ErrorTrace(errorTrace bool) *GetScriptService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *GetScriptService) FilterPath(filterPath ...string) *GetScriptService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *GetScriptService) Header(name string, value string) *GetScriptService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *GetScriptService) Headers(headers http.Header) *GetScriptService { + s.headers = headers + return s +} + +// Id is the script ID. +func (s *GetScriptService) Id(id string) *GetScriptService { + s.id = id + return s +} + +// buildURL builds the URL for the operation. +func (s *GetScriptService) buildURL() (string, string, url.Values, error) { + var ( + err error + method = "GET" + path string + ) + + path, err = uritemplates.Expand("/_scripts/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return method, path, params, nil +} + +// Validate checks if the operation is valid. +func (s *GetScriptService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *GetScriptService) Do(ctx context.Context) (*GetScriptResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + method, path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: method, + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(GetScriptResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// GetScriptResponse is the result of getting a stored script +// in Elasticsearch. +type GetScriptResponse struct { + Id string `json:"_id"` + Found bool `json:"found"` + Script json.RawMessage `json:"script"` +} diff --git a/vendor/github.com/olivere/elastic/v7/script_put.go b/vendor/github.com/olivere/elastic/v7/script_put.go new file mode 100644 index 0000000000..f8b831a29d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/script_put.go @@ -0,0 +1,226 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// PutScriptService adds or updates a stored script in Elasticsearch. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html +// for details. +type PutScriptService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + context string + timeout string + masterTimeout string + bodyJson interface{} + bodyString string +} + +// NewPutScriptService creates a new PutScriptService. +func NewPutScriptService(client *Client) *PutScriptService { + return &PutScriptService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *PutScriptService) Pretty(pretty bool) *PutScriptService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *PutScriptService) Human(human bool) *PutScriptService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *PutScriptService) ErrorTrace(errorTrace bool) *PutScriptService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *PutScriptService) FilterPath(filterPath ...string) *PutScriptService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *PutScriptService) Header(name string, value string) *PutScriptService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *PutScriptService) Headers(headers http.Header) *PutScriptService { + s.headers = headers + return s +} + +// Id is the script ID. +func (s *PutScriptService) Id(id string) *PutScriptService { + s.id = id + return s +} + +// Context specifies the script context (optional). +func (s *PutScriptService) Context(context string) *PutScriptService { + s.context = context + return s +} + +// Timeout is an explicit operation timeout. +func (s *PutScriptService) Timeout(timeout string) *PutScriptService { + s.timeout = timeout + return s +} + +// MasterTimeout is the timeout for connecting to master. +func (s *PutScriptService) MasterTimeout(masterTimeout string) *PutScriptService { + s.masterTimeout = masterTimeout + return s +} + +// BodyJson is the document as a serializable JSON interface. +func (s *PutScriptService) BodyJson(body interface{}) *PutScriptService { + s.bodyJson = body + return s +} + +// BodyString is the document encoded as a string. +func (s *PutScriptService) BodyString(body string) *PutScriptService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *PutScriptService) buildURL() (string, string, url.Values, error) { + var ( + err error + method = "PUT" + path string + ) + + if s.context != "" { + path, err = uritemplates.Expand("/_scripts/{id}/{context}", map[string]string{ + "id": s.id, + "context": s.context, + }) + } else { + path, err = uritemplates.Expand("/_scripts/{id}", map[string]string{ + "id": s.id, + }) + } + if err != nil { + return "", "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timestamp", s.masterTimeout) + } + return method, path, params, nil +} + +// Validate checks if the operation is valid. +func (s *PutScriptService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *PutScriptService) Do(ctx context.Context) (*PutScriptResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + method, path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: method, + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(PutScriptResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// PutScriptResponse is the result of saving a stored script +// in Elasticsearch. +type PutScriptResponse struct { + AcknowledgedResponse +} diff --git a/vendor/github.com/olivere/elastic/v7/scroll.go b/vendor/github.com/olivere/elastic/v7/scroll.go new file mode 100644 index 0000000000..53639599fa --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/scroll.go @@ -0,0 +1,590 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "io" + "net/http" + "net/url" + "strings" + "sync" + + "github.com/olivere/elastic/v7/uritemplates" +) + +const ( + // DefaultScrollKeepAlive is the default time a scroll cursor will be kept alive. + DefaultScrollKeepAlive = "5m" +) + +// ScrollService iterates over pages of search results from Elasticsearch. +type ScrollService struct { + client *Client + retrier Retrier + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + indices []string + types []string + keepAlive string + body interface{} + ss *SearchSource + size *int + routing string + preference string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + maxResponseSize int64 + + mu sync.RWMutex + scrollId string +} + +// NewScrollService initializes and returns a new ScrollService. +func NewScrollService(client *Client) *ScrollService { + builder := &ScrollService{ + client: client, + ss: NewSearchSource(), + keepAlive: DefaultScrollKeepAlive, + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ScrollService) Pretty(pretty bool) *ScrollService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ScrollService) Human(human bool) *ScrollService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ScrollService) ErrorTrace(errorTrace bool) *ScrollService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ScrollService) FilterPath(filterPath ...string) *ScrollService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ScrollService) Header(name string, value string) *ScrollService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ScrollService) Headers(headers http.Header) *ScrollService { + s.headers = headers + return s +} + +// Retrier allows to set specific retry logic for this ScrollService. +// If not specified, it will use the client's default retrier. +func (s *ScrollService) Retrier(retrier Retrier) *ScrollService { + s.retrier = retrier + return s +} + +// Index sets the name of one or more indices to iterate over. +func (s *ScrollService) Index(indices ...string) *ScrollService { + if s.indices == nil { + s.indices = make([]string, 0) + } + s.indices = append(s.indices, indices...) + return s +} + +// Type sets the name of one or more types to iterate over. +func (s *ScrollService) Type(types ...string) *ScrollService { + if s.types == nil { + s.types = make([]string, 0) + } + s.types = append(s.types, types...) + return s +} + +// Scroll is an alias for KeepAlive, the time to keep +// the cursor alive (e.g. "5m" for 5 minutes). +func (s *ScrollService) Scroll(keepAlive string) *ScrollService { + s.keepAlive = keepAlive + return s +} + +// KeepAlive sets the maximum time after which the cursor will expire. +// It is "5m" by default. +func (s *ScrollService) KeepAlive(keepAlive string) *ScrollService { + s.keepAlive = keepAlive + return s +} + +// Size specifies the number of documents Elasticsearch should return +// from each shard, per page. +func (s *ScrollService) Size(size int) *ScrollService { + s.size = &size + return s +} + +// Highlight allows to highlight search results on one or more fields +func (s *ScrollService) Highlight(highlight *Highlight) *ScrollService { + s.ss = s.ss.Highlight(highlight) + return s +} + +// Body sets the raw body to send to Elasticsearch. This can be e.g. a string, +// a map[string]interface{} or anything that can be serialized into JSON. +// Notice that setting the body disables the use of SearchSource and many +// other properties of the ScanService. +func (s *ScrollService) Body(body interface{}) *ScrollService { + s.body = body + return s +} + +// SearchSource sets the search source builder to use with this iterator. +// Notice that only a certain number of properties can be used when scrolling, +// e.g. query and sorting. +func (s *ScrollService) SearchSource(searchSource *SearchSource) *ScrollService { + s.ss = searchSource + if s.ss == nil { + s.ss = NewSearchSource() + } + return s +} + +// Query sets the query to perform, e.g. a MatchAllQuery. +func (s *ScrollService) Query(query Query) *ScrollService { + s.ss = s.ss.Query(query) + return s +} + +// PostFilter is executed as the last filter. It only affects the +// search hits but not facets. See +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-post-filter.html +// for details. +func (s *ScrollService) PostFilter(postFilter Query) *ScrollService { + s.ss = s.ss.PostFilter(postFilter) + return s +} + +// Slice allows slicing the scroll request into several batches. +// This is supported in Elasticsearch 5.0 or later. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-scroll.html#sliced-scroll +// for details. +func (s *ScrollService) Slice(sliceQuery Query) *ScrollService { + s.ss = s.ss.Slice(sliceQuery) + return s +} + +// FetchSource indicates whether the response should contain the stored +// _source for every hit. +func (s *ScrollService) FetchSource(fetchSource bool) *ScrollService { + s.ss = s.ss.FetchSource(fetchSource) + return s +} + +// FetchSourceContext indicates how the _source should be fetched. +func (s *ScrollService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *ScrollService { + s.ss = s.ss.FetchSourceContext(fetchSourceContext) + return s +} + +// Version can be set to true to return a version for each search hit. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-version.html. +func (s *ScrollService) Version(version bool) *ScrollService { + s.ss = s.ss.Version(version) + return s +} + +// Sort adds a sort order. This can have negative effects on the performance +// of the scroll operation as Elasticsearch needs to sort first. +func (s *ScrollService) Sort(field string, ascending bool) *ScrollService { + s.ss = s.ss.Sort(field, ascending) + return s +} + +// SortWithInfo specifies a sort order. Notice that sorting can have a +// negative impact on scroll performance. +func (s *ScrollService) SortWithInfo(info SortInfo) *ScrollService { + s.ss = s.ss.SortWithInfo(info) + return s +} + +// SortBy specifies a sort order. Notice that sorting can have a +// negative impact on scroll performance. +func (s *ScrollService) SortBy(sorter ...Sorter) *ScrollService { + s.ss = s.ss.SortBy(sorter...) + return s +} + +// TrackTotalHits controls if the total hit count for the query should be tracked. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-request-track-total-hits.html +// for details. +func (s *ScrollService) TrackTotalHits(trackTotalHits interface{}) *ScrollService { + s.ss = s.ss.TrackTotalHits(trackTotalHits) + return s +} + +// Routing is a list of specific routing values to control the shards +// the search will be executed on. +func (s *ScrollService) Routing(routings ...string) *ScrollService { + s.routing = strings.Join(routings, ",") + return s +} + +// Preference sets the preference to execute the search. Defaults to +// randomize across shards ("random"). Can be set to "_local" to prefer +// local shards, "_primary" to execute on primary shards only, +// or a custom value which guarantees that the same order will be used +// across different requests. +func (s *ScrollService) Preference(preference string) *ScrollService { + s.preference = preference + return s +} + +// IgnoreUnavailable indicates whether the specified concrete indices +// should be ignored when unavailable (missing or closed). +func (s *ScrollService) IgnoreUnavailable(ignoreUnavailable bool) *ScrollService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` string +// or when no indices have been specified). +func (s *ScrollService) AllowNoIndices(allowNoIndices bool) *ScrollService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *ScrollService) ExpandWildcards(expandWildcards string) *ScrollService { + s.expandWildcards = expandWildcards + return s +} + +// MaxResponseSize sets an upper limit on the response body size that we accept, +// to guard against OOM situations. +func (s *ScrollService) MaxResponseSize(maxResponseSize int64) *ScrollService { + s.maxResponseSize = maxResponseSize + return s +} + +// ScrollId specifies the identifier of a scroll in action. +func (s *ScrollService) ScrollId(scrollId string) *ScrollService { + s.mu.Lock() + s.scrollId = scrollId + s.mu.Unlock() + return s +} + +// Do returns the next search result. It will return io.EOF as error if there +// are no more search results. +func (s *ScrollService) Do(ctx context.Context) (*SearchResult, error) { + s.mu.RLock() + nextScrollId := s.scrollId + s.mu.RUnlock() + if len(nextScrollId) == 0 { + return s.first(ctx) + } + return s.next(ctx) +} + +// Clear cancels the current scroll operation. If you don't do this manually, +// the scroll will be expired automatically by Elasticsearch. You can control +// how long a scroll cursor is kept alive with the KeepAlive func. +func (s *ScrollService) Clear(ctx context.Context) error { + s.mu.RLock() + scrollId := s.scrollId + s.mu.RUnlock() + if len(scrollId) == 0 { + return nil + } + + path := "/_search/scroll" + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + body := struct { + ScrollId []string `json:"scroll_id,omitempty"` + }{ + ScrollId: []string{scrollId}, + } + + _, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Body: body, + Retrier: s.retrier, + }) + if err != nil { + return err + } + + return nil +} + +// -- First -- + +// first takes the first page of search results. +func (s *ScrollService) first(ctx context.Context) (*SearchResult, error) { + // Get URL and parameters for request + path, params, err := s.buildFirstURL() + if err != nil { + return nil, err + } + + // Get HTTP request body + body, err := s.bodyFirst() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Retrier: s.retrier, + Headers: s.headers, + MaxResponseSize: s.maxResponseSize, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SearchResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + s.mu.Lock() + s.scrollId = ret.ScrollId + s.mu.Unlock() + if ret.Hits == nil || len(ret.Hits.Hits) == 0 { + return ret, io.EOF + } + return ret, nil +} + +// buildFirstURL builds the URL for retrieving the first page. +func (s *ScrollService) buildFirstURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.indices) == 0 && len(s.types) == 0 { + path = "/_search" + } else if len(s.indices) > 0 && len(s.types) == 0 { + path, err = uritemplates.Expand("/{index}/_search", map[string]string{ + "index": strings.Join(s.indices, ","), + }) + } else if len(s.indices) == 0 && len(s.types) > 0 { + path, err = uritemplates.Expand("/_all/{typ}/_search", map[string]string{ + "typ": strings.Join(s.types, ","), + }) + } else { + path, err = uritemplates.Expand("/{index}/{typ}/_search", map[string]string{ + "index": strings.Join(s.indices, ","), + "typ": strings.Join(s.types, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + // Always add "hits._scroll_id", otherwise we cannot scroll + var found bool + for _, path := range s.filterPath { + if path == "_scroll_id" { + found = true + break + } + } + if !found { + s.filterPath = append(s.filterPath, "_scroll_id") + } + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.size != nil && *s.size > 0 { + params.Set("size", fmt.Sprintf("%d", *s.size)) + } + if len(s.keepAlive) > 0 { + params.Set("scroll", s.keepAlive) + } + if len(s.routing) > 0 { + params.Set("routing", s.routing) + } + if len(s.preference) > 0 { + params.Set("preference", s.preference) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if len(s.expandWildcards) > 0 { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + + return path, params, nil +} + +// bodyFirst returns the request to fetch the first batch of results. +func (s *ScrollService) bodyFirst() (interface{}, error) { + var err error + var body interface{} + + if s.body != nil { + body = s.body + } else { + // Use _doc sort by default if none is specified + if !s.ss.hasSort() { + // Use efficient sorting when no user-defined query/body is specified + s.ss = s.ss.SortBy(SortByDoc{}) + } + + // Body from search source + body, err = s.ss.Source() + if err != nil { + return nil, err + } + } + + return body, nil +} + +// -- Next -- + +func (s *ScrollService) next(ctx context.Context) (*SearchResult, error) { + // Get URL for request + path, params, err := s.buildNextURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body, err := s.bodyNext() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Retrier: s.retrier, + Headers: s.headers, + MaxResponseSize: s.maxResponseSize, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SearchResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + s.mu.Lock() + s.scrollId = ret.ScrollId + s.mu.Unlock() + if ret.Hits == nil || len(ret.Hits.Hits) == 0 { + return ret, io.EOF + } + return ret, nil +} + +// buildNextURL builds the URL for the operation. +func (s *ScrollService) buildNextURL() (string, url.Values, error) { + path := "/_search/scroll" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + // Always add "hits._scroll_id", otherwise we cannot scroll + var found bool + for _, path := range s.filterPath { + if path == "_scroll_id" { + found = true + break + } + } + if !found { + s.filterPath = append(s.filterPath, "_scroll_id") + } + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + + return path, params, nil +} + +// body returns the request to fetch the next batch of results. +func (s *ScrollService) bodyNext() (interface{}, error) { + s.mu.RLock() + body := struct { + Scroll string `json:"scroll"` + ScrollId string `json:"scroll_id,omitempty"` + }{ + Scroll: s.keepAlive, + ScrollId: s.scrollId, + } + s.mu.RUnlock() + return body, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search.go b/vendor/github.com/olivere/elastic/v7/search.go new file mode 100644 index 0000000000..4c386997c9 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search.go @@ -0,0 +1,831 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "reflect" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// Search for documents in Elasticsearch. +type SearchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + searchSource *SearchSource // q + source interface{} + searchType string // search_type + index []string + typ []string + routing string // routing + preference string // preference + requestCache *bool // request_cache + ignoreUnavailable *bool // ignore_unavailable + ignoreThrottled *bool // ignore_throttled + allowNoIndices *bool // allow_no_indices + expandWildcards string // expand_wildcards + lenient *bool // lenient + maxResponseSize int64 + allowPartialSearchResults *bool // allow_partial_search_results + typedKeys *bool // typed_keys + seqNoPrimaryTerm *bool // seq_no_primary_term + batchedReduceSize *int // batched_reduce_size + maxConcurrentShardRequests *int // max_concurrent_shard_requests + preFilterShardSize *int // pre_filter_shard_size + restTotalHitsAsInt *bool // rest_total_hits_as_int + + ccsMinimizeRoundtrips *bool // ccs_minimize_roundtrips + +} + +// NewSearchService creates a new service for searching in Elasticsearch. +func NewSearchService(client *Client) *SearchService { + builder := &SearchService{ + client: client, + searchSource: NewSearchSource(), + } + return builder +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SearchService) Pretty(pretty bool) *SearchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SearchService) Human(human bool) *SearchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SearchService) ErrorTrace(errorTrace bool) *SearchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SearchService) FilterPath(filterPath ...string) *SearchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SearchService) Header(name string, value string) *SearchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SearchService) Headers(headers http.Header) *SearchService { + s.headers = headers + return s +} + +// SearchSource sets the search source builder to use with this service. +func (s *SearchService) SearchSource(searchSource *SearchSource) *SearchService { + s.searchSource = searchSource + if s.searchSource == nil { + s.searchSource = NewSearchSource() + } + return s +} + +// Source allows the user to set the request body manually without using +// any of the structs and interfaces in Elastic. +func (s *SearchService) Source(source interface{}) *SearchService { + s.source = source + return s +} + +// Index sets the names of the indices to use for search. +func (s *SearchService) Index(index ...string) *SearchService { + s.index = append(s.index, index...) + return s +} + +// Type adds search restrictions for a list of types. +// +// Deprecated: Types are in the process of being removed. Instead of using a type, prefer to +// filter on a field on the document. +func (s *SearchService) Type(typ ...string) *SearchService { + s.typ = append(s.typ, typ...) + return s +} + +// Timeout sets the timeout to use, e.g. "1s" or "1000ms". +func (s *SearchService) Timeout(timeout string) *SearchService { + s.searchSource = s.searchSource.Timeout(timeout) + return s +} + +// Profile sets the Profile API flag on the search source. +// When enabled, a search executed by this service will return query +// profiling data. +func (s *SearchService) Profile(profile bool) *SearchService { + s.searchSource = s.searchSource.Profile(profile) + return s +} + +// Collapse adds field collapsing. +func (s *SearchService) Collapse(collapse *CollapseBuilder) *SearchService { + s.searchSource = s.searchSource.Collapse(collapse) + return s +} + +// TimeoutInMillis sets the timeout in milliseconds. +func (s *SearchService) TimeoutInMillis(timeoutInMillis int) *SearchService { + s.searchSource = s.searchSource.TimeoutInMillis(timeoutInMillis) + return s +} + +// TerminateAfter specifies the maximum number of documents to collect for +// each shard, upon reaching which the query execution will terminate early. +func (s *SearchService) TerminateAfter(terminateAfter int) *SearchService { + s.searchSource = s.searchSource.TerminateAfter(terminateAfter) + return s +} + +// SearchType sets the search operation type. Valid values are: +// "dfs_query_then_fetch" and "query_then_fetch". +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-search-type.html +// for details. +func (s *SearchService) SearchType(searchType string) *SearchService { + s.searchType = searchType + return s +} + +// Routing is a list of specific routing values to control the shards +// the search will be executed on. +func (s *SearchService) Routing(routings ...string) *SearchService { + s.routing = strings.Join(routings, ",") + return s +} + +// Preference sets the preference to execute the search. Defaults to +// randomize across shards ("random"). Can be set to "_local" to prefer +// local shards, "_primary" to execute on primary shards only, +// or a custom value which guarantees that the same order will be used +// across different requests. +func (s *SearchService) Preference(preference string) *SearchService { + s.preference = preference + return s +} + +// RequestCache indicates whether the cache should be used for this +// request or not, defaults to index level setting. +func (s *SearchService) RequestCache(requestCache bool) *SearchService { + s.requestCache = &requestCache + return s +} + +// Query sets the query to perform, e.g. MatchAllQuery. +func (s *SearchService) Query(query Query) *SearchService { + s.searchSource = s.searchSource.Query(query) + return s +} + +// PostFilter will be executed after the query has been executed and +// only affects the search hits, not the aggregations. +// This filter is always executed as the last filtering mechanism. +func (s *SearchService) PostFilter(postFilter Query) *SearchService { + s.searchSource = s.searchSource.PostFilter(postFilter) + return s +} + +// FetchSource indicates whether the response should contain the stored +// _source for every hit. +func (s *SearchService) FetchSource(fetchSource bool) *SearchService { + s.searchSource = s.searchSource.FetchSource(fetchSource) + return s +} + +// FetchSourceContext indicates how the _source should be fetched. +func (s *SearchService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *SearchService { + s.searchSource = s.searchSource.FetchSourceContext(fetchSourceContext) + return s +} + +// Highlight adds highlighting to the search. +func (s *SearchService) Highlight(highlight *Highlight) *SearchService { + s.searchSource = s.searchSource.Highlight(highlight) + return s +} + +// GlobalSuggestText defines the global text to use with all suggesters. +// This avoids repetition. +func (s *SearchService) GlobalSuggestText(globalText string) *SearchService { + s.searchSource = s.searchSource.GlobalSuggestText(globalText) + return s +} + +// Suggester adds a suggester to the search. +func (s *SearchService) Suggester(suggester Suggester) *SearchService { + s.searchSource = s.searchSource.Suggester(suggester) + return s +} + +// Aggregation adds an aggreation to perform as part of the search. +func (s *SearchService) Aggregation(name string, aggregation Aggregation) *SearchService { + s.searchSource = s.searchSource.Aggregation(name, aggregation) + return s +} + +// MinScore sets the minimum score below which docs will be filtered out. +func (s *SearchService) MinScore(minScore float64) *SearchService { + s.searchSource = s.searchSource.MinScore(minScore) + return s +} + +// From index to start the search from. Defaults to 0. +func (s *SearchService) From(from int) *SearchService { + s.searchSource = s.searchSource.From(from) + return s +} + +// Size is the number of search hits to return. Defaults to 10. +func (s *SearchService) Size(size int) *SearchService { + s.searchSource = s.searchSource.Size(size) + return s +} + +// Explain indicates whether each search hit should be returned with +// an explanation of the hit (ranking). +func (s *SearchService) Explain(explain bool) *SearchService { + s.searchSource = s.searchSource.Explain(explain) + return s +} + +// Version indicates whether each search hit should be returned with +// a version associated to it. +func (s *SearchService) Version(version bool) *SearchService { + s.searchSource = s.searchSource.Version(version) + return s +} + +// Sort adds a sort order. +func (s *SearchService) Sort(field string, ascending bool) *SearchService { + s.searchSource = s.searchSource.Sort(field, ascending) + return s +} + +// SortWithInfo adds a sort order. +func (s *SearchService) SortWithInfo(info SortInfo) *SearchService { + s.searchSource = s.searchSource.SortWithInfo(info) + return s +} + +// SortBy adds a sort order. +func (s *SearchService) SortBy(sorter ...Sorter) *SearchService { + s.searchSource = s.searchSource.SortBy(sorter...) + return s +} + +// DocvalueField adds a single field to load from the field data cache +// and return as part of the search. +func (s *SearchService) DocvalueField(docvalueField string) *SearchService { + s.searchSource = s.searchSource.DocvalueField(docvalueField) + return s +} + +// DocvalueFieldWithFormat adds a single field to load from the field data cache +// and return as part of the search. +func (s *SearchService) DocvalueFieldWithFormat(docvalueField DocvalueField) *SearchService { + s.searchSource = s.searchSource.DocvalueFieldWithFormat(docvalueField) + return s +} + +// DocvalueFields adds one or more fields to load from the field data cache +// and return as part of the search. +func (s *SearchService) DocvalueFields(docvalueFields ...string) *SearchService { + s.searchSource = s.searchSource.DocvalueFields(docvalueFields...) + return s +} + +// DocvalueFieldsWithFormat adds one or more fields to load from the field data cache +// and return as part of the search. +func (s *SearchService) DocvalueFieldsWithFormat(docvalueFields ...DocvalueField) *SearchService { + s.searchSource = s.searchSource.DocvalueFieldsWithFormat(docvalueFields...) + return s +} + +// NoStoredFields indicates that no stored fields should be loaded, resulting in only +// id and type to be returned per field. +func (s *SearchService) NoStoredFields() *SearchService { + s.searchSource = s.searchSource.NoStoredFields() + return s +} + +// StoredField adds a single field to load and return (note, must be stored) as +// part of the search request. If none are specified, the source of the +// document will be returned. +func (s *SearchService) StoredField(fieldName string) *SearchService { + s.searchSource = s.searchSource.StoredField(fieldName) + return s +} + +// StoredFields sets the fields to load and return as part of the search request. +// If none are specified, the source of the document will be returned. +func (s *SearchService) StoredFields(fields ...string) *SearchService { + s.searchSource = s.searchSource.StoredFields(fields...) + return s +} + +// TrackScores is applied when sorting and controls if scores will be +// tracked as well. Defaults to false. +func (s *SearchService) TrackScores(trackScores bool) *SearchService { + s.searchSource = s.searchSource.TrackScores(trackScores) + return s +} + +// TrackTotalHits controls if the total hit count for the query should be tracked. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-request-track-total-hits.html +// for details. +func (s *SearchService) TrackTotalHits(trackTotalHits interface{}) *SearchService { + s.searchSource = s.searchSource.TrackTotalHits(trackTotalHits) + return s +} + +// SearchAfter allows a different form of pagination by using a live cursor, +// using the results of the previous page to help the retrieval of the next. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-search-after.html +func (s *SearchService) SearchAfter(sortValues ...interface{}) *SearchService { + s.searchSource = s.searchSource.SearchAfter(sortValues...) + return s +} + +// IgnoreUnavailable indicates whether the specified concrete indices +// should be ignored when unavailable (missing or closed). +func (s *SearchService) IgnoreUnavailable(ignoreUnavailable bool) *SearchService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// IgnoreThrottled indicates whether specified concrete, expanded or aliased +// indices should be ignored when throttled. +func (s *SearchService) IgnoreThrottled(ignoreThrottled bool) *SearchService { + s.ignoreThrottled = &ignoreThrottled + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` string +// or when no indices have been specified). +func (s *SearchService) AllowNoIndices(allowNoIndices bool) *SearchService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *SearchService) ExpandWildcards(expandWildcards string) *SearchService { + s.expandWildcards = expandWildcards + return s +} + +// Lenient specifies whether format-based query failures (such as providing +// text to a numeric field) should be ignored. +func (s *SearchService) Lenient(lenient bool) *SearchService { + s.lenient = &lenient + return s +} + +// MaxResponseSize sets an upper limit on the response body size that we accept, +// to guard against OOM situations. +func (s *SearchService) MaxResponseSize(maxResponseSize int64) *SearchService { + s.maxResponseSize = maxResponseSize + return s +} + +// AllowPartialSearchResults indicates if an error should be returned if +// there is a partial search failure or timeout. +func (s *SearchService) AllowPartialSearchResults(enabled bool) *SearchService { + s.allowPartialSearchResults = &enabled + return s +} + +// TypedKeys specifies whether aggregation and suggester names should be +// prefixed by their respective types in the response. +func (s *SearchService) TypedKeys(enabled bool) *SearchService { + s.typedKeys = &enabled + return s +} + +// SeqNoPrimaryTerm specifies whether to return sequence number and +// primary term of the last modification of each hit. +func (s *SearchService) SeqNoPrimaryTerm(enabled bool) *SearchService { + s.seqNoPrimaryTerm = &enabled + return s +} + +// BatchedReduceSize specifies the number of shard results that should be reduced +// at once on the coordinating node. This value should be used as a protection +// mechanism to reduce the memory overhead per search request if the potential +// number of shards in the request can be large. +func (s *SearchService) BatchedReduceSize(size int) *SearchService { + s.batchedReduceSize = &size + return s +} + +// MaxConcurrentShardRequests specifies the number of concurrent shard requests +// this search executes concurrently. This value should be used to limit the +// impact of the search on the cluster in order to limit the number of +// concurrent shard requests. +func (s *SearchService) MaxConcurrentShardRequests(max int) *SearchService { + s.maxConcurrentShardRequests = &max + return s +} + +// PreFilterShardSize specifies a threshold that enforces a pre-filter roundtrip +// to prefilter search shards based on query rewriting if the number of shards +// the search request expands to exceeds the threshold. This filter roundtrip +// can limit the number of shards significantly if for instance a shard can +// not match any documents based on it's rewrite method i.e. if date filters are +// mandatory to match but the shard bounds and the query are disjoint. +func (s *SearchService) PreFilterShardSize(threshold int) *SearchService { + s.preFilterShardSize = &threshold + return s +} + +// RestTotalHitsAsInt indicates whether hits.total should be rendered as an +// integer or an object in the rest search response. +func (s *SearchService) RestTotalHitsAsInt(enabled bool) *SearchService { + s.restTotalHitsAsInt = &enabled + return s +} + +// CCSMinimizeRoundtrips indicates whether network round-trips should be minimized +// as part of cross-cluster search requests execution. +func (s *SearchService) CCSMinimizeRoundtrips(enabled bool) *SearchService { + s.ccsMinimizeRoundtrips = &enabled + return s +} + +// buildURL builds the URL for the operation. +func (s *SearchService) buildURL() (string, url.Values, error) { + var err error + var path string + + if len(s.index) > 0 && len(s.typ) > 0 { + path, err = uritemplates.Expand("/{index}/{type}/_search", map[string]string{ + "index": strings.Join(s.index, ","), + "type": strings.Join(s.typ, ","), + }) + } else if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_search", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else if len(s.typ) > 0 { + path, err = uritemplates.Expand("/_all/{type}/_search", map[string]string{ + "type": strings.Join(s.typ, ","), + }) + } else { + path = "/_search" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.searchType != "" { + params.Set("search_type", s.searchType) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if v := s.requestCache; v != nil { + params.Set("request_cache", fmt.Sprint(*v)) + } + if v := s.allowNoIndices; v != nil { + params.Set("allow_no_indices", fmt.Sprint(*v)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if v := s.lenient; v != nil { + params.Set("lenient", fmt.Sprint(*v)) + } + if v := s.ignoreUnavailable; v != nil { + params.Set("ignore_unavailable", fmt.Sprint(*v)) + } + if v := s.ignoreThrottled; v != nil { + params.Set("ignore_throttled", fmt.Sprint(*v)) + } + if s.seqNoPrimaryTerm != nil { + params.Set("seq_no_primary_term", fmt.Sprint(*s.seqNoPrimaryTerm)) + } + if v := s.allowPartialSearchResults; v != nil { + params.Set("allow_partial_search_results", fmt.Sprint(*v)) + } + if v := s.typedKeys; v != nil { + params.Set("typed_keys", fmt.Sprint(*v)) + } + if v := s.batchedReduceSize; v != nil { + params.Set("batched_reduce_size", fmt.Sprint(*v)) + } + if v := s.maxConcurrentShardRequests; v != nil { + params.Set("max_concurrent_shard_requests", fmt.Sprint(*v)) + } + if v := s.preFilterShardSize; v != nil { + params.Set("pre_filter_shard_size", fmt.Sprint(*v)) + } + if v := s.restTotalHitsAsInt; v != nil { + params.Set("rest_total_hits_as_int", fmt.Sprint(*v)) + } + if v := s.ccsMinimizeRoundtrips; v != nil { + params.Set("ccs_minimize_roundtrips", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SearchService) Validate() error { + return nil +} + +// Do executes the search and returns a SearchResult. +func (s *SearchService) Do(ctx context.Context) (*SearchResult, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Perform request + var body interface{} + if s.source != nil { + body = s.source + } else { + src, err := s.searchSource.Source() + if err != nil { + return nil, err + } + body = src + } + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + MaxResponseSize: s.maxResponseSize, + }) + if err != nil { + return nil, err + } + + // Return search results + ret := new(SearchResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + ret.Header = res.Header + return nil, err + } + ret.Header = res.Header + return ret, nil +} + +// SearchResult is the result of a search in Elasticsearch. +type SearchResult struct { + Header http.Header `json:"-"` + TookInMillis int64 `json:"took,omitempty"` // search time in milliseconds + TerminatedEarly bool `json:"terminated_early,omitempty"` // request terminated early + NumReducePhases int `json:"num_reduce_phases,omitempty"` + Clusters []*SearchResultCluster `json:"_clusters,omitempty"` // 6.1.0+ + ScrollId string `json:"_scroll_id,omitempty"` // only used with Scroll and Scan operations + Hits *SearchHits `json:"hits,omitempty"` // the actual search hits + Suggest SearchSuggest `json:"suggest,omitempty"` // results from suggesters + Aggregations Aggregations `json:"aggregations,omitempty"` // results from aggregations + TimedOut bool `json:"timed_out,omitempty"` // true if the search timed out + Error *ErrorDetails `json:"error,omitempty"` // only used in MultiGet + Profile *SearchProfile `json:"profile,omitempty"` // profiling results, if optional Profile API was active for this search + Shards *ShardsInfo `json:"_shards,omitempty"` // shard information + Status int `json:"status,omitempty"` // used in MultiSearch +} + +// SearchResultCluster holds information about a search response +// from a cluster. +type SearchResultCluster struct { + Successful int `json:"successful,omitempty"` + Total int `json:"total,omitempty"` + Skipped int `json:"skipped,omitempty"` +} + +// TotalHits is a convenience function to return the number of hits for +// a search result. The return value might not be accurate, unless +// track_total_hits parameter has set to true. +func (r *SearchResult) TotalHits() int64 { + if r.Hits != nil && r.Hits.TotalHits != nil { + return r.Hits.TotalHits.Value + } + return 0 +} + +// Each is a utility function to iterate over all hits. It saves you from +// checking for nil values. Notice that Each will ignore errors in +// serializing JSON and hits with empty/nil _source will get an empty +// value +func (r *SearchResult) Each(typ reflect.Type) []interface{} { + if r.Hits == nil || r.Hits.Hits == nil || len(r.Hits.Hits) == 0 { + return nil + } + var slice []interface{} + for _, hit := range r.Hits.Hits { + v := reflect.New(typ).Elem() + if hit.Source == nil { + slice = append(slice, v.Interface()) + continue + } + if err := json.Unmarshal(hit.Source, v.Addr().Interface()); err == nil { + slice = append(slice, v.Interface()) + } + } + return slice +} + +// SearchHits specifies the list of search hits. +type SearchHits struct { + TotalHits *TotalHits `json:"total,omitempty"` // total number of hits found + MaxScore *float64 `json:"max_score,omitempty"` // maximum score of all hits + Hits []*SearchHit `json:"hits,omitempty"` // the actual hits returned +} + +// NestedHit is a nested innerhit +type NestedHit struct { + Field string `json:"field"` + Offset int `json:"offset,omitempty"` + Child *NestedHit `json:"_nested,omitempty"` +} + +// TotalHits specifies total number of hits and its relation +type TotalHits struct { + Value int64 `json:"value"` // value of the total hit count + Relation string `json:"relation"` // how the value should be interpreted: accurate ("eq") or a lower bound ("gte") +} + +// SearchHit is a single hit. +type SearchHit struct { + Score *float64 `json:"_score,omitempty"` // computed score + Index string `json:"_index,omitempty"` // index name + Type string `json:"_type,omitempty"` // type meta field + Id string `json:"_id,omitempty"` // external or internal + Uid string `json:"_uid,omitempty"` // uid meta field (see MapperService.java for all meta fields) + Routing string `json:"_routing,omitempty"` // routing meta field + Parent string `json:"_parent,omitempty"` // parent meta field + Version *int64 `json:"_version,omitempty"` // version number, when Version is set to true in SearchService + SeqNo *int64 `json:"_seq_no"` + PrimaryTerm *int64 `json:"_primary_term"` + Sort []interface{} `json:"sort,omitempty"` // sort information + Highlight SearchHitHighlight `json:"highlight,omitempty"` // highlighter information + Source json.RawMessage `json:"_source,omitempty"` // stored document source + Fields map[string]interface{} `json:"fields,omitempty"` // returned (stored) fields + Explanation *SearchExplanation `json:"_explanation,omitempty"` // explains how the score was computed + MatchedQueries []string `json:"matched_queries,omitempty"` // matched queries + InnerHits map[string]*SearchHitInnerHits `json:"inner_hits,omitempty"` // inner hits with ES >= 1.5.0 + Nested *NestedHit `json:"_nested,omitempty"` // for nested inner hits + Shard string `json:"_shard,omitempty"` // used e.g. in Search Explain + Node string `json:"_node,omitempty"` // used e.g. in Search Explain + + // HighlightFields + // SortValues + // MatchedFilters +} + +// SearchHitInnerHits is used for inner hits. +type SearchHitInnerHits struct { + Hits *SearchHits `json:"hits,omitempty"` +} + +// SearchExplanation explains how the score for a hit was computed. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-explain.html. +type SearchExplanation struct { + Value float64 `json:"value"` // e.g. 1.0 + Description string `json:"description"` // e.g. "boost" or "ConstantScore(*:*), product of:" + Details []SearchExplanation `json:"details,omitempty"` // recursive details +} + +// Suggest + +// SearchSuggest is a map of suggestions. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters.html. +type SearchSuggest map[string][]SearchSuggestion + +// SearchSuggestion is a single search suggestion. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters.html. +type SearchSuggestion struct { + Text string `json:"text"` + Offset int `json:"offset"` + Length int `json:"length"` + Options []SearchSuggestionOption `json:"options"` +} + +// SearchSuggestionOption is an option of a SearchSuggestion. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters.html. +type SearchSuggestionOption struct { + Text string `json:"text"` + Index string `json:"_index"` + Type string `json:"_type"` + Id string `json:"_id"` + Score float64 `json:"score"` // term and phrase suggesters uses "score" as of 6.2.4 + ScoreUnderscore float64 `json:"_score"` // completion and context suggesters uses "_score" as of 6.2.4 + Highlighted string `json:"highlighted"` + CollateMatch bool `json:"collate_match"` + Freq int `json:"freq"` // from TermSuggestion.Option in Java API + Source json.RawMessage `json:"_source"` + Contexts map[string][]string `json:"contexts,omitempty"` +} + +// SearchProfile is a list of shard profiling data collected during +// query execution in the "profile" section of a SearchResult +type SearchProfile struct { + Shards []SearchProfileShardResult `json:"shards"` +} + +// SearchProfileShardResult returns the profiling data for a single shard +// accessed during the search query or aggregation. +type SearchProfileShardResult struct { + ID string `json:"id"` + Searches []QueryProfileShardResult `json:"searches"` + Aggregations []ProfileResult `json:"aggregations"` +} + +// QueryProfileShardResult is a container class to hold the profile results +// for a single shard in the request. It comtains a list of query profiles, +// a collector tree and a total rewrite tree. +type QueryProfileShardResult struct { + Query []ProfileResult `json:"query,omitempty"` + RewriteTime int64 `json:"rewrite_time,omitempty"` + Collector []interface{} `json:"collector,omitempty"` +} + +// CollectorResult holds the profile timings of the collectors used in the +// search. Children's CollectorResults may be embedded inside of a parent +// CollectorResult. +type CollectorResult struct { + Name string `json:"name,omitempty"` + Reason string `json:"reason,omitempty"` + Time string `json:"time,omitempty"` + TimeNanos int64 `json:"time_in_nanos,omitempty"` + Children []CollectorResult `json:"children,omitempty"` +} + +// ProfileResult is the internal representation of a profiled query, +// corresponding to a single node in the query tree. +type ProfileResult struct { + Type string `json:"type"` + Description string `json:"description,omitempty"` + NodeTime string `json:"time,omitempty"` + NodeTimeNanos int64 `json:"time_in_nanos,omitempty"` + Breakdown map[string]int64 `json:"breakdown,omitempty"` + Children []ProfileResult `json:"children,omitempty"` +} + +// Aggregations (see search_aggs.go) + +// Highlighting + +// SearchHitHighlight is the highlight information of a search hit. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-highlighting.html +// for a general discussion of highlighting. +type SearchHitHighlight map[string][]string diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs.go b/vendor/github.com/olivere/elastic/v7/search_aggs.go new file mode 100644 index 0000000000..9080d12be3 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs.go @@ -0,0 +1,1764 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "bytes" + "encoding/json" +) + +// Aggregations can be seen as a unit-of-work that build +// analytic information over a set of documents. It is +// (in many senses) the follow-up of facets in Elasticsearch. +// For more details about aggregations, visit: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations.html +type Aggregation interface { + // Source returns a JSON-serializable aggregation that is a fragment + // of the request sent to Elasticsearch. + Source() (interface{}, error) +} + +// Aggregations is a list of aggregations that are part of a search result. +type Aggregations map[string]json.RawMessage + +// Min returns min aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-min-aggregation.html +func (a Aggregations) Min(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Max returns max aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-max-aggregation.html +func (a Aggregations) Max(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Sum returns sum aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-sum-aggregation.html +func (a Aggregations) Sum(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Avg returns average aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-avg-aggregation.html +func (a Aggregations) Avg(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// WeightedAvg computes the weighted average of numeric values that are extracted from the aggregated documents. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-weight-avg-aggregation.html +func (a Aggregations) WeightedAvg(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// ValueCount returns value-count aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-valuecount-aggregation.html +func (a Aggregations) ValueCount(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Cardinality returns cardinality aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-cardinality-aggregation.html +func (a Aggregations) Cardinality(name string) (*AggregationValueMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationValueMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Stats returns stats aggregation results. +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-stats-aggregation.html +func (a Aggregations) Stats(name string) (*AggregationStatsMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationStatsMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// ExtendedStats returns extended stats aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-extendedstats-aggregation.html +func (a Aggregations) ExtendedStats(name string) (*AggregationExtendedStatsMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationExtendedStatsMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// MatrixStats returns matrix stats aggregation results. +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-matrix-stats-aggregation.html +func (a Aggregations) MatrixStats(name string) (*AggregationMatrixStats, bool) { + if raw, found := a[name]; found { + agg := new(AggregationMatrixStats) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Percentiles returns percentiles results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-percentile-aggregation.html +func (a Aggregations) Percentiles(name string) (*AggregationPercentilesMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPercentilesMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// PercentileRanks returns percentile ranks results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-percentile-rank-aggregation.html +func (a Aggregations) PercentileRanks(name string) (*AggregationPercentilesMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPercentilesMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// TopHits returns top-hits aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-top-hits-aggregation.html +func (a Aggregations) TopHits(name string) (*AggregationTopHitsMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationTopHitsMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Global returns global results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-global-aggregation.html +func (a Aggregations) Global(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Filter returns filter results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-filter-aggregation.html +func (a Aggregations) Filter(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Filters returns filters results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-filters-aggregation.html +func (a Aggregations) Filters(name string) (*AggregationBucketFilters, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketFilters) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// AdjacencyMatrix returning a form of adjacency matrix. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-adjacency-matrix-aggregation.html +func (a Aggregations) AdjacencyMatrix(name string) (*AggregationBucketAdjacencyMatrix, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketAdjacencyMatrix) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Missing returns missing results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-missing-aggregation.html +func (a Aggregations) Missing(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Nested returns nested results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-nested-aggregation.html +func (a Aggregations) Nested(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// ReverseNested returns reverse-nested results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-reverse-nested-aggregation.html +func (a Aggregations) ReverseNested(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Children returns children results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-children-aggregation.html +func (a Aggregations) Children(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Terms returns terms aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-terms-aggregation.html +func (a Aggregations) Terms(name string) (*AggregationBucketKeyItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketKeyItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// SignificantTerms returns significant terms aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html +func (a Aggregations) SignificantTerms(name string) (*AggregationBucketSignificantTerms, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketSignificantTerms) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Sampler returns sampler aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-sampler-aggregation.html +func (a Aggregations) Sampler(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// DiversifiedSampler returns diversified_sampler aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-diversified-sampler-aggregation.html +func (a Aggregations) DiversifiedSampler(name string) (*AggregationSingleBucket, bool) { + if raw, found := a[name]; found { + agg := new(AggregationSingleBucket) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Range returns range aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-range-aggregation.html +func (a Aggregations) Range(name string) (*AggregationBucketRangeItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketRangeItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// KeyedRange returns keyed range aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-range-aggregation.html. +func (a Aggregations) KeyedRange(name string) (*AggregationBucketKeyedRangeItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketKeyedRangeItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// DateRange returns date range aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-daterange-aggregation.html +func (a Aggregations) DateRange(name string) (*AggregationBucketRangeItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketRangeItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// IPRange returns IP range aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-iprange-aggregation.html +func (a Aggregations) IPRange(name string) (*AggregationBucketRangeItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketRangeItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Histogram returns histogram aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-histogram-aggregation.html +func (a Aggregations) Histogram(name string) (*AggregationBucketHistogramItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketHistogramItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// AutoDateHistogram returns auto date histogram aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-datehistogram-aggregation.html +func (a Aggregations) AutoDateHistogram(name string) (*AggregationBucketHistogramItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketHistogramItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// DateHistogram returns date histogram aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-datehistogram-aggregation.html +func (a Aggregations) DateHistogram(name string) (*AggregationBucketHistogramItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketHistogramItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// KeyedDateHistogram returns date histogram aggregation results for keyed responses. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-datehistogram-aggregation.html#_keyed_response_3 +func (a Aggregations) KeyedDateHistogram(name string) (*AggregationBucketKeyedHistogramItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketKeyedHistogramItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// GeoBounds returns geo-bounds aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-geobounds-aggregation.html +func (a Aggregations) GeoBounds(name string) (*AggregationGeoBoundsMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationGeoBoundsMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// GeoHash returns geo-hash aggregation results. +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-geohashgrid-aggregation.html +func (a Aggregations) GeoHash(name string) (*AggregationBucketKeyItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketKeyItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// GeoCentroid returns geo-centroid aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-geocentroid-aggregation.html +func (a Aggregations) GeoCentroid(name string) (*AggregationGeoCentroidMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationGeoCentroidMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// GeoDistance returns geo distance aggregation results. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-geodistance-aggregation.html +func (a Aggregations) GeoDistance(name string) (*AggregationBucketRangeItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketRangeItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// AvgBucket returns average bucket pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-avg-bucket-aggregation.html +func (a Aggregations) AvgBucket(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// SumBucket returns sum bucket pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-sum-bucket-aggregation.html +func (a Aggregations) SumBucket(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// StatsBucket returns stats bucket pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-stats-bucket-aggregation.html +func (a Aggregations) StatsBucket(name string) (*AggregationPipelineStatsMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineStatsMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// PercentilesBucket returns stats bucket pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-percentiles-bucket-aggregation.html +func (a Aggregations) PercentilesBucket(name string) (*AggregationPipelinePercentilesMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelinePercentilesMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// MaxBucket returns maximum bucket pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-max-bucket-aggregation.html +func (a Aggregations) MaxBucket(name string) (*AggregationPipelineBucketMetricValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineBucketMetricValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// MinBucket returns minimum bucket pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-min-bucket-aggregation.html +func (a Aggregations) MinBucket(name string) (*AggregationPipelineBucketMetricValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineBucketMetricValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// MovAvg returns moving average pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html +// +// Deprecated: The MovAvgAggregation has been deprecated in 6.4.0. Use the more generate MovFnAggregation instead. +func (a Aggregations) MovAvg(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// MovFn returns moving function pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movfn-aggregation.html +func (a Aggregations) MovFn(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Derivative returns derivative pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-derivative-aggregation.html +func (a Aggregations) Derivative(name string) (*AggregationPipelineDerivative, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineDerivative) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// CumulativeSum returns a cumulative sum pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-cumulative-sum-aggregation.html +func (a Aggregations) CumulativeSum(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// BucketScript returns bucket script pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-bucket-script-aggregation.html +func (a Aggregations) BucketScript(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// SerialDiff returns serial differencing pipeline aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-serialdiff-aggregation.html +func (a Aggregations) SerialDiff(name string) (*AggregationPipelineSimpleValue, bool) { + if raw, found := a[name]; found { + agg := new(AggregationPipelineSimpleValue) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// Composite returns composite bucket aggregation results. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-composite-aggregation.html +// for details. +func (a Aggregations) Composite(name string) (*AggregationBucketCompositeItems, bool) { + if raw, found := a[name]; found { + agg := new(AggregationBucketCompositeItems) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// ScriptedMetric returns scripted metric aggregation results. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-aggregations-metrics-scripted-metric-aggregation.html +// for details. +func (a Aggregations) ScriptedMetric(name string) (*AggregationScriptedMetric, bool) { + if raw, found := a[name]; found { + agg := new(AggregationScriptedMetric) + if raw == nil { + return agg, true + } + if err := json.Unmarshal(raw, agg); err == nil { + return agg, true + } + } + return nil, false +} + +// -- Single value metric -- + +// AggregationValueMetric is a single-value metric, returned e.g. by a +// Min or Max aggregation. +type AggregationValueMetric struct { + Aggregations + + Value *float64 //`json:"value"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationValueMetric structure. +func (a *AggregationValueMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["value"]; ok && v != nil { + json.Unmarshal(v, &a.Value) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Stats metric -- + +// AggregationStatsMetric is a multi-value metric, returned by a Stats aggregation. +type AggregationStatsMetric struct { + Aggregations + + Count int64 // `json:"count"` + Min *float64 //`json:"min,omitempty"` + Max *float64 //`json:"max,omitempty"` + Avg *float64 //`json:"avg,omitempty"` + Sum *float64 //`json:"sum,omitempty"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationStatsMetric structure. +func (a *AggregationStatsMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["count"]; ok && v != nil { + json.Unmarshal(v, &a.Count) + } + if v, ok := aggs["min"]; ok && v != nil { + json.Unmarshal(v, &a.Min) + } + if v, ok := aggs["max"]; ok && v != nil { + json.Unmarshal(v, &a.Max) + } + if v, ok := aggs["avg"]; ok && v != nil { + json.Unmarshal(v, &a.Avg) + } + if v, ok := aggs["sum"]; ok && v != nil { + json.Unmarshal(v, &a.Sum) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Extended stats metric -- + +// AggregationExtendedStatsMetric is a multi-value metric, returned by an ExtendedStats aggregation. +type AggregationExtendedStatsMetric struct { + Aggregations + + Count int64 // `json:"count"` + Min *float64 //`json:"min,omitempty"` + Max *float64 //`json:"max,omitempty"` + Avg *float64 //`json:"avg,omitempty"` + Sum *float64 //`json:"sum,omitempty"` + SumOfSquares *float64 //`json:"sum_of_squares,omitempty"` + Variance *float64 //`json:"variance,omitempty"` + StdDeviation *float64 //`json:"std_deviation,omitempty"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationExtendedStatsMetric structure. +func (a *AggregationExtendedStatsMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["count"]; ok && v != nil { + json.Unmarshal(v, &a.Count) + } + if v, ok := aggs["min"]; ok && v != nil { + json.Unmarshal(v, &a.Min) + } + if v, ok := aggs["max"]; ok && v != nil { + json.Unmarshal(v, &a.Max) + } + if v, ok := aggs["avg"]; ok && v != nil { + json.Unmarshal(v, &a.Avg) + } + if v, ok := aggs["sum"]; ok && v != nil { + json.Unmarshal(v, &a.Sum) + } + if v, ok := aggs["sum_of_squares"]; ok && v != nil { + json.Unmarshal(v, &a.SumOfSquares) + } + if v, ok := aggs["variance"]; ok && v != nil { + json.Unmarshal(v, &a.Variance) + } + if v, ok := aggs["std_deviation"]; ok && v != nil { + json.Unmarshal(v, &a.StdDeviation) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Matrix Stats -- + +// AggregationMatrixStats is returned by a MatrixStats aggregation. +type AggregationMatrixStats struct { + Aggregations + + Fields []*AggregationMatrixStatsField // `json:"field,omitempty"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// AggregationMatrixStatsField represents running stats of a single field +// returned from MatrixStats aggregation. +type AggregationMatrixStatsField struct { + Name string `json:"name"` + Count int64 `json:"count"` + Mean float64 `json:"mean,omitempty"` + Variance float64 `json:"variance,omitempty"` + Skewness float64 `json:"skewness,omitempty"` + Kurtosis float64 `json:"kurtosis,omitempty"` + Covariance map[string]float64 `json:"covariance,omitempty"` + Correlation map[string]float64 `json:"correlation,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationMatrixStats structure. +func (a *AggregationMatrixStats) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["fields"]; ok && v != nil { + // RunningStats for every field + json.Unmarshal(v, &a.Fields) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Percentiles metric -- + +// AggregationPercentilesMetric is a multi-value metric, returned by a Percentiles aggregation. +type AggregationPercentilesMetric struct { + Aggregations + + Values map[string]float64 // `json:"values"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationPercentilesMetric structure. +func (a *AggregationPercentilesMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["values"]; ok && v != nil { + json.Unmarshal(v, &a.Values) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Top-hits metric -- + +// AggregationTopHitsMetric is a metric returned by a TopHits aggregation. +type AggregationTopHitsMetric struct { + Aggregations + + Hits *SearchHits //`json:"hits"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationTopHitsMetric structure. +func (a *AggregationTopHitsMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + a.Aggregations = aggs + a.Hits = new(SearchHits) + if v, ok := aggs["hits"]; ok && v != nil { + json.Unmarshal(v, &a.Hits) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + return nil +} + +// -- Geo-bounds metric -- + +// AggregationGeoBoundsMetric is a metric as returned by a GeoBounds aggregation. +type AggregationGeoBoundsMetric struct { + Aggregations + + Bounds struct { + TopLeft struct { + Latitude float64 `json:"lat"` + Longitude float64 `json:"lon"` + } `json:"top_left"` + BottomRight struct { + Latitude float64 `json:"lat"` + Longitude float64 `json:"lon"` + } `json:"bottom_right"` + } `json:"bounds"` + + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationGeoBoundsMetric structure. +func (a *AggregationGeoBoundsMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["bounds"]; ok && v != nil { + json.Unmarshal(v, &a.Bounds) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationGeoCentroidMetric is a metric as returned by a GeoCentroid aggregation. +type AggregationGeoCentroidMetric struct { + Aggregations + + Location struct { + Latitude float64 `json:"lat"` + Longitude float64 `json:"lon"` + } `json:"location"` + + Count int // `json:"count,omitempty"` + + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationGeoCentroidMetric structure. +func (a *AggregationGeoCentroidMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["location"]; ok && v != nil { + json.Unmarshal(v, &a.Location) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + if v, ok := aggs["count"]; ok && v != nil { + json.Unmarshal(v, &a.Count) + } + a.Aggregations = aggs + return nil +} + +// -- Single bucket -- + +// AggregationSingleBucket is a single bucket, returned e.g. via an aggregation of type Global. +type AggregationSingleBucket struct { + Aggregations + + DocCount int64 // `json:"doc_count"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationSingleBucket structure. +func (a *AggregationSingleBucket) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Bucket range items -- + +// AggregationBucketRangeItems is a bucket aggregation that is e.g. returned +// with a range aggregation. +type AggregationBucketRangeItems struct { + Aggregations + + DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"` + SumOfOtherDocCount int64 //`json:"sum_other_doc_count"` + Buckets []*AggregationBucketRangeItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItems structure. +func (a *AggregationBucketRangeItems) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil { + json.Unmarshal(v, &a.DocCountErrorUpperBound) + } + if v, ok := aggs["sum_other_doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.SumOfOtherDocCount) + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketKeyedRangeItems is a bucket aggregation that is e.g. returned +// with a keyed range aggregation. +type AggregationBucketKeyedRangeItems struct { + Aggregations + + DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"` + SumOfOtherDocCount int64 //`json:"sum_other_doc_count"` + Buckets map[string]*AggregationBucketRangeItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItems structure. +func (a *AggregationBucketKeyedRangeItems) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil { + json.Unmarshal(v, &a.DocCountErrorUpperBound) + } + if v, ok := aggs["sum_other_doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.SumOfOtherDocCount) + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketRangeItem is a single bucket of an AggregationBucketRangeItems structure. +type AggregationBucketRangeItem struct { + Aggregations + + Key string //`json:"key"` + DocCount int64 //`json:"doc_count"` + From *float64 //`json:"from"` + FromAsString string //`json:"from_as_string"` + To *float64 //`json:"to"` + ToAsString string //`json:"to_as_string"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketRangeItem structure. +func (a *AggregationBucketRangeItem) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["key"]; ok && v != nil { + json.Unmarshal(v, &a.Key) + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + if v, ok := aggs["from"]; ok && v != nil { + json.Unmarshal(v, &a.From) + } + if v, ok := aggs["from_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.FromAsString) + } + if v, ok := aggs["to"]; ok && v != nil { + json.Unmarshal(v, &a.To) + } + if v, ok := aggs["to_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.ToAsString) + } + a.Aggregations = aggs + return nil +} + +// -- Bucket key items -- + +// AggregationBucketKeyItems is a bucket aggregation that is e.g. returned +// with a terms aggregation. +type AggregationBucketKeyItems struct { + Aggregations + + DocCountErrorUpperBound int64 //`json:"doc_count_error_upper_bound"` + SumOfOtherDocCount int64 //`json:"sum_other_doc_count"` + Buckets []*AggregationBucketKeyItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyItems structure. +func (a *AggregationBucketKeyItems) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["doc_count_error_upper_bound"]; ok && v != nil { + json.Unmarshal(v, &a.DocCountErrorUpperBound) + } + if v, ok := aggs["sum_other_doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.SumOfOtherDocCount) + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketKeyItem is a single bucket of an AggregationBucketKeyItems structure. +type AggregationBucketKeyItem struct { + Aggregations + + Key interface{} //`json:"key"` + KeyAsString *string //`json:"key_as_string"` + KeyNumber json.Number + DocCount int64 //`json:"doc_count"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyItem structure. +func (a *AggregationBucketKeyItem) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + dec := json.NewDecoder(bytes.NewReader(data)) + dec.UseNumber() + if err := dec.Decode(&aggs); err != nil { + return err + } + if v, ok := aggs["key"]; ok && v != nil { + json.Unmarshal(v, &a.Key) + json.Unmarshal(v, &a.KeyNumber) + } + if v, ok := aggs["key_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.KeyAsString) + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + a.Aggregations = aggs + return nil +} + +// -- Bucket types for significant terms -- + +// AggregationBucketSignificantTerms is a bucket aggregation returned +// with a significant terms aggregation. +type AggregationBucketSignificantTerms struct { + Aggregations + + DocCount int64 //`json:"doc_count"` + Buckets []*AggregationBucketSignificantTerm //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketSignificantTerms structure. +func (a *AggregationBucketSignificantTerms) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketSignificantTerm is a single bucket of an AggregationBucketSignificantTerms structure. +type AggregationBucketSignificantTerm struct { + Aggregations + + Key string //`json:"key"` + DocCount int64 //`json:"doc_count"` + BgCount int64 //`json:"bg_count"` + Score float64 //`json:"score"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketSignificantTerm structure. +func (a *AggregationBucketSignificantTerm) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["key"]; ok && v != nil { + json.Unmarshal(v, &a.Key) + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + if v, ok := aggs["bg_count"]; ok && v != nil { + json.Unmarshal(v, &a.BgCount) + } + if v, ok := aggs["score"]; ok && v != nil { + json.Unmarshal(v, &a.Score) + } + a.Aggregations = aggs + return nil +} + +// -- Bucket filters -- + +// AggregationBucketFilters is a multi-bucket aggregation that is returned +// with a filters aggregation. +type AggregationBucketFilters struct { + Aggregations + + Buckets []*AggregationBucketKeyItem //`json:"buckets"` + NamedBuckets map[string]*AggregationBucketKeyItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketFilters structure. +func (a *AggregationBucketFilters) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + json.Unmarshal(v, &a.NamedBuckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Bucket AdjacencyMatrix -- + +// AggregationBucketAdjacencyMatrix is a multi-bucket aggregation that is returned +// with a AdjacencyMatrix aggregation. +type AggregationBucketAdjacencyMatrix struct { + Aggregations + + Buckets []*AggregationBucketKeyItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketAdjacencyMatrix structure. +func (a *AggregationBucketAdjacencyMatrix) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Bucket histogram items -- + +// AggregationBucketHistogramItems is a bucket aggregation that is returned +// with a date histogram aggregation. +type AggregationBucketHistogramItems struct { + Aggregations + + Buckets []*AggregationBucketHistogramItem //`json:"buckets"` + Interval interface{} // `json:"interval"` // can be numeric or a string + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketHistogramItems structure. +func (a *AggregationBucketHistogramItems) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["interval"]; ok && v != nil { + json.Unmarshal(v, &a.Interval) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketKeyedHistogramItems is a bucket aggregation that is returned +// with a (keyed) date histogram aggregation. +type AggregationBucketKeyedHistogramItems struct { + Aggregations + + Buckets map[string]*AggregationBucketHistogramItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketKeyedHistogramItems structure. +func (a *AggregationBucketKeyedHistogramItems) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketHistogramItem is a single bucket of an AggregationBucketHistogramItems structure. +type AggregationBucketHistogramItem struct { + Aggregations + + Key float64 //`json:"key"` + KeyAsString *string //`json:"key_as_string"` + DocCount int64 //`json:"doc_count"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketHistogramItem structure. +func (a *AggregationBucketHistogramItem) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["key"]; ok && v != nil { + json.Unmarshal(v, &a.Key) + } + if v, ok := aggs["key_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.KeyAsString) + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + a.Aggregations = aggs + return nil +} + +// -- Pipeline simple value -- + +// AggregationPipelineSimpleValue is a simple value, returned e.g. by a +// MovAvg aggregation. +type AggregationPipelineSimpleValue struct { + Aggregations + + Value *float64 // `json:"value"` + ValueAsString string // `json:"value_as_string"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationPipelineSimpleValue structure. +func (a *AggregationPipelineSimpleValue) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["value"]; ok && v != nil { + json.Unmarshal(v, &a.Value) + } + if v, ok := aggs["value_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.ValueAsString) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Pipeline simple value -- + +// AggregationPipelineBucketMetricValue is a value returned e.g. by a +// MaxBucket aggregation. +type AggregationPipelineBucketMetricValue struct { + Aggregations + + Keys []interface{} // `json:"keys"` + Value *float64 // `json:"value"` + ValueAsString string // `json:"value_as_string"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationPipelineBucketMetricValue structure. +func (a *AggregationPipelineBucketMetricValue) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["keys"]; ok && v != nil { + json.Unmarshal(v, &a.Keys) + } + if v, ok := aggs["value"]; ok && v != nil { + json.Unmarshal(v, &a.Value) + } + if v, ok := aggs["value_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.ValueAsString) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Pipeline derivative -- + +// AggregationPipelineDerivative is the value returned by a +// Derivative aggregation. +type AggregationPipelineDerivative struct { + Aggregations + + Value *float64 // `json:"value"` + ValueAsString string // `json:"value_as_string"` + NormalizedValue *float64 // `json:"normalized_value"` + NormalizedValueAsString string // `json:"normalized_value_as_string"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationPipelineDerivative structure. +func (a *AggregationPipelineDerivative) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["value"]; ok && v != nil { + json.Unmarshal(v, &a.Value) + } + if v, ok := aggs["value_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.ValueAsString) + } + if v, ok := aggs["normalized_value"]; ok && v != nil { + json.Unmarshal(v, &a.NormalizedValue) + } + if v, ok := aggs["normalized_value_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.NormalizedValueAsString) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Pipeline stats metric -- + +// AggregationPipelineStatsMetric is a simple value, returned e.g. by a +// MovAvg aggregation. +type AggregationPipelineStatsMetric struct { + Aggregations + + Count int64 // `json:"count"` + CountAsString string // `json:"count_as_string"` + Min *float64 // `json:"min"` + MinAsString string // `json:"min_as_string"` + Max *float64 // `json:"max"` + MaxAsString string // `json:"max_as_string"` + Avg *float64 // `json:"avg"` + AvgAsString string // `json:"avg_as_string"` + Sum *float64 // `json:"sum"` + SumAsString string // `json:"sum_as_string"` + + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationPipelineStatsMetric structure. +func (a *AggregationPipelineStatsMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["count"]; ok && v != nil { + json.Unmarshal(v, &a.Count) + } + if v, ok := aggs["count_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.CountAsString) + } + if v, ok := aggs["min"]; ok && v != nil { + json.Unmarshal(v, &a.Min) + } + if v, ok := aggs["min_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.MinAsString) + } + if v, ok := aggs["max"]; ok && v != nil { + json.Unmarshal(v, &a.Max) + } + if v, ok := aggs["max_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.MaxAsString) + } + if v, ok := aggs["avg"]; ok && v != nil { + json.Unmarshal(v, &a.Avg) + } + if v, ok := aggs["avg_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.AvgAsString) + } + if v, ok := aggs["sum"]; ok && v != nil { + json.Unmarshal(v, &a.Sum) + } + if v, ok := aggs["sum_as_string"]; ok && v != nil { + json.Unmarshal(v, &a.SumAsString) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Pipeline percentiles + +// AggregationPipelinePercentilesMetric is the value returned by a pipeline +// percentiles Metric aggregation +type AggregationPipelinePercentilesMetric struct { + Aggregations + + Values map[string]float64 // `json:"values"` + Meta map[string]interface{} // `json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationPipelinePercentilesMetric structure. +func (a *AggregationPipelinePercentilesMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["values"]; ok && v != nil { + json.Unmarshal(v, &a.Values) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} + +// -- Composite key items -- + +// AggregationBucketCompositeItems implements the response structure +// for a bucket aggregation of type composite. +type AggregationBucketCompositeItems struct { + Aggregations + + Buckets []*AggregationBucketCompositeItem //`json:"buckets"` + Meta map[string]interface{} // `json:"meta,omitempty"` + AfterKey map[string]interface{} // `json:"after_key,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketCompositeItems structure. +func (a *AggregationBucketCompositeItems) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["buckets"]; ok && v != nil { + json.Unmarshal(v, &a.Buckets) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + if v, ok := aggs["after_key"]; ok && v != nil { + json.Unmarshal(v, &a.AfterKey) + } + a.Aggregations = aggs + return nil +} + +// AggregationBucketCompositeItem is a single bucket of an AggregationBucketCompositeItems structure. +type AggregationBucketCompositeItem struct { + Aggregations + + Key map[string]interface{} //`json:"key"` + DocCount int64 //`json:"doc_count"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationBucketCompositeItem structure. +func (a *AggregationBucketCompositeItem) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + dec := json.NewDecoder(bytes.NewReader(data)) + dec.UseNumber() + if err := dec.Decode(&aggs); err != nil { + return err + } + if v, ok := aggs["key"]; ok && v != nil { + json.Unmarshal(v, &a.Key) + } + if v, ok := aggs["doc_count"]; ok && v != nil { + json.Unmarshal(v, &a.DocCount) + } + a.Aggregations = aggs + return nil +} + +// AggregationScriptedMetric is the value return by a scripted metric aggregation. +// Value maybe one of map[string]interface{}/[]interface{}/string/bool/json.Number +type AggregationScriptedMetric struct { + Aggregations + + Value interface{} //`json:"value"` + Meta map[string]interface{} //`json:"meta,omitempty"` +} + +// UnmarshalJSON decodes JSON data and initializes an AggregationScriptedMetric structure. +func (a *AggregationScriptedMetric) UnmarshalJSON(data []byte) error { + var aggs map[string]json.RawMessage + if err := json.Unmarshal(data, &aggs); err != nil { + return err + } + if v, ok := aggs["value"]; ok && v != nil { + decoder := json.NewDecoder(bytes.NewReader(v)) + decoder.UseNumber() + decoder.Decode(&a.Value) + } + if v, ok := aggs["meta"]; ok && v != nil { + json.Unmarshal(v, &a.Meta) + } + a.Aggregations = aggs + return nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_adjacency_matrix.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_adjacency_matrix.go new file mode 100644 index 0000000000..7dff0e025c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_adjacency_matrix.go @@ -0,0 +1,96 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// AdjacencyMatrixAggregation returning a form of adjacency matrix. +// The request provides a collection of named filter expressions, +// similar to the filters aggregation request. Each bucket in the +// response represents a non-empty cell in the matrix of intersecting filters. +// +// For details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-adjacency-matrix-aggregation.html +type AdjacencyMatrixAggregation struct { + filters map[string]Query + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +// NewAdjacencyMatrixAggregation initializes a new AdjacencyMatrixAggregation. +func NewAdjacencyMatrixAggregation() *AdjacencyMatrixAggregation { + return &AdjacencyMatrixAggregation{ + filters: make(map[string]Query), + subAggregations: make(map[string]Aggregation), + } +} + +// Filters adds the filter +func (a *AdjacencyMatrixAggregation) Filters(name string, filter Query) *AdjacencyMatrixAggregation { + a.filters[name] = filter + return a +} + +// SubAggregation adds a sub-aggregation to this aggregation. +func (a *AdjacencyMatrixAggregation) SubAggregation(name string, subAggregation Aggregation) *AdjacencyMatrixAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *AdjacencyMatrixAggregation) Meta(metaData map[string]interface{}) *AdjacencyMatrixAggregation { + a.meta = metaData + return a +} + +// Source returns the a JSON-serializable interface. +func (a *AdjacencyMatrixAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "interactions" : { + // "adjacency_matrix" : { + // "filters" : { + // "grpA" : { "terms" : { "accounts" : ["hillary", "sidney"] }}, + // "grpB" : { "terms" : { "accounts" : ["donald", "mitt"] }}, + // "grpC" : { "terms" : { "accounts" : ["vladimir", "nigel"] }} + // } + // } + // } + // } + // This method returns only the (outer) { "adjacency_matrix" : {} } part. + + source := make(map[string]interface{}) + adjacencyMatrix := make(map[string]interface{}) + source["adjacency_matrix"] = adjacencyMatrix + + dict := make(map[string]interface{}) + for key, filter := range a.filters { + src, err := filter.Source() + if err != nil { + return nil, err + } + dict[key] = src + } + adjacencyMatrix["filters"] = dict + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_auto_date_histogram.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_auto_date_histogram.go new file mode 100644 index 0000000000..281ee233b0 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_auto_date_histogram.go @@ -0,0 +1,163 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// AutoDateHistogramAggregation is a multi-bucket aggregation similar to the +// histogram except it can only be applied on date values, and the buckets num can bin pointed. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations-bucket-autodatehistogram-aggregation.html +type AutoDateHistogramAggregation struct { + field string + script *Script + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + + buckets int + minDocCount *int64 + timeZone string + format string + minimumInterval string +} + +// NewAutoDateHistogramAggregation creates a new AutoDateHistogramAggregation. +func NewAutoDateHistogramAggregation() *AutoDateHistogramAggregation { + return &AutoDateHistogramAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +// Field on which the aggregation is processed. +func (a *AutoDateHistogramAggregation) Field(field string) *AutoDateHistogramAggregation { + a.field = field + return a +} + +// Script on which th +func (a *AutoDateHistogramAggregation) Script(script *Script) *AutoDateHistogramAggregation { + a.script = script + return a +} + +// Missing configures the value to use when documents miss a value. +func (a *AutoDateHistogramAggregation) Missing(missing interface{}) *AutoDateHistogramAggregation { + a.missing = missing + return a +} + +// SubAggregation sub aggregation +func (a *AutoDateHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *AutoDateHistogramAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *AutoDateHistogramAggregation) Meta(metaData map[string]interface{}) *AutoDateHistogramAggregation { + a.meta = metaData + return a +} + +// Buckets buckets num by which the aggregation gets processed. +func (a *AutoDateHistogramAggregation) Buckets(buckets int) *AutoDateHistogramAggregation { + a.buckets = buckets + return a +} + +// MinDocCount sets the minimum document count per bucket. +// Buckets with less documents than this min value will not be returned. +func (a *AutoDateHistogramAggregation) MinDocCount(minDocCount int64) *AutoDateHistogramAggregation { + a.minDocCount = &minDocCount + return a +} + +// TimeZone sets the timezone in which to translate dates before computing buckets. +func (a *AutoDateHistogramAggregation) TimeZone(timeZone string) *AutoDateHistogramAggregation { + a.timeZone = timeZone + return a +} + +// Format sets the format to use for dates. +func (a *AutoDateHistogramAggregation) Format(format string) *AutoDateHistogramAggregation { + a.format = format + return a +} + +// MinimumInterval accepted units for minimum_interval are: year/month/day/hour/minute/second +func (a *AutoDateHistogramAggregation) MinimumInterval(interval string) *AutoDateHistogramAggregation { + a.minimumInterval = interval + return a +} + +// Source source for AutoDateHistogramAggregation +func (a *AutoDateHistogramAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "articles_over_time" : { + // "auto_date_histogram" : { + // "field" : "date", + // "buckets" : 10 + // } + // } + // } + // } + // + // This method returns only the { "auto_date_histogram" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["auto_date_histogram"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.missing != nil { + opts["missing"] = a.missing + } + + if a.buckets > 0 { + opts["buckets"] = a.buckets + } + + if a.minDocCount != nil { + opts["min_doc_count"] = *a.minDocCount + } + if a.timeZone != "" { + opts["time_zone"] = a.timeZone + } + if a.format != "" { + opts["format"] = a.format + } + if a.minimumInterval != "" { + opts["minimum_interval"] = a.minimumInterval + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_children.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_children.go new file mode 100644 index 0000000000..4078b1de0a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_children.go @@ -0,0 +1,76 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ChildrenAggregation is a special single bucket aggregation that enables +// aggregating from buckets on parent document types to buckets on child documents. +// It is available from 1.4.0.Beta1 upwards. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-children-aggregation.html +type ChildrenAggregation struct { + typ string + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewChildrenAggregation() *ChildrenAggregation { + return &ChildrenAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *ChildrenAggregation) Type(typ string) *ChildrenAggregation { + a.typ = typ + return a +} + +func (a *ChildrenAggregation) SubAggregation(name string, subAggregation Aggregation) *ChildrenAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *ChildrenAggregation) Meta(metaData map[string]interface{}) *ChildrenAggregation { + a.meta = metaData + return a +} + +func (a *ChildrenAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "to-answers" : { + // "children": { + // "type" : "answer" + // } + // } + // } + // } + // This method returns only the { "type" : ... } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["children"] = opts + opts["type"] = a.typ + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_composite.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_composite.go new file mode 100644 index 0000000000..859c9e44c4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_composite.go @@ -0,0 +1,577 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// CompositeAggregation is a multi-bucket values source based aggregation +// that can be used to calculate unique composite values from source documents. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-composite-aggregation.html +// for details. +type CompositeAggregation struct { + after map[string]interface{} + size *int + sources []CompositeAggregationValuesSource + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +// NewCompositeAggregation creates a new CompositeAggregation. +func NewCompositeAggregation() *CompositeAggregation { + return &CompositeAggregation{ + sources: make([]CompositeAggregationValuesSource, 0), + subAggregations: make(map[string]Aggregation), + } +} + +// Size represents the number of composite buckets to return. +// Defaults to 10 as of Elasticsearch 6.1. +func (a *CompositeAggregation) Size(size int) *CompositeAggregation { + a.size = &size + return a +} + +// AggregateAfter sets the values that indicate which composite bucket this +// request should "aggregate after". +func (a *CompositeAggregation) AggregateAfter(after map[string]interface{}) *CompositeAggregation { + a.after = after + return a +} + +// Sources specifies the list of CompositeAggregationValuesSource instances to +// use in the aggregation. +func (a *CompositeAggregation) Sources(sources ...CompositeAggregationValuesSource) *CompositeAggregation { + a.sources = append(a.sources, sources...) + return a +} + +// SubAggregations of this aggregation. +func (a *CompositeAggregation) SubAggregation(name string, subAggregation Aggregation) *CompositeAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *CompositeAggregation) Meta(metaData map[string]interface{}) *CompositeAggregation { + a.meta = metaData + return a +} + +// Source returns the serializable JSON for this aggregation. +func (a *CompositeAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "my_composite_agg" : { + // "composite" : { + // "sources": [ + // {"my_term": { "terms": { "field": "product" }}}, + // {"my_histo": { "histogram": { "field": "price", "interval": 5 }}}, + // {"my_date": { "date_histogram": { "field": "timestamp", "interval": "1d" }}}, + // ], + // "size" : 10, + // "after" : ["a", 2, "c"] + // } + // } + // } + // } + // + // This method returns only the { "histogram" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["composite"] = opts + + sources := make([]interface{}, len(a.sources)) + for i, s := range a.sources { + src, err := s.Source() + if err != nil { + return nil, err + } + sources[i] = src + } + opts["sources"] = sources + + if a.size != nil { + opts["size"] = *a.size + } + + if a.after != nil { + opts["after"] = a.after + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} + +// -- Generic interface for CompositeAggregationValues -- + +// CompositeAggregationValuesSource specifies the interface that +// all implementations for CompositeAggregation's Sources method +// need to implement. +// +// The different implementations are described in +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-composite-aggregation.html#_values_source_2. +type CompositeAggregationValuesSource interface { + Source() (interface{}, error) +} + +// -- CompositeAggregationTermsValuesSource -- + +// CompositeAggregationTermsValuesSource is a source for the CompositeAggregation that handles terms +// it works very similar to a terms aggregation with slightly different syntax +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-composite-aggregation.html#_terms +// for details. +type CompositeAggregationTermsValuesSource struct { + name string + field string + script *Script + valueType string + missing interface{} + missingBucket *bool + order string +} + +// NewCompositeAggregationTermsValuesSource creates and initializes +// a new CompositeAggregationTermsValuesSource. +func NewCompositeAggregationTermsValuesSource(name string) *CompositeAggregationTermsValuesSource { + return &CompositeAggregationTermsValuesSource{ + name: name, + } +} + +// Field to use for this source. +func (a *CompositeAggregationTermsValuesSource) Field(field string) *CompositeAggregationTermsValuesSource { + a.field = field + return a +} + +// Script to use for this source. +func (a *CompositeAggregationTermsValuesSource) Script(script *Script) *CompositeAggregationTermsValuesSource { + a.script = script + return a +} + +// ValueType specifies the type of values produced by this source, +// e.g. "string" or "date". +func (a *CompositeAggregationTermsValuesSource) ValueType(valueType string) *CompositeAggregationTermsValuesSource { + a.valueType = valueType + return a +} + +// Order specifies the order in the values produced by this source. +// It can be either "asc" or "desc". +func (a *CompositeAggregationTermsValuesSource) Order(order string) *CompositeAggregationTermsValuesSource { + a.order = order + return a +} + +// Asc ensures the order of the values produced is ascending. +func (a *CompositeAggregationTermsValuesSource) Asc() *CompositeAggregationTermsValuesSource { + a.order = "asc" + return a +} + +// Desc ensures the order of the values produced is descending. +func (a *CompositeAggregationTermsValuesSource) Desc() *CompositeAggregationTermsValuesSource { + a.order = "desc" + return a +} + +// Missing specifies the value to use when the source finds a missing +// value in a document. +// +// Deprecated: Use MissingBucket instead. +func (a *CompositeAggregationTermsValuesSource) Missing(missing interface{}) *CompositeAggregationTermsValuesSource { + a.missing = missing + return a +} + +// MissingBucket, if true, will create an explicit null bucket which represents +// documents with missing values. +func (a *CompositeAggregationTermsValuesSource) MissingBucket(missingBucket bool) *CompositeAggregationTermsValuesSource { + a.missingBucket = &missingBucket + return a +} + +// Source returns the serializable JSON for this values source. +func (a *CompositeAggregationTermsValuesSource) Source() (interface{}, error) { + source := make(map[string]interface{}) + name := make(map[string]interface{}) + source[a.name] = name + values := make(map[string]interface{}) + name["terms"] = values + + // field + if a.field != "" { + values["field"] = a.field + } + + // script + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + values["script"] = src + } + + // missing + if a.missing != nil { + values["missing"] = a.missing + } + + // missing_bucket + if a.missingBucket != nil { + values["missing_bucket"] = *a.missingBucket + } + + // value_type + if a.valueType != "" { + values["value_type"] = a.valueType + } + + // order + if a.order != "" { + values["order"] = a.order + } + + return source, nil + +} + +// -- CompositeAggregationHistogramValuesSource -- + +// CompositeAggregationHistogramValuesSource is a source for the CompositeAggregation that handles histograms +// it works very similar to a terms histogram with slightly different syntax +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-composite-aggregation.html#_histogram +// for details. +type CompositeAggregationHistogramValuesSource struct { + name string + field string + script *Script + valueType string + missing interface{} + missingBucket *bool + order string + interval float64 +} + +// NewCompositeAggregationHistogramValuesSource creates and initializes +// a new CompositeAggregationHistogramValuesSource. +func NewCompositeAggregationHistogramValuesSource(name string, interval float64) *CompositeAggregationHistogramValuesSource { + return &CompositeAggregationHistogramValuesSource{ + name: name, + interval: interval, + } +} + +// Field to use for this source. +func (a *CompositeAggregationHistogramValuesSource) Field(field string) *CompositeAggregationHistogramValuesSource { + a.field = field + return a +} + +// Script to use for this source. +func (a *CompositeAggregationHistogramValuesSource) Script(script *Script) *CompositeAggregationHistogramValuesSource { + a.script = script + return a +} + +// ValueType specifies the type of values produced by this source, +// e.g. "string" or "date". +func (a *CompositeAggregationHistogramValuesSource) ValueType(valueType string) *CompositeAggregationHistogramValuesSource { + a.valueType = valueType + return a +} + +// Missing specifies the value to use when the source finds a missing +// value in a document. +// +// Deprecated: Use MissingBucket instead. +func (a *CompositeAggregationHistogramValuesSource) Missing(missing interface{}) *CompositeAggregationHistogramValuesSource { + a.missing = missing + return a +} + +// MissingBucket, if true, will create an explicit null bucket which represents +// documents with missing values. +func (a *CompositeAggregationHistogramValuesSource) MissingBucket(missingBucket bool) *CompositeAggregationHistogramValuesSource { + a.missingBucket = &missingBucket + return a +} + +// Order specifies the order in the values produced by this source. +// It can be either "asc" or "desc". +func (a *CompositeAggregationHistogramValuesSource) Order(order string) *CompositeAggregationHistogramValuesSource { + a.order = order + return a +} + +// Asc ensures the order of the values produced is ascending. +func (a *CompositeAggregationHistogramValuesSource) Asc() *CompositeAggregationHistogramValuesSource { + a.order = "asc" + return a +} + +// Desc ensures the order of the values produced is descending. +func (a *CompositeAggregationHistogramValuesSource) Desc() *CompositeAggregationHistogramValuesSource { + a.order = "desc" + return a +} + +// Interval specifies the interval to use. +func (a *CompositeAggregationHistogramValuesSource) Interval(interval float64) *CompositeAggregationHistogramValuesSource { + a.interval = interval + return a +} + +// Source returns the serializable JSON for this values source. +func (a *CompositeAggregationHistogramValuesSource) Source() (interface{}, error) { + source := make(map[string]interface{}) + name := make(map[string]interface{}) + source[a.name] = name + values := make(map[string]interface{}) + name["histogram"] = values + + // field + if a.field != "" { + values["field"] = a.field + } + + // script + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + values["script"] = src + } + + // missing + if a.missing != nil { + values["missing"] = a.missing + } + + // missing_bucket + if a.missingBucket != nil { + values["missing_bucket"] = *a.missingBucket + } + + // value_type + if a.valueType != "" { + values["value_type"] = a.valueType + } + + // order + if a.order != "" { + values["order"] = a.order + } + + // Histogram-related properties + values["interval"] = a.interval + + return source, nil + +} + +// -- CompositeAggregationDateHistogramValuesSource -- + +// CompositeAggregationDateHistogramValuesSource is a source for the CompositeAggregation that handles date histograms +// it works very similar to a date histogram aggregation with slightly different syntax +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-bucket-composite-aggregation.html#_date_histogram +// for details. +type CompositeAggregationDateHistogramValuesSource struct { + name string + field string + script *Script + valueType string + missing interface{} + missingBucket *bool + order string + interval interface{} + fixedInterval interface{} + calendarInterval interface{} + format string + timeZone string +} + +// NewCompositeAggregationDateHistogramValuesSource creates and initializes +// a new CompositeAggregationDateHistogramValuesSource. +func NewCompositeAggregationDateHistogramValuesSource(name string) *CompositeAggregationDateHistogramValuesSource { + return &CompositeAggregationDateHistogramValuesSource{ + name: name, + } +} + +// Field to use for this source. +func (a *CompositeAggregationDateHistogramValuesSource) Field(field string) *CompositeAggregationDateHistogramValuesSource { + a.field = field + return a +} + +// Script to use for this source. +func (a *CompositeAggregationDateHistogramValuesSource) Script(script *Script) *CompositeAggregationDateHistogramValuesSource { + a.script = script + return a +} + +// ValueType specifies the type of values produced by this source, +// e.g. "string" or "date". +func (a *CompositeAggregationDateHistogramValuesSource) ValueType(valueType string) *CompositeAggregationDateHistogramValuesSource { + a.valueType = valueType + return a +} + +// Missing specifies the value to use when the source finds a missing +// value in a document. +// +// Deprecated: Use MissingBucket instead. +func (a *CompositeAggregationDateHistogramValuesSource) Missing(missing interface{}) *CompositeAggregationDateHistogramValuesSource { + a.missing = missing + return a +} + +// MissingBucket, if true, will create an explicit null bucket which represents +// documents with missing values. +func (a *CompositeAggregationDateHistogramValuesSource) MissingBucket(missingBucket bool) *CompositeAggregationDateHistogramValuesSource { + a.missingBucket = &missingBucket + return a +} + +// Order specifies the order in the values produced by this source. +// It can be either "asc" or "desc". +func (a *CompositeAggregationDateHistogramValuesSource) Order(order string) *CompositeAggregationDateHistogramValuesSource { + a.order = order + return a +} + +// Asc ensures the order of the values produced is ascending. +func (a *CompositeAggregationDateHistogramValuesSource) Asc() *CompositeAggregationDateHistogramValuesSource { + a.order = "asc" + return a +} + +// Desc ensures the order of the values produced is descending. +func (a *CompositeAggregationDateHistogramValuesSource) Desc() *CompositeAggregationDateHistogramValuesSource { + a.order = "desc" + return a +} + +// Interval to use for the date histogram, e.g. "1d" or a numeric value like "60". +// +// Deprecated: Use FixedInterval or CalendarInterval instead. +func (a *CompositeAggregationDateHistogramValuesSource) Interval(interval interface{}) *CompositeAggregationDateHistogramValuesSource { + a.interval = interval + return a +} + +// FixedInterval to use for the date histogram, e.g. "1d" or a numeric value like "60". +func (a *CompositeAggregationDateHistogramValuesSource) FixedInterval(fixedInterval interface{}) *CompositeAggregationDateHistogramValuesSource { + a.fixedInterval = fixedInterval + return a +} + +// CalendarInterval to use for the date histogram, e.g. "1d" or a numeric value like "60". +func (a *CompositeAggregationDateHistogramValuesSource) CalendarInterval(calendarInterval interface{}) *CompositeAggregationDateHistogramValuesSource { + a.calendarInterval = calendarInterval + return a +} + +// Format to use for the date histogram, e.g. "strict_date_optional_time" +func (a *CompositeAggregationDateHistogramValuesSource) Format(format string) *CompositeAggregationDateHistogramValuesSource { + a.format = format + return a +} + +// TimeZone to use for the dates. +func (a *CompositeAggregationDateHistogramValuesSource) TimeZone(timeZone string) *CompositeAggregationDateHistogramValuesSource { + a.timeZone = timeZone + return a +} + +// Source returns the serializable JSON for this values source. +func (a *CompositeAggregationDateHistogramValuesSource) Source() (interface{}, error) { + source := make(map[string]interface{}) + name := make(map[string]interface{}) + source[a.name] = name + values := make(map[string]interface{}) + name["date_histogram"] = values + + // field + if a.field != "" { + values["field"] = a.field + } + + // script + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + values["script"] = src + } + + // missing + if a.missing != nil { + values["missing"] = a.missing + } + + // missing_bucket + if a.missingBucket != nil { + values["missing_bucket"] = *a.missingBucket + } + + // value_type + if a.valueType != "" { + values["value_type"] = a.valueType + } + + // order + if a.order != "" { + values["order"] = a.order + } + + if a.format != "" { + values["format"] = a.format + } + + // DateHistogram-related properties + if v := a.interval; v != nil { + values["interval"] = v + } + if v := a.fixedInterval; v != nil { + values["fixed_interval"] = v + } + if v := a.calendarInterval; v != nil { + values["calendar_interval"] = v + } + + // timeZone + if a.timeZone != "" { + values["time_zone"] = a.timeZone + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_count_thresholds.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_count_thresholds.go new file mode 100644 index 0000000000..53efdaf5fd --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_count_thresholds.go @@ -0,0 +1,13 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// BucketCountThresholds is used in e.g. terms and significant text aggregations. +type BucketCountThresholds struct { + MinDocCount *int64 + ShardMinDocCount *int64 + RequiredSize *int + ShardSize *int +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_histogram.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_histogram.go new file mode 100644 index 0000000000..68f7d88155 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_histogram.go @@ -0,0 +1,342 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// DateHistogramAggregation is a multi-bucket aggregation similar to the +// histogram except it can only be applied on date values. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-datehistogram-aggregation.html +type DateHistogramAggregation struct { + field string + script *Script + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + + interval string + fixedInterval string + calendarInterval string + order string + orderAsc bool + minDocCount *int64 + extendedBoundsMin interface{} + extendedBoundsMax interface{} + timeZone string + format string + offset string + keyed *bool +} + +// NewDateHistogramAggregation creates a new DateHistogramAggregation. +func NewDateHistogramAggregation() *DateHistogramAggregation { + return &DateHistogramAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +// Field on which the aggregation is processed. +func (a *DateHistogramAggregation) Field(field string) *DateHistogramAggregation { + a.field = field + return a +} + +func (a *DateHistogramAggregation) Script(script *Script) *DateHistogramAggregation { + a.script = script + return a +} + +// Missing configures the value to use when documents miss a value. +func (a *DateHistogramAggregation) Missing(missing interface{}) *DateHistogramAggregation { + a.missing = missing + return a +} + +func (a *DateHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *DateHistogramAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *DateHistogramAggregation) Meta(metaData map[string]interface{}) *DateHistogramAggregation { + a.meta = metaData + return a +} + +// Interval by which the aggregation gets processed. This field +// will be replaced by the two FixedInterval and CalendarInterval +// fields (see below). +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-bucket-datehistogram-aggregation.html +// +// Deprecated: This field will be removed in the future. +func (a *DateHistogramAggregation) Interval(interval string) *DateHistogramAggregation { + a.interval = interval + return a +} + +// FixedInterval by which the aggregation gets processed. +// +// Allowed values are: "year", "1y", "quarter", "1q", "month", "1M", +// "week", "1w", "day", "1d", "hour", "1h", "minute", "1m", "second", +// or "1s". It also supports time settings like "1.5h". +// +// These units are not calendar-aware and are simply multiples of +// fixed, SI units. This is mutually exclusive with CalendarInterval. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-bucket-datehistogram-aggregation.html +func (a *DateHistogramAggregation) FixedInterval(fixedInterval string) *DateHistogramAggregation { + a.fixedInterval = fixedInterval + return a +} + +// CalendarInterval by which the aggregation gets processed. +// +// Allowed values are: "year", "1y", "quarter", "1q", "month", "1M", +// "week", "1w", "day", "1d", "hour", "1h", "minute", "1m", "second", +// or "1s". It also supports time settings like "1.5h". +// +// These units are calendar-aware, meaning they respect leap +// additions, variable days per month etc. This is mutually +// exclusive with FixedInterval. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-bucket-datehistogram-aggregation.html +func (a *DateHistogramAggregation) CalendarInterval(calendarInterval string) *DateHistogramAggregation { + a.calendarInterval = calendarInterval + return a +} + +// Order specifies the sort order. Valid values for order are: +// "_key", "_count", a sub-aggregation name, or a sub-aggregation name +// with a metric. +func (a *DateHistogramAggregation) Order(order string, asc bool) *DateHistogramAggregation { + a.order = order + a.orderAsc = asc + return a +} + +func (a *DateHistogramAggregation) OrderByCount(asc bool) *DateHistogramAggregation { + // "order" : { "_count" : "asc" } + a.order = "_count" + a.orderAsc = asc + return a +} + +func (a *DateHistogramAggregation) OrderByCountAsc() *DateHistogramAggregation { + return a.OrderByCount(true) +} + +func (a *DateHistogramAggregation) OrderByCountDesc() *DateHistogramAggregation { + return a.OrderByCount(false) +} + +func (a *DateHistogramAggregation) OrderByKey(asc bool) *DateHistogramAggregation { + // "order" : { "_key" : "asc" } + a.order = "_key" + a.orderAsc = asc + return a +} + +func (a *DateHistogramAggregation) OrderByKeyAsc() *DateHistogramAggregation { + return a.OrderByKey(true) +} + +func (a *DateHistogramAggregation) OrderByKeyDesc() *DateHistogramAggregation { + return a.OrderByKey(false) +} + +// OrderByAggregation creates a bucket ordering strategy which sorts buckets +// based on a single-valued calc get. +func (a *DateHistogramAggregation) OrderByAggregation(aggName string, asc bool) *DateHistogramAggregation { + // { + // "aggs" : { + // "genders" : { + // "terms" : { + // "field" : "gender", + // "order" : { "avg_height" : "desc" } + // }, + // "aggs" : { + // "avg_height" : { "avg" : { "field" : "height" } } + // } + // } + // } + // } + a.order = aggName + a.orderAsc = asc + return a +} + +// OrderByAggregationAndMetric creates a bucket ordering strategy which +// sorts buckets based on a multi-valued calc get. +func (a *DateHistogramAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) *DateHistogramAggregation { + // { + // "aggs" : { + // "genders" : { + // "terms" : { + // "field" : "gender", + // "order" : { "height_stats.avg" : "desc" } + // }, + // "aggs" : { + // "height_stats" : { "stats" : { "field" : "height" } } + // } + // } + // } + // } + a.order = aggName + "." + metric + a.orderAsc = asc + return a +} + +// MinDocCount sets the minimum document count per bucket. +// Buckets with less documents than this min value will not be returned. +func (a *DateHistogramAggregation) MinDocCount(minDocCount int64) *DateHistogramAggregation { + a.minDocCount = &minDocCount + return a +} + +// TimeZone sets the timezone in which to translate dates before computing buckets. +func (a *DateHistogramAggregation) TimeZone(timeZone string) *DateHistogramAggregation { + a.timeZone = timeZone + return a +} + +// Format sets the format to use for dates. +func (a *DateHistogramAggregation) Format(format string) *DateHistogramAggregation { + a.format = format + return a +} + +// Offset sets the offset of time intervals in the histogram, e.g. "+6h". +func (a *DateHistogramAggregation) Offset(offset string) *DateHistogramAggregation { + a.offset = offset + return a +} + +// ExtendedBounds accepts int, int64, string, or time.Time values. +// In case the lower value in the histogram would be greater than min or the +// upper value would be less than max, empty buckets will be generated. +func (a *DateHistogramAggregation) ExtendedBounds(min, max interface{}) *DateHistogramAggregation { + a.extendedBoundsMin = min + a.extendedBoundsMax = max + return a +} + +// ExtendedBoundsMin accepts int, int64, string, or time.Time values. +func (a *DateHistogramAggregation) ExtendedBoundsMin(min interface{}) *DateHistogramAggregation { + a.extendedBoundsMin = min + return a +} + +// ExtendedBoundsMax accepts int, int64, string, or time.Time values. +func (a *DateHistogramAggregation) ExtendedBoundsMax(max interface{}) *DateHistogramAggregation { + a.extendedBoundsMax = max + return a +} + +// Keyed specifies whether to return the results with a keyed response (or not). +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-datehistogram-aggregation.html#_keyed_response_3. +func (a *DateHistogramAggregation) Keyed(keyed bool) *DateHistogramAggregation { + a.keyed = &keyed + return a +} + +func (a *DateHistogramAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "articles_over_time" : { + // "date_histogram" : { + // "field" : "date", + // "fixed_interval" : "month" + // } + // } + // } + // } + // + // This method returns only the { "date_histogram" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["date_histogram"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.missing != nil { + opts["missing"] = a.missing + } + + if s := a.interval; s != "" { + opts["interval"] = s + } + if s := a.fixedInterval; s != "" { + opts["fixed_interval"] = s + } + if s := a.calendarInterval; s != "" { + opts["calendar_interval"] = s + } + + if a.minDocCount != nil { + opts["min_doc_count"] = *a.minDocCount + } + if a.order != "" { + o := make(map[string]interface{}) + if a.orderAsc { + o[a.order] = "asc" + } else { + o[a.order] = "desc" + } + opts["order"] = o + } + if a.timeZone != "" { + opts["time_zone"] = a.timeZone + } + if a.offset != "" { + opts["offset"] = a.offset + } + if a.format != "" { + opts["format"] = a.format + } + if a.extendedBoundsMin != nil || a.extendedBoundsMax != nil { + bounds := make(map[string]interface{}) + if a.extendedBoundsMin != nil { + bounds["min"] = a.extendedBoundsMin + } + if a.extendedBoundsMax != nil { + bounds["max"] = a.extendedBoundsMax + } + opts["extended_bounds"] = bounds + } + if a.keyed != nil { + opts["keyed"] = *a.keyed + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_range.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_range.go new file mode 100644 index 0000000000..a4becaae90 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_date_range.go @@ -0,0 +1,255 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "time" +) + +// DateRangeAggregation is a range aggregation that is dedicated for +// date values. The main difference between this aggregation and the +// normal range aggregation is that the from and to values can be expressed +// in Date Math expressions, and it is also possible to specify a +// date format by which the from and to response fields will be returned. +// Note that this aggregration includes the from value and excludes the to +// value for each range. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-daterange-aggregation.html +type DateRangeAggregation struct { + field string + script *Script + subAggregations map[string]Aggregation + meta map[string]interface{} + keyed *bool + unmapped *bool + timeZone string + format string + entries []DateRangeAggregationEntry +} + +type DateRangeAggregationEntry struct { + Key string + From interface{} + To interface{} +} + +func NewDateRangeAggregation() *DateRangeAggregation { + return &DateRangeAggregation{ + subAggregations: make(map[string]Aggregation), + entries: make([]DateRangeAggregationEntry, 0), + } +} + +func (a *DateRangeAggregation) Field(field string) *DateRangeAggregation { + a.field = field + return a +} + +func (a *DateRangeAggregation) Script(script *Script) *DateRangeAggregation { + a.script = script + return a +} + +func (a *DateRangeAggregation) SubAggregation(name string, subAggregation Aggregation) *DateRangeAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *DateRangeAggregation) Meta(metaData map[string]interface{}) *DateRangeAggregation { + a.meta = metaData + return a +} + +func (a *DateRangeAggregation) Keyed(keyed bool) *DateRangeAggregation { + a.keyed = &keyed + return a +} + +func (a *DateRangeAggregation) Unmapped(unmapped bool) *DateRangeAggregation { + a.unmapped = &unmapped + return a +} + +func (a *DateRangeAggregation) TimeZone(timeZone string) *DateRangeAggregation { + a.timeZone = timeZone + return a +} + +func (a *DateRangeAggregation) Format(format string) *DateRangeAggregation { + a.format = format + return a +} + +func (a *DateRangeAggregation) AddRange(from, to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: to}) + return a +} + +func (a *DateRangeAggregation) AddRangeWithKey(key string, from, to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: to}) + return a +} + +func (a *DateRangeAggregation) AddUnboundedTo(from interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: nil}) + return a +} + +func (a *DateRangeAggregation) AddUnboundedToWithKey(key string, from interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: nil}) + return a +} + +func (a *DateRangeAggregation) AddUnboundedFrom(to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{From: nil, To: to}) + return a +} + +func (a *DateRangeAggregation) AddUnboundedFromWithKey(key string, to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: nil, To: to}) + return a +} + +func (a *DateRangeAggregation) Lt(to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{From: nil, To: to}) + return a +} + +func (a *DateRangeAggregation) LtWithKey(key string, to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: nil, To: to}) + return a +} + +func (a *DateRangeAggregation) Between(from, to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: to}) + return a +} + +func (a *DateRangeAggregation) BetweenWithKey(key string, from, to interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: to}) + return a +} + +func (a *DateRangeAggregation) Gt(from interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{From: from, To: nil}) + return a +} + +func (a *DateRangeAggregation) GtWithKey(key string, from interface{}) *DateRangeAggregation { + a.entries = append(a.entries, DateRangeAggregationEntry{Key: key, From: from, To: nil}) + return a +} + +func (a *DateRangeAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "range" : { + // "date_range": { + // "field": "date", + // "format": "MM-yyy", + // "ranges": [ + // { "to": "now-10M/M" }, + // { "from": "now-10M/M" } + // ] + // } + // } + // } + // } + // } + // + // This method returns only the { "date_range" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["date_range"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + + if a.keyed != nil { + opts["keyed"] = *a.keyed + } + if a.unmapped != nil { + opts["unmapped"] = *a.unmapped + } + if a.timeZone != "" { + opts["time_zone"] = a.timeZone + } + if a.format != "" { + opts["format"] = a.format + } + + var ranges []interface{} + for _, ent := range a.entries { + r := make(map[string]interface{}) + if ent.Key != "" { + r["key"] = ent.Key + } + if ent.From != nil { + switch from := ent.From.(type) { + case int, int16, int32, int64, float32, float64: + r["from"] = from + case *int, *int16, *int32, *int64, *float32, *float64: + r["from"] = from + case time.Time: + r["from"] = from.Format(time.RFC3339) + case *time.Time: + r["from"] = from.Format(time.RFC3339) + case string: + r["from"] = from + case *string: + r["from"] = from + } + } + if ent.To != nil { + switch to := ent.To.(type) { + case int, int16, int32, int64, float32, float64: + r["to"] = to + case *int, *int16, *int32, *int64, *float32, *float64: + r["to"] = to + case time.Time: + r["to"] = to.Format(time.RFC3339) + case *time.Time: + r["to"] = to.Format(time.RFC3339) + case string: + r["to"] = to + case *string: + r["to"] = to + } + } + ranges = append(ranges, r) + } + opts["ranges"] = ranges + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_diversified_sampler.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_diversified_sampler.go new file mode 100644 index 0000000000..6d3a448291 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_diversified_sampler.go @@ -0,0 +1,126 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// DiversifiedSamplerAggregation Like the ‘sampler` aggregation this is a filtering aggregation used to limit any +// sub aggregations’ processing to a sample of the top-scoring documents. The diversified_sampler aggregation adds +// the ability to limit the number of matches that share a common value such as an "author". +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-diversified-sampler-aggregation.html +type DiversifiedSamplerAggregation struct { + subAggregations map[string]Aggregation + meta map[string]interface{} + field string + script *Script + shardSize int + maxDocsPerValue int + executionHint string +} + +func NewDiversifiedSamplerAggregation() *DiversifiedSamplerAggregation { + return &DiversifiedSamplerAggregation{ + shardSize: -1, + maxDocsPerValue: -1, + subAggregations: make(map[string]Aggregation), + } +} + +func (a *DiversifiedSamplerAggregation) SubAggregation(name string, subAggregation Aggregation) *DiversifiedSamplerAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *DiversifiedSamplerAggregation) Meta(metaData map[string]interface{}) *DiversifiedSamplerAggregation { + a.meta = metaData + return a +} + +// Field on which the aggregation is processed. +func (a *DiversifiedSamplerAggregation) Field(field string) *DiversifiedSamplerAggregation { + a.field = field + return a +} + +func (a *DiversifiedSamplerAggregation) Script(script *Script) *DiversifiedSamplerAggregation { + a.script = script + return a +} + +// ShardSize sets the maximum number of docs returned from each shard. +func (a *DiversifiedSamplerAggregation) ShardSize(shardSize int) *DiversifiedSamplerAggregation { + a.shardSize = shardSize + return a +} + +func (a *DiversifiedSamplerAggregation) MaxDocsPerValue(maxDocsPerValue int) *DiversifiedSamplerAggregation { + a.maxDocsPerValue = maxDocsPerValue + return a +} + +func (a *DiversifiedSamplerAggregation) ExecutionHint(hint string) *DiversifiedSamplerAggregation { + a.executionHint = hint + return a +} + +func (a *DiversifiedSamplerAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs": { + // "my_unbiased_sample": { + // "diversified_sampler": { + // "shard_size": 200, + // "field" : "author" + // } + // } + // } + // } + // + // This method returns only the { "diversified_sampler" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["diversified_sampler"] = opts + + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.shardSize >= 0 { + opts["shard_size"] = a.shardSize + } + if a.maxDocsPerValue >= 0 { + opts["max_docs_per_value"] = a.maxDocsPerValue + } + if a.executionHint != "" { + opts["execution_hint"] = a.executionHint + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filter.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filter.go new file mode 100644 index 0000000000..c002c81331 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filter.go @@ -0,0 +1,77 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// FilterAggregation defines a single bucket of all the documents +// in the current document set context that match a specified filter. +// Often this will be used to narrow down the current aggregation context +// to a specific set of documents. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-filter-aggregation.html +type FilterAggregation struct { + filter Query + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewFilterAggregation() *FilterAggregation { + return &FilterAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *FilterAggregation) SubAggregation(name string, subAggregation Aggregation) *FilterAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *FilterAggregation) Meta(metaData map[string]interface{}) *FilterAggregation { + a.meta = metaData + return a +} + +func (a *FilterAggregation) Filter(filter Query) *FilterAggregation { + a.filter = filter + return a +} + +func (a *FilterAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "in_stock_products" : { + // "filter" : { "range" : { "stock" : { "gt" : 0 } } } + // } + // } + // } + // This method returns only the { "filter" : {} } part. + + src, err := a.filter.Source() + if err != nil { + return nil, err + } + source := make(map[string]interface{}) + source["filter"] = src + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filters.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filters.go new file mode 100644 index 0000000000..d2ea5eaf92 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_filters.go @@ -0,0 +1,138 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// FiltersAggregation defines a multi bucket aggregations where each bucket +// is associated with a filter. Each bucket will collect all documents that +// match its associated filter. +// +// Notice that the caller has to decide whether to add filters by name +// (using FilterWithName) or unnamed filters (using Filter or Filters). One cannot +// use both named and unnamed filters. +// +// For details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-filters-aggregation.html +type FiltersAggregation struct { + unnamedFilters []Query + namedFilters map[string]Query + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +// NewFiltersAggregation initializes a new FiltersAggregation. +func NewFiltersAggregation() *FiltersAggregation { + return &FiltersAggregation{ + unnamedFilters: make([]Query, 0), + namedFilters: make(map[string]Query), + subAggregations: make(map[string]Aggregation), + } +} + +// Filter adds an unnamed filter. Notice that you can +// either use named or unnamed filters, but not both. +func (a *FiltersAggregation) Filter(filter Query) *FiltersAggregation { + a.unnamedFilters = append(a.unnamedFilters, filter) + return a +} + +// Filters adds one or more unnamed filters. Notice that you can +// either use named or unnamed filters, but not both. +func (a *FiltersAggregation) Filters(filters ...Query) *FiltersAggregation { + if len(filters) > 0 { + a.unnamedFilters = append(a.unnamedFilters, filters...) + } + return a +} + +// FilterWithName adds a filter with a specific name. Notice that you can +// either use named or unnamed filters, but not both. +func (a *FiltersAggregation) FilterWithName(name string, filter Query) *FiltersAggregation { + a.namedFilters[name] = filter + return a +} + +// SubAggregation adds a sub-aggregation to this aggregation. +func (a *FiltersAggregation) SubAggregation(name string, subAggregation Aggregation) *FiltersAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *FiltersAggregation) Meta(metaData map[string]interface{}) *FiltersAggregation { + a.meta = metaData + return a +} + +// Source returns the a JSON-serializable interface. +// If the aggregation is invalid, an error is returned. This may e.g. happen +// if you mixed named and unnamed filters. +func (a *FiltersAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "messages" : { + // "filters" : { + // "filters" : { + // "errors" : { "term" : { "body" : "error" }}, + // "warnings" : { "term" : { "body" : "warning" }} + // } + // } + // } + // } + // } + // This method returns only the (outer) { "filters" : {} } part. + + source := make(map[string]interface{}) + filters := make(map[string]interface{}) + source["filters"] = filters + + if len(a.unnamedFilters) > 0 && len(a.namedFilters) > 0 { + return nil, errors.New("elastic: use either named or unnamed filters with FiltersAggregation but not both") + } + + if len(a.unnamedFilters) > 0 { + arr := make([]interface{}, len(a.unnamedFilters)) + for i, filter := range a.unnamedFilters { + src, err := filter.Source() + if err != nil { + return nil, err + } + arr[i] = src + } + filters["filters"] = arr + } else { + dict := make(map[string]interface{}) + for key, filter := range a.namedFilters { + src, err := filter.Source() + if err != nil { + return nil, err + } + dict[key] = src + } + filters["filters"] = dict + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geo_distance.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geo_distance.go new file mode 100644 index 0000000000..ee8dd0cb4f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geo_distance.go @@ -0,0 +1,198 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// GeoDistanceAggregation is a multi-bucket aggregation that works on geo_point fields +// and conceptually works very similar to the range aggregation. +// The user can define a point of origin and a set of distance range buckets. +// The aggregation evaluate the distance of each document value from +// the origin point and determines the buckets it belongs to based on +// the ranges (a document belongs to a bucket if the distance between the +// document and the origin falls within the distance range of the bucket). +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-geodistance-aggregation.html +type GeoDistanceAggregation struct { + field string + unit string + distanceType string + point string + ranges []geoDistAggRange + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +type geoDistAggRange struct { + Key string + From interface{} + To interface{} +} + +func NewGeoDistanceAggregation() *GeoDistanceAggregation { + return &GeoDistanceAggregation{ + subAggregations: make(map[string]Aggregation), + ranges: make([]geoDistAggRange, 0), + } +} + +func (a *GeoDistanceAggregation) Field(field string) *GeoDistanceAggregation { + a.field = field + return a +} + +func (a *GeoDistanceAggregation) Unit(unit string) *GeoDistanceAggregation { + a.unit = unit + return a +} + +func (a *GeoDistanceAggregation) DistanceType(distanceType string) *GeoDistanceAggregation { + a.distanceType = distanceType + return a +} + +func (a *GeoDistanceAggregation) Point(latLon string) *GeoDistanceAggregation { + a.point = latLon + return a +} + +func (a *GeoDistanceAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoDistanceAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *GeoDistanceAggregation) Meta(metaData map[string]interface{}) *GeoDistanceAggregation { + a.meta = metaData + return a +} +func (a *GeoDistanceAggregation) AddRange(from, to interface{}) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{From: from, To: to}) + return a +} + +func (a *GeoDistanceAggregation) AddRangeWithKey(key string, from, to interface{}) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: to}) + return a +} + +func (a *GeoDistanceAggregation) AddUnboundedTo(from float64) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{From: from, To: nil}) + return a +} + +func (a *GeoDistanceAggregation) AddUnboundedToWithKey(key string, from float64) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: nil}) + return a +} + +func (a *GeoDistanceAggregation) AddUnboundedFrom(to float64) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{From: nil, To: to}) + return a +} + +func (a *GeoDistanceAggregation) AddUnboundedFromWithKey(key string, to float64) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: nil, To: to}) + return a +} + +func (a *GeoDistanceAggregation) Between(from, to interface{}) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{From: from, To: to}) + return a +} + +func (a *GeoDistanceAggregation) BetweenWithKey(key string, from, to interface{}) *GeoDistanceAggregation { + a.ranges = append(a.ranges, geoDistAggRange{Key: key, From: from, To: to}) + return a +} + +func (a *GeoDistanceAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "rings_around_amsterdam" : { + // "geo_distance" : { + // "field" : "location", + // "origin" : "52.3760, 4.894", + // "ranges" : [ + // { "to" : 100 }, + // { "from" : 100, "to" : 300 }, + // { "from" : 300 } + // ] + // } + // } + // } + // } + // + // This method returns only the { "range" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["geo_distance"] = opts + + if a.field != "" { + opts["field"] = a.field + } + if a.unit != "" { + opts["unit"] = a.unit + } + if a.distanceType != "" { + opts["distance_type"] = a.distanceType + } + if a.point != "" { + opts["origin"] = a.point + } + + var ranges []interface{} + for _, ent := range a.ranges { + r := make(map[string]interface{}) + if ent.Key != "" { + r["key"] = ent.Key + } + if ent.From != nil { + switch from := ent.From.(type) { + case int, int16, int32, int64, float32, float64: + r["from"] = from + case *int, *int16, *int32, *int64, *float32, *float64: + r["from"] = from + case string: + r["from"] = from + case *string: + r["from"] = from + } + } + if ent.To != nil { + switch to := ent.To.(type) { + case int, int16, int32, int64, float32, float64: + r["to"] = to + case *int, *int16, *int32, *int64, *float32, *float64: + r["to"] = to + case string: + r["to"] = to + case *string: + r["to"] = to + } + } + ranges = append(ranges, r) + } + opts["ranges"] = ranges + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geohash_grid.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geohash_grid.go new file mode 100644 index 0000000000..1a56b57528 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_geohash_grid.go @@ -0,0 +1,104 @@ +package elastic + +type GeoHashGridAggregation struct { + field string + precision interface{} + size int + shardSize int + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewGeoHashGridAggregation() *GeoHashGridAggregation { + return &GeoHashGridAggregation{ + subAggregations: make(map[string]Aggregation), + size: -1, + shardSize: -1, + } +} + +func (a *GeoHashGridAggregation) Field(field string) *GeoHashGridAggregation { + a.field = field + return a +} + +// Precision accepts the level as int value between 1 and 12 or Distance Units like "2km", "5mi" as described at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/common-options.html#distance-units and +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-geohashgrid-aggregation.html +func (a *GeoHashGridAggregation) Precision(precision interface{}) *GeoHashGridAggregation { + a.precision = precision + return a +} + +func (a *GeoHashGridAggregation) Size(size int) *GeoHashGridAggregation { + a.size = size + return a +} + +func (a *GeoHashGridAggregation) ShardSize(shardSize int) *GeoHashGridAggregation { + a.shardSize = shardSize + return a +} + +func (a *GeoHashGridAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoHashGridAggregation { + a.subAggregations[name] = subAggregation + return a +} + +func (a *GeoHashGridAggregation) Meta(metaData map[string]interface{}) *GeoHashGridAggregation { + a.meta = metaData + return a +} + +func (a *GeoHashGridAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs": { + // "new_york": { + // "geohash_grid": { + // "field": "location", + // "precision": 5 + // } + // } + // } + // } + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["geohash_grid"] = opts + + if a.field != "" { + opts["field"] = a.field + } + + if a.precision != nil { + opts["precision"] = a.precision + } + + if a.size != -1 { + opts["size"] = a.size + } + + if a.shardSize != -1 { + opts["shard_size"] = a.shardSize + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_global.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_global.go new file mode 100644 index 0000000000..2e07f5d557 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_global.go @@ -0,0 +1,71 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// GlobalAggregation defines a single bucket of all the documents within +// the search execution context. This context is defined by the indices +// and the document types you’re searching on, but is not influenced +// by the search query itself. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-global-aggregation.html +type GlobalAggregation struct { + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewGlobalAggregation() *GlobalAggregation { + return &GlobalAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *GlobalAggregation) SubAggregation(name string, subAggregation Aggregation) *GlobalAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *GlobalAggregation) Meta(metaData map[string]interface{}) *GlobalAggregation { + a.meta = metaData + return a +} + +func (a *GlobalAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "all_products" : { + // "global" : {}, + // "aggs" : { + // "avg_price" : { "avg" : { "field" : "price" } } + // } + // } + // } + // } + // This method returns only the { "global" : {} } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["global"] = opts + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_histogram.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_histogram.go new file mode 100644 index 0000000000..69021e6f89 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_histogram.go @@ -0,0 +1,265 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// HistogramAggregation is a multi-bucket values source based aggregation +// that can be applied on numeric values extracted from the documents. +// It dynamically builds fixed size (a.k.a. interval) buckets over the +// values. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-histogram-aggregation.html +type HistogramAggregation struct { + field string + script *Script + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + + interval float64 + order string + orderAsc bool + minDocCount *int64 + minBounds *float64 + maxBounds *float64 + offset *float64 +} + +func NewHistogramAggregation() *HistogramAggregation { + return &HistogramAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *HistogramAggregation) Field(field string) *HistogramAggregation { + a.field = field + return a +} + +func (a *HistogramAggregation) Script(script *Script) *HistogramAggregation { + a.script = script + return a +} + +// Missing configures the value to use when documents miss a value. +func (a *HistogramAggregation) Missing(missing interface{}) *HistogramAggregation { + a.missing = missing + return a +} + +func (a *HistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *HistogramAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *HistogramAggregation) Meta(metaData map[string]interface{}) *HistogramAggregation { + a.meta = metaData + return a +} + +// Interval for this builder, must be greater than 0. +func (a *HistogramAggregation) Interval(interval float64) *HistogramAggregation { + a.interval = interval + return a +} + +// Order specifies the sort order. Valid values for order are: +// "_key", "_count", a sub-aggregation name, or a sub-aggregation name +// with a metric. +func (a *HistogramAggregation) Order(order string, asc bool) *HistogramAggregation { + a.order = order + a.orderAsc = asc + return a +} + +func (a *HistogramAggregation) OrderByCount(asc bool) *HistogramAggregation { + // "order" : { "_count" : "asc" } + a.order = "_count" + a.orderAsc = asc + return a +} + +func (a *HistogramAggregation) OrderByCountAsc() *HistogramAggregation { + return a.OrderByCount(true) +} + +func (a *HistogramAggregation) OrderByCountDesc() *HistogramAggregation { + return a.OrderByCount(false) +} + +func (a *HistogramAggregation) OrderByKey(asc bool) *HistogramAggregation { + // "order" : { "_key" : "asc" } + a.order = "_key" + a.orderAsc = asc + return a +} + +func (a *HistogramAggregation) OrderByKeyAsc() *HistogramAggregation { + return a.OrderByKey(true) +} + +func (a *HistogramAggregation) OrderByKeyDesc() *HistogramAggregation { + return a.OrderByKey(false) +} + +// OrderByAggregation creates a bucket ordering strategy which sorts buckets +// based on a single-valued calc get. +func (a *HistogramAggregation) OrderByAggregation(aggName string, asc bool) *HistogramAggregation { + // { + // "aggs" : { + // "genders" : { + // "terms" : { + // "field" : "gender", + // "order" : { "avg_height" : "desc" } + // }, + // "aggs" : { + // "avg_height" : { "avg" : { "field" : "height" } } + // } + // } + // } + // } + a.order = aggName + a.orderAsc = asc + return a +} + +// OrderByAggregationAndMetric creates a bucket ordering strategy which +// sorts buckets based on a multi-valued calc get. +func (a *HistogramAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) *HistogramAggregation { + // { + // "aggs" : { + // "genders" : { + // "terms" : { + // "field" : "gender", + // "order" : { "height_stats.avg" : "desc" } + // }, + // "aggs" : { + // "height_stats" : { "stats" : { "field" : "height" } } + // } + // } + // } + // } + a.order = aggName + "." + metric + a.orderAsc = asc + return a +} + +func (a *HistogramAggregation) MinDocCount(minDocCount int64) *HistogramAggregation { + a.minDocCount = &minDocCount + return a +} + +func (a *HistogramAggregation) ExtendedBounds(min, max float64) *HistogramAggregation { + a.minBounds = &min + a.maxBounds = &max + return a +} + +func (a *HistogramAggregation) ExtendedBoundsMin(min float64) *HistogramAggregation { + a.minBounds = &min + return a +} + +func (a *HistogramAggregation) MinBounds(min float64) *HistogramAggregation { + a.minBounds = &min + return a +} + +func (a *HistogramAggregation) ExtendedBoundsMax(max float64) *HistogramAggregation { + a.maxBounds = &max + return a +} + +func (a *HistogramAggregation) MaxBounds(max float64) *HistogramAggregation { + a.maxBounds = &max + return a +} + +// Offset into the histogram +func (a *HistogramAggregation) Offset(offset float64) *HistogramAggregation { + a.offset = &offset + return a +} + +func (a *HistogramAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "prices" : { + // "histogram" : { + // "field" : "price", + // "interval" : 50 + // } + // } + // } + // } + // + // This method returns only the { "histogram" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["histogram"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.missing != nil { + opts["missing"] = a.missing + } + + opts["interval"] = a.interval + if a.order != "" { + o := make(map[string]interface{}) + if a.orderAsc { + o[a.order] = "asc" + } else { + o[a.order] = "desc" + } + opts["order"] = o + } + if a.offset != nil { + opts["offset"] = *a.offset + } + if a.minDocCount != nil { + opts["min_doc_count"] = *a.minDocCount + } + if a.minBounds != nil || a.maxBounds != nil { + bounds := make(map[string]interface{}) + if a.minBounds != nil { + bounds["min"] = a.minBounds + } + if a.maxBounds != nil { + bounds["max"] = a.maxBounds + } + opts["extended_bounds"] = bounds + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_ip_range.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_ip_range.go new file mode 100644 index 0000000000..76ebe0d404 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_ip_range.go @@ -0,0 +1,195 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// IPRangeAggregation is a range aggregation that is dedicated for +// IP addresses. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-iprange-aggregation.html +type IPRangeAggregation struct { + field string + subAggregations map[string]Aggregation + meta map[string]interface{} + keyed *bool + entries []IPRangeAggregationEntry +} + +type IPRangeAggregationEntry struct { + Key string + Mask string + From string + To string +} + +func NewIPRangeAggregation() *IPRangeAggregation { + return &IPRangeAggregation{ + subAggregations: make(map[string]Aggregation), + entries: make([]IPRangeAggregationEntry, 0), + } +} + +func (a *IPRangeAggregation) Field(field string) *IPRangeAggregation { + a.field = field + return a +} + +func (a *IPRangeAggregation) SubAggregation(name string, subAggregation Aggregation) *IPRangeAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *IPRangeAggregation) Meta(metaData map[string]interface{}) *IPRangeAggregation { + a.meta = metaData + return a +} + +func (a *IPRangeAggregation) Keyed(keyed bool) *IPRangeAggregation { + a.keyed = &keyed + return a +} + +func (a *IPRangeAggregation) AddMaskRange(mask string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Mask: mask}) + return a +} + +func (a *IPRangeAggregation) AddMaskRangeWithKey(key, mask string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, Mask: mask}) + return a +} + +func (a *IPRangeAggregation) AddRange(from, to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: to}) + return a +} + +func (a *IPRangeAggregation) AddRangeWithKey(key, from, to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: to}) + return a +} + +func (a *IPRangeAggregation) AddUnboundedTo(from string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: ""}) + return a +} + +func (a *IPRangeAggregation) AddUnboundedToWithKey(key, from string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: ""}) + return a +} + +func (a *IPRangeAggregation) AddUnboundedFrom(to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{From: "", To: to}) + return a +} + +func (a *IPRangeAggregation) AddUnboundedFromWithKey(key, to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: "", To: to}) + return a +} + +func (a *IPRangeAggregation) Lt(to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{From: "", To: to}) + return a +} + +func (a *IPRangeAggregation) LtWithKey(key, to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: "", To: to}) + return a +} + +func (a *IPRangeAggregation) Between(from, to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: to}) + return a +} + +func (a *IPRangeAggregation) BetweenWithKey(key, from, to string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: to}) + return a +} + +func (a *IPRangeAggregation) Gt(from string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{From: from, To: ""}) + return a +} + +func (a *IPRangeAggregation) GtWithKey(key, from string) *IPRangeAggregation { + a.entries = append(a.entries, IPRangeAggregationEntry{Key: key, From: from, To: ""}) + return a +} + +func (a *IPRangeAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "range" : { + // "ip_range": { + // "field": "ip", + // "ranges": [ + // { "to": "10.0.0.5" }, + // { "from": "10.0.0.5" } + // ] + // } + // } + // } + // } + // } + // + // This method returns only the { "ip_range" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["ip_range"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + + if a.keyed != nil { + opts["keyed"] = *a.keyed + } + + var ranges []interface{} + for _, ent := range a.entries { + r := make(map[string]interface{}) + if ent.Key != "" { + r["key"] = ent.Key + } + if ent.Mask != "" { + r["mask"] = ent.Mask + } else { + if ent.From != "" { + r["from"] = ent.From + } + if ent.To != "" { + r["to"] = ent.To + } + } + ranges = append(ranges, r) + } + opts["ranges"] = ranges + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_missing.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_missing.go new file mode 100644 index 0000000000..68eba983b5 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_missing.go @@ -0,0 +1,81 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MissingAggregation is a field data based single bucket aggregation, +// that creates a bucket of all documents in the current document set context +// that are missing a field value (effectively, missing a field or having +// the configured NULL value set). This aggregator will often be used in +// conjunction with other field data bucket aggregators (such as ranges) +// to return information for all the documents that could not be placed +// in any of the other buckets due to missing field data values. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-missing-aggregation.html +type MissingAggregation struct { + field string + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewMissingAggregation() *MissingAggregation { + return &MissingAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *MissingAggregation) Field(field string) *MissingAggregation { + a.field = field + return a +} + +func (a *MissingAggregation) SubAggregation(name string, subAggregation Aggregation) *MissingAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MissingAggregation) Meta(metaData map[string]interface{}) *MissingAggregation { + a.meta = metaData + return a +} + +func (a *MissingAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "products_without_a_price" : { + // "missing" : { "field" : "price" } + // } + // } + // } + // This method returns only the { "missing" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["missing"] = opts + + if a.field != "" { + opts["field"] = a.field + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_nested.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_nested.go new file mode 100644 index 0000000000..5481f92146 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_nested.go @@ -0,0 +1,82 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// NestedAggregation is a special single bucket aggregation that enables +// aggregating nested documents. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-nested-aggregation.html +type NestedAggregation struct { + path string + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewNestedAggregation() *NestedAggregation { + return &NestedAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *NestedAggregation) SubAggregation(name string, subAggregation Aggregation) *NestedAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *NestedAggregation) Meta(metaData map[string]interface{}) *NestedAggregation { + a.meta = metaData + return a +} + +func (a *NestedAggregation) Path(path string) *NestedAggregation { + a.path = path + return a +} + +func (a *NestedAggregation) Source() (interface{}, error) { + // Example: + // { + // "query" : { + // "match" : { "name" : "led tv" } + // } + // "aggs" : { + // "resellers" : { + // "nested" : { + // "path" : "resellers" + // }, + // "aggs" : { + // "min_price" : { "min" : { "field" : "resellers.price" } } + // } + // } + // } + // } + // This method returns only the { "nested" : {} } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["nested"] = opts + + opts["path"] = a.path + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_range.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_range.go new file mode 100644 index 0000000000..7e786d9fbc --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_range.go @@ -0,0 +1,244 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "time" +) + +// RangeAggregation is a multi-bucket value source based aggregation that +// enables the user to define a set of ranges - each representing a bucket. +// During the aggregation process, the values extracted from each document +// will be checked against each bucket range and "bucket" the +// relevant/matching document. Note that this aggregration includes the +// from value and excludes the to value for each range. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-range-aggregation.html +type RangeAggregation struct { + field string + script *Script + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + keyed *bool + unmapped *bool + entries []rangeAggregationEntry +} + +type rangeAggregationEntry struct { + Key string + From interface{} + To interface{} +} + +func NewRangeAggregation() *RangeAggregation { + return &RangeAggregation{ + subAggregations: make(map[string]Aggregation), + entries: make([]rangeAggregationEntry, 0), + } +} + +func (a *RangeAggregation) Field(field string) *RangeAggregation { + a.field = field + return a +} + +func (a *RangeAggregation) Script(script *Script) *RangeAggregation { + a.script = script + return a +} + +// Missing configures the value to use when documents miss a value. +func (a *RangeAggregation) Missing(missing interface{}) *RangeAggregation { + a.missing = missing + return a +} + +func (a *RangeAggregation) SubAggregation(name string, subAggregation Aggregation) *RangeAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *RangeAggregation) Meta(metaData map[string]interface{}) *RangeAggregation { + a.meta = metaData + return a +} + +func (a *RangeAggregation) Keyed(keyed bool) *RangeAggregation { + a.keyed = &keyed + return a +} + +func (a *RangeAggregation) Unmapped(unmapped bool) *RangeAggregation { + a.unmapped = &unmapped + return a +} + +func (a *RangeAggregation) AddRange(from, to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{From: from, To: to}) + return a +} + +func (a *RangeAggregation) AddRangeWithKey(key string, from, to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: to}) + return a +} + +func (a *RangeAggregation) AddUnboundedTo(from interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{From: from, To: nil}) + return a +} + +func (a *RangeAggregation) AddUnboundedToWithKey(key string, from interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: nil}) + return a +} + +func (a *RangeAggregation) AddUnboundedFrom(to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{From: nil, To: to}) + return a +} + +func (a *RangeAggregation) AddUnboundedFromWithKey(key string, to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: nil, To: to}) + return a +} + +func (a *RangeAggregation) Lt(to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{From: nil, To: to}) + return a +} + +func (a *RangeAggregation) LtWithKey(key string, to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: nil, To: to}) + return a +} + +func (a *RangeAggregation) Between(from, to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{From: from, To: to}) + return a +} + +func (a *RangeAggregation) BetweenWithKey(key string, from, to interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: to}) + return a +} + +func (a *RangeAggregation) Gt(from interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{From: from, To: nil}) + return a +} + +func (a *RangeAggregation) GtWithKey(key string, from interface{}) *RangeAggregation { + a.entries = append(a.entries, rangeAggregationEntry{Key: key, From: from, To: nil}) + return a +} + +func (a *RangeAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "price_ranges" : { + // "range" : { + // "field" : "price", + // "ranges" : [ + // { "to" : 50 }, + // { "from" : 50, "to" : 100 }, + // { "from" : 100 } + // ] + // } + // } + // } + // } + // + // This method returns only the { "range" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["range"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.missing != nil { + opts["missing"] = a.missing + } + + if a.keyed != nil { + opts["keyed"] = *a.keyed + } + if a.unmapped != nil { + opts["unmapped"] = *a.unmapped + } + + var ranges []interface{} + for _, ent := range a.entries { + r := make(map[string]interface{}) + if ent.Key != "" { + r["key"] = ent.Key + } + if ent.From != nil { + switch from := ent.From.(type) { + case int, int16, int32, int64, float32, float64: + r["from"] = from + case *int, *int16, *int32, *int64, *float32, *float64: + r["from"] = from + case time.Time: + r["from"] = from.Format(time.RFC3339) + case *time.Time: + r["from"] = from.Format(time.RFC3339) + case string: + r["from"] = from + case *string: + r["from"] = from + } + } + if ent.To != nil { + switch to := ent.To.(type) { + case int, int16, int32, int64, float32, float64: + r["to"] = to + case *int, *int16, *int32, *int64, *float32, *float64: + r["to"] = to + case time.Time: + r["to"] = to.Format(time.RFC3339) + case *time.Time: + r["to"] = to.Format(time.RFC3339) + case string: + r["to"] = to + case *string: + r["to"] = to + } + } + ranges = append(ranges, r) + } + opts["ranges"] = ranges + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_reverse_nested.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_reverse_nested.go new file mode 100644 index 0000000000..8f454f8fce --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_reverse_nested.go @@ -0,0 +1,86 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ReverseNestedAggregation defines a special single bucket aggregation +// that enables aggregating on parent docs from nested documents. +// Effectively this aggregation can break out of the nested block +// structure and link to other nested structures or the root document, +// which allows nesting other aggregations that aren’t part of +// the nested object in a nested aggregation. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-reverse-nested-aggregation.html +type ReverseNestedAggregation struct { + path string + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +// NewReverseNestedAggregation initializes a new ReverseNestedAggregation +// bucket aggregation. +func NewReverseNestedAggregation() *ReverseNestedAggregation { + return &ReverseNestedAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +// Path set the path to use for this nested aggregation. The path must match +// the path to a nested object in the mappings. If it is not specified +// then this aggregation will go back to the root document. +func (a *ReverseNestedAggregation) Path(path string) *ReverseNestedAggregation { + a.path = path + return a +} + +func (a *ReverseNestedAggregation) SubAggregation(name string, subAggregation Aggregation) *ReverseNestedAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *ReverseNestedAggregation) Meta(metaData map[string]interface{}) *ReverseNestedAggregation { + a.meta = metaData + return a +} + +func (a *ReverseNestedAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "reverse_nested" : { + // "path": "..." + // } + // } + // } + // This method returns only the { "reverse_nested" : {} } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["reverse_nested"] = opts + + if a.path != "" { + opts["path"] = a.path + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_sampler.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_sampler.go new file mode 100644 index 0000000000..d64e6b3dd6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_sampler.go @@ -0,0 +1,111 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SamplerAggregation is a filtering aggregation used to limit any +// sub aggregations' processing to a sample of the top-scoring documents. +// Optionally, diversity settings can be used to limit the number of matches +// that share a common value such as an "author". +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-sampler-aggregation.html +type SamplerAggregation struct { + subAggregations map[string]Aggregation + meta map[string]interface{} + + shardSize int + maxDocsPerValue int + executionHint string +} + +func NewSamplerAggregation() *SamplerAggregation { + return &SamplerAggregation{ + shardSize: -1, + maxDocsPerValue: -1, + subAggregations: make(map[string]Aggregation), + } +} + +func (a *SamplerAggregation) SubAggregation(name string, subAggregation Aggregation) *SamplerAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *SamplerAggregation) Meta(metaData map[string]interface{}) *SamplerAggregation { + a.meta = metaData + return a +} + +// ShardSize sets the maximum number of docs returned from each shard. +func (a *SamplerAggregation) ShardSize(shardSize int) *SamplerAggregation { + a.shardSize = shardSize + return a +} + +func (a *SamplerAggregation) MaxDocsPerValue(maxDocsPerValue int) *SamplerAggregation { + a.maxDocsPerValue = maxDocsPerValue + return a +} + +func (a *SamplerAggregation) ExecutionHint(hint string) *SamplerAggregation { + a.executionHint = hint + return a +} + +func (a *SamplerAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "sample" : { + // "sampler" : { + // "shard_size" : 200 + // }, + // "aggs": { + // "keywords": { + // "significant_terms": { + // "field": "text" + // } + // } + // } + // } + // } + // } + // + // This method returns only the { "sampler" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["sampler"] = opts + + if a.shardSize >= 0 { + opts["shard_size"] = a.shardSize + } + if a.maxDocsPerValue >= 0 { + opts["max_docs_per_value"] = a.maxDocsPerValue + } + if a.executionHint != "" { + opts["execution_hint"] = a.executionHint + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_terms.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_terms.go new file mode 100644 index 0000000000..e30cb22f1a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_terms.go @@ -0,0 +1,450 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SignificantTermsAggregation is an aggregation that returns interesting +// or unusual occurrences of terms in a set. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html +type SignificantTermsAggregation struct { + field string + subAggregations map[string]Aggregation + meta map[string]interface{} + + minDocCount *int + shardMinDocCount *int + requiredSize *int + shardSize *int + filter Query + executionHint string + significanceHeuristic SignificanceHeuristic + includeExclude *TermsAggregationIncludeExclude +} + +func NewSignificantTermsAggregation() *SignificantTermsAggregation { + return &SignificantTermsAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *SignificantTermsAggregation) Field(field string) *SignificantTermsAggregation { + a.field = field + return a +} + +func (a *SignificantTermsAggregation) SubAggregation(name string, subAggregation Aggregation) *SignificantTermsAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *SignificantTermsAggregation) Meta(metaData map[string]interface{}) *SignificantTermsAggregation { + a.meta = metaData + return a +} + +func (a *SignificantTermsAggregation) MinDocCount(minDocCount int) *SignificantTermsAggregation { + a.minDocCount = &minDocCount + return a +} + +func (a *SignificantTermsAggregation) ShardMinDocCount(shardMinDocCount int) *SignificantTermsAggregation { + a.shardMinDocCount = &shardMinDocCount + return a +} + +func (a *SignificantTermsAggregation) RequiredSize(requiredSize int) *SignificantTermsAggregation { + a.requiredSize = &requiredSize + return a +} + +func (a *SignificantTermsAggregation) ShardSize(shardSize int) *SignificantTermsAggregation { + a.shardSize = &shardSize + return a +} + +func (a *SignificantTermsAggregation) BackgroundFilter(filter Query) *SignificantTermsAggregation { + a.filter = filter + return a +} + +func (a *SignificantTermsAggregation) ExecutionHint(hint string) *SignificantTermsAggregation { + a.executionHint = hint + return a +} + +func (a *SignificantTermsAggregation) SignificanceHeuristic(heuristic SignificanceHeuristic) *SignificantTermsAggregation { + a.significanceHeuristic = heuristic + return a +} + +func (a *SignificantTermsAggregation) Include(regexp string) *SignificantTermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Include = regexp + return a +} + +func (a *SignificantTermsAggregation) IncludeValues(values ...interface{}) *SignificantTermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...) + return a +} + +func (a *SignificantTermsAggregation) Exclude(regexp string) *SignificantTermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Exclude = regexp + return a +} + +func (a *SignificantTermsAggregation) ExcludeValues(values ...interface{}) *SignificantTermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...) + return a +} + +func (a *SignificantTermsAggregation) Partition(p int) *SignificantTermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Partition = p + return a +} + +func (a *SignificantTermsAggregation) NumPartitions(n int) *SignificantTermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.NumPartitions = n + return a +} + +func (a *SignificantTermsAggregation) IncludeExclude(includeExclude *TermsAggregationIncludeExclude) *SignificantTermsAggregation { + a.includeExclude = includeExclude + return a +} + +func (a *SignificantTermsAggregation) Source() (interface{}, error) { + // Example: + // { + // "query" : { + // "terms" : {"force" : [ "British Transport Police" ]} + // }, + // "aggregations" : { + // "significantCrimeTypes" : { + // "significant_terms" : { "field" : "crime_type" } + // } + // } + // } + // + // This method returns only the + // { "significant_terms" : { "field" : "crime_type" } + // part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["significant_terms"] = opts + + if a.field != "" { + opts["field"] = a.field + } + if a.requiredSize != nil { + opts["size"] = *a.requiredSize // not a typo! + } + if a.shardSize != nil { + opts["shard_size"] = *a.shardSize + } + if a.minDocCount != nil { + opts["min_doc_count"] = *a.minDocCount + } + if a.shardMinDocCount != nil { + opts["shard_min_doc_count"] = *a.shardMinDocCount + } + if a.executionHint != "" { + opts["execution_hint"] = a.executionHint + } + if a.filter != nil { + src, err := a.filter.Source() + if err != nil { + return nil, err + } + opts["background_filter"] = src + } + if a.significanceHeuristic != nil { + name := a.significanceHeuristic.Name() + src, err := a.significanceHeuristic.Source() + if err != nil { + return nil, err + } + opts[name] = src + } + + // Include/Exclude + if ie := a.includeExclude; ie != nil { + if err := ie.MergeInto(opts); err != nil { + return nil, err + } + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} + +// -- Significance heuristics -- + +type SignificanceHeuristic interface { + Name() string + Source() (interface{}, error) +} + +// -- Chi Square -- + +// ChiSquareSignificanceHeuristic implements Chi square as described +// in "Information Retrieval", Manning et al., Chapter 13.5.2. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_chi_square +// for details. +type ChiSquareSignificanceHeuristic struct { + backgroundIsSuperset *bool + includeNegatives *bool +} + +// NewChiSquareSignificanceHeuristic initializes a new ChiSquareSignificanceHeuristic. +func NewChiSquareSignificanceHeuristic() *ChiSquareSignificanceHeuristic { + return &ChiSquareSignificanceHeuristic{} +} + +// Name returns the name of the heuristic in the REST interface. +func (sh *ChiSquareSignificanceHeuristic) Name() string { + return "chi_square" +} + +// BackgroundIsSuperset indicates whether you defined a custom background +// filter that represents a difference set of documents that you want to +// compare to. +func (sh *ChiSquareSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *ChiSquareSignificanceHeuristic { + sh.backgroundIsSuperset = &backgroundIsSuperset + return sh +} + +// IncludeNegatives indicates whether to filter out the terms that appear +// much less in the subset than in the background without the subset. +func (sh *ChiSquareSignificanceHeuristic) IncludeNegatives(includeNegatives bool) *ChiSquareSignificanceHeuristic { + sh.includeNegatives = &includeNegatives + return sh +} + +// Source returns the parameters that need to be added to the REST parameters. +func (sh *ChiSquareSignificanceHeuristic) Source() (interface{}, error) { + source := make(map[string]interface{}) + if sh.backgroundIsSuperset != nil { + source["background_is_superset"] = *sh.backgroundIsSuperset + } + if sh.includeNegatives != nil { + source["include_negatives"] = *sh.includeNegatives + } + return source, nil +} + +// -- GND -- + +// GNDSignificanceHeuristic implements the "Google Normalized Distance" +// as described in "The Google Similarity Distance", Cilibrasi and Vitanyi, +// 2007. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_google_normalized_distance +// for details. +type GNDSignificanceHeuristic struct { + backgroundIsSuperset *bool +} + +// NewGNDSignificanceHeuristic implements a new GNDSignificanceHeuristic. +func NewGNDSignificanceHeuristic() *GNDSignificanceHeuristic { + return &GNDSignificanceHeuristic{} +} + +// Name returns the name of the heuristic in the REST interface. +func (sh *GNDSignificanceHeuristic) Name() string { + return "gnd" +} + +// BackgroundIsSuperset indicates whether you defined a custom background +// filter that represents a difference set of documents that you want to +// compare to. +func (sh *GNDSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *GNDSignificanceHeuristic { + sh.backgroundIsSuperset = &backgroundIsSuperset + return sh +} + +// Source returns the parameters that need to be added to the REST parameters. +func (sh *GNDSignificanceHeuristic) Source() (interface{}, error) { + source := make(map[string]interface{}) + if sh.backgroundIsSuperset != nil { + source["background_is_superset"] = *sh.backgroundIsSuperset + } + return source, nil +} + +// -- JLH Score -- + +// JLHScoreSignificanceHeuristic implements the JLH score as described in +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_jlh_score. +type JLHScoreSignificanceHeuristic struct{} + +// NewJLHScoreSignificanceHeuristic initializes a new JLHScoreSignificanceHeuristic. +func NewJLHScoreSignificanceHeuristic() *JLHScoreSignificanceHeuristic { + return &JLHScoreSignificanceHeuristic{} +} + +// Name returns the name of the heuristic in the REST interface. +func (sh *JLHScoreSignificanceHeuristic) Name() string { + return "jlh" +} + +// Source returns the parameters that need to be added to the REST parameters. +func (sh *JLHScoreSignificanceHeuristic) Source() (interface{}, error) { + source := make(map[string]interface{}) + return source, nil +} + +// -- Mutual Information -- + +// MutualInformationSignificanceHeuristic implements Mutual information +// as described in "Information Retrieval", Manning et al., Chapter 13.5.1. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_mutual_information +// for details. +type MutualInformationSignificanceHeuristic struct { + backgroundIsSuperset *bool + includeNegatives *bool +} + +// NewMutualInformationSignificanceHeuristic initializes a new instance of +// MutualInformationSignificanceHeuristic. +func NewMutualInformationSignificanceHeuristic() *MutualInformationSignificanceHeuristic { + return &MutualInformationSignificanceHeuristic{} +} + +// Name returns the name of the heuristic in the REST interface. +func (sh *MutualInformationSignificanceHeuristic) Name() string { + return "mutual_information" +} + +// BackgroundIsSuperset indicates whether you defined a custom background +// filter that represents a difference set of documents that you want to +// compare to. +func (sh *MutualInformationSignificanceHeuristic) BackgroundIsSuperset(backgroundIsSuperset bool) *MutualInformationSignificanceHeuristic { + sh.backgroundIsSuperset = &backgroundIsSuperset + return sh +} + +// IncludeNegatives indicates whether to filter out the terms that appear +// much less in the subset than in the background without the subset. +func (sh *MutualInformationSignificanceHeuristic) IncludeNegatives(includeNegatives bool) *MutualInformationSignificanceHeuristic { + sh.includeNegatives = &includeNegatives + return sh +} + +// Source returns the parameters that need to be added to the REST parameters. +func (sh *MutualInformationSignificanceHeuristic) Source() (interface{}, error) { + source := make(map[string]interface{}) + if sh.backgroundIsSuperset != nil { + source["background_is_superset"] = *sh.backgroundIsSuperset + } + if sh.includeNegatives != nil { + source["include_negatives"] = *sh.includeNegatives + } + return source, nil +} + +// -- Percentage Score -- + +// PercentageScoreSignificanceHeuristic implements the algorithm described +// in https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_percentage. +type PercentageScoreSignificanceHeuristic struct{} + +// NewPercentageScoreSignificanceHeuristic initializes a new instance of +// PercentageScoreSignificanceHeuristic. +func NewPercentageScoreSignificanceHeuristic() *PercentageScoreSignificanceHeuristic { + return &PercentageScoreSignificanceHeuristic{} +} + +// Name returns the name of the heuristic in the REST interface. +func (sh *PercentageScoreSignificanceHeuristic) Name() string { + return "percentage" +} + +// Source returns the parameters that need to be added to the REST parameters. +func (sh *PercentageScoreSignificanceHeuristic) Source() (interface{}, error) { + source := make(map[string]interface{}) + return source, nil +} + +// -- Script -- + +// ScriptSignificanceHeuristic implements a scripted significance heuristic. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_scripted +// for details. +type ScriptSignificanceHeuristic struct { + script *Script +} + +// NewScriptSignificanceHeuristic initializes a new instance of +// ScriptSignificanceHeuristic. +func NewScriptSignificanceHeuristic() *ScriptSignificanceHeuristic { + return &ScriptSignificanceHeuristic{} +} + +// Name returns the name of the heuristic in the REST interface. +func (sh *ScriptSignificanceHeuristic) Name() string { + return "script_heuristic" +} + +// Script specifies the script to use to get custom scores. The following +// parameters are available in the script: `_subset_freq`, `_superset_freq`, +// `_subset_size`, and `_superset_size`. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significantterms-aggregation.html#_scripted +// for details. +func (sh *ScriptSignificanceHeuristic) Script(script *Script) *ScriptSignificanceHeuristic { + sh.script = script + return sh +} + +// Source returns the parameters that need to be added to the REST parameters. +func (sh *ScriptSignificanceHeuristic) Source() (interface{}, error) { + source := make(map[string]interface{}) + if sh.script != nil { + src, err := sh.script.Source() + if err != nil { + return nil, err + } + source["script"] = src + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_text.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_text.go new file mode 100644 index 0000000000..2602df9075 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_significant_text.go @@ -0,0 +1,250 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SignificantTextAggregation returns interesting or unusual occurrences +// of free-text terms in a set. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-significanttext-aggregation.html +type SignificantTextAggregation struct { + field string + subAggregations map[string]Aggregation + meta map[string]interface{} + + sourceFieldNames []string + filterDuplicateText *bool + includeExclude *TermsAggregationIncludeExclude + filter Query + bucketCountThresholds *BucketCountThresholds + significanceHeuristic SignificanceHeuristic +} + +func NewSignificantTextAggregation() *SignificantTextAggregation { + return &SignificantTextAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *SignificantTextAggregation) Field(field string) *SignificantTextAggregation { + a.field = field + return a +} + +func (a *SignificantTextAggregation) SubAggregation(name string, subAggregation Aggregation) *SignificantTextAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *SignificantTextAggregation) Meta(metaData map[string]interface{}) *SignificantTextAggregation { + a.meta = metaData + return a +} + +func (a *SignificantTextAggregation) SourceFieldNames(names ...string) *SignificantTextAggregation { + a.sourceFieldNames = names + return a +} + +func (a *SignificantTextAggregation) FilterDuplicateText(filter bool) *SignificantTextAggregation { + a.filterDuplicateText = &filter + return a +} + +func (a *SignificantTextAggregation) MinDocCount(minDocCount int64) *SignificantTextAggregation { + if a.bucketCountThresholds == nil { + a.bucketCountThresholds = &BucketCountThresholds{} + } + a.bucketCountThresholds.MinDocCount = &minDocCount + return a +} + +func (a *SignificantTextAggregation) ShardMinDocCount(shardMinDocCount int64) *SignificantTextAggregation { + if a.bucketCountThresholds == nil { + a.bucketCountThresholds = &BucketCountThresholds{} + } + a.bucketCountThresholds.ShardMinDocCount = &shardMinDocCount + return a +} + +func (a *SignificantTextAggregation) Size(size int) *SignificantTextAggregation { + if a.bucketCountThresholds == nil { + a.bucketCountThresholds = &BucketCountThresholds{} + } + a.bucketCountThresholds.RequiredSize = &size + return a +} + +func (a *SignificantTextAggregation) ShardSize(shardSize int) *SignificantTextAggregation { + if a.bucketCountThresholds == nil { + a.bucketCountThresholds = &BucketCountThresholds{} + } + a.bucketCountThresholds.ShardSize = &shardSize + return a +} + +func (a *SignificantTextAggregation) BackgroundFilter(filter Query) *SignificantTextAggregation { + a.filter = filter + return a +} + +func (a *SignificantTextAggregation) SignificanceHeuristic(heuristic SignificanceHeuristic) *SignificantTextAggregation { + a.significanceHeuristic = heuristic + return a +} + +func (a *SignificantTextAggregation) Include(regexp string) *SignificantTextAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Include = regexp + return a +} + +func (a *SignificantTextAggregation) IncludeValues(values ...interface{}) *SignificantTextAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...) + return a +} + +func (a *SignificantTextAggregation) Exclude(regexp string) *SignificantTextAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Exclude = regexp + return a +} + +func (a *SignificantTextAggregation) ExcludeValues(values ...interface{}) *SignificantTextAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...) + return a +} + +func (a *SignificantTextAggregation) Partition(p int) *SignificantTextAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Partition = p + return a +} + +func (a *SignificantTextAggregation) NumPartitions(n int) *SignificantTextAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.NumPartitions = n + return a +} + +func (a *SignificantTextAggregation) IncludeExclude(includeExclude *TermsAggregationIncludeExclude) *SignificantTextAggregation { + a.includeExclude = includeExclude + return a +} + +func (a *SignificantTextAggregation) Source() (interface{}, error) { + // Example: + // { + // "query" : { + // "match" : {"content" : "Bird flu"} + // }, + // "aggregations" : { + // "my_sample" : { + // "sampler": { + // "shard_size" : 100 + // }, + // "aggregations": { + // "keywords" : { + // "significant_text" : { "field" : "content" } + // } + // } + // } + // } + // } + // + // This method returns only the + // { "significant_text" : { "field" : "content" } + // part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["significant_text"] = opts + + if a.field != "" { + opts["field"] = a.field + } + if a.bucketCountThresholds != nil { + if a.bucketCountThresholds.RequiredSize != nil { + opts["size"] = (*a.bucketCountThresholds).RequiredSize + } + if a.bucketCountThresholds.ShardSize != nil { + opts["shard_size"] = (*a.bucketCountThresholds).ShardSize + } + if a.bucketCountThresholds.MinDocCount != nil { + opts["min_doc_count"] = (*a.bucketCountThresholds).MinDocCount + } + if a.bucketCountThresholds.ShardMinDocCount != nil { + opts["shard_min_doc_count"] = (*a.bucketCountThresholds).ShardMinDocCount + } + } + if a.filter != nil { + src, err := a.filter.Source() + if err != nil { + return nil, err + } + opts["background_filter"] = src + } + if a.significanceHeuristic != nil { + name := a.significanceHeuristic.Name() + src, err := a.significanceHeuristic.Source() + if err != nil { + return nil, err + } + opts[name] = src + } + // Include/Exclude + if ie := a.includeExclude; ie != nil { + // Include + if ie.Include != "" { + opts["include"] = ie.Include + } else if len(ie.IncludeValues) > 0 { + opts["include"] = ie.IncludeValues + } else if ie.NumPartitions > 0 { + inc := make(map[string]interface{}) + inc["partition"] = ie.Partition + inc["num_partitions"] = ie.NumPartitions + opts["include"] = inc + } + // Exclude + if ie.Exclude != "" { + opts["exclude"] = ie.Exclude + } else if len(ie.ExcludeValues) > 0 { + opts["exclude"] = ie.ExcludeValues + } + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_terms.go b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_terms.go new file mode 100644 index 0000000000..8f047be5f2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_bucket_terms.go @@ -0,0 +1,422 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "fmt" + +// TermsAggregation is a multi-bucket value source based aggregation +// where buckets are dynamically built - one per unique value. +// +// See: http://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket-terms-aggregation.html +type TermsAggregation struct { + field string + script *Script + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + + size *int + shardSize *int + requiredSize *int + minDocCount *int + shardMinDocCount *int + valueType string + includeExclude *TermsAggregationIncludeExclude + executionHint string + collectionMode string + showTermDocCountError *bool + order []TermsOrder +} + +func NewTermsAggregation() *TermsAggregation { + return &TermsAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *TermsAggregation) Field(field string) *TermsAggregation { + a.field = field + return a +} + +func (a *TermsAggregation) Script(script *Script) *TermsAggregation { + a.script = script + return a +} + +// Missing configures the value to use when documents miss a value. +func (a *TermsAggregation) Missing(missing interface{}) *TermsAggregation { + a.missing = missing + return a +} + +func (a *TermsAggregation) SubAggregation(name string, subAggregation Aggregation) *TermsAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *TermsAggregation) Meta(metaData map[string]interface{}) *TermsAggregation { + a.meta = metaData + return a +} + +func (a *TermsAggregation) Size(size int) *TermsAggregation { + a.size = &size + return a +} + +func (a *TermsAggregation) RequiredSize(requiredSize int) *TermsAggregation { + a.requiredSize = &requiredSize + return a +} + +func (a *TermsAggregation) ShardSize(shardSize int) *TermsAggregation { + a.shardSize = &shardSize + return a +} + +func (a *TermsAggregation) MinDocCount(minDocCount int) *TermsAggregation { + a.minDocCount = &minDocCount + return a +} + +func (a *TermsAggregation) ShardMinDocCount(shardMinDocCount int) *TermsAggregation { + a.shardMinDocCount = &shardMinDocCount + return a +} + +func (a *TermsAggregation) Include(regexp string) *TermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Include = regexp + return a +} + +func (a *TermsAggregation) IncludeValues(values ...interface{}) *TermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...) + return a +} + +func (a *TermsAggregation) Exclude(regexp string) *TermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Exclude = regexp + return a +} + +func (a *TermsAggregation) ExcludeValues(values ...interface{}) *TermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...) + return a +} + +func (a *TermsAggregation) Partition(p int) *TermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.Partition = p + return a +} + +func (a *TermsAggregation) NumPartitions(n int) *TermsAggregation { + if a.includeExclude == nil { + a.includeExclude = &TermsAggregationIncludeExclude{} + } + a.includeExclude.NumPartitions = n + return a +} + +func (a *TermsAggregation) IncludeExclude(includeExclude *TermsAggregationIncludeExclude) *TermsAggregation { + a.includeExclude = includeExclude + return a +} + +// ValueType can be string, long, or double. +func (a *TermsAggregation) ValueType(valueType string) *TermsAggregation { + a.valueType = valueType + return a +} + +func (a *TermsAggregation) Order(order string, asc bool) *TermsAggregation { + a.order = append(a.order, TermsOrder{Field: order, Ascending: asc}) + return a +} + +func (a *TermsAggregation) OrderByCount(asc bool) *TermsAggregation { + // "order" : { "_count" : "asc" } + a.order = append(a.order, TermsOrder{Field: "_count", Ascending: asc}) + return a +} + +func (a *TermsAggregation) OrderByCountAsc() *TermsAggregation { + return a.OrderByCount(true) +} + +func (a *TermsAggregation) OrderByCountDesc() *TermsAggregation { + return a.OrderByCount(false) +} + +// Deprecated: Use OrderByKey instead. +func (a *TermsAggregation) OrderByTerm(asc bool) *TermsAggregation { + // "order" : { "_term" : "asc" } + a.order = append(a.order, TermsOrder{Field: "_term", Ascending: asc}) + return a +} + +// Deprecated: Use OrderByKeyAsc instead. +func (a *TermsAggregation) OrderByTermAsc() *TermsAggregation { + return a.OrderByTerm(true) +} + +// Deprecated: Use OrderByKeyDesc instead. +func (a *TermsAggregation) OrderByTermDesc() *TermsAggregation { + return a.OrderByTerm(false) +} + +func (a *TermsAggregation) OrderByKey(asc bool) *TermsAggregation { + // "order" : { "_term" : "asc" } + a.order = append(a.order, TermsOrder{Field: "_key", Ascending: asc}) + return a +} + +func (a *TermsAggregation) OrderByKeyAsc() *TermsAggregation { + return a.OrderByKey(true) +} + +func (a *TermsAggregation) OrderByKeyDesc() *TermsAggregation { + return a.OrderByKey(false) +} + +// OrderByAggregation creates a bucket ordering strategy which sorts buckets +// based on a single-valued calc get. +func (a *TermsAggregation) OrderByAggregation(aggName string, asc bool) *TermsAggregation { + // { + // "aggs" : { + // "genders" : { + // "terms" : { + // "field" : "gender", + // "order" : { "avg_height" : "desc" } + // }, + // "aggs" : { + // "avg_height" : { "avg" : { "field" : "height" } } + // } + // } + // } + // } + a.order = append(a.order, TermsOrder{Field: aggName, Ascending: asc}) + return a +} + +// OrderByAggregationAndMetric creates a bucket ordering strategy which +// sorts buckets based on a multi-valued calc get. +func (a *TermsAggregation) OrderByAggregationAndMetric(aggName, metric string, asc bool) *TermsAggregation { + // { + // "aggs" : { + // "genders" : { + // "terms" : { + // "field" : "gender", + // "order" : { "height_stats.avg" : "desc" } + // }, + // "aggs" : { + // "height_stats" : { "stats" : { "field" : "height" } } + // } + // } + // } + // } + a.order = append(a.order, TermsOrder{Field: aggName + "." + metric, Ascending: asc}) + return a +} + +func (a *TermsAggregation) ExecutionHint(hint string) *TermsAggregation { + a.executionHint = hint + return a +} + +// Collection mode can be depth_first or breadth_first as of 1.4.0. +func (a *TermsAggregation) CollectionMode(collectionMode string) *TermsAggregation { + a.collectionMode = collectionMode + return a +} + +func (a *TermsAggregation) ShowTermDocCountError(showTermDocCountError bool) *TermsAggregation { + a.showTermDocCountError = &showTermDocCountError + return a +} + +func (a *TermsAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "genders" : { + // "terms" : { "field" : "gender" } + // } + // } + // } + // This method returns only the { "terms" : { "field" : "gender" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["terms"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.missing != nil { + opts["missing"] = a.missing + } + + // TermsBuilder + if a.size != nil && *a.size >= 0 { + opts["size"] = *a.size + } + if a.shardSize != nil && *a.shardSize >= 0 { + opts["shard_size"] = *a.shardSize + } + if a.requiredSize != nil && *a.requiredSize >= 0 { + opts["required_size"] = *a.requiredSize + } + if a.minDocCount != nil && *a.minDocCount >= 0 { + opts["min_doc_count"] = *a.minDocCount + } + if a.shardMinDocCount != nil && *a.shardMinDocCount >= 0 { + opts["shard_min_doc_count"] = *a.shardMinDocCount + } + if a.showTermDocCountError != nil { + opts["show_term_doc_count_error"] = *a.showTermDocCountError + } + if a.collectionMode != "" { + opts["collect_mode"] = a.collectionMode + } + if a.valueType != "" { + opts["value_type"] = a.valueType + } + if len(a.order) > 0 { + var orderSlice []interface{} + for _, order := range a.order { + src, err := order.Source() + if err != nil { + return nil, err + } + orderSlice = append(orderSlice, src) + } + opts["order"] = orderSlice + } + + // Include/Exclude + if ie := a.includeExclude; ie != nil { + if err := ie.MergeInto(opts); err != nil { + return nil, err + } + } + + if a.executionHint != "" { + opts["execution_hint"] = a.executionHint + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} + +// TermsAggregationIncludeExclude allows for include/exclude in a TermsAggregation. +type TermsAggregationIncludeExclude struct { + Include string + Exclude string + IncludeValues []interface{} + ExcludeValues []interface{} + Partition int + NumPartitions int +} + +// Source returns a JSON serializable struct. +func (ie *TermsAggregationIncludeExclude) Source() (interface{}, error) { + source := make(map[string]interface{}) + + // Include + if ie.Include != "" { + source["include"] = ie.Include + } else if len(ie.IncludeValues) > 0 { + source["include"] = ie.IncludeValues + } else if ie.NumPartitions > 0 { + inc := make(map[string]interface{}) + inc["partition"] = ie.Partition + inc["num_partitions"] = ie.NumPartitions + source["include"] = inc + } + + // Exclude + if ie.Exclude != "" { + source["exclude"] = ie.Exclude + } else if len(ie.ExcludeValues) > 0 { + source["exclude"] = ie.ExcludeValues + } + + return source, nil +} + +// MergeInto merges the values of the include/exclude options into source. +func (ie *TermsAggregationIncludeExclude) MergeInto(source map[string]interface{}) error { + values, err := ie.Source() + if err != nil { + return err + } + mv, ok := values.(map[string]interface{}) + if !ok { + return fmt.Errorf("IncludeExclude: expected a map[string]interface{}, got %T", values) + } + for k, v := range mv { + source[k] = v + } + return nil +} + +// TermsOrder specifies a single order field for a terms aggregation. +type TermsOrder struct { + Field string + Ascending bool +} + +// Source returns serializable JSON of the TermsOrder. +func (order *TermsOrder) Source() (interface{}, error) { + source := make(map[string]string) + if order.Ascending { + source[order.Field] = "asc" + } else { + source[order.Field] = "desc" + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_matrix_stats.go b/vendor/github.com/olivere/elastic/v7/search_aggs_matrix_stats.go new file mode 100644 index 0000000000..f0eb457ff6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_matrix_stats.go @@ -0,0 +1,126 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MatrixMatrixStatsAggregation is a multi-value metrics aggregation +// that computes stats over numeric values extracted from the +// aggregated documents. These values can be extracted either from +// specific numeric fields in the documents, or be generated by a provided script. +// +// The stats that are returned consist of: min, max, sum, count and avg. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-stats-aggregation.html +// for details. +type MatrixStatsAggregation struct { + fields []string + missing interface{} + format string + valueType interface{} + mode string + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +// NewMatrixStatsAggregation initializes a new MatrixStatsAggregation. +func NewMatrixStatsAggregation() *MatrixStatsAggregation { + return &MatrixStatsAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *MatrixStatsAggregation) Fields(fields ...string) *MatrixStatsAggregation { + a.fields = append(a.fields, fields...) + return a +} + +// Missing configures the value to use when documents miss a value. +func (a *MatrixStatsAggregation) Missing(missing interface{}) *MatrixStatsAggregation { + a.missing = missing + return a +} + +// Mode specifies how to operate. Valid values are: sum, avg, median, min, or max. +func (a *MatrixStatsAggregation) Mode(mode string) *MatrixStatsAggregation { + a.mode = mode + return a +} + +func (a *MatrixStatsAggregation) Format(format string) *MatrixStatsAggregation { + a.format = format + return a +} + +func (a *MatrixStatsAggregation) ValueType(valueType interface{}) *MatrixStatsAggregation { + a.valueType = valueType + return a +} + +func (a *MatrixStatsAggregation) SubAggregation(name string, subAggregation Aggregation) *MatrixStatsAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MatrixStatsAggregation) Meta(metaData map[string]interface{}) *MatrixStatsAggregation { + a.meta = metaData + return a +} + +// Source returns the JSON to serialize into the request, or an error. +func (a *MatrixStatsAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "matrixstats" : { + // "matrix_stats" : { + // "fields" : ["poverty", "income"], + // "missing": {"income": 50000}, + // "mode": "avg", + // ... + // } + // } + // } + // } + // This method returns only the { "matrix_stats" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["matrix_stats"] = opts + + // MatrixStatsAggregationBuilder + opts["fields"] = a.fields + if a.missing != nil { + opts["missing"] = a.missing + } + if a.format != "" { + opts["format"] = a.format + } + if a.valueType != nil { + opts["value_type"] = a.valueType + } + if a.mode != "" { + opts["mode"] = a.mode + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_avg.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_avg.go new file mode 100644 index 0000000000..7f58caf316 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_avg.go @@ -0,0 +1,112 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// AvgAggregation is a single-value metrics aggregation that computes +// the average of numeric values that are extracted from the +// aggregated documents. These values can be extracted either from +// specific numeric fields in the documents, or be generated by +// a provided script. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-avg-aggregation.html +type AvgAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewAvgAggregation() *AvgAggregation { + return &AvgAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *AvgAggregation) Field(field string) *AvgAggregation { + a.field = field + return a +} + +func (a *AvgAggregation) Script(script *Script) *AvgAggregation { + a.script = script + return a +} + +func (a *AvgAggregation) Format(format string) *AvgAggregation { + a.format = format + return a +} + +func (a *AvgAggregation) Missing(missing interface{}) *AvgAggregation { + a.missing = missing + return a +} + +func (a *AvgAggregation) SubAggregation(name string, subAggregation Aggregation) *AvgAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *AvgAggregation) Meta(metaData map[string]interface{}) *AvgAggregation { + a.meta = metaData + return a +} + +func (a *AvgAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "avg_grade" : { "avg" : { "field" : "grade" } } + // } + // } + // This method returns only the { "avg" : { "field" : "grade" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["avg"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + + if a.format != "" { + opts["format"] = a.format + } + + if a.missing != nil { + opts["missing"] = a.missing + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_cardinality.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_cardinality.go new file mode 100644 index 0000000000..f0599a68e7 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_cardinality.go @@ -0,0 +1,127 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// CardinalityAggregation is a single-value metrics aggregation that +// calculates an approximate count of distinct values. +// Values can be extracted either from specific fields in the document +// or generated by a script. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-cardinality-aggregation.html +type CardinalityAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + precisionThreshold *int64 + rehash *bool +} + +func NewCardinalityAggregation() *CardinalityAggregation { + return &CardinalityAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *CardinalityAggregation) Field(field string) *CardinalityAggregation { + a.field = field + return a +} + +func (a *CardinalityAggregation) Script(script *Script) *CardinalityAggregation { + a.script = script + return a +} + +func (a *CardinalityAggregation) Format(format string) *CardinalityAggregation { + a.format = format + return a +} + +func (a *CardinalityAggregation) Missing(missing interface{}) *CardinalityAggregation { + a.missing = missing + return a +} +func (a *CardinalityAggregation) SubAggregation(name string, subAggregation Aggregation) *CardinalityAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *CardinalityAggregation) Meta(metaData map[string]interface{}) *CardinalityAggregation { + a.meta = metaData + return a +} + +func (a *CardinalityAggregation) PrecisionThreshold(threshold int64) *CardinalityAggregation { + a.precisionThreshold = &threshold + return a +} + +func (a *CardinalityAggregation) Rehash(rehash bool) *CardinalityAggregation { + a.rehash = &rehash + return a +} + +func (a *CardinalityAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "author_count" : { + // "cardinality" : { "field" : "author" } + // } + // } + // } + // This method returns only the "cardinality" : { "field" : "author" } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["cardinality"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.missing != nil { + opts["missing"] = a.missing + } + if a.format != "" { + opts["format"] = a.format + } + if a.precisionThreshold != nil { + opts["precision_threshold"] = *a.precisionThreshold + } + if a.rehash != nil { + opts["rehash"] = *a.rehash + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_extended_stats.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_extended_stats.go new file mode 100644 index 0000000000..3008a8d124 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_extended_stats.go @@ -0,0 +1,117 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ExtendedExtendedStatsAggregation is a multi-value metrics aggregation that +// computes stats over numeric values extracted from the aggregated documents. +// These values can be extracted either from specific numeric fields +// in the documents, or be generated by a provided script. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-extendedstats-aggregation.html +type ExtendedStatsAggregation struct { + field string + script *Script + format string + missing interface{} + sigma *float64 + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewExtendedStatsAggregation() *ExtendedStatsAggregation { + return &ExtendedStatsAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *ExtendedStatsAggregation) Field(field string) *ExtendedStatsAggregation { + a.field = field + return a +} + +func (a *ExtendedStatsAggregation) Script(script *Script) *ExtendedStatsAggregation { + a.script = script + return a +} + +func (a *ExtendedStatsAggregation) Format(format string) *ExtendedStatsAggregation { + a.format = format + return a +} + +func (a *ExtendedStatsAggregation) Missing(missing interface{}) *ExtendedStatsAggregation { + a.missing = missing + return a +} + +func (a *ExtendedStatsAggregation) Sigma(sigma float64) *ExtendedStatsAggregation { + a.sigma = &sigma + return a +} + +func (a *ExtendedStatsAggregation) SubAggregation(name string, subAggregation Aggregation) *ExtendedStatsAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *ExtendedStatsAggregation) Meta(metaData map[string]interface{}) *ExtendedStatsAggregation { + a.meta = metaData + return a +} + +func (a *ExtendedStatsAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "grades_stats" : { "extended_stats" : { "field" : "grade" } } + // } + // } + // This method returns only the { "extended_stats" : { "field" : "grade" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["extended_stats"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if v := a.missing; v != nil { + opts["missing"] = v + } + if v := a.sigma; v != nil { + opts["sigma"] = *v + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_bounds.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_bounds.go new file mode 100644 index 0000000000..007e3029ae --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_bounds.go @@ -0,0 +1,105 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// GeoBoundsAggregation is a metric aggregation that computes the +// bounding box containing all geo_point values for a field. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-geobounds-aggregation.html +type GeoBoundsAggregation struct { + field string + script *Script + wrapLongitude *bool + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewGeoBoundsAggregation() *GeoBoundsAggregation { + return &GeoBoundsAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *GeoBoundsAggregation) Field(field string) *GeoBoundsAggregation { + a.field = field + return a +} + +func (a *GeoBoundsAggregation) Script(script *Script) *GeoBoundsAggregation { + a.script = script + return a +} + +func (a *GeoBoundsAggregation) WrapLongitude(wrapLongitude bool) *GeoBoundsAggregation { + a.wrapLongitude = &wrapLongitude + return a +} + +func (a *GeoBoundsAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoBoundsAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *GeoBoundsAggregation) Meta(metaData map[string]interface{}) *GeoBoundsAggregation { + a.meta = metaData + return a +} + +func (a *GeoBoundsAggregation) Source() (interface{}, error) { + // Example: + // { + // "query" : { + // "match" : { "business_type" : "shop" } + // }, + // "aggs" : { + // "viewport" : { + // "geo_bounds" : { + // "field" : "location" + // "wrap_longitude" : "true" + // } + // } + // } + // } + // + // This method returns only the { "geo_bounds" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["geo_bounds"] = opts + + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.wrapLongitude != nil { + opts["wrap_longitude"] = *a.wrapLongitude + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_centroid.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_centroid.go new file mode 100644 index 0000000000..55b3f48a3a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_geo_centroid.go @@ -0,0 +1,95 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// GeoCentroidAggregation is a metric aggregation that computes the weighted centroid +// from all coordinate values for a Geo-point datatype field. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-geocentroid-aggregation.html +type GeoCentroidAggregation struct { + field string + script *Script + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewGeoCentroidAggregation() *GeoCentroidAggregation { + return &GeoCentroidAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *GeoCentroidAggregation) Field(field string) *GeoCentroidAggregation { + a.field = field + return a +} + +func (a *GeoCentroidAggregation) Script(script *Script) *GeoCentroidAggregation { + a.script = script + return a +} + +func (a *GeoCentroidAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoCentroidAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *GeoCentroidAggregation) Meta(metaData map[string]interface{}) *GeoCentroidAggregation { + a.meta = metaData + return a +} + +func (a *GeoCentroidAggregation) Source() (interface{}, error) { + // Example: + // { + // "query" : { + // "match" : { "business_type" : "shop" } + // }, + // "aggs" : { + // "centroid" : { + // "geo_centroid" : { + // "field" : "location" + // } + // } + // } + // } + // + // This method returns only the { "geo_centroid" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["geo_centroid"] = opts + + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_max.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_max.go new file mode 100644 index 0000000000..9128a2522c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_max.go @@ -0,0 +1,108 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MaxAggregation is a single-value metrics aggregation that keeps track and +// returns the maximum value among the numeric values extracted from +// the aggregated documents. These values can be extracted either from +// specific numeric fields in the documents, or be generated by +// a provided script. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-max-aggregation.html +type MaxAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewMaxAggregation() *MaxAggregation { + return &MaxAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *MaxAggregation) Field(field string) *MaxAggregation { + a.field = field + return a +} + +func (a *MaxAggregation) Script(script *Script) *MaxAggregation { + a.script = script + return a +} + +func (a *MaxAggregation) Format(format string) *MaxAggregation { + a.format = format + return a +} + +func (a *MaxAggregation) Missing(missing interface{}) *MaxAggregation { + a.missing = missing + return a +} + +func (a *MaxAggregation) SubAggregation(name string, subAggregation Aggregation) *MaxAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MaxAggregation) Meta(metaData map[string]interface{}) *MaxAggregation { + a.meta = metaData + return a +} +func (a *MaxAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "max_price" : { "max" : { "field" : "price" } } + // } + // } + // This method returns only the { "max" : { "field" : "price" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["max"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if a.missing != nil { + opts["missing"] = a.missing + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_min.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_min.go new file mode 100644 index 0000000000..b6e40f9404 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_min.go @@ -0,0 +1,109 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MinAggregation is a single-value metrics aggregation that keeps track and +// returns the minimum value among numeric values extracted from the +// aggregated documents. These values can be extracted either from +// specific numeric fields in the documents, or be generated by a +// provided script. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-min-aggregation.html +type MinAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewMinAggregation() *MinAggregation { + return &MinAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *MinAggregation) Field(field string) *MinAggregation { + a.field = field + return a +} + +func (a *MinAggregation) Script(script *Script) *MinAggregation { + a.script = script + return a +} + +func (a *MinAggregation) Format(format string) *MinAggregation { + a.format = format + return a +} + +func (a *MinAggregation) Missing(missing interface{}) *MinAggregation { + a.missing = missing + return a +} + +func (a *MinAggregation) SubAggregation(name string, subAggregation Aggregation) *MinAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MinAggregation) Meta(metaData map[string]interface{}) *MinAggregation { + a.meta = metaData + return a +} + +func (a *MinAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "min_price" : { "min" : { "field" : "price" } } + // } + // } + // This method returns only the { "min" : { "field" : "price" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["min"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if a.missing != nil { + opts["missing"] = a.missing + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentile_ranks.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentile_ranks.go new file mode 100644 index 0000000000..a62ff4c345 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentile_ranks.go @@ -0,0 +1,140 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// PercentileRanksAggregation +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-percentile-rank-aggregation.html +type PercentileRanksAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + values []float64 + compression *float64 + estimator string +} + +func NewPercentileRanksAggregation() *PercentileRanksAggregation { + return &PercentileRanksAggregation{ + subAggregations: make(map[string]Aggregation), + values: make([]float64, 0), + } +} + +func (a *PercentileRanksAggregation) Field(field string) *PercentileRanksAggregation { + a.field = field + return a +} + +func (a *PercentileRanksAggregation) Script(script *Script) *PercentileRanksAggregation { + a.script = script + return a +} + +func (a *PercentileRanksAggregation) Format(format string) *PercentileRanksAggregation { + a.format = format + return a +} + +func (a *PercentileRanksAggregation) Missing(missing interface{}) *PercentileRanksAggregation { + a.missing = missing + return a +} + +func (a *PercentileRanksAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentileRanksAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *PercentileRanksAggregation) Meta(metaData map[string]interface{}) *PercentileRanksAggregation { + a.meta = metaData + return a +} + +func (a *PercentileRanksAggregation) Values(values ...float64) *PercentileRanksAggregation { + a.values = append(a.values, values...) + return a +} + +func (a *PercentileRanksAggregation) Compression(compression float64) *PercentileRanksAggregation { + a.compression = &compression + return a +} + +func (a *PercentileRanksAggregation) Estimator(estimator string) *PercentileRanksAggregation { + a.estimator = estimator + return a +} + +func (a *PercentileRanksAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "load_time_outlier" : { + // "percentile_ranks" : { + // "field" : "load_time" + // "values" : [15, 30] + // } + // } + // } + // } + // This method returns only the + // { "percentile_ranks" : { "field" : "load_time", "values" : [15, 30] } } + // part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["percentile_ranks"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if a.missing != nil { + opts["missing"] = a.missing + } + if len(a.values) > 0 { + opts["values"] = a.values + } + if a.compression != nil { + opts["compression"] = *a.compression + } + if a.estimator != "" { + opts["estimator"] = a.estimator + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentiles.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentiles.go new file mode 100644 index 0000000000..1eb62de54b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_percentiles.go @@ -0,0 +1,169 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// PercentilesAggregation is a multi-value metrics aggregation +// that calculates one or more percentiles over numeric values +// extracted from the aggregated documents. These values can +// be extracted either from specific numeric fields in the documents, +// or be generated by a provided script. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-percentile-aggregation.html +type PercentilesAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} + percentiles []float64 + method string + compression *float64 + numberOfSignificantValueDigits *int + estimator string +} + +func NewPercentilesAggregation() *PercentilesAggregation { + return &PercentilesAggregation{ + subAggregations: make(map[string]Aggregation), + percentiles: make([]float64, 0), + method: "tdigest", + } +} + +func (a *PercentilesAggregation) Field(field string) *PercentilesAggregation { + a.field = field + return a +} + +func (a *PercentilesAggregation) Script(script *Script) *PercentilesAggregation { + a.script = script + return a +} + +func (a *PercentilesAggregation) Format(format string) *PercentilesAggregation { + a.format = format + return a +} + +func (a *PercentilesAggregation) Missing(missing interface{}) *PercentilesAggregation { + a.missing = missing + return a +} + +func (a *PercentilesAggregation) SubAggregation(name string, subAggregation Aggregation) *PercentilesAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *PercentilesAggregation) Meta(metaData map[string]interface{}) *PercentilesAggregation { + a.meta = metaData + return a +} + +func (a *PercentilesAggregation) Percentiles(percentiles ...float64) *PercentilesAggregation { + a.percentiles = append(a.percentiles, percentiles...) + return a +} + +// Method is the percentiles method, which can be "tdigest" (default) or "hdr". +func (a *PercentilesAggregation) Method(method string) *PercentilesAggregation { + a.method = method + return a +} + +func (a *PercentilesAggregation) Compression(compression float64) *PercentilesAggregation { + a.compression = &compression + return a +} + +func (a *PercentilesAggregation) NumberOfSignificantValueDigits(digits int) *PercentilesAggregation { + a.numberOfSignificantValueDigits = &digits + return a +} + +func (a *PercentilesAggregation) Estimator(estimator string) *PercentilesAggregation { + a.estimator = estimator + return a +} + +func (a *PercentilesAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "load_time_outlier" : { + // "percentiles" : { + // "field" : "load_time" + // } + // } + // } + // } + // This method returns only the + // { "percentiles" : { "field" : "load_time" } } + // part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["percentiles"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if a.missing != nil { + opts["missing"] = a.missing + } + if len(a.percentiles) > 0 { + opts["percents"] = a.percentiles + } + switch a.method { + case "tdigest": + if c := a.compression; c != nil { + opts[a.method] = map[string]interface{}{ + "compression": *c, + } + } + case "hdr": + if n := a.numberOfSignificantValueDigits; n != nil { + opts[a.method] = map[string]interface{}{ + "number_of_significant_value_digits": *n, + } + } + } + if a.estimator != "" { + opts["estimator"] = a.estimator + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_scripted_metric.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_scripted_metric.go new file mode 100644 index 0000000000..0b26c9d6c4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_scripted_metric.go @@ -0,0 +1,112 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. +package elastic + +// ScriptedMetricAggregation is a a metric aggregation that executes using scripts to provide a metric output. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-scripted-metric-aggregation.html +type ScriptedMetricAggregation struct { + initScript *Script + mapScript *Script + combineScript *Script + reduceScript *Script + + params map[string]interface{} + meta map[string]interface{} +} + +func NewScriptedMetricAggregation() *ScriptedMetricAggregation { + a := &ScriptedMetricAggregation{} + return a +} + +func (a *ScriptedMetricAggregation) InitScript(script *Script) *ScriptedMetricAggregation { + a.initScript = script + return a +} + +func (a *ScriptedMetricAggregation) MapScript(script *Script) *ScriptedMetricAggregation { + a.mapScript = script + return a +} + +func (a *ScriptedMetricAggregation) CombineScript(script *Script) *ScriptedMetricAggregation { + a.combineScript = script + return a +} + +func (a *ScriptedMetricAggregation) ReduceScript(script *Script) *ScriptedMetricAggregation { + a.reduceScript = script + return a +} + +func (a *ScriptedMetricAggregation) Params(params map[string]interface{}) *ScriptedMetricAggregation { + a.params = params + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *ScriptedMetricAggregation) Meta(metaData map[string]interface{}) *ScriptedMetricAggregation { + a.meta = metaData + return a +} + +func (a *ScriptedMetricAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "magic_script" : { "scripted_metric" : { + // "init_script" : "state.transactions = []", + // "map_script" : "state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)", + // "combine_script" : "double profit = 0; for (t in state.transactions) { profit += t } return profit", + // "reduce_script" : "double profit = 0; for (a in states) { profit += a } return profit" + // } } + // } + // } + // This method returns only the { "scripted_metric" : { ... } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["scripted_metric"] = opts + + if a.initScript != nil { + src, err := a.initScript.Source() + if err != nil { + return nil, err + } + opts["init_script"] = src + } + if a.mapScript != nil { + src, err := a.mapScript.Source() + if err != nil { + return nil, err + } + opts["map_script"] = src + } + if a.combineScript != nil { + src, err := a.combineScript.Source() + if err != nil { + return nil, err + } + opts["combine_script"] = src + } + if a.reduceScript != nil { + src, err := a.reduceScript.Source() + if err != nil { + return nil, err + } + opts["reduce_script"] = src + } + + if a.params != nil && len(a.params) > 0 { + opts["params"] = a.params + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_stats.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_stats.go new file mode 100644 index 0000000000..6c00f6855b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_stats.go @@ -0,0 +1,108 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// StatsAggregation is a multi-value metrics aggregation that computes stats +// over numeric values extracted from the aggregated documents. +// These values can be extracted either from specific numeric fields +// in the documents, or be generated by a provided script. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-stats-aggregation.html +type StatsAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewStatsAggregation() *StatsAggregation { + return &StatsAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *StatsAggregation) Field(field string) *StatsAggregation { + a.field = field + return a +} + +func (a *StatsAggregation) Script(script *Script) *StatsAggregation { + a.script = script + return a +} + +func (a *StatsAggregation) Format(format string) *StatsAggregation { + a.format = format + return a +} + +func (a *StatsAggregation) Missing(missing interface{}) *StatsAggregation { + a.missing = missing + return a +} + +func (a *StatsAggregation) SubAggregation(name string, subAggregation Aggregation) *StatsAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *StatsAggregation) Meta(metaData map[string]interface{}) *StatsAggregation { + a.meta = metaData + return a +} + +func (a *StatsAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "grades_stats" : { "stats" : { "field" : "grade" } } + // } + // } + // This method returns only the { "stats" : { "field" : "grade" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["stats"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if a.missing != nil { + opts["missing"] = a.missing + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_sum.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_sum.go new file mode 100644 index 0000000000..6de8f1d5b7 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_sum.go @@ -0,0 +1,108 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SumAggregation is a single-value metrics aggregation that sums up +// numeric values that are extracted from the aggregated documents. +// These values can be extracted either from specific numeric fields +// in the documents, or be generated by a provided script. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-sum-aggregation.html +type SumAggregation struct { + field string + script *Script + format string + missing interface{} + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewSumAggregation() *SumAggregation { + return &SumAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *SumAggregation) Field(field string) *SumAggregation { + a.field = field + return a +} + +func (a *SumAggregation) Script(script *Script) *SumAggregation { + a.script = script + return a +} + +func (a *SumAggregation) Format(format string) *SumAggregation { + a.format = format + return a +} + +func (a *SumAggregation) Missing(missing interface{}) *SumAggregation { + a.missing = missing + return a +} + +func (a *SumAggregation) SubAggregation(name string, subAggregation Aggregation) *SumAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *SumAggregation) Meta(metaData map[string]interface{}) *SumAggregation { + a.meta = metaData + return a +} + +func (a *SumAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "intraday_return" : { "sum" : { "field" : "change" } } + // } + // } + // This method returns only the { "sum" : { "field" : "change" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["sum"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + if a.missing != nil { + opts["missing"] = a.missing + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_top_hits.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_top_hits.go new file mode 100644 index 0000000000..7321667d39 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_top_hits.go @@ -0,0 +1,161 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TopHitsAggregation keeps track of the most relevant document +// being aggregated. This aggregator is intended to be used as a +// sub aggregator, so that the top matching documents +// can be aggregated per bucket. +// +// It can effectively be used to group result sets by certain fields via +// a bucket aggregator. One or more bucket aggregators determines by +// which properties a result set get sliced into. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-top-hits-aggregation.html +type TopHitsAggregation struct { + searchSource *SearchSource +} + +func NewTopHitsAggregation() *TopHitsAggregation { + return &TopHitsAggregation{ + searchSource: NewSearchSource(), + } +} + +func (a *TopHitsAggregation) SearchSource(searchSource *SearchSource) *TopHitsAggregation { + a.searchSource = searchSource + if a.searchSource == nil { + a.searchSource = NewSearchSource() + } + return a +} + +func (a *TopHitsAggregation) From(from int) *TopHitsAggregation { + a.searchSource = a.searchSource.From(from) + return a +} + +func (a *TopHitsAggregation) Size(size int) *TopHitsAggregation { + a.searchSource = a.searchSource.Size(size) + return a +} + +func (a *TopHitsAggregation) TrackScores(trackScores bool) *TopHitsAggregation { + a.searchSource = a.searchSource.TrackScores(trackScores) + return a +} + +func (a *TopHitsAggregation) Explain(explain bool) *TopHitsAggregation { + a.searchSource = a.searchSource.Explain(explain) + return a +} + +func (a *TopHitsAggregation) Version(version bool) *TopHitsAggregation { + a.searchSource = a.searchSource.Version(version) + return a +} + +func (a *TopHitsAggregation) NoStoredFields() *TopHitsAggregation { + a.searchSource = a.searchSource.NoStoredFields() + return a +} + +func (a *TopHitsAggregation) FetchSource(fetchSource bool) *TopHitsAggregation { + a.searchSource = a.searchSource.FetchSource(fetchSource) + return a +} + +func (a *TopHitsAggregation) FetchSourceContext(fetchSourceContext *FetchSourceContext) *TopHitsAggregation { + a.searchSource = a.searchSource.FetchSourceContext(fetchSourceContext) + return a +} + +func (a *TopHitsAggregation) DocvalueFields(docvalueFields ...string) *TopHitsAggregation { + a.searchSource = a.searchSource.DocvalueFields(docvalueFields...) + return a +} + +func (a *TopHitsAggregation) DocvalueFieldsWithFormat(docvalueFields ...DocvalueField) *TopHitsAggregation { + a.searchSource = a.searchSource.DocvalueFieldsWithFormat(docvalueFields...) + return a +} + +func (a *TopHitsAggregation) DocvalueField(docvalueField string) *TopHitsAggregation { + a.searchSource = a.searchSource.DocvalueField(docvalueField) + return a +} + +func (a *TopHitsAggregation) DocvalueFieldWithFormat(docvalueField DocvalueField) *TopHitsAggregation { + a.searchSource = a.searchSource.DocvalueFieldWithFormat(docvalueField) + return a +} + +func (a *TopHitsAggregation) ScriptFields(scriptFields ...*ScriptField) *TopHitsAggregation { + a.searchSource = a.searchSource.ScriptFields(scriptFields...) + return a +} + +func (a *TopHitsAggregation) ScriptField(scriptField *ScriptField) *TopHitsAggregation { + a.searchSource = a.searchSource.ScriptField(scriptField) + return a +} + +func (a *TopHitsAggregation) Sort(field string, ascending bool) *TopHitsAggregation { + a.searchSource = a.searchSource.Sort(field, ascending) + return a +} + +func (a *TopHitsAggregation) SortWithInfo(info SortInfo) *TopHitsAggregation { + a.searchSource = a.searchSource.SortWithInfo(info) + return a +} + +func (a *TopHitsAggregation) SortBy(sorter ...Sorter) *TopHitsAggregation { + a.searchSource = a.searchSource.SortBy(sorter...) + return a +} + +func (a *TopHitsAggregation) Highlight(highlight *Highlight) *TopHitsAggregation { + a.searchSource = a.searchSource.Highlight(highlight) + return a +} + +func (a *TopHitsAggregation) Highlighter() *Highlight { + return a.searchSource.Highlighter() +} + +func (a *TopHitsAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs": { + // "top_tag_hits": { + // "top_hits": { + // "sort": [ + // { + // "last_activity_date": { + // "order": "desc" + // } + // } + // ], + // "_source": { + // "include": [ + // "title" + // ] + // }, + // "size" : 1 + // } + // } + // } + // } + // This method returns only the { "top_hits" : { ... } } part. + + source := make(map[string]interface{}) + src, err := a.searchSource.Source() + if err != nil { + return nil, err + } + source["top_hits"] = src + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_value_count.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_value_count.go new file mode 100644 index 0000000000..b948dcccc4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_value_count.go @@ -0,0 +1,102 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ValueCountAggregation is a single-value metrics aggregation that counts +// the number of values that are extracted from the aggregated documents. +// These values can be extracted either from specific fields in the documents, +// or be generated by a provided script. Typically, this aggregator will be +// used in conjunction with other single-value aggregations. +// For example, when computing the avg one might be interested in the +// number of values the average is computed over. +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-valuecount-aggregation.html +type ValueCountAggregation struct { + field string + script *Script + format string + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewValueCountAggregation() *ValueCountAggregation { + return &ValueCountAggregation{ + subAggregations: make(map[string]Aggregation), + } +} + +func (a *ValueCountAggregation) Field(field string) *ValueCountAggregation { + a.field = field + return a +} + +func (a *ValueCountAggregation) Script(script *Script) *ValueCountAggregation { + a.script = script + return a +} + +func (a *ValueCountAggregation) Format(format string) *ValueCountAggregation { + a.format = format + return a +} + +func (a *ValueCountAggregation) SubAggregation(name string, subAggregation Aggregation) *ValueCountAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *ValueCountAggregation) Meta(metaData map[string]interface{}) *ValueCountAggregation { + a.meta = metaData + return a +} + +func (a *ValueCountAggregation) Source() (interface{}, error) { + // Example: + // { + // "aggs" : { + // "grades_count" : { "value_count" : { "field" : "grade" } } + // } + // } + // This method returns only the { "value_count" : { "field" : "grade" } } part. + + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["value_count"] = opts + + // ValuesSourceAggregationBuilder + if a.field != "" { + opts["field"] = a.field + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + opts["script"] = src + } + if a.format != "" { + opts["format"] = a.format + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_weighted_avg.go b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_weighted_avg.go new file mode 100644 index 0000000000..cd7ad7ee79 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_metrics_weighted_avg.go @@ -0,0 +1,156 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// WeightedAvgAggregation is a single-value metrics aggregation that +// computes the weighted average of numeric values that are extracted +// from the aggregated documents. These values can be extracted either +// from specific numeric fields in the documents. +// +// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-metrics-weight-avg-aggregation.html +type WeightedAvgAggregation struct { + fields map[string]*MultiValuesSourceFieldConfig + valueType string + format string + value *MultiValuesSourceFieldConfig + weight *MultiValuesSourceFieldConfig + subAggregations map[string]Aggregation + meta map[string]interface{} +} + +func NewWeightedAvgAggregation() *WeightedAvgAggregation { + return &WeightedAvgAggregation{ + fields: make(map[string]*MultiValuesSourceFieldConfig), + subAggregations: make(map[string]Aggregation), + } +} + +func (a *WeightedAvgAggregation) Field(field string, config *MultiValuesSourceFieldConfig) *WeightedAvgAggregation { + a.fields[field] = config + return a +} + +func (a *WeightedAvgAggregation) ValueType(valueType string) *WeightedAvgAggregation { + a.valueType = valueType + return a +} + +func (a *WeightedAvgAggregation) Format(format string) *WeightedAvgAggregation { + a.format = format + return a +} + +func (a *WeightedAvgAggregation) Value(value *MultiValuesSourceFieldConfig) *WeightedAvgAggregation { + a.value = value + return a +} + +func (a *WeightedAvgAggregation) Weight(weight *MultiValuesSourceFieldConfig) *WeightedAvgAggregation { + a.weight = weight + return a +} + +func (a *WeightedAvgAggregation) SubAggregation(name string, subAggregation Aggregation) *WeightedAvgAggregation { + a.subAggregations[name] = subAggregation + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *WeightedAvgAggregation) Meta(metaData map[string]interface{}) *WeightedAvgAggregation { + a.meta = metaData + return a +} + +func (a *WeightedAvgAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + opts := make(map[string]interface{}) + source["weighted_avg"] = opts + + if len(a.fields) > 0 { + f := make(map[string]interface{}) + for name, config := range a.fields { + cfg, err := config.Source() + if err != nil { + return nil, err + } + f[name] = cfg + } + opts["fields"] = f + } + + if v := a.format; v != "" { + opts["format"] = v + } + + if v := a.valueType; v != "" { + opts["value_type"] = v + } + + if v := a.value; v != nil { + cfg, err := v.Source() + if err != nil { + return nil, err + } + opts["value"] = cfg + } + + if v := a.weight; v != nil { + cfg, err := v.Source() + if err != nil { + return nil, err + } + opts["weight"] = cfg + } + + // AggregationBuilder (SubAggregations) + if len(a.subAggregations) > 0 { + aggsMap := make(map[string]interface{}) + source["aggregations"] = aggsMap + for name, aggregate := range a.subAggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} + +// MultiValuesSourceFieldConfig represents a field configuration +// used e.g. in WeightedAvgAggregation. +type MultiValuesSourceFieldConfig struct { + FieldName string + Missing interface{} + Script *Script + TimeZone string +} + +func (f *MultiValuesSourceFieldConfig) Source() (interface{}, error) { + source := make(map[string]interface{}) + if v := f.Missing; v != nil { + source["missing"] = v + } + if v := f.Script; v != nil { + src, err := v.Source() + if err != nil { + return nil, err + } + source["script"] = src + } + if v := f.FieldName; v != "" { + source["field"] = v + } + if v := f.TimeZone; v != "" { + source["time_zone"] = v + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_avg_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_avg_bucket.go new file mode 100644 index 0000000000..5c130d6176 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_avg_bucket.go @@ -0,0 +1,94 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// AvgBucketAggregation is a sibling pipeline aggregation which calculates +// the (mean) average value of a specified metric in a sibling aggregation. +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-avg-bucket-aggregation.html +type AvgBucketAggregation struct { + format string + gapPolicy string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewAvgBucketAggregation creates and initializes a new AvgBucketAggregation. +func NewAvgBucketAggregation() *AvgBucketAggregation { + return &AvgBucketAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *AvgBucketAggregation) Format(format string) *AvgBucketAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *AvgBucketAggregation) GapPolicy(gapPolicy string) *AvgBucketAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *AvgBucketAggregation) GapInsertZeros() *AvgBucketAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *AvgBucketAggregation) GapSkip() *AvgBucketAggregation { + a.gapPolicy = "skip" + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *AvgBucketAggregation) Meta(metaData map[string]interface{}) *AvgBucketAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *AvgBucketAggregation) BucketsPath(bucketsPaths ...string) *AvgBucketAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *AvgBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["avg_bucket"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_script.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_script.go new file mode 100644 index 0000000000..bc5229b9cf --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_script.go @@ -0,0 +1,113 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// BucketScriptAggregation is a parent pipeline aggregation which executes +// a script which can perform per bucket computations on specified metrics +// in the parent multi-bucket aggregation. The specified metric must be +// numeric and the script must return a numeric value. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-bucket-script-aggregation.html +type BucketScriptAggregation struct { + format string + gapPolicy string + script *Script + + meta map[string]interface{} + bucketsPathsMap map[string]string +} + +// NewBucketScriptAggregation creates and initializes a new BucketScriptAggregation. +func NewBucketScriptAggregation() *BucketScriptAggregation { + return &BucketScriptAggregation{ + bucketsPathsMap: make(map[string]string), + } +} + +// Format to use on the output of this aggregation. +func (a *BucketScriptAggregation) Format(format string) *BucketScriptAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *BucketScriptAggregation) GapPolicy(gapPolicy string) *BucketScriptAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *BucketScriptAggregation) GapInsertZeros() *BucketScriptAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *BucketScriptAggregation) GapSkip() *BucketScriptAggregation { + a.gapPolicy = "skip" + return a +} + +// Script is the script to run. +func (a *BucketScriptAggregation) Script(script *Script) *BucketScriptAggregation { + a.script = script + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *BucketScriptAggregation) Meta(metaData map[string]interface{}) *BucketScriptAggregation { + a.meta = metaData + return a +} + +// BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator. +func (a *BucketScriptAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketScriptAggregation { + a.bucketsPathsMap = bucketsPathsMap + return a +} + +// AddBucketsPath adds a bucket path to use for this pipeline aggregator. +func (a *BucketScriptAggregation) AddBucketsPath(name, path string) *BucketScriptAggregation { + if a.bucketsPathsMap == nil { + a.bucketsPathsMap = make(map[string]string) + } + a.bucketsPathsMap[name] = path + return a +} + +// Source returns the a JSON-serializable interface. +func (a *BucketScriptAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["bucket_script"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + params["script"] = src + } + + // Add buckets paths + if len(a.bucketsPathsMap) > 0 { + params["buckets_path"] = a.bucketsPathsMap + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_selector.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_selector.go new file mode 100644 index 0000000000..1051f1ae31 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_selector.go @@ -0,0 +1,115 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// BucketSelectorAggregation is a parent pipeline aggregation which +// determines whether the current bucket will be retained in the parent +// multi-bucket aggregation. The specific metric must be numeric and +// the script must return a boolean value. If the script language is +// expression then a numeric return value is permitted. In this case 0.0 +// will be evaluated as false and all other values will evaluate to true. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-bucket-selector-aggregation.html +type BucketSelectorAggregation struct { + format string + gapPolicy string + script *Script + + meta map[string]interface{} + bucketsPathsMap map[string]string +} + +// NewBucketSelectorAggregation creates and initializes a new BucketSelectorAggregation. +func NewBucketSelectorAggregation() *BucketSelectorAggregation { + return &BucketSelectorAggregation{ + bucketsPathsMap: make(map[string]string), + } +} + +// Format to use on the output of this aggregation. +func (a *BucketSelectorAggregation) Format(format string) *BucketSelectorAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *BucketSelectorAggregation) GapPolicy(gapPolicy string) *BucketSelectorAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *BucketSelectorAggregation) GapInsertZeros() *BucketSelectorAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *BucketSelectorAggregation) GapSkip() *BucketSelectorAggregation { + a.gapPolicy = "skip" + return a +} + +// Script is the script to run. +func (a *BucketSelectorAggregation) Script(script *Script) *BucketSelectorAggregation { + a.script = script + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *BucketSelectorAggregation) Meta(metaData map[string]interface{}) *BucketSelectorAggregation { + a.meta = metaData + return a +} + +// BucketsPathsMap sets the paths to the buckets to use for this pipeline aggregator. +func (a *BucketSelectorAggregation) BucketsPathsMap(bucketsPathsMap map[string]string) *BucketSelectorAggregation { + a.bucketsPathsMap = bucketsPathsMap + return a +} + +// AddBucketsPath adds a bucket path to use for this pipeline aggregator. +func (a *BucketSelectorAggregation) AddBucketsPath(name, path string) *BucketSelectorAggregation { + if a.bucketsPathsMap == nil { + a.bucketsPathsMap = make(map[string]string) + } + a.bucketsPathsMap[name] = path + return a +} + +// Source returns the a JSON-serializable interface. +func (a *BucketSelectorAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["bucket_selector"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + params["script"] = src + } + + // Add buckets paths + if len(a.bucketsPathsMap) > 0 { + params["buckets_path"] = a.bucketsPathsMap + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_sort.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_sort.go new file mode 100644 index 0000000000..3d060df2b2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_bucket_sort.go @@ -0,0 +1,119 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// BucketSortAggregation parent pipeline aggregation which sorts the buckets +// of its parent multi-bucket aggregation. Zero or more sort fields may be +// specified together with the corresponding sort order. Each bucket may be +// sorted based on its _key, _count or its sub-aggregations. In addition, +// parameters from and size may be set in order to truncate the result buckets. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-bucket-sort-aggregation.html +type BucketSortAggregation struct { + sorters []Sorter + from int + size int + gapPolicy string + + meta map[string]interface{} +} + +// NewBucketSortAggregation creates and initializes a new BucketSortAggregation. +func NewBucketSortAggregation() *BucketSortAggregation { + return &BucketSortAggregation{ + size: -1, + } +} + +// Sort adds a sort order to the list of sorters. +func (a *BucketSortAggregation) Sort(field string, ascending bool) *BucketSortAggregation { + a.sorters = append(a.sorters, SortInfo{Field: field, Ascending: ascending}) + return a +} + +// SortWithInfo adds a SortInfo to the list of sorters. +func (a *BucketSortAggregation) SortWithInfo(info SortInfo) *BucketSortAggregation { + a.sorters = append(a.sorters, info) + return a +} + +// From adds the "from" parameter to the aggregation. +func (a *BucketSortAggregation) From(from int) *BucketSortAggregation { + a.from = from + return a +} + +// Size adds the "size" parameter to the aggregation. +func (a *BucketSortAggregation) Size(size int) *BucketSortAggregation { + a.size = size + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "skip". +func (a *BucketSortAggregation) GapPolicy(gapPolicy string) *BucketSortAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *BucketSortAggregation) GapInsertZeros() *BucketSortAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *BucketSortAggregation) GapSkip() *BucketSortAggregation { + a.gapPolicy = "skip" + return a +} + +// Meta sets the meta data in the aggregation. +// Although metadata is supported for this aggregation by Elasticsearch, it's important to +// note that there's no use to it because this aggregation does not include new data in the +// response. It merely reorders parent buckets. +func (a *BucketSortAggregation) Meta(meta map[string]interface{}) *BucketSortAggregation { + a.meta = meta + return a +} + +// Source returns the a JSON-serializable interface. +func (a *BucketSortAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["bucket_sort"] = params + + if a.from != 0 { + params["from"] = a.from + } + if a.size != -1 { + params["size"] = a.size + } + + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + + // Parses sorters to JSON-serializable interface. + if len(a.sorters) > 0 { + sorters := make([]interface{}, len(a.sorters)) + params["sort"] = sorters + for idx, sorter := range a.sorters { + src, err := sorter.Source() + if err != nil { + return nil, err + } + sorters[idx] = src + } + } + + // Add metadata if available. + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_cumulative_sum.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_cumulative_sum.go new file mode 100644 index 0000000000..1074d86b12 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_cumulative_sum.go @@ -0,0 +1,71 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// CumulativeSumAggregation is a parent pipeline aggregation which calculates +// the cumulative sum of a specified metric in a parent histogram (or date_histogram) +// aggregation. The specified metric must be numeric and the enclosing +// histogram must have min_doc_count set to 0 (default for histogram aggregations). +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-cumulative-sum-aggregation.html +type CumulativeSumAggregation struct { + format string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewCumulativeSumAggregation creates and initializes a new CumulativeSumAggregation. +func NewCumulativeSumAggregation() *CumulativeSumAggregation { + return &CumulativeSumAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *CumulativeSumAggregation) Format(format string) *CumulativeSumAggregation { + a.format = format + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *CumulativeSumAggregation) Meta(metaData map[string]interface{}) *CumulativeSumAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *CumulativeSumAggregation) BucketsPath(bucketsPaths ...string) *CumulativeSumAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *CumulativeSumAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["cumulative_sum"] = params + + if a.format != "" { + params["format"] = a.format + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_derivative.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_derivative.go new file mode 100644 index 0000000000..a22219cdf9 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_derivative.go @@ -0,0 +1,105 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// DerivativeAggregation is a parent pipeline aggregation which calculates +// the derivative of a specified metric in a parent histogram (or date_histogram) +// aggregation. The specified metric must be numeric and the enclosing +// histogram must have min_doc_count set to 0 (default for histogram aggregations). +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-derivative-aggregation.html +type DerivativeAggregation struct { + format string + gapPolicy string + unit string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewDerivativeAggregation creates and initializes a new DerivativeAggregation. +func NewDerivativeAggregation() *DerivativeAggregation { + return &DerivativeAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *DerivativeAggregation) Format(format string) *DerivativeAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *DerivativeAggregation) GapPolicy(gapPolicy string) *DerivativeAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *DerivativeAggregation) GapInsertZeros() *DerivativeAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *DerivativeAggregation) GapSkip() *DerivativeAggregation { + a.gapPolicy = "skip" + return a +} + +// Unit sets the unit provided, e.g. "1d" or "1y". +// It is only useful when calculating the derivative using a date_histogram. +func (a *DerivativeAggregation) Unit(unit string) *DerivativeAggregation { + a.unit = unit + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *DerivativeAggregation) Meta(metaData map[string]interface{}) *DerivativeAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *DerivativeAggregation) BucketsPath(bucketsPaths ...string) *DerivativeAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *DerivativeAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["derivative"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + if a.unit != "" { + params["unit"] = a.unit + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_extended_stats_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_extended_stats_bucket.go new file mode 100644 index 0000000000..4e816d416e --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_extended_stats_bucket.go @@ -0,0 +1,107 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ExtendedStatsBucketAggregation is a sibling pipeline aggregation which calculates +// a variety of stats across all bucket of a specified metric in a sibling aggregation. +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// This aggregation provides a few more statistics (sum of squares, standard deviation, etc) +// compared to the stats_bucket aggregation. +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-extended-stats-bucket-aggregation.html +type ExtendedStatsBucketAggregation struct { + format string + gapPolicy string + sigma *float32 + meta map[string]interface{} + bucketsPaths []string +} + +// NewExtendedStatsBucketAggregation creates and initializes a new ExtendedStatsBucketAggregation. +func NewExtendedStatsBucketAggregation() *ExtendedStatsBucketAggregation { + return &ExtendedStatsBucketAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (s *ExtendedStatsBucketAggregation) Format(format string) *ExtendedStatsBucketAggregation { + s.format = format + return s +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (s *ExtendedStatsBucketAggregation) GapPolicy(gapPolicy string) *ExtendedStatsBucketAggregation { + s.gapPolicy = gapPolicy + return s +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (s *ExtendedStatsBucketAggregation) GapInsertZeros() *ExtendedStatsBucketAggregation { + s.gapPolicy = "insert_zeros" + return s +} + +// GapSkip skips gaps in the series. +func (s *ExtendedStatsBucketAggregation) GapSkip() *ExtendedStatsBucketAggregation { + s.gapPolicy = "skip" + return s +} + +// Meta sets the meta data to be included in the aggregation response. +func (s *ExtendedStatsBucketAggregation) Meta(metaData map[string]interface{}) *ExtendedStatsBucketAggregation { + s.meta = metaData + return s +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (s *ExtendedStatsBucketAggregation) BucketsPath(bucketsPaths ...string) *ExtendedStatsBucketAggregation { + s.bucketsPaths = append(s.bucketsPaths, bucketsPaths...) + return s +} + +// Sigma sets number of standard deviations above/below the mean to display +func (s *ExtendedStatsBucketAggregation) Sigma(sigma float32) *ExtendedStatsBucketAggregation { + s.sigma = &sigma + return s +} + +// Source returns the a JSON-serializable interface. +func (s *ExtendedStatsBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["extended_stats_bucket"] = params + + if s.format != "" { + params["format"] = s.format + } + if s.gapPolicy != "" { + params["gap_policy"] = s.gapPolicy + } + + // Add buckets paths + switch len(s.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = s.bucketsPaths[0] + default: + params["buckets_path"] = s.bucketsPaths + } + + // Add sigma is not zero or less + if s.sigma != nil && *s.sigma >= 0 { + params["sigma"] = *s.sigma + } + + // Add Meta data if available + if len(s.meta) > 0 { + source["meta"] = s.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_max_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_max_bucket.go new file mode 100644 index 0000000000..6cd087eeb2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_max_bucket.go @@ -0,0 +1,95 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MaxBucketAggregation is a sibling pipeline aggregation which identifies +// the bucket(s) with the maximum value of a specified metric in a sibling +// aggregation and outputs both the value and the key(s) of the bucket(s). +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-max-bucket-aggregation.html +type MaxBucketAggregation struct { + format string + gapPolicy string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewMaxBucketAggregation creates and initializes a new MaxBucketAggregation. +func NewMaxBucketAggregation() *MaxBucketAggregation { + return &MaxBucketAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *MaxBucketAggregation) Format(format string) *MaxBucketAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *MaxBucketAggregation) GapPolicy(gapPolicy string) *MaxBucketAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *MaxBucketAggregation) GapInsertZeros() *MaxBucketAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *MaxBucketAggregation) GapSkip() *MaxBucketAggregation { + a.gapPolicy = "skip" + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MaxBucketAggregation) Meta(metaData map[string]interface{}) *MaxBucketAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *MaxBucketAggregation) BucketsPath(bucketsPaths ...string) *MaxBucketAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *MaxBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["max_bucket"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_min_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_min_bucket.go new file mode 100644 index 0000000000..c87f0d969f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_min_bucket.go @@ -0,0 +1,95 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MinBucketAggregation is a sibling pipeline aggregation which identifies +// the bucket(s) with the maximum value of a specified metric in a sibling +// aggregation and outputs both the value and the key(s) of the bucket(s). +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-min-bucket-aggregation.html +type MinBucketAggregation struct { + format string + gapPolicy string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewMinBucketAggregation creates and initializes a new MinBucketAggregation. +func NewMinBucketAggregation() *MinBucketAggregation { + return &MinBucketAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *MinBucketAggregation) Format(format string) *MinBucketAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *MinBucketAggregation) GapPolicy(gapPolicy string) *MinBucketAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *MinBucketAggregation) GapInsertZeros() *MinBucketAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *MinBucketAggregation) GapSkip() *MinBucketAggregation { + a.gapPolicy = "skip" + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MinBucketAggregation) Meta(metaData map[string]interface{}) *MinBucketAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *MinBucketAggregation) BucketsPath(bucketsPaths ...string) *MinBucketAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *MinBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["min_bucket"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_avg.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_avg.go new file mode 100644 index 0000000000..035dc0e990 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_avg.go @@ -0,0 +1,378 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MovAvgAggregation operates on a series of data. It will slide a window +// across the data and emit the average value of that window. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html +// +// Deprecated: The MovAvgAggregation has been deprecated in 6.4.0. Use the more generate MovFnAggregation instead. +type MovAvgAggregation struct { + format string + gapPolicy string + model MovAvgModel + window *int + predict *int + minimize *bool + + meta map[string]interface{} + bucketsPaths []string +} + +// NewMovAvgAggregation creates and initializes a new MovAvgAggregation. +// +// Deprecated: The MovAvgAggregation has been deprecated in 6.4.0. Use the more generate MovFnAggregation instead. +func NewMovAvgAggregation() *MovAvgAggregation { + return &MovAvgAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *MovAvgAggregation) Format(format string) *MovAvgAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *MovAvgAggregation) GapPolicy(gapPolicy string) *MovAvgAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *MovAvgAggregation) GapInsertZeros() *MovAvgAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *MovAvgAggregation) GapSkip() *MovAvgAggregation { + a.gapPolicy = "skip" + return a +} + +// Model is used to define what type of moving average you want to use +// in the series. +func (a *MovAvgAggregation) Model(model MovAvgModel) *MovAvgAggregation { + a.model = model + return a +} + +// Window sets the window size for the moving average. This window will +// "slide" across the series, and the values inside that window will +// be used to calculate the moving avg value. +func (a *MovAvgAggregation) Window(window int) *MovAvgAggregation { + a.window = &window + return a +} + +// Predict sets the number of predictions that should be returned. +// Each prediction will be spaced at the intervals in the histogram. +// E.g. a predict of 2 will return two new buckets at the end of the +// histogram with the predicted values. +func (a *MovAvgAggregation) Predict(numPredictions int) *MovAvgAggregation { + a.predict = &numPredictions + return a +} + +// Minimize determines if the model should be fit to the data using a +// cost minimizing algorithm. +func (a *MovAvgAggregation) Minimize(minimize bool) *MovAvgAggregation { + a.minimize = &minimize + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MovAvgAggregation) Meta(metaData map[string]interface{}) *MovAvgAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *MovAvgAggregation) BucketsPath(bucketsPaths ...string) *MovAvgAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *MovAvgAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["moving_avg"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + if a.model != nil { + params["model"] = a.model.Name() + settings := a.model.Settings() + if len(settings) > 0 { + params["settings"] = settings + } + } + if a.window != nil { + params["window"] = *a.window + } + if a.predict != nil { + params["predict"] = *a.predict + } + if a.minimize != nil { + params["minimize"] = *a.minimize + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} + +// -- Models for moving averages -- +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html#_models + +// MovAvgModel specifies the model to use with the MovAvgAggregation. +type MovAvgModel interface { + Name() string + Settings() map[string]interface{} +} + +// -- EWMA -- + +// EWMAMovAvgModel calculates an exponentially weighted moving average. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html#_ewma_exponentially_weighted +type EWMAMovAvgModel struct { + alpha *float64 +} + +// NewEWMAMovAvgModel creates and initializes a new EWMAMovAvgModel. +func NewEWMAMovAvgModel() *EWMAMovAvgModel { + return &EWMAMovAvgModel{} +} + +// Alpha controls the smoothing of the data. Alpha = 1 retains no memory +// of past values (e.g. a random walk), while alpha = 0 retains infinite +// memory of past values (e.g. the series mean). Useful values are somewhere +// in between. Defaults to 0.5. +func (m *EWMAMovAvgModel) Alpha(alpha float64) *EWMAMovAvgModel { + m.alpha = &alpha + return m +} + +// Name of the model. +func (m *EWMAMovAvgModel) Name() string { + return "ewma" +} + +// Settings of the model. +func (m *EWMAMovAvgModel) Settings() map[string]interface{} { + settings := make(map[string]interface{}) + if m.alpha != nil { + settings["alpha"] = *m.alpha + } + return settings +} + +// -- Holt linear -- + +// HoltLinearMovAvgModel calculates a doubly exponential weighted moving average. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html#_holt_linear +type HoltLinearMovAvgModel struct { + alpha *float64 + beta *float64 +} + +// NewHoltLinearMovAvgModel creates and initializes a new HoltLinearMovAvgModel. +func NewHoltLinearMovAvgModel() *HoltLinearMovAvgModel { + return &HoltLinearMovAvgModel{} +} + +// Alpha controls the smoothing of the data. Alpha = 1 retains no memory +// of past values (e.g. a random walk), while alpha = 0 retains infinite +// memory of past values (e.g. the series mean). Useful values are somewhere +// in between. Defaults to 0.5. +func (m *HoltLinearMovAvgModel) Alpha(alpha float64) *HoltLinearMovAvgModel { + m.alpha = &alpha + return m +} + +// Beta is equivalent to Alpha but controls the smoothing of the trend +// instead of the data. +func (m *HoltLinearMovAvgModel) Beta(beta float64) *HoltLinearMovAvgModel { + m.beta = &beta + return m +} + +// Name of the model. +func (m *HoltLinearMovAvgModel) Name() string { + return "holt" +} + +// Settings of the model. +func (m *HoltLinearMovAvgModel) Settings() map[string]interface{} { + settings := make(map[string]interface{}) + if m.alpha != nil { + settings["alpha"] = *m.alpha + } + if m.beta != nil { + settings["beta"] = *m.beta + } + return settings +} + +// -- Holt Winters -- + +// HoltWintersMovAvgModel calculates a triple exponential weighted moving average. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html#_holt_winters +type HoltWintersMovAvgModel struct { + alpha *float64 + beta *float64 + gamma *float64 + period *int + seasonalityType string + pad *bool +} + +// NewHoltWintersMovAvgModel creates and initializes a new HoltWintersMovAvgModel. +func NewHoltWintersMovAvgModel() *HoltWintersMovAvgModel { + return &HoltWintersMovAvgModel{} +} + +// Alpha controls the smoothing of the data. Alpha = 1 retains no memory +// of past values (e.g. a random walk), while alpha = 0 retains infinite +// memory of past values (e.g. the series mean). Useful values are somewhere +// in between. Defaults to 0.5. +func (m *HoltWintersMovAvgModel) Alpha(alpha float64) *HoltWintersMovAvgModel { + m.alpha = &alpha + return m +} + +// Beta is equivalent to Alpha but controls the smoothing of the trend +// instead of the data. +func (m *HoltWintersMovAvgModel) Beta(beta float64) *HoltWintersMovAvgModel { + m.beta = &beta + return m +} + +func (m *HoltWintersMovAvgModel) Gamma(gamma float64) *HoltWintersMovAvgModel { + m.gamma = &gamma + return m +} + +func (m *HoltWintersMovAvgModel) Period(period int) *HoltWintersMovAvgModel { + m.period = &period + return m +} + +func (m *HoltWintersMovAvgModel) SeasonalityType(typ string) *HoltWintersMovAvgModel { + m.seasonalityType = typ + return m +} + +func (m *HoltWintersMovAvgModel) Pad(pad bool) *HoltWintersMovAvgModel { + m.pad = &pad + return m +} + +// Name of the model. +func (m *HoltWintersMovAvgModel) Name() string { + return "holt_winters" +} + +// Settings of the model. +func (m *HoltWintersMovAvgModel) Settings() map[string]interface{} { + settings := make(map[string]interface{}) + if m.alpha != nil { + settings["alpha"] = *m.alpha + } + if m.beta != nil { + settings["beta"] = *m.beta + } + if m.gamma != nil { + settings["gamma"] = *m.gamma + } + if m.period != nil { + settings["period"] = *m.period + } + if m.pad != nil { + settings["pad"] = *m.pad + } + if m.seasonalityType != "" { + settings["type"] = m.seasonalityType + } + return settings +} + +// -- Linear -- + +// LinearMovAvgModel calculates a linearly weighted moving average, such +// that older values are linearly less important. "Time" is determined +// by position in collection. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html#_linear +type LinearMovAvgModel struct { +} + +// NewLinearMovAvgModel creates and initializes a new LinearMovAvgModel. +func NewLinearMovAvgModel() *LinearMovAvgModel { + return &LinearMovAvgModel{} +} + +// Name of the model. +func (m *LinearMovAvgModel) Name() string { + return "linear" +} + +// Settings of the model. +func (m *LinearMovAvgModel) Settings() map[string]interface{} { + return nil +} + +// -- Simple -- + +// SimpleMovAvgModel calculates a simple unweighted (arithmetic) moving average. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movavg-aggregation.html#_simple +type SimpleMovAvgModel struct { +} + +// NewSimpleMovAvgModel creates and initializes a new SimpleMovAvgModel. +func NewSimpleMovAvgModel() *SimpleMovAvgModel { + return &SimpleMovAvgModel{} +} + +// Name of the model. +func (m *SimpleMovAvgModel) Name() string { + return "simple" +} + +// Settings of the model. +func (m *SimpleMovAvgModel) Settings() map[string]interface{} { + return nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_fn.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_fn.go new file mode 100644 index 0000000000..ea8cbdc2f0 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_mov_fn.go @@ -0,0 +1,132 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MovFnAggregation, given an ordered series of data, will slice a window across +// the data and allow the user to specify a custom script that is executed for +// each window of data. +// +// You must pass a script to process the values. There are a number of predefined +// script functions you can use as described here: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movfn-aggregation.html#_pre_built_functions. +// +// Example: +// agg := elastic.NewMovFnAggregation( +// "the_sum", // bucket path +// elastic.NewScript("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), +// 10, // window size +// ) +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-movfn-aggregation.html. +type MovFnAggregation struct { + script *Script + format string + gapPolicy string + window int + + meta map[string]interface{} + bucketsPaths []string +} + +// NewMovFnAggregation creates and initializes a new MovFnAggregation. +// +// Deprecated: The MovFnAggregation has been deprecated in 6.4.0. Use the more generate MovFnAggregation instead. +func NewMovFnAggregation(bucketsPath string, script *Script, window int) *MovFnAggregation { + return &MovFnAggregation{ + bucketsPaths: []string{bucketsPath}, + script: script, + window: window, + } +} + +// Script is the script to run. +func (a *MovFnAggregation) Script(script *Script) *MovFnAggregation { + a.script = script + return a +} + +// Format to use on the output of this aggregation. +func (a *MovFnAggregation) Format(format string) *MovFnAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *MovFnAggregation) GapPolicy(gapPolicy string) *MovFnAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *MovFnAggregation) GapInsertZeros() *MovFnAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *MovFnAggregation) GapSkip() *MovFnAggregation { + a.gapPolicy = "skip" + return a +} + +// Window sets the window size for this aggregation. +func (a *MovFnAggregation) Window(window int) *MovFnAggregation { + a.window = window + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *MovFnAggregation) Meta(metaData map[string]interface{}) *MovFnAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *MovFnAggregation) BucketsPath(bucketsPaths ...string) *MovFnAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *MovFnAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["moving_fn"] = params + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Script + if a.script != nil { + src, err := a.script.Source() + if err != nil { + return nil, err + } + params["script"] = src + } + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + params["window"] = a.window + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_percentiles_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_percentiles_bucket.go new file mode 100644 index 0000000000..a5a3fdf686 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_percentiles_bucket.go @@ -0,0 +1,104 @@ +// Copyright 2012-2015 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// PercentilesBucketAggregation is a sibling pipeline aggregation which calculates +// percentiles across all bucket of a specified metric in a sibling aggregation. +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-percentiles-bucket-aggregation.html +type PercentilesBucketAggregation struct { + format string + gapPolicy string + percents []float64 + bucketsPaths []string + + meta map[string]interface{} +} + +// NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation. +func NewPercentilesBucketAggregation() *PercentilesBucketAggregation { + return &PercentilesBucketAggregation{} +} + +// Format to apply the output value of this aggregation. +func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation { + p.format = format + return p +} + +// Percents to calculate percentiles for in this aggregation. +func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation { + p.percents = percents + return p +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation { + p.gapPolicy = gapPolicy + return p +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (p *PercentilesBucketAggregation) GapInsertZeros() *PercentilesBucketAggregation { + p.gapPolicy = "insert_zeros" + return p +} + +// GapSkip skips gaps in the series. +func (p *PercentilesBucketAggregation) GapSkip() *PercentilesBucketAggregation { + p.gapPolicy = "skip" + return p +} + +// Meta sets the meta data to be included in the aggregation response. +func (p *PercentilesBucketAggregation) Meta(metaData map[string]interface{}) *PercentilesBucketAggregation { + p.meta = metaData + return p +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (p *PercentilesBucketAggregation) BucketsPath(bucketsPaths ...string) *PercentilesBucketAggregation { + p.bucketsPaths = append(p.bucketsPaths, bucketsPaths...) + return p +} + +// Source returns the a JSON-serializable interface. +func (p *PercentilesBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["percentiles_bucket"] = params + + if p.format != "" { + params["format"] = p.format + } + if p.gapPolicy != "" { + params["gap_policy"] = p.gapPolicy + } + + // Add buckets paths + switch len(p.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = p.bucketsPaths[0] + default: + params["buckets_path"] = p.bucketsPaths + } + + // Add percents + if len(p.percents) > 0 { + params["percents"] = p.percents + } + + // Add Meta data if available + if len(p.meta) > 0 { + source["meta"] = p.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_serial_diff.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_serial_diff.go new file mode 100644 index 0000000000..d2a9b4c9d2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_serial_diff.go @@ -0,0 +1,105 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SerialDiffAggregation implements serial differencing. +// Serial differencing is a technique where values in a time series are +// subtracted from itself at different time lags or periods. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-serialdiff-aggregation.html +type SerialDiffAggregation struct { + format string + gapPolicy string + lag *int + + meta map[string]interface{} + bucketsPaths []string +} + +// NewSerialDiffAggregation creates and initializes a new SerialDiffAggregation. +func NewSerialDiffAggregation() *SerialDiffAggregation { + return &SerialDiffAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *SerialDiffAggregation) Format(format string) *SerialDiffAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *SerialDiffAggregation) GapPolicy(gapPolicy string) *SerialDiffAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *SerialDiffAggregation) GapInsertZeros() *SerialDiffAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *SerialDiffAggregation) GapSkip() *SerialDiffAggregation { + a.gapPolicy = "skip" + return a +} + +// Lag specifies the historical bucket to subtract from the current value. +// E.g. a lag of 7 will subtract the current value from the value 7 buckets +// ago. Lag must be a positive, non-zero integer. +func (a *SerialDiffAggregation) Lag(lag int) *SerialDiffAggregation { + a.lag = &lag + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *SerialDiffAggregation) Meta(metaData map[string]interface{}) *SerialDiffAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *SerialDiffAggregation) BucketsPath(bucketsPaths ...string) *SerialDiffAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *SerialDiffAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["serial_diff"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + if a.lag != nil { + params["lag"] = *a.lag + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_stats_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_stats_bucket.go new file mode 100644 index 0000000000..2eb61ebab0 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_stats_bucket.go @@ -0,0 +1,94 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// StatsBucketAggregation is a sibling pipeline aggregation which calculates +// a variety of stats across all bucket of a specified metric in a sibling aggregation. +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-stats-bucket-aggregation.html +type StatsBucketAggregation struct { + format string + gapPolicy string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewStatsBucketAggregation creates and initializes a new StatsBucketAggregation. +func NewStatsBucketAggregation() *StatsBucketAggregation { + return &StatsBucketAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (s *StatsBucketAggregation) Format(format string) *StatsBucketAggregation { + s.format = format + return s +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (s *StatsBucketAggregation) GapPolicy(gapPolicy string) *StatsBucketAggregation { + s.gapPolicy = gapPolicy + return s +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (s *StatsBucketAggregation) GapInsertZeros() *StatsBucketAggregation { + s.gapPolicy = "insert_zeros" + return s +} + +// GapSkip skips gaps in the series. +func (s *StatsBucketAggregation) GapSkip() *StatsBucketAggregation { + s.gapPolicy = "skip" + return s +} + +// Meta sets the meta data to be included in the aggregation response. +func (s *StatsBucketAggregation) Meta(metaData map[string]interface{}) *StatsBucketAggregation { + s.meta = metaData + return s +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (s *StatsBucketAggregation) BucketsPath(bucketsPaths ...string) *StatsBucketAggregation { + s.bucketsPaths = append(s.bucketsPaths, bucketsPaths...) + return s +} + +// Source returns the a JSON-serializable interface. +func (s *StatsBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["stats_bucket"] = params + + if s.format != "" { + params["format"] = s.format + } + if s.gapPolicy != "" { + params["gap_policy"] = s.gapPolicy + } + + // Add buckets paths + switch len(s.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = s.bucketsPaths[0] + default: + params["buckets_path"] = s.bucketsPaths + } + + // Add Meta data if available + if len(s.meta) > 0 { + source["meta"] = s.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_sum_bucket.go b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_sum_bucket.go new file mode 100644 index 0000000000..cfdafce077 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_aggs_pipeline_sum_bucket.go @@ -0,0 +1,94 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SumBucketAggregation is a sibling pipeline aggregation which calculates +// the sum across all buckets of a specified metric in a sibling aggregation. +// The specified metric must be numeric and the sibling aggregation must +// be a multi-bucket aggregation. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-pipeline-sum-bucket-aggregation.html +type SumBucketAggregation struct { + format string + gapPolicy string + + meta map[string]interface{} + bucketsPaths []string +} + +// NewSumBucketAggregation creates and initializes a new SumBucketAggregation. +func NewSumBucketAggregation() *SumBucketAggregation { + return &SumBucketAggregation{ + bucketsPaths: make([]string, 0), + } +} + +// Format to use on the output of this aggregation. +func (a *SumBucketAggregation) Format(format string) *SumBucketAggregation { + a.format = format + return a +} + +// GapPolicy defines what should be done when a gap in the series is discovered. +// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". +func (a *SumBucketAggregation) GapPolicy(gapPolicy string) *SumBucketAggregation { + a.gapPolicy = gapPolicy + return a +} + +// GapInsertZeros inserts zeros for gaps in the series. +func (a *SumBucketAggregation) GapInsertZeros() *SumBucketAggregation { + a.gapPolicy = "insert_zeros" + return a +} + +// GapSkip skips gaps in the series. +func (a *SumBucketAggregation) GapSkip() *SumBucketAggregation { + a.gapPolicy = "skip" + return a +} + +// Meta sets the meta data to be included in the aggregation response. +func (a *SumBucketAggregation) Meta(metaData map[string]interface{}) *SumBucketAggregation { + a.meta = metaData + return a +} + +// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. +func (a *SumBucketAggregation) BucketsPath(bucketsPaths ...string) *SumBucketAggregation { + a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) + return a +} + +// Source returns the a JSON-serializable interface. +func (a *SumBucketAggregation) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["sum_bucket"] = params + + if a.format != "" { + params["format"] = a.format + } + if a.gapPolicy != "" { + params["gap_policy"] = a.gapPolicy + } + + // Add buckets paths + switch len(a.bucketsPaths) { + case 0: + case 1: + params["buckets_path"] = a.bucketsPaths[0] + default: + params["buckets_path"] = a.bucketsPaths + } + + // Add Meta data if available + if len(a.meta) > 0 { + source["meta"] = a.meta + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_collapse_builder.go b/vendor/github.com/olivere/elastic/v7/search_collapse_builder.go new file mode 100644 index 0000000000..0de4eed616 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_collapse_builder.go @@ -0,0 +1,68 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// CollapseBuilder enables field collapsing on a search request. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-collapse.html +// for details. +type CollapseBuilder struct { + field string + innerHit *InnerHit + maxConcurrentGroupRequests *int +} + +// NewCollapseBuilder creates a new CollapseBuilder. +func NewCollapseBuilder(field string) *CollapseBuilder { + return &CollapseBuilder{field: field} +} + +// Field to collapse. +func (b *CollapseBuilder) Field(field string) *CollapseBuilder { + b.field = field + return b +} + +// InnerHit option to expand the collapsed results. +func (b *CollapseBuilder) InnerHit(innerHit *InnerHit) *CollapseBuilder { + b.innerHit = innerHit + return b +} + +// MaxConcurrentGroupRequests is the maximum number of group requests that are +// allowed to be ran concurrently in the inner_hits phase. +func (b *CollapseBuilder) MaxConcurrentGroupRequests(max int) *CollapseBuilder { + b.maxConcurrentGroupRequests = &max + return b +} + +// Source generates the JSON serializable fragment for the CollapseBuilder. +func (b *CollapseBuilder) Source() (interface{}, error) { + // { + // "field": "user", + // "inner_hits": { + // "name": "last_tweets", + // "size": 5, + // "sort": [{ "date": "asc" }] + // }, + // "max_concurrent_group_searches": 4 + // } + src := map[string]interface{}{ + "field": b.field, + } + + if b.innerHit != nil { + hits, err := b.innerHit.Source() + if err != nil { + return nil, err + } + src["inner_hits"] = hits + } + + if b.maxConcurrentGroupRequests != nil { + src["max_concurrent_group_searches"] = *b.maxConcurrentGroupRequests + } + + return src, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_bool.go b/vendor/github.com/olivere/elastic/v7/search_queries_bool.go new file mode 100644 index 0000000000..d192d79ed6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_bool.go @@ -0,0 +1,203 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "fmt" + +// A bool query matches documents matching boolean +// combinations of other queries. +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-bool-query.html +type BoolQuery struct { + Query + mustClauses []Query + mustNotClauses []Query + filterClauses []Query + shouldClauses []Query + boost *float64 + minimumShouldMatch string + adjustPureNegative *bool + queryName string +} + +// Creates a new bool query. +func NewBoolQuery() *BoolQuery { + return &BoolQuery{ + mustClauses: make([]Query, 0), + mustNotClauses: make([]Query, 0), + filterClauses: make([]Query, 0), + shouldClauses: make([]Query, 0), + } +} + +func (q *BoolQuery) Must(queries ...Query) *BoolQuery { + q.mustClauses = append(q.mustClauses, queries...) + return q +} + +func (q *BoolQuery) MustNot(queries ...Query) *BoolQuery { + q.mustNotClauses = append(q.mustNotClauses, queries...) + return q +} + +func (q *BoolQuery) Filter(filters ...Query) *BoolQuery { + q.filterClauses = append(q.filterClauses, filters...) + return q +} + +func (q *BoolQuery) Should(queries ...Query) *BoolQuery { + q.shouldClauses = append(q.shouldClauses, queries...) + return q +} + +func (q *BoolQuery) Boost(boost float64) *BoolQuery { + q.boost = &boost + return q +} + +func (q *BoolQuery) MinimumShouldMatch(minimumShouldMatch string) *BoolQuery { + q.minimumShouldMatch = minimumShouldMatch + return q +} + +func (q *BoolQuery) MinimumNumberShouldMatch(minimumNumberShouldMatch int) *BoolQuery { + q.minimumShouldMatch = fmt.Sprintf("%d", minimumNumberShouldMatch) + return q +} + +func (q *BoolQuery) AdjustPureNegative(adjustPureNegative bool) *BoolQuery { + q.adjustPureNegative = &adjustPureNegative + return q +} + +func (q *BoolQuery) QueryName(queryName string) *BoolQuery { + q.queryName = queryName + return q +} + +// Creates the query source for the bool query. +func (q *BoolQuery) Source() (interface{}, error) { + // { + // "bool" : { + // "must" : { + // "term" : { "user" : "kimchy" } + // }, + // "must_not" : { + // "range" : { + // "age" : { "from" : 10, "to" : 20 } + // } + // }, + // "filter" : [ + // ... + // ] + // "should" : [ + // { + // "term" : { "tag" : "wow" } + // }, + // { + // "term" : { "tag" : "elasticsearch" } + // } + // ], + // "minimum_should_match" : 1, + // "boost" : 1.0 + // } + // } + + query := make(map[string]interface{}) + + boolClause := make(map[string]interface{}) + query["bool"] = boolClause + + // must + if len(q.mustClauses) == 1 { + src, err := q.mustClauses[0].Source() + if err != nil { + return nil, err + } + boolClause["must"] = src + } else if len(q.mustClauses) > 1 { + var clauses []interface{} + for _, subQuery := range q.mustClauses { + src, err := subQuery.Source() + if err != nil { + return nil, err + } + clauses = append(clauses, src) + } + boolClause["must"] = clauses + } + + // must_not + if len(q.mustNotClauses) == 1 { + src, err := q.mustNotClauses[0].Source() + if err != nil { + return nil, err + } + boolClause["must_not"] = src + } else if len(q.mustNotClauses) > 1 { + var clauses []interface{} + for _, subQuery := range q.mustNotClauses { + src, err := subQuery.Source() + if err != nil { + return nil, err + } + clauses = append(clauses, src) + } + boolClause["must_not"] = clauses + } + + // filter + if len(q.filterClauses) == 1 { + src, err := q.filterClauses[0].Source() + if err != nil { + return nil, err + } + boolClause["filter"] = src + } else if len(q.filterClauses) > 1 { + var clauses []interface{} + for _, subQuery := range q.filterClauses { + src, err := subQuery.Source() + if err != nil { + return nil, err + } + clauses = append(clauses, src) + } + boolClause["filter"] = clauses + } + + // should + if len(q.shouldClauses) == 1 { + src, err := q.shouldClauses[0].Source() + if err != nil { + return nil, err + } + boolClause["should"] = src + } else if len(q.shouldClauses) > 1 { + var clauses []interface{} + for _, subQuery := range q.shouldClauses { + src, err := subQuery.Source() + if err != nil { + return nil, err + } + clauses = append(clauses, src) + } + boolClause["should"] = clauses + } + + if q.boost != nil { + boolClause["boost"] = *q.boost + } + if q.minimumShouldMatch != "" { + boolClause["minimum_should_match"] = q.minimumShouldMatch + } + if q.adjustPureNegative != nil { + boolClause["adjust_pure_negative"] = *q.adjustPureNegative + } + if q.queryName != "" { + boolClause["_name"] = q.queryName + } + + return query, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_boosting.go b/vendor/github.com/olivere/elastic/v7/search_queries_boosting.go new file mode 100644 index 0000000000..7a76efbf90 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_boosting.go @@ -0,0 +1,97 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// A boosting query can be used to effectively +// demote results that match a given query. +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-boosting-query.html +type BoostingQuery struct { + Query + positiveClause Query + negativeClause Query + negativeBoost *float64 + boost *float64 +} + +// Creates a new boosting query. +func NewBoostingQuery() *BoostingQuery { + return &BoostingQuery{} +} + +func (q *BoostingQuery) Positive(positive Query) *BoostingQuery { + q.positiveClause = positive + return q +} + +func (q *BoostingQuery) Negative(negative Query) *BoostingQuery { + q.negativeClause = negative + return q +} + +func (q *BoostingQuery) NegativeBoost(negativeBoost float64) *BoostingQuery { + q.negativeBoost = &negativeBoost + return q +} + +func (q *BoostingQuery) Boost(boost float64) *BoostingQuery { + q.boost = &boost + return q +} + +// Creates the query source for the boosting query. +func (q *BoostingQuery) Source() (interface{}, error) { + // { + // "boosting" : { + // "positive" : { + // "term" : { + // "field1" : "value1" + // } + // }, + // "negative" : { + // "term" : { + // "field2" : "value2" + // } + // }, + // "negative_boost" : 0.2 + // } + // } + + query := make(map[string]interface{}) + + boostingClause := make(map[string]interface{}) + query["boosting"] = boostingClause + + // Negative and positive clause as well as negative boost + // are mandatory in the Java client. + + // positive + if q.positiveClause != nil { + src, err := q.positiveClause.Source() + if err != nil { + return nil, err + } + boostingClause["positive"] = src + } + + // negative + if q.negativeClause != nil { + src, err := q.negativeClause.Source() + if err != nil { + return nil, err + } + boostingClause["negative"] = src + } + + if q.negativeBoost != nil { + boostingClause["negative_boost"] = *q.negativeBoost + } + + if q.boost != nil { + boostingClause["boost"] = *q.boost + } + + return query, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_common_terms.go b/vendor/github.com/olivere/elastic/v7/search_queries_common_terms.go new file mode 100644 index 0000000000..a10a213bcb --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_common_terms.go @@ -0,0 +1,141 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// CommonTermsQuery is a modern alternative to stopwords +// which improves the precision and recall of search results +// (by taking stopwords into account), without sacrificing performance. +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-common-terms-query.html +// +// Deprecated: Use Match query instead (7.3.0+), which skips blocks of +// documents efficiently, without any configuration, provided that the +// total number of hits is not tracked. +type CommonTermsQuery struct { + Query + name string + text interface{} + cutoffFreq *float64 + highFreq *float64 + highFreqOp string + highFreqMinimumShouldMatch string + lowFreq *float64 + lowFreqOp string + lowFreqMinimumShouldMatch string + analyzer string + boost *float64 + queryName string +} + +// NewCommonTermsQuery creates and initializes a new common terms query. +func NewCommonTermsQuery(name string, text interface{}) *CommonTermsQuery { + return &CommonTermsQuery{name: name, text: text} +} + +func (q *CommonTermsQuery) CutoffFrequency(f float64) *CommonTermsQuery { + q.cutoffFreq = &f + return q +} + +func (q *CommonTermsQuery) HighFreq(f float64) *CommonTermsQuery { + q.highFreq = &f + return q +} + +func (q *CommonTermsQuery) HighFreqOperator(op string) *CommonTermsQuery { + q.highFreqOp = op + return q +} + +func (q *CommonTermsQuery) HighFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery { + q.highFreqMinimumShouldMatch = minShouldMatch + return q +} + +func (q *CommonTermsQuery) LowFreq(f float64) *CommonTermsQuery { + q.lowFreq = &f + return q +} + +func (q *CommonTermsQuery) LowFreqOperator(op string) *CommonTermsQuery { + q.lowFreqOp = op + return q +} + +func (q *CommonTermsQuery) LowFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery { + q.lowFreqMinimumShouldMatch = minShouldMatch + return q +} + +func (q *CommonTermsQuery) Analyzer(analyzer string) *CommonTermsQuery { + q.analyzer = analyzer + return q +} + +func (q *CommonTermsQuery) Boost(boost float64) *CommonTermsQuery { + q.boost = &boost + return q +} + +func (q *CommonTermsQuery) QueryName(queryName string) *CommonTermsQuery { + q.queryName = queryName + return q +} + +// Creates the query source for the common query. +func (q *CommonTermsQuery) Source() (interface{}, error) { + // { + // "common": { + // "body": { + // "query": "this is bonsai cool", + // "cutoff_frequency": 0.001 + // } + // } + // } + source := make(map[string]interface{}) + body := make(map[string]interface{}) + query := make(map[string]interface{}) + + source["common"] = body + body[q.name] = query + query["query"] = q.text + + if q.cutoffFreq != nil { + query["cutoff_frequency"] = *q.cutoffFreq + } + if q.highFreq != nil { + query["high_freq"] = *q.highFreq + } + if q.highFreqOp != "" { + query["high_freq_operator"] = q.highFreqOp + } + if q.lowFreq != nil { + query["low_freq"] = *q.lowFreq + } + if q.lowFreqOp != "" { + query["low_freq_operator"] = q.lowFreqOp + } + if q.lowFreqMinimumShouldMatch != "" || q.highFreqMinimumShouldMatch != "" { + mm := make(map[string]interface{}) + if q.lowFreqMinimumShouldMatch != "" { + mm["low_freq"] = q.lowFreqMinimumShouldMatch + } + if q.highFreqMinimumShouldMatch != "" { + mm["high_freq"] = q.highFreqMinimumShouldMatch + } + query["minimum_should_match"] = mm + } + if q.analyzer != "" { + query["analyzer"] = q.analyzer + } + if q.boost != nil { + query["boost"] = *q.boost + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_constant_score.go b/vendor/github.com/olivere/elastic/v7/search_queries_constant_score.go new file mode 100644 index 0000000000..cfa8dff795 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_constant_score.go @@ -0,0 +1,59 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ConstantScoreQuery is a query that wraps a filter and simply returns +// a constant score equal to the query boost for every document in the filter. +// +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-constant-score-query.html +type ConstantScoreQuery struct { + filter Query + boost *float64 +} + +// ConstantScoreQuery creates and initializes a new constant score query. +func NewConstantScoreQuery(filter Query) *ConstantScoreQuery { + return &ConstantScoreQuery{ + filter: filter, + } +} + +// Boost sets the boost for this query. Documents matching this query +// will (in addition to the normal weightings) have their score multiplied +// by the boost provided. +func (q *ConstantScoreQuery) Boost(boost float64) *ConstantScoreQuery { + q.boost = &boost + return q +} + +// Source returns the query source. +func (q *ConstantScoreQuery) Source() (interface{}, error) { + // "constant_score" : { + // "filter" : { + // .... + // }, + // "boost" : 1.5 + // } + + query := make(map[string]interface{}) + + params := make(map[string]interface{}) + query["constant_score"] = params + + // filter + src, err := q.filter.Source() + if err != nil { + return nil, err + } + params["filter"] = src + + // boost + if q.boost != nil { + params["boost"] = *q.boost + } + + return query, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_dis_max.go b/vendor/github.com/olivere/elastic/v7/search_queries_dis_max.go new file mode 100644 index 0000000000..48376d6dcb --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_dis_max.go @@ -0,0 +1,104 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// DisMaxQuery is a query that generates the union of documents produced by +// its subqueries, and that scores each document with the maximum score +// for that document as produced by any subquery, plus a tie breaking +// increment for any additional matching subqueries. +// +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-dis-max-query.html +type DisMaxQuery struct { + queries []Query + boost *float64 + tieBreaker *float64 + queryName string +} + +// NewDisMaxQuery creates and initializes a new dis max query. +func NewDisMaxQuery() *DisMaxQuery { + return &DisMaxQuery{ + queries: make([]Query, 0), + } +} + +// Query adds one or more queries to the dis max query. +func (q *DisMaxQuery) Query(queries ...Query) *DisMaxQuery { + q.queries = append(q.queries, queries...) + return q +} + +// Boost sets the boost for this query. Documents matching this query will +// (in addition to the normal weightings) have their score multiplied by +// the boost provided. +func (q *DisMaxQuery) Boost(boost float64) *DisMaxQuery { + q.boost = &boost + return q +} + +// TieBreaker is the factor by which the score of each non-maximum disjunct +// for a document is multiplied with and added into the final score. +// +// If non-zero, the value should be small, on the order of 0.1, which says +// that 10 occurrences of word in a lower-scored field that is also in a +// higher scored field is just as good as a unique word in the lower scored +// field (i.e., one that is not in any higher scored field). +func (q *DisMaxQuery) TieBreaker(tieBreaker float64) *DisMaxQuery { + q.tieBreaker = &tieBreaker + return q +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched filters per hit. +func (q *DisMaxQuery) QueryName(queryName string) *DisMaxQuery { + q.queryName = queryName + return q +} + +// Source returns the JSON serializable content for this query. +func (q *DisMaxQuery) Source() (interface{}, error) { + // { + // "dis_max" : { + // "tie_breaker" : 0.7, + // "boost" : 1.2, + // "queries" : { + // { + // "term" : { "age" : 34 } + // }, + // { + // "term" : { "age" : 35 } + // } + // ] + // } + // } + + query := make(map[string]interface{}) + params := make(map[string]interface{}) + query["dis_max"] = params + + if q.tieBreaker != nil { + params["tie_breaker"] = *q.tieBreaker + } + if q.boost != nil { + params["boost"] = *q.boost + } + if q.queryName != "" { + params["_name"] = q.queryName + } + + // queries + var clauses []interface{} + for _, subQuery := range q.queries { + src, err := subQuery.Source() + if err != nil { + return nil, err + } + clauses = append(clauses, src) + } + params["queries"] = clauses + + return query, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_distance_feature_query.go b/vendor/github.com/olivere/elastic/v7/search_queries_distance_feature_query.go new file mode 100644 index 0000000000..1a8a061c4a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_distance_feature_query.go @@ -0,0 +1,119 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" +) + +// DistanceFeatureQuery uses a script to provide a custom score for returned documents. +// +// A DistanceFeatureQuery query is useful if, for example, a scoring function is +// expensive and you only need to calculate the score of a filtered set of documents. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-distance-feature-query.html +type DistanceFeatureQuery struct { + field string + pivot string + origin interface{} + boost *float64 + queryName string +} + +// NewDistanceFeatureQuery creates and initializes a new script_score query. +func NewDistanceFeatureQuery(field string, origin interface{}, pivot string) *DistanceFeatureQuery { + return &DistanceFeatureQuery{ + field: field, + origin: origin, + pivot: pivot, + } +} + +// Field to be used in the DistanceFeatureQuery. +func (q *DistanceFeatureQuery) Field(name string) *DistanceFeatureQuery { + q.field = name + return q +} + +// Origin is the date or point of origin used to calculate distances. +// +// If the field is a date or date_nanos field, the origin value must be a +// date. Date math such as "now-1h" is supported. +// +// If the field is a geo_point field, the origin must be a GeoPoint. +func (q *DistanceFeatureQuery) Origin(origin interface{}) *DistanceFeatureQuery { + q.origin = origin + return q +} + +// Pivot is distance from the origin at which relevance scores +// receive half of the boost value. +// +// If field is a date or date_nanos field, the pivot value must be a time +// unit, such as "1h" or "10d". +// +// If field is a geo_point field, the pivot value must be a distance unit, +// such as "1km" or "12m". You can pass a string, or a GeoPoint. +func (q *DistanceFeatureQuery) Pivot(pivot string) *DistanceFeatureQuery { + q.pivot = pivot + return q +} + +// Boost sets the boost for this query. +func (q *DistanceFeatureQuery) Boost(boost float64) *DistanceFeatureQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter. +func (q *DistanceFeatureQuery) QueryName(queryName string) *DistanceFeatureQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *DistanceFeatureQuery) Source() (interface{}, error) { + // { + // "distance_feature" : { + // "field" : "production_date", + // "pivot" : "7d", + // "origin" : "now" + // } + // } + // { + // "distance_feature" : { + // "field" : "location", + // "pivot" : "1000m", + // "origin" : [-71.3, 41.15] + // } + // } + + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["distance_feature"] = query + + query["field"] = q.field + query["pivot"] = q.pivot + switch v := q.origin.(type) { + default: + return nil, fmt.Errorf("DistanceFeatureQuery: unable to serialize Origin from type %T", v) + case string: + query["origin"] = v + case *GeoPoint: + query["origin"] = v.Source() + case GeoPoint: + query["origin"] = v.Source() + } + + if v := q.boost; v != nil { + query["boost"] = *v + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_exists.go b/vendor/github.com/olivere/elastic/v7/search_queries_exists.go new file mode 100644 index 0000000000..33f15893b8 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_exists.go @@ -0,0 +1,49 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ExistsQuery is a query that only matches on documents that the field +// has a value in them. +// +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-exists-query.html +type ExistsQuery struct { + name string + queryName string +} + +// NewExistsQuery creates and initializes a new exists query. +func NewExistsQuery(name string) *ExistsQuery { + return &ExistsQuery{ + name: name, + } +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched queries per hit. +func (q *ExistsQuery) QueryName(queryName string) *ExistsQuery { + q.queryName = queryName + return q +} + +// Source returns the JSON serializable content for this query. +func (q *ExistsQuery) Source() (interface{}, error) { + // { + // "exists" : { + // "field" : "user" + // } + // } + + query := make(map[string]interface{}) + params := make(map[string]interface{}) + query["exists"] = params + + params["field"] = q.name + if q.queryName != "" { + params["_name"] = q.queryName + } + + return query, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_fsq.go b/vendor/github.com/olivere/elastic/v7/search_queries_fsq.go new file mode 100644 index 0000000000..ce00122b7a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_fsq.go @@ -0,0 +1,159 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// FunctionScoreQuery allows you to modify the score of documents that +// are retrieved by a query. This can be useful if, for example, +// a score function is computationally expensive and it is sufficient +// to compute the score on a filtered set of documents. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html +type FunctionScoreQuery struct { + query Query + filter Query + boost *float64 + maxBoost *float64 + scoreMode string + boostMode string + filters []Query + scoreFuncs []ScoreFunction + minScore *float64 +} + +// NewFunctionScoreQuery creates and initializes a new function score query. +func NewFunctionScoreQuery() *FunctionScoreQuery { + return &FunctionScoreQuery{ + filters: make([]Query, 0), + scoreFuncs: make([]ScoreFunction, 0), + } +} + +// Query sets the query for the function score query. +func (q *FunctionScoreQuery) Query(query Query) *FunctionScoreQuery { + q.query = query + return q +} + +// Filter sets the filter for the function score query. +func (q *FunctionScoreQuery) Filter(filter Query) *FunctionScoreQuery { + q.filter = filter + return q +} + +// Add adds a score function that will execute on all the documents +// matching the filter. +func (q *FunctionScoreQuery) Add(filter Query, scoreFunc ScoreFunction) *FunctionScoreQuery { + q.filters = append(q.filters, filter) + q.scoreFuncs = append(q.scoreFuncs, scoreFunc) + return q +} + +// AddScoreFunc adds a score function that will execute the function on all documents. +func (q *FunctionScoreQuery) AddScoreFunc(scoreFunc ScoreFunction) *FunctionScoreQuery { + q.filters = append(q.filters, nil) + q.scoreFuncs = append(q.scoreFuncs, scoreFunc) + return q +} + +// ScoreMode defines how results of individual score functions will be aggregated. +// Can be first, avg, max, sum, min, or multiply. +func (q *FunctionScoreQuery) ScoreMode(scoreMode string) *FunctionScoreQuery { + q.scoreMode = scoreMode + return q +} + +// BoostMode defines how the combined result of score functions will +// influence the final score together with the sub query score. +func (q *FunctionScoreQuery) BoostMode(boostMode string) *FunctionScoreQuery { + q.boostMode = boostMode + return q +} + +// MaxBoost is the maximum boost that will be applied by function score. +func (q *FunctionScoreQuery) MaxBoost(maxBoost float64) *FunctionScoreQuery { + q.maxBoost = &maxBoost + return q +} + +// Boost sets the boost for this query. Documents matching this query will +// (in addition to the normal weightings) have their score multiplied by the +// boost provided. +func (q *FunctionScoreQuery) Boost(boost float64) *FunctionScoreQuery { + q.boost = &boost + return q +} + +// MinScore sets the minimum score. +func (q *FunctionScoreQuery) MinScore(minScore float64) *FunctionScoreQuery { + q.minScore = &minScore + return q +} + +// Source returns JSON for the function score query. +func (q *FunctionScoreQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["function_score"] = query + + if q.query != nil { + src, err := q.query.Source() + if err != nil { + return nil, err + } + query["query"] = src + } + if q.filter != nil { + src, err := q.filter.Source() + if err != nil { + return nil, err + } + query["filter"] = src + } + + if len(q.filters) > 0 { + funcs := make([]interface{}, len(q.filters)) + for i, filter := range q.filters { + hsh := make(map[string]interface{}) + if filter != nil { + src, err := filter.Source() + if err != nil { + return nil, err + } + hsh["filter"] = src + } + // Weight needs to be serialized on this level. + if weight := q.scoreFuncs[i].GetWeight(); weight != nil { + hsh["weight"] = weight + } + // Serialize the score function + src, err := q.scoreFuncs[i].Source() + if err != nil { + return nil, err + } + hsh[q.scoreFuncs[i].Name()] = src + funcs[i] = hsh + } + query["functions"] = funcs + } + + if q.scoreMode != "" { + query["score_mode"] = q.scoreMode + } + if q.boostMode != "" { + query["boost_mode"] = q.boostMode + } + if q.maxBoost != nil { + query["max_boost"] = *q.maxBoost + } + if q.boost != nil { + query["boost"] = *q.boost + } + if q.minScore != nil { + query["min_score"] = *q.minScore + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_fsq_score_funcs.go b/vendor/github.com/olivere/elastic/v7/search_queries_fsq_score_funcs.go new file mode 100644 index 0000000000..b14f3b7cf5 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_fsq_score_funcs.go @@ -0,0 +1,582 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "strings" +) + +// ScoreFunction is used in combination with the Function Score Query. +type ScoreFunction interface { + Name() string + GetWeight() *float64 // returns the weight which must be serialized at the level of FunctionScoreQuery + Source() (interface{}, error) +} + +// -- Exponential Decay -- + +// ExponentialDecayFunction builds an exponential decay score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html +// for details. +type ExponentialDecayFunction struct { + fieldName string + origin interface{} + scale interface{} + decay *float64 + offset interface{} + multiValueMode string + weight *float64 +} + +// NewExponentialDecayFunction creates a new ExponentialDecayFunction. +func NewExponentialDecayFunction() *ExponentialDecayFunction { + return &ExponentialDecayFunction{} +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *ExponentialDecayFunction) Name() string { + return "exp" +} + +// FieldName specifies the name of the field to which this decay function is applied to. +func (fn *ExponentialDecayFunction) FieldName(fieldName string) *ExponentialDecayFunction { + fn.fieldName = fieldName + return fn +} + +// Origin defines the "central point" by which the decay function calculates +// "distance". +func (fn *ExponentialDecayFunction) Origin(origin interface{}) *ExponentialDecayFunction { + fn.origin = origin + return fn +} + +// Scale defines the scale to be used with Decay. +func (fn *ExponentialDecayFunction) Scale(scale interface{}) *ExponentialDecayFunction { + fn.scale = scale + return fn +} + +// Decay defines how documents are scored at the distance given a Scale. +// If no decay is defined, documents at the distance Scale will be scored 0.5. +func (fn *ExponentialDecayFunction) Decay(decay float64) *ExponentialDecayFunction { + fn.decay = &decay + return fn +} + +// Offset, if defined, computes the decay function only for a distance +// greater than the defined offset. +func (fn *ExponentialDecayFunction) Offset(offset interface{}) *ExponentialDecayFunction { + fn.offset = offset + return fn +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *ExponentialDecayFunction) Weight(weight float64) *ExponentialDecayFunction { + fn.weight = &weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *ExponentialDecayFunction) GetWeight() *float64 { + return fn.weight +} + +// MultiValueMode specifies how the decay function should be calculated +// on a field that has multiple values. +// Valid modes are: min, max, avg, and sum. +func (fn *ExponentialDecayFunction) MultiValueMode(mode string) *ExponentialDecayFunction { + fn.multiValueMode = mode + return fn +} + +// Source returns the serializable JSON data of this score function. +func (fn *ExponentialDecayFunction) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source[fn.fieldName] = params + if fn.origin != nil { + params["origin"] = fn.origin + } + params["scale"] = fn.scale + if fn.decay != nil && *fn.decay > 0 { + params["decay"] = *fn.decay + } + if fn.offset != nil { + params["offset"] = fn.offset + } + if fn.multiValueMode != "" { + source["multi_value_mode"] = fn.multiValueMode + } + return source, nil +} + +// -- Gauss Decay -- + +// GaussDecayFunction builds a gauss decay score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html +// for details. +type GaussDecayFunction struct { + fieldName string + origin interface{} + scale interface{} + decay *float64 + offset interface{} + multiValueMode string + weight *float64 +} + +// NewGaussDecayFunction returns a new GaussDecayFunction. +func NewGaussDecayFunction() *GaussDecayFunction { + return &GaussDecayFunction{} +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *GaussDecayFunction) Name() string { + return "gauss" +} + +// FieldName specifies the name of the field to which this decay function is applied to. +func (fn *GaussDecayFunction) FieldName(fieldName string) *GaussDecayFunction { + fn.fieldName = fieldName + return fn +} + +// Origin defines the "central point" by which the decay function calculates +// "distance". +func (fn *GaussDecayFunction) Origin(origin interface{}) *GaussDecayFunction { + fn.origin = origin + return fn +} + +// Scale defines the scale to be used with Decay. +func (fn *GaussDecayFunction) Scale(scale interface{}) *GaussDecayFunction { + fn.scale = scale + return fn +} + +// Decay defines how documents are scored at the distance given a Scale. +// If no decay is defined, documents at the distance Scale will be scored 0.5. +func (fn *GaussDecayFunction) Decay(decay float64) *GaussDecayFunction { + fn.decay = &decay + return fn +} + +// Offset, if defined, computes the decay function only for a distance +// greater than the defined offset. +func (fn *GaussDecayFunction) Offset(offset interface{}) *GaussDecayFunction { + fn.offset = offset + return fn +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *GaussDecayFunction) Weight(weight float64) *GaussDecayFunction { + fn.weight = &weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *GaussDecayFunction) GetWeight() *float64 { + return fn.weight +} + +// MultiValueMode specifies how the decay function should be calculated +// on a field that has multiple values. +// Valid modes are: min, max, avg, and sum. +func (fn *GaussDecayFunction) MultiValueMode(mode string) *GaussDecayFunction { + fn.multiValueMode = mode + return fn +} + +// Source returns the serializable JSON data of this score function. +func (fn *GaussDecayFunction) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source[fn.fieldName] = params + if fn.origin != nil { + params["origin"] = fn.origin + } + params["scale"] = fn.scale + if fn.decay != nil && *fn.decay > 0 { + params["decay"] = *fn.decay + } + if fn.offset != nil { + params["offset"] = fn.offset + } + if fn.multiValueMode != "" { + source["multi_value_mode"] = fn.multiValueMode + } + // Notice that the weight has to be serialized in FunctionScoreQuery. + return source, nil +} + +// -- Linear Decay -- + +// LinearDecayFunction builds a linear decay score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html +// for details. +type LinearDecayFunction struct { + fieldName string + origin interface{} + scale interface{} + decay *float64 + offset interface{} + multiValueMode string + weight *float64 +} + +// NewLinearDecayFunction initializes and returns a new LinearDecayFunction. +func NewLinearDecayFunction() *LinearDecayFunction { + return &LinearDecayFunction{} +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *LinearDecayFunction) Name() string { + return "linear" +} + +// FieldName specifies the name of the field to which this decay function is applied to. +func (fn *LinearDecayFunction) FieldName(fieldName string) *LinearDecayFunction { + fn.fieldName = fieldName + return fn +} + +// Origin defines the "central point" by which the decay function calculates +// "distance". +func (fn *LinearDecayFunction) Origin(origin interface{}) *LinearDecayFunction { + fn.origin = origin + return fn +} + +// Scale defines the scale to be used with Decay. +func (fn *LinearDecayFunction) Scale(scale interface{}) *LinearDecayFunction { + fn.scale = scale + return fn +} + +// Decay defines how documents are scored at the distance given a Scale. +// If no decay is defined, documents at the distance Scale will be scored 0.5. +func (fn *LinearDecayFunction) Decay(decay float64) *LinearDecayFunction { + fn.decay = &decay + return fn +} + +// Offset, if defined, computes the decay function only for a distance +// greater than the defined offset. +func (fn *LinearDecayFunction) Offset(offset interface{}) *LinearDecayFunction { + fn.offset = offset + return fn +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *LinearDecayFunction) Weight(weight float64) *LinearDecayFunction { + fn.weight = &weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *LinearDecayFunction) GetWeight() *float64 { + return fn.weight +} + +// MultiValueMode specifies how the decay function should be calculated +// on a field that has multiple values. +// Valid modes are: min, max, avg, and sum. +func (fn *LinearDecayFunction) MultiValueMode(mode string) *LinearDecayFunction { + fn.multiValueMode = mode + return fn +} + +// GetMultiValueMode returns how the decay function should be calculated +// on a field that has multiple values. +// Valid modes are: min, max, avg, and sum. +func (fn *LinearDecayFunction) GetMultiValueMode() string { + return fn.multiValueMode +} + +// Source returns the serializable JSON data of this score function. +func (fn *LinearDecayFunction) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source[fn.fieldName] = params + if fn.origin != nil { + params["origin"] = fn.origin + } + params["scale"] = fn.scale + if fn.decay != nil && *fn.decay > 0 { + params["decay"] = *fn.decay + } + if fn.offset != nil { + params["offset"] = fn.offset + } + if fn.multiValueMode != "" { + source["multi_value_mode"] = fn.multiValueMode + } + // Notice that the weight has to be serialized in FunctionScoreQuery. + return source, nil +} + +// -- Script -- + +// ScriptFunction builds a script score function. It uses a script to +// compute or influence the score of documents that match with the inner +// query or filter. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_script_score +// for details. +type ScriptFunction struct { + script *Script + weight *float64 +} + +// NewScriptFunction initializes and returns a new ScriptFunction. +func NewScriptFunction(script *Script) *ScriptFunction { + return &ScriptFunction{ + script: script, + } +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *ScriptFunction) Name() string { + return "script_score" +} + +// Script specifies the script to be executed. +func (fn *ScriptFunction) Script(script *Script) *ScriptFunction { + fn.script = script + return fn +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *ScriptFunction) Weight(weight float64) *ScriptFunction { + fn.weight = &weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *ScriptFunction) GetWeight() *float64 { + return fn.weight +} + +// Source returns the serializable JSON data of this score function. +func (fn *ScriptFunction) Source() (interface{}, error) { + source := make(map[string]interface{}) + if fn.script != nil { + src, err := fn.script.Source() + if err != nil { + return nil, err + } + source["script"] = src + } + // Notice that the weight has to be serialized in FunctionScoreQuery. + return source, nil +} + +// -- Field value factor -- + +// FieldValueFactorFunction is a function score function that allows you +// to use a field from a document to influence the score. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_field_value_factor. +type FieldValueFactorFunction struct { + field string + factor *float64 + missing *float64 + weight *float64 + modifier string +} + +// NewFieldValueFactorFunction initializes and returns a new FieldValueFactorFunction. +func NewFieldValueFactorFunction() *FieldValueFactorFunction { + return &FieldValueFactorFunction{} +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *FieldValueFactorFunction) Name() string { + return "field_value_factor" +} + +// Field is the field to be extracted from the document. +func (fn *FieldValueFactorFunction) Field(field string) *FieldValueFactorFunction { + fn.field = field + return fn +} + +// Factor is the (optional) factor to multiply the field with. If you do not +// specify a factor, the default is 1. +func (fn *FieldValueFactorFunction) Factor(factor float64) *FieldValueFactorFunction { + fn.factor = &factor + return fn +} + +// Modifier to apply to the field value. It can be one of: none, log, log1p, +// log2p, ln, ln1p, ln2p, square, sqrt, or reciprocal. Defaults to: none. +func (fn *FieldValueFactorFunction) Modifier(modifier string) *FieldValueFactorFunction { + fn.modifier = modifier + return fn +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *FieldValueFactorFunction) Weight(weight float64) *FieldValueFactorFunction { + fn.weight = &weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *FieldValueFactorFunction) GetWeight() *float64 { + return fn.weight +} + +// Missing is used if a document does not have that field. +func (fn *FieldValueFactorFunction) Missing(missing float64) *FieldValueFactorFunction { + fn.missing = &missing + return fn +} + +// Source returns the serializable JSON data of this score function. +func (fn *FieldValueFactorFunction) Source() (interface{}, error) { + source := make(map[string]interface{}) + if fn.field != "" { + source["field"] = fn.field + } + if fn.factor != nil { + source["factor"] = *fn.factor + } + if fn.missing != nil { + source["missing"] = *fn.missing + } + if fn.modifier != "" { + source["modifier"] = strings.ToLower(fn.modifier) + } + // Notice that the weight has to be serialized in FunctionScoreQuery. + return source, nil +} + +// -- Weight Factor -- + +// WeightFactorFunction builds a weight factor function that multiplies +// the weight to the score. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_weight +// for details. +type WeightFactorFunction struct { + weight float64 +} + +// NewWeightFactorFunction initializes and returns a new WeightFactorFunction. +func NewWeightFactorFunction(weight float64) *WeightFactorFunction { + return &WeightFactorFunction{weight: weight} +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *WeightFactorFunction) Name() string { + return "weight" +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *WeightFactorFunction) Weight(weight float64) *WeightFactorFunction { + fn.weight = weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *WeightFactorFunction) GetWeight() *float64 { + return &fn.weight +} + +// Source returns the serializable JSON data of this score function. +func (fn *WeightFactorFunction) Source() (interface{}, error) { + // Notice that the weight has to be serialized in FunctionScoreQuery. + return fn.weight, nil +} + +// -- Random -- + +// RandomFunction builds a random score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_random +// for details. +type RandomFunction struct { + field string + seed interface{} + weight *float64 +} + +// NewRandomFunction initializes and returns a new RandomFunction. +func NewRandomFunction() *RandomFunction { + return &RandomFunction{} +} + +// Name represents the JSON field name under which the output of Source +// needs to be serialized by FunctionScoreQuery (see FunctionScoreQuery.Source). +func (fn *RandomFunction) Name() string { + return "random_score" +} + +// Field is the field to be used for random number generation. +// This parameter is compulsory when a Seed is set and ignored +// otherwise. Note that documents that have the same value for a +// field will get the same score. +func (fn *RandomFunction) Field(field string) *RandomFunction { + fn.field = field + return fn +} + +// Seed sets the seed based on which the random number will be generated. +// Using the same seed is guaranteed to generate the same random number for a specific doc. +// Seed must be an integer, e.g. int or int64. It is specified as an interface{} +// here for compatibility with older versions (which also accepted strings). +func (fn *RandomFunction) Seed(seed interface{}) *RandomFunction { + fn.seed = seed + return fn +} + +// Weight adjusts the score of the score function. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-function-score-query.html#_using_function_score +// for details. +func (fn *RandomFunction) Weight(weight float64) *RandomFunction { + fn.weight = &weight + return fn +} + +// GetWeight returns the adjusted score. It is part of the ScoreFunction interface. +// Returns nil if weight is not specified. +func (fn *RandomFunction) GetWeight() *float64 { + return fn.weight +} + +// Source returns the serializable JSON data of this score function. +func (fn *RandomFunction) Source() (interface{}, error) { + source := make(map[string]interface{}) + if fn.field != "" { + source["field"] = fn.field + } + if fn.seed != nil { + source["seed"] = fn.seed + } + // Notice that the weight has to be serialized in FunctionScoreQuery. + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_fuzzy.go b/vendor/github.com/olivere/elastic/v7/search_queries_fuzzy.go new file mode 100644 index 0000000000..a81acfcfdd --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_fuzzy.go @@ -0,0 +1,120 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// FuzzyQuery uses similarity based on Levenshtein edit distance for +// string fields, and a +/- margin on numeric and date fields. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-fuzzy-query.html +type FuzzyQuery struct { + name string + value interface{} + boost *float64 + fuzziness interface{} + prefixLength *int + maxExpansions *int + transpositions *bool + rewrite string + queryName string +} + +// NewFuzzyQuery creates a new fuzzy query. +func NewFuzzyQuery(name string, value interface{}) *FuzzyQuery { + q := &FuzzyQuery{ + name: name, + value: value, + } + return q +} + +// Boost sets the boost for this query. Documents matching this query will +// (in addition to the normal weightings) have their score multiplied by +// the boost provided. +func (q *FuzzyQuery) Boost(boost float64) *FuzzyQuery { + q.boost = &boost + return q +} + +// Fuzziness can be an integer/long like 0, 1 or 2 as well as strings +// like "auto", "0..1", "1..4" or "0.0..1.0". +func (q *FuzzyQuery) Fuzziness(fuzziness interface{}) *FuzzyQuery { + q.fuzziness = fuzziness + return q +} + +func (q *FuzzyQuery) PrefixLength(prefixLength int) *FuzzyQuery { + q.prefixLength = &prefixLength + return q +} + +func (q *FuzzyQuery) MaxExpansions(maxExpansions int) *FuzzyQuery { + q.maxExpansions = &maxExpansions + return q +} + +func (q *FuzzyQuery) Transpositions(transpositions bool) *FuzzyQuery { + q.transpositions = &transpositions + return q +} + +func (q *FuzzyQuery) Rewrite(rewrite string) *FuzzyQuery { + q.rewrite = rewrite + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *FuzzyQuery) QueryName(queryName string) *FuzzyQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *FuzzyQuery) Source() (interface{}, error) { + // { + // "fuzzy" : { + // "user" : { + // "value" : "ki", + // "boost" : 1.0, + // "fuzziness" : 2, + // "prefix_length" : 0, + // "max_expansions" : 100 + // } + // } + + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["fuzzy"] = query + + fq := make(map[string]interface{}) + query[q.name] = fq + + fq["value"] = q.value + + if q.boost != nil { + fq["boost"] = *q.boost + } + if q.transpositions != nil { + fq["transpositions"] = *q.transpositions + } + if q.fuzziness != nil { + fq["fuzziness"] = q.fuzziness + } + if q.prefixLength != nil { + fq["prefix_length"] = *q.prefixLength + } + if q.maxExpansions != nil { + fq["max_expansions"] = *q.maxExpansions + } + if q.rewrite != "" { + fq["rewrite"] = q.rewrite + } + if q.queryName != "" { + fq["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_geo_bounding_box.go b/vendor/github.com/olivere/elastic/v7/search_queries_geo_bounding_box.go new file mode 100644 index 0000000000..6345867f69 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_geo_bounding_box.go @@ -0,0 +1,121 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// GeoBoundingBoxQuery allows to filter hits based on a point location using +// a bounding box. +// +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-geo-bounding-box-query.html +type GeoBoundingBoxQuery struct { + name string + top *float64 + left *float64 + bottom *float64 + right *float64 + typ string + queryName string +} + +// NewGeoBoundingBoxQuery creates and initializes a new GeoBoundingBoxQuery. +func NewGeoBoundingBoxQuery(name string) *GeoBoundingBoxQuery { + return &GeoBoundingBoxQuery{ + name: name, + } +} + +func (q *GeoBoundingBoxQuery) TopLeft(top, left float64) *GeoBoundingBoxQuery { + q.top = &top + q.left = &left + return q +} + +func (q *GeoBoundingBoxQuery) TopLeftFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery { + return q.TopLeft(point.Lat, point.Lon) +} + +func (q *GeoBoundingBoxQuery) BottomRight(bottom, right float64) *GeoBoundingBoxQuery { + q.bottom = &bottom + q.right = &right + return q +} + +func (q *GeoBoundingBoxQuery) BottomRightFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery { + return q.BottomRight(point.Lat, point.Lon) +} + +func (q *GeoBoundingBoxQuery) BottomLeft(bottom, left float64) *GeoBoundingBoxQuery { + q.bottom = &bottom + q.left = &left + return q +} + +func (q *GeoBoundingBoxQuery) BottomLeftFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery { + return q.BottomLeft(point.Lat, point.Lon) +} + +func (q *GeoBoundingBoxQuery) TopRight(top, right float64) *GeoBoundingBoxQuery { + q.top = &top + q.right = &right + return q +} + +func (q *GeoBoundingBoxQuery) TopRightFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery { + return q.TopRight(point.Lat, point.Lon) +} + +// Type sets the type of executing the geo bounding box. It can be either +// memory or indexed. It defaults to memory. +func (q *GeoBoundingBoxQuery) Type(typ string) *GeoBoundingBoxQuery { + q.typ = typ + return q +} + +func (q *GeoBoundingBoxQuery) QueryName(queryName string) *GeoBoundingBoxQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *GeoBoundingBoxQuery) Source() (interface{}, error) { + // { + // "geo_bounding_box" : { + // ... + // } + // } + + if q.top == nil { + return nil, errors.New("geo_bounding_box requires top latitude to be set") + } + if q.bottom == nil { + return nil, errors.New("geo_bounding_box requires bottom latitude to be set") + } + if q.right == nil { + return nil, errors.New("geo_bounding_box requires right longitude to be set") + } + if q.left == nil { + return nil, errors.New("geo_bounding_box requires left longitude to be set") + } + + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["geo_bounding_box"] = params + + box := make(map[string]interface{}) + box["top_left"] = []float64{*q.left, *q.top} + box["bottom_right"] = []float64{*q.right, *q.bottom} + params[q.name] = box + + if q.typ != "" { + params["type"] = q.typ + } + if q.queryName != "" { + params["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_geo_distance.go b/vendor/github.com/olivere/elastic/v7/search_queries_geo_distance.go new file mode 100644 index 0000000000..4e6878a4b3 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_geo_distance.go @@ -0,0 +1,107 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// GeoDistanceQuery filters documents that include only hits that exists +// within a specific distance from a geo point. +// +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-geo-distance-query.html +type GeoDistanceQuery struct { + name string + distance string + lat float64 + lon float64 + geohash string + distanceType string + queryName string +} + +// NewGeoDistanceQuery creates and initializes a new GeoDistanceQuery. +func NewGeoDistanceQuery(name string) *GeoDistanceQuery { + return &GeoDistanceQuery{name: name} +} + +func (q *GeoDistanceQuery) GeoPoint(point *GeoPoint) *GeoDistanceQuery { + q.lat = point.Lat + q.lon = point.Lon + return q +} + +func (q *GeoDistanceQuery) Point(lat, lon float64) *GeoDistanceQuery { + q.lat = lat + q.lon = lon + return q +} + +func (q *GeoDistanceQuery) Lat(lat float64) *GeoDistanceQuery { + q.lat = lat + return q +} + +func (q *GeoDistanceQuery) Lon(lon float64) *GeoDistanceQuery { + q.lon = lon + return q +} + +func (q *GeoDistanceQuery) GeoHash(geohash string) *GeoDistanceQuery { + q.geohash = geohash + return q +} + +func (q *GeoDistanceQuery) Distance(distance string) *GeoDistanceQuery { + q.distance = distance + return q +} + +func (q *GeoDistanceQuery) DistanceType(distanceType string) *GeoDistanceQuery { + q.distanceType = distanceType + return q +} + +func (q *GeoDistanceQuery) QueryName(queryName string) *GeoDistanceQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *GeoDistanceQuery) Source() (interface{}, error) { + // { + // "geo_distance" : { + // "distance" : "200km", + // "pin.location" : { + // "lat" : 40, + // "lon" : -70 + // } + // } + // } + + source := make(map[string]interface{}) + + params := make(map[string]interface{}) + + if q.geohash != "" { + params[q.name] = q.geohash + } else { + location := make(map[string]interface{}) + location["lat"] = q.lat + location["lon"] = q.lon + params[q.name] = location + } + + if q.distance != "" { + params["distance"] = q.distance + } + if q.distanceType != "" { + params["distance_type"] = q.distanceType + } + if q.queryName != "" { + params["_name"] = q.queryName + } + + source["geo_distance"] = params + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_geo_polygon.go b/vendor/github.com/olivere/elastic/v7/search_queries_geo_polygon.go new file mode 100644 index 0000000000..98cd5fe342 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_geo_polygon.go @@ -0,0 +1,72 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// GeoPolygonQuery allows to include hits that only fall within a polygon of points. +// +// For more details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-geo-polygon-query.html +type GeoPolygonQuery struct { + name string + points []*GeoPoint + queryName string +} + +// NewGeoPolygonQuery creates and initializes a new GeoPolygonQuery. +func NewGeoPolygonQuery(name string) *GeoPolygonQuery { + return &GeoPolygonQuery{ + name: name, + points: make([]*GeoPoint, 0), + } +} + +// AddPoint adds a point from latitude and longitude. +func (q *GeoPolygonQuery) AddPoint(lat, lon float64) *GeoPolygonQuery { + q.points = append(q.points, GeoPointFromLatLon(lat, lon)) + return q +} + +// AddGeoPoint adds a GeoPoint. +func (q *GeoPolygonQuery) AddGeoPoint(point *GeoPoint) *GeoPolygonQuery { + q.points = append(q.points, point) + return q +} + +func (q *GeoPolygonQuery) QueryName(queryName string) *GeoPolygonQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *GeoPolygonQuery) Source() (interface{}, error) { + // "geo_polygon" : { + // "person.location" : { + // "points" : [ + // {"lat" : 40, "lon" : -70}, + // {"lat" : 30, "lon" : -80}, + // {"lat" : 20, "lon" : -90} + // ] + // } + // } + source := make(map[string]interface{}) + + params := make(map[string]interface{}) + source["geo_polygon"] = params + + polygon := make(map[string]interface{}) + params[q.name] = polygon + + var points []interface{} + for _, point := range q.points { + points = append(points, point.Source()) + } + polygon["points"] = points + + if q.queryName != "" { + params["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_has_child.go b/vendor/github.com/olivere/elastic/v7/search_queries_has_child.go new file mode 100644 index 0000000000..d2ce2f9577 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_has_child.go @@ -0,0 +1,131 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// HasChildQuery accepts a query and the child type to run against, and results +// in parent documents that have child docs matching the query. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-has-child-query.html +type HasChildQuery struct { + query Query + childType string + boost *float64 + scoreMode string + minChildren *int + maxChildren *int + shortCircuitCutoff *int + queryName string + innerHit *InnerHit +} + +// NewHasChildQuery creates and initializes a new has_child query. +func NewHasChildQuery(childType string, query Query) *HasChildQuery { + return &HasChildQuery{ + query: query, + childType: childType, + } +} + +// Boost sets the boost for this query. +func (q *HasChildQuery) Boost(boost float64) *HasChildQuery { + q.boost = &boost + return q +} + +// ScoreMode defines how the scores from the matching child documents +// are mapped into the parent document. Allowed values are: min, max, +// avg, or none. +func (q *HasChildQuery) ScoreMode(scoreMode string) *HasChildQuery { + q.scoreMode = scoreMode + return q +} + +// MinChildren defines the minimum number of children that are required +// to match for the parent to be considered a match. +func (q *HasChildQuery) MinChildren(minChildren int) *HasChildQuery { + q.minChildren = &minChildren + return q +} + +// MaxChildren defines the maximum number of children that are required +// to match for the parent to be considered a match. +func (q *HasChildQuery) MaxChildren(maxChildren int) *HasChildQuery { + q.maxChildren = &maxChildren + return q +} + +// ShortCircuitCutoff configures what cut off point only to evaluate +// parent documents that contain the matching parent id terms instead +// of evaluating all parent docs. +func (q *HasChildQuery) ShortCircuitCutoff(shortCircuitCutoff int) *HasChildQuery { + q.shortCircuitCutoff = &shortCircuitCutoff + return q +} + +// QueryName specifies the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *HasChildQuery) QueryName(queryName string) *HasChildQuery { + q.queryName = queryName + return q +} + +// InnerHit sets the inner hit definition in the scope of this query and +// reusing the defined type and query. +func (q *HasChildQuery) InnerHit(innerHit *InnerHit) *HasChildQuery { + q.innerHit = innerHit + return q +} + +// Source returns JSON for the function score query. +func (q *HasChildQuery) Source() (interface{}, error) { + // { + // "has_child" : { + // "type" : "blog_tag", + // "score_mode" : "min", + // "query" : { + // "term" : { + // "tag" : "something" + // } + // } + // } + // } + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["has_child"] = query + + src, err := q.query.Source() + if err != nil { + return nil, err + } + query["query"] = src + query["type"] = q.childType + if q.boost != nil { + query["boost"] = *q.boost + } + if q.scoreMode != "" { + query["score_mode"] = q.scoreMode + } + if q.minChildren != nil { + query["min_children"] = *q.minChildren + } + if q.maxChildren != nil { + query["max_children"] = *q.maxChildren + } + if q.shortCircuitCutoff != nil { + query["short_circuit_cutoff"] = *q.shortCircuitCutoff + } + if q.queryName != "" { + query["_name"] = q.queryName + } + if q.innerHit != nil { + src, err := q.innerHit.Source() + if err != nil { + return nil, err + } + query["inner_hits"] = src + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_has_parent.go b/vendor/github.com/olivere/elastic/v7/search_queries_has_parent.go new file mode 100644 index 0000000000..1723f002cc --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_has_parent.go @@ -0,0 +1,108 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// HasParentQuery accepts a query and a parent type. The query is executed +// in the parent document space which is specified by the parent type. +// This query returns child documents which associated parents have matched. +// For the rest has_parent query has the same options and works in the +// same manner as has_child query. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-has-parent-query.html +type HasParentQuery struct { + query Query + parentType string + boost *float64 + score *bool + queryName string + innerHit *InnerHit + ignoreUnmapped *bool +} + +// NewHasParentQuery creates and initializes a new has_parent query. +func NewHasParentQuery(parentType string, query Query) *HasParentQuery { + return &HasParentQuery{ + query: query, + parentType: parentType, + } +} + +// Boost sets the boost for this query. +func (q *HasParentQuery) Boost(boost float64) *HasParentQuery { + q.boost = &boost + return q +} + +// Score defines if the parent score is mapped into the child documents. +func (q *HasParentQuery) Score(score bool) *HasParentQuery { + q.score = &score + return q +} + +// QueryName specifies the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *HasParentQuery) QueryName(queryName string) *HasParentQuery { + q.queryName = queryName + return q +} + +// InnerHit sets the inner hit definition in the scope of this query and +// reusing the defined type and query. +func (q *HasParentQuery) InnerHit(innerHit *InnerHit) *HasParentQuery { + q.innerHit = innerHit + return q +} + +// IgnoreUnmapped specifies whether unmapped types should be ignored. +// If set to false, the query failes when an unmapped type is found. +func (q *HasParentQuery) IgnoreUnmapped(ignore bool) *HasParentQuery { + q.ignoreUnmapped = &ignore + return q +} + +// Source returns JSON for the function score query. +func (q *HasParentQuery) Source() (interface{}, error) { + // { + // "has_parent" : { + // "parent_type" : "blog", + // "query" : { + // "term" : { + // "tag" : "something" + // } + // } + // } + // } + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["has_parent"] = query + + src, err := q.query.Source() + if err != nil { + return nil, err + } + query["query"] = src + query["parent_type"] = q.parentType + if q.boost != nil { + query["boost"] = *q.boost + } + if q.score != nil { + query["score"] = *q.score + } + if q.queryName != "" { + query["_name"] = q.queryName + } + if q.innerHit != nil { + src, err := q.innerHit.Source() + if err != nil { + return nil, err + } + query["inner_hits"] = src + } + if q.ignoreUnmapped != nil { + query["ignore_unmapped"] = *q.ignoreUnmapped + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_ids.go b/vendor/github.com/olivere/elastic/v7/search_queries_ids.go new file mode 100644 index 0000000000..8873fc2af6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_ids.go @@ -0,0 +1,78 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// IdsQuery filters documents that only have the provided ids. +// Note, this query uses the _uid field. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-ids-query.html +type IdsQuery struct { + types []string + values []string + boost *float64 + queryName string +} + +// NewIdsQuery creates and initializes a new ids query. +// +// Deprecated: Types are in the process of being removed, prefer to filter on a field instead. +func NewIdsQuery(types ...string) *IdsQuery { + return &IdsQuery{ + types: types, + values: make([]string, 0), + } +} + +// Ids adds ids to the filter. +func (q *IdsQuery) Ids(ids ...string) *IdsQuery { + q.values = append(q.values, ids...) + return q +} + +// Boost sets the boost for this query. +func (q *IdsQuery) Boost(boost float64) *IdsQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter. +func (q *IdsQuery) QueryName(queryName string) *IdsQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *IdsQuery) Source() (interface{}, error) { + // { + // "ids" : { + // "type" : "my_type", + // "values" : ["1", "4", "100"] + // } + // } + + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["ids"] = query + + // type(s) + if len(q.types) == 1 { + query["type"] = q.types[0] + } else if len(q.types) > 1 { + query["types"] = q.types + } + + // values + query["values"] = q.values + + if q.boost != nil { + query["boost"] = *q.boost + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_match.go b/vendor/github.com/olivere/elastic/v7/search_queries_match.go new file mode 100644 index 0000000000..fce10781a9 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_match.go @@ -0,0 +1,189 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MatchQuery is a family of queries that accepts text/numerics/dates, +// analyzes them, and constructs a query. +// +// To create a new MatchQuery, use NewMatchQuery. To create specific types +// of queries, e.g. a match_phrase query, use NewMatchPhrQuery(...).Type("phrase"), +// or use one of the shortcuts e.g. NewMatchPhraseQuery(...). +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-match-query.html +type MatchQuery struct { + name string + text interface{} + operator string // or / and + analyzer string + boost *float64 + fuzziness string + prefixLength *int + maxExpansions *int + minimumShouldMatch string + fuzzyRewrite string + lenient *bool + fuzzyTranspositions *bool + zeroTermsQuery string + cutoffFrequency *float64 + queryName string +} + +// NewMatchQuery creates and initializes a new MatchQuery. +func NewMatchQuery(name string, text interface{}) *MatchQuery { + return &MatchQuery{name: name, text: text} +} + +// Operator sets the operator to use when using a boolean query. +// Can be "AND" or "OR" (default). +func (q *MatchQuery) Operator(operator string) *MatchQuery { + q.operator = operator + return q +} + +// Analyzer explicitly sets the analyzer to use. It defaults to use explicit +// mapping config for the field, or, if not set, the default search analyzer. +func (q *MatchQuery) Analyzer(analyzer string) *MatchQuery { + q.analyzer = analyzer + return q +} + +// Fuzziness sets the fuzziness when evaluated to a fuzzy query type. +// Defaults to "AUTO". +func (q *MatchQuery) Fuzziness(fuzziness string) *MatchQuery { + q.fuzziness = fuzziness + return q +} + +// PrefixLength sets the length of a length of common (non-fuzzy) +// prefix for fuzzy match queries. It must be non-negative. +func (q *MatchQuery) PrefixLength(prefixLength int) *MatchQuery { + q.prefixLength = &prefixLength + return q +} + +// MaxExpansions is used with fuzzy or prefix type queries. It specifies +// the number of term expansions to use. It defaults to unbounded so that +// its recommended to set it to a reasonable value for faster execution. +func (q *MatchQuery) MaxExpansions(maxExpansions int) *MatchQuery { + q.maxExpansions = &maxExpansions + return q +} + +// CutoffFrequency can be a value in [0..1] (or an absolute number >=1). +// It represents the maximum treshold of a terms document frequency to be +// considered a low frequency term. +func (q *MatchQuery) CutoffFrequency(cutoff float64) *MatchQuery { + q.cutoffFrequency = &cutoff + return q +} + +// MinimumShouldMatch sets the optional minimumShouldMatch value to +// apply to the query. +func (q *MatchQuery) MinimumShouldMatch(minimumShouldMatch string) *MatchQuery { + q.minimumShouldMatch = minimumShouldMatch + return q +} + +// FuzzyRewrite sets the fuzzy_rewrite parameter controlling how the +// fuzzy query will get rewritten. +func (q *MatchQuery) FuzzyRewrite(fuzzyRewrite string) *MatchQuery { + q.fuzzyRewrite = fuzzyRewrite + return q +} + +// FuzzyTranspositions sets whether transpositions are supported in +// fuzzy queries. +// +// The default metric used by fuzzy queries to determine a match is +// the Damerau-Levenshtein distance formula which supports transpositions. +// Setting transposition to false will +// * switch to classic Levenshtein distance. +// * If not set, Damerau-Levenshtein distance metric will be used. +func (q *MatchQuery) FuzzyTranspositions(fuzzyTranspositions bool) *MatchQuery { + q.fuzzyTranspositions = &fuzzyTranspositions + return q +} + +// Lenient specifies whether format based failures will be ignored. +func (q *MatchQuery) Lenient(lenient bool) *MatchQuery { + q.lenient = &lenient + return q +} + +// ZeroTermsQuery can be "all" or "none". +func (q *MatchQuery) ZeroTermsQuery(zeroTermsQuery string) *MatchQuery { + q.zeroTermsQuery = zeroTermsQuery + return q +} + +// Boost sets the boost to apply to this query. +func (q *MatchQuery) Boost(boost float64) *MatchQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *MatchQuery) QueryName(queryName string) *MatchQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *MatchQuery) Source() (interface{}, error) { + // {"match":{"name":{"query":"value","type":"boolean/phrase"}}} + source := make(map[string]interface{}) + + match := make(map[string]interface{}) + source["match"] = match + + query := make(map[string]interface{}) + match[q.name] = query + + query["query"] = q.text + + if q.operator != "" { + query["operator"] = q.operator + } + if q.analyzer != "" { + query["analyzer"] = q.analyzer + } + if q.fuzziness != "" { + query["fuzziness"] = q.fuzziness + } + if q.prefixLength != nil { + query["prefix_length"] = *q.prefixLength + } + if q.maxExpansions != nil { + query["max_expansions"] = *q.maxExpansions + } + if q.minimumShouldMatch != "" { + query["minimum_should_match"] = q.minimumShouldMatch + } + if q.fuzzyRewrite != "" { + query["fuzzy_rewrite"] = q.fuzzyRewrite + } + if q.lenient != nil { + query["lenient"] = *q.lenient + } + if q.fuzzyTranspositions != nil { + query["fuzzy_transpositions"] = *q.fuzzyTranspositions + } + if q.zeroTermsQuery != "" { + query["zero_terms_query"] = q.zeroTermsQuery + } + if q.cutoffFrequency != nil { + query["cutoff_frequency"] = *q.cutoffFrequency + } + if q.boost != nil { + query["boost"] = *q.boost + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_match_all.go b/vendor/github.com/olivere/elastic/v7/search_queries_match_all.go new file mode 100644 index 0000000000..921ead5859 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_match_all.go @@ -0,0 +1,51 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MatchAllQuery is the most simple query, which matches all documents, +// giving them all a _score of 1.0. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-match-all-query.html +type MatchAllQuery struct { + boost *float64 + queryName string +} + +// NewMatchAllQuery creates and initializes a new match all query. +func NewMatchAllQuery() *MatchAllQuery { + return &MatchAllQuery{} +} + +// Boost sets the boost for this query. Documents matching this query will +// (in addition to the normal weightings) have their score multiplied by the +// boost provided. +func (q *MatchAllQuery) Boost(boost float64) *MatchAllQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name. +func (q *MatchAllQuery) QueryName(name string) *MatchAllQuery { + q.queryName = name + return q +} + +// Source returns JSON for the match all query. +func (q MatchAllQuery) Source() (interface{}, error) { + // { + // "match_all" : { ... } + // } + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["match_all"] = params + if q.boost != nil { + params["boost"] = *q.boost + } + if q.queryName != "" { + params["_name"] = q.queryName + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_match_none.go b/vendor/github.com/olivere/elastic/v7/search_queries_match_none.go new file mode 100644 index 0000000000..a1d1f87b56 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_match_none.go @@ -0,0 +1,39 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MatchNoneQuery returns no documents. It is the inverse of +// MatchAllQuery. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-match-all-query.html +type MatchNoneQuery struct { + queryName string +} + +// NewMatchNoneQuery creates and initializes a new match none query. +func NewMatchNoneQuery() *MatchNoneQuery { + return &MatchNoneQuery{} +} + +// QueryName sets the query name. +func (q *MatchNoneQuery) QueryName(name string) *MatchNoneQuery { + q.queryName = name + return q +} + +// Source returns JSON for the match none query. +func (q MatchNoneQuery) Source() (interface{}, error) { + // { + // "match_none" : { ... } + // } + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["match_none"] = params + if q.queryName != "" { + params["_name"] = q.queryName + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_match_phrase.go b/vendor/github.com/olivere/elastic/v7/search_queries_match_phrase.go new file mode 100644 index 0000000000..974a49acbc --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_match_phrase.go @@ -0,0 +1,79 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MatchPhraseQuery analyzes the text and creates a phrase query out of +// the analyzed text. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-match-query-phrase.html +type MatchPhraseQuery struct { + name string + value interface{} + analyzer string + slop *int + boost *float64 + queryName string +} + +// NewMatchPhraseQuery creates and initializes a new MatchPhraseQuery. +func NewMatchPhraseQuery(name string, value interface{}) *MatchPhraseQuery { + return &MatchPhraseQuery{name: name, value: value} +} + +// Analyzer explicitly sets the analyzer to use. It defaults to use explicit +// mapping config for the field, or, if not set, the default search analyzer. +func (q *MatchPhraseQuery) Analyzer(analyzer string) *MatchPhraseQuery { + q.analyzer = analyzer + return q +} + +// Slop sets the phrase slop if evaluated to a phrase query type. +func (q *MatchPhraseQuery) Slop(slop int) *MatchPhraseQuery { + q.slop = &slop + return q +} + +// Boost sets the boost to apply to this query. +func (q *MatchPhraseQuery) Boost(boost float64) *MatchPhraseQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *MatchPhraseQuery) QueryName(queryName string) *MatchPhraseQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *MatchPhraseQuery) Source() (interface{}, error) { + // {"match_phrase":{"name":{"query":"value","analyzer":"my_analyzer"}}} + source := make(map[string]interface{}) + + match := make(map[string]interface{}) + source["match_phrase"] = match + + query := make(map[string]interface{}) + match[q.name] = query + + query["query"] = q.value + + if q.analyzer != "" { + query["analyzer"] = q.analyzer + } + if q.slop != nil { + query["slop"] = *q.slop + } + if q.boost != nil { + query["boost"] = *q.boost + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_match_phrase_prefix.go b/vendor/github.com/olivere/elastic/v7/search_queries_match_phrase_prefix.go new file mode 100644 index 0000000000..fb366d31d7 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_match_phrase_prefix.go @@ -0,0 +1,89 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// MatchPhrasePrefixQuery is the same as match_phrase, except that it allows for +// prefix matches on the last term in the text. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-match-query-phrase-prefix.html +type MatchPhrasePrefixQuery struct { + name string + value interface{} + analyzer string + slop *int + maxExpansions *int + boost *float64 + queryName string +} + +// NewMatchPhrasePrefixQuery creates and initializes a new MatchPhrasePrefixQuery. +func NewMatchPhrasePrefixQuery(name string, value interface{}) *MatchPhrasePrefixQuery { + return &MatchPhrasePrefixQuery{name: name, value: value} +} + +// Analyzer explicitly sets the analyzer to use. It defaults to use explicit +// mapping config for the field, or, if not set, the default search analyzer. +func (q *MatchPhrasePrefixQuery) Analyzer(analyzer string) *MatchPhrasePrefixQuery { + q.analyzer = analyzer + return q +} + +// Slop sets the phrase slop if evaluated to a phrase query type. +func (q *MatchPhrasePrefixQuery) Slop(slop int) *MatchPhrasePrefixQuery { + q.slop = &slop + return q +} + +// MaxExpansions sets the number of term expansions to use. +func (q *MatchPhrasePrefixQuery) MaxExpansions(n int) *MatchPhrasePrefixQuery { + q.maxExpansions = &n + return q +} + +// Boost sets the boost to apply to this query. +func (q *MatchPhrasePrefixQuery) Boost(boost float64) *MatchPhrasePrefixQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *MatchPhrasePrefixQuery) QueryName(queryName string) *MatchPhrasePrefixQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *MatchPhrasePrefixQuery) Source() (interface{}, error) { + // {"match_phrase_prefix":{"name":{"query":"value","max_expansions":10}}} + source := make(map[string]interface{}) + + match := make(map[string]interface{}) + source["match_phrase_prefix"] = match + + query := make(map[string]interface{}) + match[q.name] = query + + query["query"] = q.value + + if q.analyzer != "" { + query["analyzer"] = q.analyzer + } + if q.slop != nil { + query["slop"] = *q.slop + } + if q.maxExpansions != nil { + query["max_expansions"] = *q.maxExpansions + } + if q.boost != nil { + query["boost"] = *q.boost + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_more_like_this.go b/vendor/github.com/olivere/elastic/v7/search_queries_more_like_this.go new file mode 100644 index 0000000000..fae8d46573 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_more_like_this.go @@ -0,0 +1,414 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// MoreLikeThis query (MLT Query) finds documents that are "like" a given +// set of documents. In order to do so, MLT selects a set of representative +// terms of these input documents, forms a query using these terms, executes +// the query and returns the results. The user controls the input documents, +// how the terms should be selected and how the query is formed. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-mlt-query.html +type MoreLikeThisQuery struct { + fields []string + docs []*MoreLikeThisQueryItem + unlikeDocs []*MoreLikeThisQueryItem + include *bool + minimumShouldMatch string + minTermFreq *int + maxQueryTerms *int + stopWords []string + minDocFreq *int + maxDocFreq *int + minWordLength *int + maxWordLength *int + boostTerms *float64 + boost *float64 + analyzer string + failOnUnsupportedField *bool + queryName string +} + +// NewMoreLikeThisQuery creates and initializes a new MoreLikeThisQuery. +func NewMoreLikeThisQuery() *MoreLikeThisQuery { + return &MoreLikeThisQuery{ + fields: make([]string, 0), + stopWords: make([]string, 0), + docs: make([]*MoreLikeThisQueryItem, 0), + unlikeDocs: make([]*MoreLikeThisQueryItem, 0), + } +} + +// Field adds one or more field names to the query. +func (q *MoreLikeThisQuery) Field(fields ...string) *MoreLikeThisQuery { + q.fields = append(q.fields, fields...) + return q +} + +// StopWord sets the stopwords. Any word in this set is considered +// "uninteresting" and ignored. Even if your Analyzer allows stopwords, +// you might want to tell the MoreLikeThis code to ignore them, as for +// the purposes of document similarity it seems reasonable to assume that +// "a stop word is never interesting". +func (q *MoreLikeThisQuery) StopWord(stopWords ...string) *MoreLikeThisQuery { + q.stopWords = append(q.stopWords, stopWords...) + return q +} + +// LikeText sets the text to use in order to find documents that are "like" this. +func (q *MoreLikeThisQuery) LikeText(likeTexts ...string) *MoreLikeThisQuery { + for _, s := range likeTexts { + item := NewMoreLikeThisQueryItem().LikeText(s) + q.docs = append(q.docs, item) + } + return q +} + +// LikeItems sets the documents to use in order to find documents that are "like" this. +func (q *MoreLikeThisQuery) LikeItems(docs ...*MoreLikeThisQueryItem) *MoreLikeThisQuery { + q.docs = append(q.docs, docs...) + return q +} + +// IgnoreLikeText sets the text from which the terms should not be selected from. +func (q *MoreLikeThisQuery) IgnoreLikeText(ignoreLikeText ...string) *MoreLikeThisQuery { + for _, s := range ignoreLikeText { + item := NewMoreLikeThisQueryItem().LikeText(s) + q.unlikeDocs = append(q.unlikeDocs, item) + } + return q +} + +// IgnoreLikeItems sets the documents from which the terms should not be selected from. +func (q *MoreLikeThisQuery) IgnoreLikeItems(ignoreDocs ...*MoreLikeThisQueryItem) *MoreLikeThisQuery { + q.unlikeDocs = append(q.unlikeDocs, ignoreDocs...) + return q +} + +// Ids sets the document ids to use in order to find documents that are "like" this. +func (q *MoreLikeThisQuery) Ids(ids ...string) *MoreLikeThisQuery { + for _, id := range ids { + item := NewMoreLikeThisQueryItem().Id(id) + q.docs = append(q.docs, item) + } + return q +} + +// Include specifies whether the input documents should also be included +// in the results returned. Defaults to false. +func (q *MoreLikeThisQuery) Include(include bool) *MoreLikeThisQuery { + q.include = &include + return q +} + +// MinimumShouldMatch sets the number of terms that must match the generated +// query expressed in the common syntax for minimum should match. +// The default value is "30%". +// +// This used to be "PercentTermsToMatch" in Elasticsearch versions before 2.0. +func (q *MoreLikeThisQuery) MinimumShouldMatch(minimumShouldMatch string) *MoreLikeThisQuery { + q.minimumShouldMatch = minimumShouldMatch + return q +} + +// MinTermFreq is the frequency below which terms will be ignored in the +// source doc. The default frequency is 2. +func (q *MoreLikeThisQuery) MinTermFreq(minTermFreq int) *MoreLikeThisQuery { + q.minTermFreq = &minTermFreq + return q +} + +// MaxQueryTerms sets the maximum number of query terms that will be included +// in any generated query. It defaults to 25. +func (q *MoreLikeThisQuery) MaxQueryTerms(maxQueryTerms int) *MoreLikeThisQuery { + q.maxQueryTerms = &maxQueryTerms + return q +} + +// MinDocFreq sets the frequency at which words will be ignored which do +// not occur in at least this many docs. The default is 5. +func (q *MoreLikeThisQuery) MinDocFreq(minDocFreq int) *MoreLikeThisQuery { + q.minDocFreq = &minDocFreq + return q +} + +// MaxDocFreq sets the maximum frequency for which words may still appear. +// Words that appear in more than this many docs will be ignored. +// It defaults to unbounded. +func (q *MoreLikeThisQuery) MaxDocFreq(maxDocFreq int) *MoreLikeThisQuery { + q.maxDocFreq = &maxDocFreq + return q +} + +// MinWordLength sets the minimum word length below which words will be +// ignored. It defaults to 0. +func (q *MoreLikeThisQuery) MinWordLength(minWordLength int) *MoreLikeThisQuery { + q.minWordLength = &minWordLength + return q +} + +// MaxWordLength sets the maximum word length above which words will be ignored. +// Defaults to unbounded (0). +func (q *MoreLikeThisQuery) MaxWordLength(maxWordLength int) *MoreLikeThisQuery { + q.maxWordLength = &maxWordLength + return q +} + +// BoostTerms sets the boost factor to use when boosting terms. +// It defaults to 1. +func (q *MoreLikeThisQuery) BoostTerms(boostTerms float64) *MoreLikeThisQuery { + q.boostTerms = &boostTerms + return q +} + +// Analyzer specifies the analyzer that will be use to analyze the text. +// Defaults to the analyzer associated with the field. +func (q *MoreLikeThisQuery) Analyzer(analyzer string) *MoreLikeThisQuery { + q.analyzer = analyzer + return q +} + +// Boost sets the boost for this query. +func (q *MoreLikeThisQuery) Boost(boost float64) *MoreLikeThisQuery { + q.boost = &boost + return q +} + +// FailOnUnsupportedField indicates whether to fail or return no result +// when this query is run against a field which is not supported such as +// a binary/numeric field. +func (q *MoreLikeThisQuery) FailOnUnsupportedField(fail bool) *MoreLikeThisQuery { + q.failOnUnsupportedField = &fail + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched_filters per hit. +func (q *MoreLikeThisQuery) QueryName(queryName string) *MoreLikeThisQuery { + q.queryName = queryName + return q +} + +// Source creates the source for the MLT query. +// It may return an error if the caller forgot to specify any documents to +// be "liked" in the MoreLikeThisQuery. +func (q *MoreLikeThisQuery) Source() (interface{}, error) { + // { + // "match_all" : { ... } + // } + if len(q.docs) == 0 { + return nil, errors.New(`more_like_this requires some documents to be "liked"`) + } + + source := make(map[string]interface{}) + + params := make(map[string]interface{}) + source["more_like_this"] = params + + if len(q.fields) > 0 { + params["fields"] = q.fields + } + + var likes []interface{} + for _, doc := range q.docs { + src, err := doc.Source() + if err != nil { + return nil, err + } + likes = append(likes, src) + } + params["like"] = likes + + if len(q.unlikeDocs) > 0 { + var dontLikes []interface{} + for _, doc := range q.unlikeDocs { + src, err := doc.Source() + if err != nil { + return nil, err + } + dontLikes = append(dontLikes, src) + } + params["unlike"] = dontLikes + } + + if q.minimumShouldMatch != "" { + params["minimum_should_match"] = q.minimumShouldMatch + } + if q.minTermFreq != nil { + params["min_term_freq"] = *q.minTermFreq + } + if q.maxQueryTerms != nil { + params["max_query_terms"] = *q.maxQueryTerms + } + if len(q.stopWords) > 0 { + params["stop_words"] = q.stopWords + } + if q.minDocFreq != nil { + params["min_doc_freq"] = *q.minDocFreq + } + if q.maxDocFreq != nil { + params["max_doc_freq"] = *q.maxDocFreq + } + if q.minWordLength != nil { + params["min_word_length"] = *q.minWordLength + } + if q.maxWordLength != nil { + params["max_word_length"] = *q.maxWordLength + } + if q.boostTerms != nil { + params["boost_terms"] = *q.boostTerms + } + if q.boost != nil { + params["boost"] = *q.boost + } + if q.analyzer != "" { + params["analyzer"] = q.analyzer + } + if q.failOnUnsupportedField != nil { + params["fail_on_unsupported_field"] = *q.failOnUnsupportedField + } + if q.queryName != "" { + params["_name"] = q.queryName + } + if q.include != nil { + params["include"] = *q.include + } + + return source, nil +} + +// -- MoreLikeThisQueryItem -- + +// MoreLikeThisQueryItem represents a single item of a MoreLikeThisQuery +// to be "liked" or "unliked". +type MoreLikeThisQueryItem struct { + likeText string + + index string + typ string + id string + doc interface{} + fields []string + routing string + fsc *FetchSourceContext + version int64 + versionType string +} + +// NewMoreLikeThisQueryItem creates and initializes a MoreLikeThisQueryItem. +func NewMoreLikeThisQueryItem() *MoreLikeThisQueryItem { + return &MoreLikeThisQueryItem{ + version: -1, + } +} + +// LikeText represents a text to be "liked". +func (item *MoreLikeThisQueryItem) LikeText(likeText string) *MoreLikeThisQueryItem { + item.likeText = likeText + return item +} + +// Index represents the index of the item. +func (item *MoreLikeThisQueryItem) Index(index string) *MoreLikeThisQueryItem { + item.index = index + return item +} + +// Type represents the document type of the item. +// +// Deprecated: Types are in the process of being removed. +func (item *MoreLikeThisQueryItem) Type(typ string) *MoreLikeThisQueryItem { + item.typ = typ + return item +} + +// Id represents the document id of the item. +func (item *MoreLikeThisQueryItem) Id(id string) *MoreLikeThisQueryItem { + item.id = id + return item +} + +// Doc represents a raw document template for the item. +func (item *MoreLikeThisQueryItem) Doc(doc interface{}) *MoreLikeThisQueryItem { + item.doc = doc + return item +} + +// Fields represents the list of fields of the item. +func (item *MoreLikeThisQueryItem) Fields(fields ...string) *MoreLikeThisQueryItem { + item.fields = append(item.fields, fields...) + return item +} + +// Routing sets the routing associated with the item. +func (item *MoreLikeThisQueryItem) Routing(routing string) *MoreLikeThisQueryItem { + item.routing = routing + return item +} + +// FetchSourceContext represents the fetch source of the item which controls +// if and how _source should be returned. +func (item *MoreLikeThisQueryItem) FetchSourceContext(fsc *FetchSourceContext) *MoreLikeThisQueryItem { + item.fsc = fsc + return item +} + +// Version specifies the version of the item. +func (item *MoreLikeThisQueryItem) Version(version int64) *MoreLikeThisQueryItem { + item.version = version + return item +} + +// VersionType represents the version type of the item. +func (item *MoreLikeThisQueryItem) VersionType(versionType string) *MoreLikeThisQueryItem { + item.versionType = versionType + return item +} + +// Source returns the JSON-serializable fragment of the entity. +func (item *MoreLikeThisQueryItem) Source() (interface{}, error) { + if item.likeText != "" { + return item.likeText, nil + } + + source := make(map[string]interface{}) + + if item.index != "" { + source["_index"] = item.index + } + if item.typ != "" { + source["_type"] = item.typ + } + if item.id != "" { + source["_id"] = item.id + } + if item.doc != nil { + source["doc"] = item.doc + } + if len(item.fields) > 0 { + source["fields"] = item.fields + } + if item.routing != "" { + source["_routing"] = item.routing + } + if item.fsc != nil { + src, err := item.fsc.Source() + if err != nil { + return nil, err + } + source["_source"] = src + } + if item.version >= 0 { + source["_version"] = item.version + } + if item.versionType != "" { + source["_version_type"] = item.versionType + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_multi_match.go b/vendor/github.com/olivere/elastic/v7/search_queries_multi_match.go new file mode 100644 index 0000000000..48c5f284fd --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_multi_match.go @@ -0,0 +1,278 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" + "strings" +) + +// MultiMatchQuery builds on the MatchQuery to allow multi-field queries. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-multi-match-query.html +type MultiMatchQuery struct { + text interface{} + fields []string + fieldBoosts map[string]*float64 + typ string // best_fields, boolean, most_fields, cross_fields, phrase, phrase_prefix + operator string // AND or OR + analyzer string + boost *float64 + slop *int + fuzziness string + prefixLength *int + maxExpansions *int + minimumShouldMatch string + rewrite string + fuzzyRewrite string + tieBreaker *float64 + lenient *bool + cutoffFrequency *float64 + zeroTermsQuery string + queryName string +} + +// MultiMatchQuery creates and initializes a new MultiMatchQuery. +func NewMultiMatchQuery(text interface{}, fields ...string) *MultiMatchQuery { + q := &MultiMatchQuery{ + text: text, + fields: make([]string, 0), + fieldBoosts: make(map[string]*float64), + } + q.fields = append(q.fields, fields...) + return q +} + +// Field adds a field to run the multi match against. +func (q *MultiMatchQuery) Field(field string) *MultiMatchQuery { + q.fields = append(q.fields, field) + return q +} + +// FieldWithBoost adds a field to run the multi match against with a specific boost. +func (q *MultiMatchQuery) FieldWithBoost(field string, boost float64) *MultiMatchQuery { + q.fields = append(q.fields, field) + q.fieldBoosts[field] = &boost + return q +} + +// Type can be "best_fields", "boolean", "most_fields", "cross_fields", +// "phrase", "phrase_prefix" or "bool_prefix" +func (q *MultiMatchQuery) Type(typ string) *MultiMatchQuery { + var zero = float64(0.0) + var one = float64(1.0) + + switch strings.ToLower(typ) { + default: // best_fields / boolean + q.typ = "best_fields" + q.tieBreaker = &zero + case "most_fields": + q.typ = "most_fields" + q.tieBreaker = &one + case "cross_fields": + q.typ = "cross_fields" + q.tieBreaker = &zero + case "phrase": + q.typ = "phrase" + q.tieBreaker = &zero + case "phrase_prefix": + q.typ = "phrase_prefix" + q.tieBreaker = &zero + case "bool_prefix": + q.typ = "bool_prefix" + q.tieBreaker = &zero + } + return q +} + +// Operator sets the operator to use when using boolean query. +// It can be either AND or OR (default). +func (q *MultiMatchQuery) Operator(operator string) *MultiMatchQuery { + q.operator = operator + return q +} + +// Analyzer sets the analyzer to use explicitly. It defaults to use explicit +// mapping config for the field, or, if not set, the default search analyzer. +func (q *MultiMatchQuery) Analyzer(analyzer string) *MultiMatchQuery { + q.analyzer = analyzer + return q +} + +// Boost sets the boost for this query. +func (q *MultiMatchQuery) Boost(boost float64) *MultiMatchQuery { + q.boost = &boost + return q +} + +// Slop sets the phrase slop if evaluated to a phrase query type. +func (q *MultiMatchQuery) Slop(slop int) *MultiMatchQuery { + q.slop = &slop + return q +} + +// Fuzziness sets the fuzziness used when evaluated to a fuzzy query type. +// It defaults to "AUTO". +func (q *MultiMatchQuery) Fuzziness(fuzziness string) *MultiMatchQuery { + q.fuzziness = fuzziness + return q +} + +// PrefixLength for the fuzzy process. +func (q *MultiMatchQuery) PrefixLength(prefixLength int) *MultiMatchQuery { + q.prefixLength = &prefixLength + return q +} + +// MaxExpansions is the number of term expansions to use when using fuzzy +// or prefix type query. It defaults to unbounded so it's recommended +// to set it to a reasonable value for faster execution. +func (q *MultiMatchQuery) MaxExpansions(maxExpansions int) *MultiMatchQuery { + q.maxExpansions = &maxExpansions + return q +} + +// MinimumShouldMatch represents the minimum number of optional should clauses +// to match. +func (q *MultiMatchQuery) MinimumShouldMatch(minimumShouldMatch string) *MultiMatchQuery { + q.minimumShouldMatch = minimumShouldMatch + return q +} + +func (q *MultiMatchQuery) Rewrite(rewrite string) *MultiMatchQuery { + q.rewrite = rewrite + return q +} + +func (q *MultiMatchQuery) FuzzyRewrite(fuzzyRewrite string) *MultiMatchQuery { + q.fuzzyRewrite = fuzzyRewrite + return q +} + +// TieBreaker for "best-match" disjunction queries (OR queries). +// The tie breaker capability allows documents that match more than one +// query clause (in this case on more than one field) to be scored better +// than documents that match only the best of the fields, without confusing +// this with the better case of two distinct matches in the multiple fields. +// +// A tie-breaker value of 1.0 is interpreted as a signal to score queries as +// "most-match" queries where all matching query clauses are considered for scoring. +func (q *MultiMatchQuery) TieBreaker(tieBreaker float64) *MultiMatchQuery { + q.tieBreaker = &tieBreaker + return q +} + +// Lenient indicates whether format based failures will be ignored. +func (q *MultiMatchQuery) Lenient(lenient bool) *MultiMatchQuery { + q.lenient = &lenient + return q +} + +// CutoffFrequency sets a cutoff value in [0..1] (or absolute number >=1) +// representing the maximum threshold of a terms document frequency to be +// considered a low frequency term. +func (q *MultiMatchQuery) CutoffFrequency(cutoff float64) *MultiMatchQuery { + q.cutoffFrequency = &cutoff + return q +} + +// ZeroTermsQuery can be "all" or "none". +func (q *MultiMatchQuery) ZeroTermsQuery(zeroTermsQuery string) *MultiMatchQuery { + q.zeroTermsQuery = zeroTermsQuery + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *MultiMatchQuery) QueryName(queryName string) *MultiMatchQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the query. +func (q *MultiMatchQuery) Source() (interface{}, error) { + // + // { + // "multi_match" : { + // "query" : "this is a test", + // "fields" : [ "subject", "message" ] + // } + // } + + source := make(map[string]interface{}) + + multiMatch := make(map[string]interface{}) + source["multi_match"] = multiMatch + + multiMatch["query"] = q.text + + if len(q.fields) > 0 { + var fields []string + for _, field := range q.fields { + if boost, found := q.fieldBoosts[field]; found { + if boost != nil { + fields = append(fields, fmt.Sprintf("%s^%f", field, *boost)) + } else { + fields = append(fields, field) + } + } else { + fields = append(fields, field) + } + } + multiMatch["fields"] = fields + } + + if q.typ != "" { + multiMatch["type"] = q.typ + } + + if q.operator != "" { + multiMatch["operator"] = q.operator + } + if q.analyzer != "" { + multiMatch["analyzer"] = q.analyzer + } + if q.boost != nil { + multiMatch["boost"] = *q.boost + } + if q.slop != nil { + multiMatch["slop"] = *q.slop + } + if q.fuzziness != "" { + multiMatch["fuzziness"] = q.fuzziness + } + if q.prefixLength != nil { + multiMatch["prefix_length"] = *q.prefixLength + } + if q.maxExpansions != nil { + multiMatch["max_expansions"] = *q.maxExpansions + } + if q.minimumShouldMatch != "" { + multiMatch["minimum_should_match"] = q.minimumShouldMatch + } + if q.rewrite != "" { + multiMatch["rewrite"] = q.rewrite + } + if q.fuzzyRewrite != "" { + multiMatch["fuzzy_rewrite"] = q.fuzzyRewrite + } + if q.tieBreaker != nil { + multiMatch["tie_breaker"] = *q.tieBreaker + } + if q.lenient != nil { + multiMatch["lenient"] = *q.lenient + } + if q.cutoffFrequency != nil { + multiMatch["cutoff_frequency"] = *q.cutoffFrequency + } + if q.zeroTermsQuery != "" { + multiMatch["zero_terms_query"] = q.zeroTermsQuery + } + if q.queryName != "" { + multiMatch["_name"] = q.queryName + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_nested.go b/vendor/github.com/olivere/elastic/v7/search_queries_nested.go new file mode 100644 index 0000000000..211649d018 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_nested.go @@ -0,0 +1,96 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// NestedQuery allows to query nested objects / docs. +// The query is executed against the nested objects / docs as if they were +// indexed as separate docs (they are, internally) and resulting in the +// root parent doc (or parent nested mapping). +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-nested-query.html +type NestedQuery struct { + query Query + path string + scoreMode string + boost *float64 + queryName string + innerHit *InnerHit + ignoreUnmapped *bool +} + +// NewNestedQuery creates and initializes a new NestedQuery. +func NewNestedQuery(path string, query Query) *NestedQuery { + return &NestedQuery{path: path, query: query} +} + +// ScoreMode specifies the score mode. +func (q *NestedQuery) ScoreMode(scoreMode string) *NestedQuery { + q.scoreMode = scoreMode + return q +} + +// Boost sets the boost for this query. +func (q *NestedQuery) Boost(boost float64) *NestedQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched_filters per hit +func (q *NestedQuery) QueryName(queryName string) *NestedQuery { + q.queryName = queryName + return q +} + +// InnerHit sets the inner hit definition in the scope of this nested query +// and reusing the defined path and query. +func (q *NestedQuery) InnerHit(innerHit *InnerHit) *NestedQuery { + q.innerHit = innerHit + return q +} + +// IgnoreUnmapped sets the ignore_unmapped option for the filter that ignores +// unmapped nested fields +func (q *NestedQuery) IgnoreUnmapped(value bool) *NestedQuery { + q.ignoreUnmapped = &value + return q +} + +// Source returns JSON for the query. +func (q *NestedQuery) Source() (interface{}, error) { + query := make(map[string]interface{}) + nq := make(map[string]interface{}) + query["nested"] = nq + + src, err := q.query.Source() + if err != nil { + return nil, err + } + nq["query"] = src + + nq["path"] = q.path + + if q.scoreMode != "" { + nq["score_mode"] = q.scoreMode + } + if q.boost != nil { + nq["boost"] = *q.boost + } + if q.queryName != "" { + nq["_name"] = q.queryName + } + if q.ignoreUnmapped != nil { + nq["ignore_unmapped"] = *q.ignoreUnmapped + } + if q.innerHit != nil { + src, err := q.innerHit.Source() + if err != nil { + return nil, err + } + nq["inner_hits"] = src + } + return query, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_parent_id.go b/vendor/github.com/olivere/elastic/v7/search_queries_parent_id.go new file mode 100644 index 0000000000..46a499915c --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_parent_id.go @@ -0,0 +1,99 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// ParentIdQuery can be used to find child documents which belong to a +// particular parent. Given the following mapping definition. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-parent-id-query.html +type ParentIdQuery struct { + typ string + id string + ignoreUnmapped *bool + boost *float64 + queryName string + innerHit *InnerHit +} + +// NewParentIdQuery creates and initializes a new parent_id query. +func NewParentIdQuery(typ, id string) *ParentIdQuery { + return &ParentIdQuery{ + typ: typ, + id: id, + } +} + +// Type sets the parent type. +func (q *ParentIdQuery) Type(typ string) *ParentIdQuery { + q.typ = typ + return q +} + +// Id sets the id. +func (q *ParentIdQuery) Id(id string) *ParentIdQuery { + q.id = id + return q +} + +// IgnoreUnmapped specifies whether unmapped types should be ignored. +// If set to false, the query failes when an unmapped type is found. +func (q *ParentIdQuery) IgnoreUnmapped(ignore bool) *ParentIdQuery { + q.ignoreUnmapped = &ignore + return q +} + +// Boost sets the boost for this query. +func (q *ParentIdQuery) Boost(boost float64) *ParentIdQuery { + q.boost = &boost + return q +} + +// QueryName specifies the query name for the filter that can be used when +// searching for matched filters per hit. +func (q *ParentIdQuery) QueryName(queryName string) *ParentIdQuery { + q.queryName = queryName + return q +} + +// InnerHit sets the inner hit definition in the scope of this query and +// reusing the defined type and query. +func (q *ParentIdQuery) InnerHit(innerHit *InnerHit) *ParentIdQuery { + q.innerHit = innerHit + return q +} + +// Source returns JSON for the parent_id query. +func (q *ParentIdQuery) Source() (interface{}, error) { + // { + // "parent_id" : { + // "type" : "blog", + // "id" : "1" + // } + // } + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["parent_id"] = query + + query["type"] = q.typ + query["id"] = q.id + if q.boost != nil { + query["boost"] = *q.boost + } + if q.ignoreUnmapped != nil { + query["ignore_unmapped"] = *q.ignoreUnmapped + } + if q.queryName != "" { + query["_name"] = q.queryName + } + if q.innerHit != nil { + src, err := q.innerHit.Source() + if err != nil { + return nil, err + } + query["inner_hits"] = src + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_percolator.go b/vendor/github.com/olivere/elastic/v7/search_queries_percolator.go new file mode 100644 index 0000000000..5a459d051f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_percolator.go @@ -0,0 +1,131 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// PercolatorQuery can be used to match queries stored in an index. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-percolate-query.html +type PercolatorQuery struct { + field string + name string + documentType string // deprecated + documents []interface{} + indexedDocumentIndex string + indexedDocumentType string + indexedDocumentId string + indexedDocumentRouting string + indexedDocumentPreference string + indexedDocumentVersion *int64 +} + +// NewPercolatorQuery creates and initializes a new Percolator query. +func NewPercolatorQuery() *PercolatorQuery { + return &PercolatorQuery{} +} + +func (q *PercolatorQuery) Field(field string) *PercolatorQuery { + q.field = field + return q +} + +// Name used for identification purposes in "_percolator_document_slot" response +// field when multiple percolate queries have been specified in the main query. +func (q *PercolatorQuery) Name(name string) *PercolatorQuery { + q.name = name + return q +} + +// Deprecated: DocumentType is deprecated as of 6.0. +func (q *PercolatorQuery) DocumentType(typ string) *PercolatorQuery { + q.documentType = typ + return q +} + +func (q *PercolatorQuery) Document(docs ...interface{}) *PercolatorQuery { + q.documents = append(q.documents, docs...) + return q +} + +func (q *PercolatorQuery) IndexedDocumentIndex(index string) *PercolatorQuery { + q.indexedDocumentIndex = index + return q +} + +func (q *PercolatorQuery) IndexedDocumentType(typ string) *PercolatorQuery { + q.indexedDocumentType = typ + return q +} + +func (q *PercolatorQuery) IndexedDocumentId(id string) *PercolatorQuery { + q.indexedDocumentId = id + return q +} + +func (q *PercolatorQuery) IndexedDocumentRouting(routing string) *PercolatorQuery { + q.indexedDocumentRouting = routing + return q +} + +func (q *PercolatorQuery) IndexedDocumentPreference(preference string) *PercolatorQuery { + q.indexedDocumentPreference = preference + return q +} + +func (q *PercolatorQuery) IndexedDocumentVersion(version int64) *PercolatorQuery { + q.indexedDocumentVersion = &version + return q +} + +// Source returns JSON for the percolate query. +func (q *PercolatorQuery) Source() (interface{}, error) { + if len(q.field) == 0 { + return nil, errors.New("elastic: Field is required in PercolatorQuery") + } + + // { + // "percolate" : { ... } + // } + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["percolate"] = params + params["field"] = q.field + if q.documentType != "" { + params["document_type"] = q.documentType + } + if q.name != "" { + params["name"] = q.name + } + + switch len(q.documents) { + case 0: + case 1: + params["document"] = q.documents[0] + default: + params["documents"] = q.documents + } + + if s := q.indexedDocumentIndex; s != "" { + params["index"] = s + } + if s := q.indexedDocumentType; s != "" { + params["type"] = s + } + if s := q.indexedDocumentId; s != "" { + params["id"] = s + } + if s := q.indexedDocumentRouting; s != "" { + params["routing"] = s + } + if s := q.indexedDocumentPreference; s != "" { + params["preference"] = s + } + if v := q.indexedDocumentVersion; v != nil { + params["version"] = *v + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_prefix.go b/vendor/github.com/olivere/elastic/v7/search_queries_prefix.go new file mode 100644 index 0000000000..8178aac3f4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_prefix.go @@ -0,0 +1,67 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// PrefixQuery matches documents that have fields containing terms +// with a specified prefix (not analyzed). +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-prefix-query.html +type PrefixQuery struct { + name string + prefix string + boost *float64 + rewrite string + queryName string +} + +// NewPrefixQuery creates and initializes a new PrefixQuery. +func NewPrefixQuery(name string, prefix string) *PrefixQuery { + return &PrefixQuery{name: name, prefix: prefix} +} + +// Boost sets the boost for this query. +func (q *PrefixQuery) Boost(boost float64) *PrefixQuery { + q.boost = &boost + return q +} + +func (q *PrefixQuery) Rewrite(rewrite string) *PrefixQuery { + q.rewrite = rewrite + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched_filters per hit. +func (q *PrefixQuery) QueryName(queryName string) *PrefixQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the query. +func (q *PrefixQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["prefix"] = query + + if q.boost == nil && q.rewrite == "" && q.queryName == "" { + query[q.name] = q.prefix + } else { + subQuery := make(map[string]interface{}) + subQuery["value"] = q.prefix + if q.boost != nil { + subQuery["boost"] = *q.boost + } + if q.rewrite != "" { + subQuery["rewrite"] = q.rewrite + } + if q.queryName != "" { + subQuery["_name"] = q.queryName + } + query[q.name] = subQuery + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_query_string.go b/vendor/github.com/olivere/elastic/v7/search_queries_query_string.go new file mode 100644 index 0000000000..f9baa21d7a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_query_string.go @@ -0,0 +1,350 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" +) + +// QueryStringQuery uses the query parser in order to parse its content. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-query-string-query.html +type QueryStringQuery struct { + queryString string + defaultField string + defaultOperator string + analyzer string + quoteAnalyzer string + quoteFieldSuffix string + allowLeadingWildcard *bool + lowercaseExpandedTerms *bool // Deprecated: Decision is now made by the analyzer + enablePositionIncrements *bool + analyzeWildcard *bool + locale string // Deprecated: Decision is now made by the analyzer + boost *float64 + fuzziness string + fuzzyPrefixLength *int + fuzzyMaxExpansions *int + fuzzyRewrite string + phraseSlop *int + fields []string + fieldBoosts map[string]*float64 + tieBreaker *float64 + rewrite string + minimumShouldMatch string + lenient *bool + queryName string + timeZone string + maxDeterminizedStates *int + escape *bool + typ string +} + +// NewQueryStringQuery creates and initializes a new QueryStringQuery. +func NewQueryStringQuery(queryString string) *QueryStringQuery { + return &QueryStringQuery{ + queryString: queryString, + fields: make([]string, 0), + fieldBoosts: make(map[string]*float64), + } +} + +// DefaultField specifies the field to run against when no prefix field +// is specified. Only relevant when not explicitly adding fields the query +// string will run against. +func (q *QueryStringQuery) DefaultField(defaultField string) *QueryStringQuery { + q.defaultField = defaultField + return q +} + +// Field adds a field to run the query string against. +func (q *QueryStringQuery) Field(field string) *QueryStringQuery { + q.fields = append(q.fields, field) + return q +} + +// Type sets how multiple fields should be combined to build textual part queries, +// e.g. "best_fields". +func (q *QueryStringQuery) Type(typ string) *QueryStringQuery { + q.typ = typ + return q +} + +// FieldWithBoost adds a field to run the query string against with a specific boost. +func (q *QueryStringQuery) FieldWithBoost(field string, boost float64) *QueryStringQuery { + q.fields = append(q.fields, field) + q.fieldBoosts[field] = &boost + return q +} + +// TieBreaker is used when more than one field is used with the query string, +// and combined queries are using dismax. +func (q *QueryStringQuery) TieBreaker(tieBreaker float64) *QueryStringQuery { + q.tieBreaker = &tieBreaker + return q +} + +// DefaultOperator sets the boolean operator of the query parser used to +// parse the query string. +// +// In default mode (OR) terms without any modifiers +// are considered optional, e.g. "capital of Hungary" is equal to +// "capital OR of OR Hungary". +// +// In AND mode, terms are considered to be in conjunction. The above mentioned +// query is then parsed as "capital AND of AND Hungary". +func (q *QueryStringQuery) DefaultOperator(operator string) *QueryStringQuery { + q.defaultOperator = operator + return q +} + +// Analyzer is an optional analyzer used to analyze the query string. +// Note, if a field has search analyzer defined for it, then it will be used +// automatically. Defaults to the smart search analyzer. +func (q *QueryStringQuery) Analyzer(analyzer string) *QueryStringQuery { + q.analyzer = analyzer + return q +} + +// QuoteAnalyzer is an optional analyzer to be used to analyze the query string +// for phrase searches. Note, if a field has search analyzer defined for it, +// then it will be used automatically. Defaults to the smart search analyzer. +func (q *QueryStringQuery) QuoteAnalyzer(quoteAnalyzer string) *QueryStringQuery { + q.quoteAnalyzer = quoteAnalyzer + return q +} + +// MaxDeterminizedState protects against too-difficult regular expression queries. +func (q *QueryStringQuery) MaxDeterminizedState(maxDeterminizedStates int) *QueryStringQuery { + q.maxDeterminizedStates = &maxDeterminizedStates + return q +} + +// AllowLeadingWildcard specifies whether leading wildcards should be allowed +// or not (defaults to true). +func (q *QueryStringQuery) AllowLeadingWildcard(allowLeadingWildcard bool) *QueryStringQuery { + q.allowLeadingWildcard = &allowLeadingWildcard + return q +} + +// LowercaseExpandedTerms indicates whether terms of wildcard, prefix, fuzzy +// and range queries are automatically lower-cased or not. Default is true. +// +// Deprecated: Decision is now made by the analyzer. +func (q *QueryStringQuery) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *QueryStringQuery { + q.lowercaseExpandedTerms = &lowercaseExpandedTerms + return q +} + +// EnablePositionIncrements indicates whether to enable position increments +// in result query. Defaults to true. +// +// When set, result phrase and multi-phrase queries will be aware of position +// increments. Useful when e.g. a StopFilter increases the position increment +// of the token that follows an omitted token. +func (q *QueryStringQuery) EnablePositionIncrements(enablePositionIncrements bool) *QueryStringQuery { + q.enablePositionIncrements = &enablePositionIncrements + return q +} + +// Fuzziness sets the edit distance for fuzzy queries. Default is "AUTO". +func (q *QueryStringQuery) Fuzziness(fuzziness string) *QueryStringQuery { + q.fuzziness = fuzziness + return q +} + +// FuzzyPrefixLength sets the minimum prefix length for fuzzy queries. +// Default is 1. +func (q *QueryStringQuery) FuzzyPrefixLength(fuzzyPrefixLength int) *QueryStringQuery { + q.fuzzyPrefixLength = &fuzzyPrefixLength + return q +} + +func (q *QueryStringQuery) FuzzyMaxExpansions(fuzzyMaxExpansions int) *QueryStringQuery { + q.fuzzyMaxExpansions = &fuzzyMaxExpansions + return q +} + +func (q *QueryStringQuery) FuzzyRewrite(fuzzyRewrite string) *QueryStringQuery { + q.fuzzyRewrite = fuzzyRewrite + return q +} + +// PhraseSlop sets the default slop for phrases. If zero, then exact matches +// are required. Default value is zero. +func (q *QueryStringQuery) PhraseSlop(phraseSlop int) *QueryStringQuery { + q.phraseSlop = &phraseSlop + return q +} + +// AnalyzeWildcard indicates whether to enabled analysis on wildcard and prefix queries. +func (q *QueryStringQuery) AnalyzeWildcard(analyzeWildcard bool) *QueryStringQuery { + q.analyzeWildcard = &analyzeWildcard + return q +} + +func (q *QueryStringQuery) Rewrite(rewrite string) *QueryStringQuery { + q.rewrite = rewrite + return q +} + +func (q *QueryStringQuery) MinimumShouldMatch(minimumShouldMatch string) *QueryStringQuery { + q.minimumShouldMatch = minimumShouldMatch + return q +} + +// Boost sets the boost for this query. +func (q *QueryStringQuery) Boost(boost float64) *QueryStringQuery { + q.boost = &boost + return q +} + +// QuoteFieldSuffix is an optional field name suffix to automatically +// try and add to the field searched when using quoted text. +func (q *QueryStringQuery) QuoteFieldSuffix(quoteFieldSuffix string) *QueryStringQuery { + q.quoteFieldSuffix = quoteFieldSuffix + return q +} + +// Lenient indicates whether the query string parser should be lenient +// when parsing field values. It defaults to the index setting and if not +// set, defaults to false. +func (q *QueryStringQuery) Lenient(lenient bool) *QueryStringQuery { + q.lenient = &lenient + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched_filters per hit. +func (q *QueryStringQuery) QueryName(queryName string) *QueryStringQuery { + q.queryName = queryName + return q +} + +// Locale specifies the locale to be used for string conversions. +// +// Deprecated: Decision is now made by the analyzer. +func (q *QueryStringQuery) Locale(locale string) *QueryStringQuery { + q.locale = locale + return q +} + +// TimeZone can be used to automatically adjust to/from fields using a +// timezone. Only used with date fields, of course. +func (q *QueryStringQuery) TimeZone(timeZone string) *QueryStringQuery { + q.timeZone = timeZone + return q +} + +// Escape performs escaping of the query string. +func (q *QueryStringQuery) Escape(escape bool) *QueryStringQuery { + q.escape = &escape + return q +} + +// Source returns JSON for the query. +func (q *QueryStringQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["query_string"] = query + + query["query"] = q.queryString + + if q.defaultField != "" { + query["default_field"] = q.defaultField + } + + if len(q.fields) > 0 { + var fields []string + for _, field := range q.fields { + if boost, found := q.fieldBoosts[field]; found { + if boost != nil { + fields = append(fields, fmt.Sprintf("%s^%f", field, *boost)) + } else { + fields = append(fields, field) + } + } else { + fields = append(fields, field) + } + } + query["fields"] = fields + } + + if q.tieBreaker != nil { + query["tie_breaker"] = *q.tieBreaker + } + if q.defaultOperator != "" { + query["default_operator"] = q.defaultOperator + } + if q.analyzer != "" { + query["analyzer"] = q.analyzer + } + if q.quoteAnalyzer != "" { + query["quote_analyzer"] = q.quoteAnalyzer + } + if q.maxDeterminizedStates != nil { + query["max_determinized_states"] = *q.maxDeterminizedStates + } + if q.allowLeadingWildcard != nil { + query["allow_leading_wildcard"] = *q.allowLeadingWildcard + } + if q.lowercaseExpandedTerms != nil { + query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms + } + if q.enablePositionIncrements != nil { + query["enable_position_increments"] = *q.enablePositionIncrements + } + if q.fuzziness != "" { + query["fuzziness"] = q.fuzziness + } + if q.boost != nil { + query["boost"] = *q.boost + } + if q.fuzzyPrefixLength != nil { + query["fuzzy_prefix_length"] = *q.fuzzyPrefixLength + } + if q.fuzzyMaxExpansions != nil { + query["fuzzy_max_expansions"] = *q.fuzzyMaxExpansions + } + if q.fuzzyRewrite != "" { + query["fuzzy_rewrite"] = q.fuzzyRewrite + } + if q.phraseSlop != nil { + query["phrase_slop"] = *q.phraseSlop + } + if q.analyzeWildcard != nil { + query["analyze_wildcard"] = *q.analyzeWildcard + } + if q.rewrite != "" { + query["rewrite"] = q.rewrite + } + if q.minimumShouldMatch != "" { + query["minimum_should_match"] = q.minimumShouldMatch + } + if q.quoteFieldSuffix != "" { + query["quote_field_suffix"] = q.quoteFieldSuffix + } + if q.lenient != nil { + query["lenient"] = *q.lenient + } + if q.queryName != "" { + query["_name"] = q.queryName + } + if q.locale != "" { + query["locale"] = q.locale + } + if q.timeZone != "" { + query["time_zone"] = q.timeZone + } + if q.escape != nil { + query["escape"] = *q.escape + } + if q.typ != "" { + query["type"] = q.typ + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_range.go b/vendor/github.com/olivere/elastic/v7/search_queries_range.go new file mode 100644 index 0000000000..e3c07308b5 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_range.go @@ -0,0 +1,155 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// RangeQuery matches documents with fields that have terms within a certain range. +// +// For details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-range-query.html +type RangeQuery struct { + name string + from interface{} + to interface{} + timeZone string + includeLower bool + includeUpper bool + boost *float64 + queryName string + format string + relation string +} + +// NewRangeQuery creates and initializes a new RangeQuery. +func NewRangeQuery(name string) *RangeQuery { + return &RangeQuery{name: name, includeLower: true, includeUpper: true} +} + +// From indicates the from part of the RangeQuery. +// Use nil to indicate an unbounded from part. +func (q *RangeQuery) From(from interface{}) *RangeQuery { + q.from = from + return q +} + +// Gt indicates a greater-than value for the from part. +// Use nil to indicate an unbounded from part. +func (q *RangeQuery) Gt(from interface{}) *RangeQuery { + q.from = from + q.includeLower = false + return q +} + +// Gte indicates a greater-than-or-equal value for the from part. +// Use nil to indicate an unbounded from part. +func (q *RangeQuery) Gte(from interface{}) *RangeQuery { + q.from = from + q.includeLower = true + return q +} + +// To indicates the to part of the RangeQuery. +// Use nil to indicate an unbounded to part. +func (q *RangeQuery) To(to interface{}) *RangeQuery { + q.to = to + return q +} + +// Lt indicates a less-than value for the to part. +// Use nil to indicate an unbounded to part. +func (q *RangeQuery) Lt(to interface{}) *RangeQuery { + q.to = to + q.includeUpper = false + return q +} + +// Lte indicates a less-than-or-equal value for the to part. +// Use nil to indicate an unbounded to part. +func (q *RangeQuery) Lte(to interface{}) *RangeQuery { + q.to = to + q.includeUpper = true + return q +} + +// IncludeLower indicates whether the lower bound should be included or not. +// Defaults to true. +func (q *RangeQuery) IncludeLower(includeLower bool) *RangeQuery { + q.includeLower = includeLower + return q +} + +// IncludeUpper indicates whether the upper bound should be included or not. +// Defaults to true. +func (q *RangeQuery) IncludeUpper(includeUpper bool) *RangeQuery { + q.includeUpper = includeUpper + return q +} + +// Boost sets the boost for this query. +func (q *RangeQuery) Boost(boost float64) *RangeQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched_filters per hit. +func (q *RangeQuery) QueryName(queryName string) *RangeQuery { + q.queryName = queryName + return q +} + +// TimeZone is used for date fields. In that case, we can adjust the +// from/to fields using a timezone. +func (q *RangeQuery) TimeZone(timeZone string) *RangeQuery { + q.timeZone = timeZone + return q +} + +// Format is used for date fields. In that case, we can set the format +// to be used instead of the mapper format. +func (q *RangeQuery) Format(format string) *RangeQuery { + q.format = format + return q +} + +// Relation is used for range fields. which can be one of +// "within", "contains", "intersects" (default) and "disjoint". +func (q *RangeQuery) Relation(relation string) *RangeQuery { + q.relation = relation + return q +} + +// Source returns JSON for the query. +func (q *RangeQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + + rangeQ := make(map[string]interface{}) + source["range"] = rangeQ + + params := make(map[string]interface{}) + rangeQ[q.name] = params + + params["from"] = q.from + params["to"] = q.to + if q.timeZone != "" { + params["time_zone"] = q.timeZone + } + if q.format != "" { + params["format"] = q.format + } + if q.relation != "" { + params["relation"] = q.relation + } + if q.boost != nil { + params["boost"] = *q.boost + } + params["include_lower"] = q.includeLower + params["include_upper"] = q.includeUpper + + if q.queryName != "" { + rangeQ["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_raw_string.go b/vendor/github.com/olivere/elastic/v7/search_queries_raw_string.go new file mode 100644 index 0000000000..3f9685c41a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_raw_string.go @@ -0,0 +1,26 @@ +// Copyright 2012-present Oliver Eilhard, John Stanford. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "encoding/json" + +// RawStringQuery can be used to treat a string representation of an ES query +// as a Query. Example usage: +// q := RawStringQuery("{\"match_all\":{}}") +// db.Search().Query(q).From(1).Size(100).Do() +type RawStringQuery string + +// NewRawStringQuery ininitializes a new RawStringQuery. +// It is the same as RawStringQuery(q). +func NewRawStringQuery(q string) RawStringQuery { + return RawStringQuery(q) +} + +// Source returns the JSON encoded body +func (q RawStringQuery) Source() (interface{}, error) { + var f interface{} + err := json.Unmarshal([]byte(q), &f) + return f, err +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_regexp.go b/vendor/github.com/olivere/elastic/v7/search_queries_regexp.go new file mode 100644 index 0000000000..25a0c3d028 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_regexp.go @@ -0,0 +1,82 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// RegexpQuery allows you to use regular expression term queries. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-regexp-query.html +type RegexpQuery struct { + name string + regexp string + flags string + boost *float64 + rewrite string + queryName string + maxDeterminizedStates *int +} + +// NewRegexpQuery creates and initializes a new RegexpQuery. +func NewRegexpQuery(name string, regexp string) *RegexpQuery { + return &RegexpQuery{name: name, regexp: regexp} +} + +// Flags sets the regexp flags. +func (q *RegexpQuery) Flags(flags string) *RegexpQuery { + q.flags = flags + return q +} + +// MaxDeterminizedStates protects against complex regular expressions. +func (q *RegexpQuery) MaxDeterminizedStates(maxDeterminizedStates int) *RegexpQuery { + q.maxDeterminizedStates = &maxDeterminizedStates + return q +} + +// Boost sets the boost for this query. +func (q *RegexpQuery) Boost(boost float64) *RegexpQuery { + q.boost = &boost + return q +} + +func (q *RegexpQuery) Rewrite(rewrite string) *RegexpQuery { + q.rewrite = rewrite + return q +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched_filters per hit +func (q *RegexpQuery) QueryName(queryName string) *RegexpQuery { + q.queryName = queryName + return q +} + +// Source returns the JSON-serializable query data. +func (q *RegexpQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["regexp"] = query + + x := make(map[string]interface{}) + x["value"] = q.regexp + if q.flags != "" { + x["flags"] = q.flags + } + if q.maxDeterminizedStates != nil { + x["max_determinized_states"] = *q.maxDeterminizedStates + } + if q.boost != nil { + x["boost"] = *q.boost + } + if q.rewrite != "" { + x["rewrite"] = q.rewrite + } + if q.queryName != "" { + x["name"] = q.queryName + } + query[q.name] = x + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_script.go b/vendor/github.com/olivere/elastic/v7/search_queries_script.go new file mode 100644 index 0000000000..d99b0c0533 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_script.go @@ -0,0 +1,51 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// ScriptQuery allows to define scripts as filters. +// +// For details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-script-query.html +type ScriptQuery struct { + script *Script + queryName string +} + +// NewScriptQuery creates and initializes a new ScriptQuery. +func NewScriptQuery(script *Script) *ScriptQuery { + return &ScriptQuery{ + script: script, + } +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched_filters per hit +func (q *ScriptQuery) QueryName(queryName string) *ScriptQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the query. +func (q *ScriptQuery) Source() (interface{}, error) { + if q.script == nil { + return nil, errors.New("ScriptQuery expected a script") + } + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["script"] = params + + src, err := q.script.Source() + if err != nil { + return nil, err + } + params["script"] = src + + if q.queryName != "" { + params["_name"] = q.queryName + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_script_score.go b/vendor/github.com/olivere/elastic/v7/search_queries_script_score.go new file mode 100644 index 0000000000..7f00e0fe35 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_script_score.go @@ -0,0 +1,110 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// ScriptScoreQuery uses a script to provide a custom score for returned documents. +// +// A ScriptScoreQuery query is useful if, for example, a scoring function is +// expensive and you only need to calculate the score of a filtered set of documents. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-script-score-query.html +type ScriptScoreQuery struct { + query Query + script *Script + minScore *float64 + boost *float64 + queryName string +} + +// NewScriptScoreQuery creates and initializes a new script_score query. +func NewScriptScoreQuery(query Query, script *Script) *ScriptScoreQuery { + return &ScriptScoreQuery{ + query: query, + script: script, + } +} + +// Query to be used in the ScriptScoreQuery. +func (q *ScriptScoreQuery) Query(query Query) *ScriptScoreQuery { + q.query = query + return q +} + +// Script to calculate the score. +func (q *ScriptScoreQuery) Script(script *Script) *ScriptScoreQuery { + q.script = script + return q +} + +// MinScore sets the minimum score. +func (q *ScriptScoreQuery) MinScore(minScore float64) *ScriptScoreQuery { + q.minScore = &minScore + return q +} + +// Boost sets the boost for this query. +func (q *ScriptScoreQuery) Boost(boost float64) *ScriptScoreQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter. +func (q *ScriptScoreQuery) QueryName(queryName string) *ScriptScoreQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the function score query. +func (q *ScriptScoreQuery) Source() (interface{}, error) { + // { + // "script_score" : { + // "query" : { + // "match" : { "message": "elasticsearch" } + // }, + // "script" : { + // "source" : "doc['likes'].value / 10" + // } + // } + // } + + source := make(map[string]interface{}) + query := make(map[string]interface{}) + source["script_score"] = query + + if q.query == nil { + return nil, errors.New("ScriptScoreQuery: Query is missing") + } + if q.script == nil { + return nil, errors.New("ScriptScoreQuery: Script is missing") + } + + if src, err := q.query.Source(); err != nil { + return nil, err + } else { + query["query"] = src + } + + if src, err := q.script.Source(); err != nil { + return nil, err + } else { + query["script"] = src + } + + if v := q.minScore; v != nil { + query["min_score"] = *v + } + + if v := q.boost; v != nil { + query["boost"] = *v + } + if q.queryName != "" { + query["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_simple_query_string.go b/vendor/github.com/olivere/elastic/v7/search_queries_simple_query_string.go new file mode 100644 index 0000000000..230e041e61 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_simple_query_string.go @@ -0,0 +1,246 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" + "strings" +) + +// SimpleQueryStringQuery is a query that uses the SimpleQueryParser +// to parse its context. Unlike the regular query_string query, +// the simple_query_string query will never throw an exception, +// and discards invalid parts of the query. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-simple-query-string-query.html +type SimpleQueryStringQuery struct { + queryText string + analyzer string + quoteFieldSuffix string + defaultOperator string + fields []string + fieldBoosts map[string]*float64 + minimumShouldMatch string + flags string + boost *float64 + lowercaseExpandedTerms *bool // deprecated + lenient *bool + analyzeWildcard *bool + locale string // deprecated + queryName string + autoGenerateSynonymsPhraseQuery *bool + fuzzyPrefixLength int + fuzzyMaxExpansions int + fuzzyTranspositions *bool +} + +// NewSimpleQueryStringQuery creates and initializes a new SimpleQueryStringQuery. +func NewSimpleQueryStringQuery(text string) *SimpleQueryStringQuery { + return &SimpleQueryStringQuery{ + queryText: text, + fields: make([]string, 0), + fieldBoosts: make(map[string]*float64), + fuzzyPrefixLength: -1, + fuzzyMaxExpansions: -1, + } +} + +// Field adds a field to run the query against. +func (q *SimpleQueryStringQuery) Field(field string) *SimpleQueryStringQuery { + q.fields = append(q.fields, field) + return q +} + +// Field adds a field to run the query against with a specific boost. +func (q *SimpleQueryStringQuery) FieldWithBoost(field string, boost float64) *SimpleQueryStringQuery { + q.fields = append(q.fields, field) + q.fieldBoosts[field] = &boost + return q +} + +// Boost sets the boost for this query. +func (q *SimpleQueryStringQuery) Boost(boost float64) *SimpleQueryStringQuery { + q.boost = &boost + return q +} + +// QuoteFieldSuffix is an optional field name suffix to automatically +// try and add to the field searched when using quoted text. +func (q *SimpleQueryStringQuery) QuoteFieldSuffix(quoteFieldSuffix string) *SimpleQueryStringQuery { + q.quoteFieldSuffix = quoteFieldSuffix + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched_filters per hit. +func (q *SimpleQueryStringQuery) QueryName(queryName string) *SimpleQueryStringQuery { + q.queryName = queryName + return q +} + +// Analyzer specifies the analyzer to use for the query. +func (q *SimpleQueryStringQuery) Analyzer(analyzer string) *SimpleQueryStringQuery { + q.analyzer = analyzer + return q +} + +// DefaultOperator specifies the default operator for the query. +func (q *SimpleQueryStringQuery) DefaultOperator(defaultOperator string) *SimpleQueryStringQuery { + q.defaultOperator = defaultOperator + return q +} + +// Flags sets the flags for the query. +func (q *SimpleQueryStringQuery) Flags(flags string) *SimpleQueryStringQuery { + q.flags = flags + return q +} + +// LowercaseExpandedTerms indicates whether terms of wildcard, prefix, fuzzy +// and range queries are automatically lower-cased or not. Default is true. +// +// Deprecated: Decision is now made by the analyzer. +func (q *SimpleQueryStringQuery) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *SimpleQueryStringQuery { + q.lowercaseExpandedTerms = &lowercaseExpandedTerms + return q +} + +// Locale to be used in the query. +// +// Deprecated: Decision is now made by the analyzer. +func (q *SimpleQueryStringQuery) Locale(locale string) *SimpleQueryStringQuery { + q.locale = locale + return q +} + +// Lenient indicates whether the query string parser should be lenient +// when parsing field values. It defaults to the index setting and if not +// set, defaults to false. +func (q *SimpleQueryStringQuery) Lenient(lenient bool) *SimpleQueryStringQuery { + q.lenient = &lenient + return q +} + +// AnalyzeWildcard indicates whether to enabled analysis on wildcard and prefix queries. +func (q *SimpleQueryStringQuery) AnalyzeWildcard(analyzeWildcard bool) *SimpleQueryStringQuery { + q.analyzeWildcard = &analyzeWildcard + return q +} + +// MinimumShouldMatch specifies the minimumShouldMatch to apply to the +// resulting query should that be a Boolean query. +func (q *SimpleQueryStringQuery) MinimumShouldMatch(minimumShouldMatch string) *SimpleQueryStringQuery { + q.minimumShouldMatch = minimumShouldMatch + return q +} + +// AutoGenerateSynonymsPhraseQuery indicates whether phrase queries should be +// automatically generated for multi terms synonyms. Defaults to true. +func (q *SimpleQueryStringQuery) AutoGenerateSynonymsPhraseQuery(enable bool) *SimpleQueryStringQuery { + q.autoGenerateSynonymsPhraseQuery = &enable + return q +} + +// FuzzyPrefixLength defines the prefix length in fuzzy queries. +func (q *SimpleQueryStringQuery) FuzzyPrefixLength(fuzzyPrefixLength int) *SimpleQueryStringQuery { + q.fuzzyPrefixLength = fuzzyPrefixLength + return q +} + +// FuzzyMaxExpansions defines the number of terms fuzzy queries will expand to. +func (q *SimpleQueryStringQuery) FuzzyMaxExpansions(fuzzyMaxExpansions int) *SimpleQueryStringQuery { + q.fuzzyMaxExpansions = fuzzyMaxExpansions + return q +} + +// FuzzyTranspositions defines whether to use transpositions in fuzzy queries. +func (q *SimpleQueryStringQuery) FuzzyTranspositions(fuzzyTranspositions bool) *SimpleQueryStringQuery { + q.fuzzyTranspositions = &fuzzyTranspositions + return q +} + +// Source returns JSON for the query. +func (q *SimpleQueryStringQuery) Source() (interface{}, error) { + // { + // "simple_query_string" : { + // "query" : "\"fried eggs\" +(eggplant | potato) -frittata", + // "analyzer" : "snowball", + // "fields" : ["body^5","_all"], + // "default_operator" : "and" + // } + // } + + source := make(map[string]interface{}) + + query := make(map[string]interface{}) + source["simple_query_string"] = query + + query["query"] = q.queryText + + if len(q.fields) > 0 { + var fields []string + for _, field := range q.fields { + if boost, found := q.fieldBoosts[field]; found { + if boost != nil { + fields = append(fields, fmt.Sprintf("%s^%f", field, *boost)) + } else { + fields = append(fields, field) + } + } else { + fields = append(fields, field) + } + } + query["fields"] = fields + } + + if q.flags != "" { + query["flags"] = q.flags + } + if q.analyzer != "" { + query["analyzer"] = q.analyzer + } + if q.defaultOperator != "" { + query["default_operator"] = strings.ToLower(q.defaultOperator) + } + if q.lowercaseExpandedTerms != nil { + query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms + } + if q.lenient != nil { + query["lenient"] = *q.lenient + } + if q.analyzeWildcard != nil { + query["analyze_wildcard"] = *q.analyzeWildcard + } + if q.locale != "" { + query["locale"] = q.locale + } + if q.queryName != "" { + query["_name"] = q.queryName + } + if q.minimumShouldMatch != "" { + query["minimum_should_match"] = q.minimumShouldMatch + } + if q.quoteFieldSuffix != "" { + query["quote_field_suffix"] = q.quoteFieldSuffix + } + if q.boost != nil { + query["boost"] = *q.boost + } + if v := q.autoGenerateSynonymsPhraseQuery; v != nil { + query["auto_generate_synonyms_phrase_query"] = *v + } + if v := q.fuzzyPrefixLength; v != -1 { + query["fuzzy_prefix_length"] = v + } + if v := q.fuzzyMaxExpansions; v != -1 { + query["fuzzy_max_expansions"] = v + } + if v := q.fuzzyTranspositions; v != nil { + query["fuzzy_transpositions"] = *v + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_slice.go b/vendor/github.com/olivere/elastic/v7/search_queries_slice.go new file mode 100644 index 0000000000..0eac78436d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_slice.go @@ -0,0 +1,53 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// SliceQuery allows to partition the documents into several slices. +// It is used e.g. to slice scroll operations in Elasticsearch 5.0 or later. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-scroll.html#sliced-scroll +// for details. +type SliceQuery struct { + field string + id *int + max *int +} + +// NewSliceQuery creates a new SliceQuery. +func NewSliceQuery() *SliceQuery { + return &SliceQuery{} +} + +// Field is the name of the field to slice against (_uid by default). +func (s *SliceQuery) Field(field string) *SliceQuery { + s.field = field + return s +} + +// Id is the id of the slice. +func (s *SliceQuery) Id(id int) *SliceQuery { + s.id = &id + return s +} + +// Max is the maximum number of slices. +func (s *SliceQuery) Max(max int) *SliceQuery { + s.max = &max + return s +} + +// Source returns the JSON body. +func (s *SliceQuery) Source() (interface{}, error) { + m := make(map[string]interface{}) + if s.field != "" { + m["field"] = s.field + } + if s.id != nil { + m["id"] = *s.id + } + if s.max != nil { + m["max"] = *s.max + } + return m, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_term.go b/vendor/github.com/olivere/elastic/v7/search_queries_term.go new file mode 100644 index 0000000000..832e67e5e2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_term.go @@ -0,0 +1,58 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TermQuery finds documents that contain the exact term specified +// in the inverted index. +// +// For details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-term-query.html +type TermQuery struct { + name string + value interface{} + boost *float64 + queryName string +} + +// NewTermQuery creates and initializes a new TermQuery. +func NewTermQuery(name string, value interface{}) *TermQuery { + return &TermQuery{name: name, value: value} +} + +// Boost sets the boost for this query. +func (q *TermQuery) Boost(boost float64) *TermQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched_filters per hit +func (q *TermQuery) QueryName(queryName string) *TermQuery { + q.queryName = queryName + return q +} + +// Source returns JSON for the query. +func (q *TermQuery) Source() (interface{}, error) { + // {"term":{"name":"value"}} + source := make(map[string]interface{}) + tq := make(map[string]interface{}) + source["term"] = tq + + if q.boost == nil && q.queryName == "" { + tq[q.name] = q.value + } else { + subQ := make(map[string]interface{}) + subQ["value"] = q.value + if q.boost != nil { + subQ["boost"] = *q.boost + } + if q.queryName != "" { + subQ["_name"] = q.queryName + } + tq[q.name] = subQ + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_terms.go b/vendor/github.com/olivere/elastic/v7/search_queries_terms.go new file mode 100644 index 0000000000..b8171cdb85 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_terms.go @@ -0,0 +1,75 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TermsQuery filters documents that have fields that match any +// of the provided terms (not analyzed). +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-terms-query.html +type TermsQuery struct { + name string + values []interface{} + termsLookup *TermsLookup + queryName string + boost *float64 +} + +// NewTermsQuery creates and initializes a new TermsQuery. +func NewTermsQuery(name string, values ...interface{}) *TermsQuery { + q := &TermsQuery{ + name: name, + values: make([]interface{}, 0), + } + if len(values) > 0 { + q.values = append(q.values, values...) + } + return q +} + +// TermsLookup adds terms lookup details to the query. +func (q *TermsQuery) TermsLookup(lookup *TermsLookup) *TermsQuery { + q.termsLookup = lookup + return q +} + +// Boost sets the boost for this query. +func (q *TermsQuery) Boost(boost float64) *TermsQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched_filters per hit +func (q *TermsQuery) QueryName(queryName string) *TermsQuery { + q.queryName = queryName + return q +} + +// Creates the query source for the term query. +func (q *TermsQuery) Source() (interface{}, error) { + // {"terms":{"name":["value1","value2"]}} + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["terms"] = params + + if q.termsLookup != nil { + src, err := q.termsLookup.Source() + if err != nil { + return nil, err + } + params[q.name] = src + } else { + params[q.name] = q.values + if q.boost != nil { + params["boost"] = *q.boost + } + if q.queryName != "" { + params["_name"] = q.queryName + } + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_terms_set.go b/vendor/github.com/olivere/elastic/v7/search_queries_terms_set.go new file mode 100644 index 0000000000..6a33a2a346 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_terms_set.go @@ -0,0 +1,97 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TermsSetQuery returns any documents that match with at least +// one or more of the provided terms. The terms are not analyzed +// and thus must match exactly. The number of terms that must +// match varies per document and is either controlled by a +// minimum should match field or computed per document in a +// minimum should match script. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-terms-set-query.html +type TermsSetQuery struct { + name string + values []interface{} + minimumShouldMatchField string + minimumShouldMatchScript *Script + queryName string + boost *float64 +} + +// NewTermsSetQuery creates and initializes a new TermsSetQuery. +func NewTermsSetQuery(name string, values ...interface{}) *TermsSetQuery { + q := &TermsSetQuery{ + name: name, + values: make([]interface{}, 0), + } + if len(values) > 0 { + q.values = append(q.values, values...) + } + return q +} + +// MinimumShouldMatchField specifies the field to match. +func (q *TermsSetQuery) MinimumShouldMatchField(minimumShouldMatchField string) *TermsSetQuery { + q.minimumShouldMatchField = minimumShouldMatchField + return q +} + +// MinimumShouldMatchScript specifies the script to match. +func (q *TermsSetQuery) MinimumShouldMatchScript(minimumShouldMatchScript *Script) *TermsSetQuery { + q.minimumShouldMatchScript = minimumShouldMatchScript + return q +} + +// Boost sets the boost for this query. +func (q *TermsSetQuery) Boost(boost float64) *TermsSetQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used +// when searching for matched_filters per hit +func (q *TermsSetQuery) QueryName(queryName string) *TermsSetQuery { + q.queryName = queryName + return q +} + +// Source creates the query source for the term query. +func (q *TermsSetQuery) Source() (interface{}, error) { + // {"terms_set":{"codes":{"terms":["abc","def"],"minimum_should_match_field":"required_matches"}}} + source := make(map[string]interface{}) + inner := make(map[string]interface{}) + params := make(map[string]interface{}) + inner[q.name] = params + source["terms_set"] = inner + + // terms + params["terms"] = q.values + + // minimum_should_match_field + if match := q.minimumShouldMatchField; match != "" { + params["minimum_should_match_field"] = match + } + + // minimum_should_match_script + if match := q.minimumShouldMatchScript; match != nil { + src, err := match.Source() + if err != nil { + return nil, err + } + params["minimum_should_match_script"] = src + } + + // Common parameters for all queries + if q.boost != nil { + params["boost"] = *q.boost + } + if q.queryName != "" { + params["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_type.go b/vendor/github.com/olivere/elastic/v7/search_queries_type.go new file mode 100644 index 0000000000..61867c1e38 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_type.go @@ -0,0 +1,26 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TypeQuery filters documents matching the provided document / mapping type. +// +// For details, see: +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-type-query.html +type TypeQuery struct { + typ string +} + +func NewTypeQuery(typ string) *TypeQuery { + return &TypeQuery{typ: typ} +} + +// Source returns JSON for the query. +func (q *TypeQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + params := make(map[string]interface{}) + source["type"] = params + params["value"] = q.typ + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_wildcard.go b/vendor/github.com/olivere/elastic/v7/search_queries_wildcard.go new file mode 100644 index 0000000000..33e4ea2aab --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_wildcard.go @@ -0,0 +1,81 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// WildcardQuery matches documents that have fields matching a wildcard +// expression (not analyzed). Supported wildcards are *, which matches +// any character sequence (including the empty one), and ?, which matches +// any single character. Note this query can be slow, as it needs to iterate +// over many terms. In order to prevent extremely slow wildcard queries, +// a wildcard term should not start with one of the wildcards * or ?. +// The wildcard query maps to Lucene WildcardQuery. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-wildcard-query.html +type WildcardQuery struct { + name string + wildcard string + boost *float64 + rewrite string + queryName string +} + +// NewWildcardQuery creates and initializes a new WildcardQuery. +func NewWildcardQuery(name, wildcard string) *WildcardQuery { + return &WildcardQuery{ + name: name, + wildcard: wildcard, + } +} + +// Boost sets the boost for this query. +func (q *WildcardQuery) Boost(boost float64) *WildcardQuery { + q.boost = &boost + return q +} + +func (q *WildcardQuery) Rewrite(rewrite string) *WildcardQuery { + q.rewrite = rewrite + return q +} + +// QueryName sets the name of this query. +func (q *WildcardQuery) QueryName(queryName string) *WildcardQuery { + q.queryName = queryName + return q +} + +// Source returns the JSON serializable body of this query. +func (q *WildcardQuery) Source() (interface{}, error) { + // { + // "wildcard" : { + // "user" : { + // "wildcard" : "ki*y", + // "boost" : 1.0 + // } + // } + + source := make(map[string]interface{}) + + query := make(map[string]interface{}) + source["wildcard"] = query + + wq := make(map[string]interface{}) + query[q.name] = wq + + wq["wildcard"] = q.wildcard + + if q.boost != nil { + wq["boost"] = *q.boost + } + if q.rewrite != "" { + wq["rewrite"] = q.rewrite + } + if q.queryName != "" { + wq["_name"] = q.queryName + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_queries_wrapper.go b/vendor/github.com/olivere/elastic/v7/search_queries_wrapper.go new file mode 100644 index 0000000000..2a32d1e0ad --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_queries_wrapper.go @@ -0,0 +1,28 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// WrapperQuery accepts any other query as base64 encoded string. +// +// For details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-wrapper-query.html. +type WrapperQuery struct { + source string +} + +// NewWrapperQuery creates and initializes a new WrapperQuery. +func NewWrapperQuery(source string) *WrapperQuery { + return &WrapperQuery{source: source} +} + +// Source returns JSON for the query. +func (q *WrapperQuery) Source() (interface{}, error) { + // {"wrapper":{"query":"..."}} + source := make(map[string]interface{}) + tq := make(map[string]interface{}) + source["wrapper"] = tq + tq["query"] = q.source + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_request.go b/vendor/github.com/olivere/elastic/v7/search_request.go new file mode 100644 index 0000000000..3a444b808e --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_request.go @@ -0,0 +1,586 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "encoding/json" + "strings" +) + +// SearchRequest combines a search request and its +// query details (see SearchSource). +// It is used in combination with MultiSearch. +type SearchRequest struct { + searchType string + indices []string + types []string + routing *string + preference *string + requestCache *bool + allowPartialSearchResults *bool + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + scroll string + source interface{} + searchSource *SearchSource + batchedReduceSize *int + maxConcurrentShardRequests *int + preFilterShardSize *int +} + +// NewSearchRequest creates a new search request. +func NewSearchRequest() *SearchRequest { + return &SearchRequest{ + searchSource: NewSearchSource(), + } +} + +// SearchType must be one of "dfs_query_then_fetch", "dfs_query_and_fetch", +// "query_then_fetch", or "query_and_fetch". +func (r *SearchRequest) SearchType(searchType string) *SearchRequest { + r.searchType = searchType + return r +} + +// SearchTypeDfsQueryThenFetch sets search type to "dfs_query_then_fetch". +func (r *SearchRequest) SearchTypeDfsQueryThenFetch() *SearchRequest { + return r.SearchType("dfs_query_then_fetch") +} + +// SearchTypeQueryThenFetch sets search type to "query_then_fetch". +func (r *SearchRequest) SearchTypeQueryThenFetch() *SearchRequest { + return r.SearchType("query_then_fetch") +} + +// Index specifies the indices to use in the request. +func (r *SearchRequest) Index(indices ...string) *SearchRequest { + r.indices = append(r.indices, indices...) + return r +} + +// HasIndices returns true if there are indices used, false otherwise. +func (r *SearchRequest) HasIndices() bool { + return len(r.indices) > 0 +} + +// Type specifies one or more types to be used. +// +// Deprecated: Types are in the process of being removed. Instead of using a type, prefer to +// filter on a field on the document. +func (r *SearchRequest) Type(types ...string) *SearchRequest { + r.types = append(r.types, types...) + return r +} + +// Routing specifies the routing parameter. It is a comma-separated list. +func (r *SearchRequest) Routing(routing string) *SearchRequest { + r.routing = &routing + return r +} + +// Routings to be used in the request. +func (r *SearchRequest) Routings(routings ...string) *SearchRequest { + if routings != nil { + routings := strings.Join(routings, ",") + r.routing = &routings + } else { + r.routing = nil + } + return r +} + +// Preference to execute the search. Defaults to randomize across shards. +// Can be set to "_local" to prefer local shards, "_primary" to execute +// only on primary shards, or a custom value, which guarantees that the +// same order will be used across different requests. +func (r *SearchRequest) Preference(preference string) *SearchRequest { + r.preference = &preference + return r +} + +// RequestCache specifies if this request should use the request cache +// or not, assuming that it can. By default, will default to the index +// level setting if request cache is enabled or not. +func (r *SearchRequest) RequestCache(requestCache bool) *SearchRequest { + r.requestCache = &requestCache + return r +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *SearchRequest) IgnoreUnavailable(ignoreUnavailable bool) *SearchRequest { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified). +func (s *SearchRequest) AllowNoIndices(allowNoIndices bool) *SearchRequest { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *SearchRequest) ExpandWildcards(expandWildcards string) *SearchRequest { + s.expandWildcards = expandWildcards + return s +} + +// Scroll, if set, will enable scrolling of the search request. +// Pass a timeout value, e.g. "2m" or "30s" as a value. +func (r *SearchRequest) Scroll(scroll string) *SearchRequest { + r.scroll = scroll + return r +} + +// SearchSource allows passing your own SearchSource, overriding +// all values set on the request (except Source). +func (r *SearchRequest) SearchSource(searchSource *SearchSource) *SearchRequest { + if searchSource == nil { + r.searchSource = NewSearchSource() + return r + } + r.searchSource = searchSource + return r +} + +// Source allows passing your own request body. It will have preference over +// all other properties set on the request. +func (r *SearchRequest) Source(source interface{}) *SearchRequest { + r.source = source + return r +} + +// Timeout value for the request, e.g. "30s" or "2m". +func (r *SearchRequest) Timeout(timeout string) *SearchRequest { + r.searchSource = r.searchSource.Timeout(timeout) + return r +} + +// TerminateAfter, when set, specifies an optional document count, +// upon collecting which the search query will terminate early. +func (r *SearchRequest) TerminateAfter(docs int) *SearchRequest { + r.searchSource = r.searchSource.TerminateAfter(docs) + return r +} + +// Query for the search. +func (r *SearchRequest) Query(query Query) *SearchRequest { + r.searchSource = r.searchSource.Query(query) + return r +} + +// PostFilter is a filter that will be executed after the query +// has been executed and only has affect on the search hits +// (not aggregations). This filter is always executed as last +// filtering mechanism. +func (r *SearchRequest) PostFilter(filter Query) *SearchRequest { + r.searchSource = r.searchSource.PostFilter(filter) + return r +} + +// MinScore below which documents are filtered out. +func (r *SearchRequest) MinScore(minScore float64) *SearchRequest { + r.searchSource = r.searchSource.MinScore(minScore) + return r +} + +// From index to start search from (default is 0). +func (r *SearchRequest) From(from int) *SearchRequest { + r.searchSource = r.searchSource.From(from) + return r +} + +// Size is the number of search hits to return (default is 10). +func (r *SearchRequest) Size(size int) *SearchRequest { + r.searchSource = r.searchSource.Size(size) + return r +} + +// Explain indicates whether to return an explanation for each hit. +func (r *SearchRequest) Explain(explain bool) *SearchRequest { + r.searchSource = r.searchSource.Explain(explain) + return r +} + +// Version indicates whether each hit should be returned with +// its version. +func (r *SearchRequest) Version(version bool) *SearchRequest { + r.searchSource = r.searchSource.Version(version) + return r +} + +// IndexBoost sets a boost a specific index will receive when +// the query is executed against it. +func (r *SearchRequest) IndexBoost(index string, boost float64) *SearchRequest { + r.searchSource = r.searchSource.IndexBoost(index, boost) + return r +} + +// Stats groups that this request will be aggregated under. +func (r *SearchRequest) Stats(statsGroup ...string) *SearchRequest { + r.searchSource = r.searchSource.Stats(statsGroup...) + return r +} + +// FetchSource indicates whether the response should contain the stored +// _source for every hit. +func (r *SearchRequest) FetchSource(fetchSource bool) *SearchRequest { + r.searchSource = r.searchSource.FetchSource(fetchSource) + return r +} + +// FetchSourceIncludeExclude specifies that _source should be returned +// with each hit, where "include" and "exclude" serve as a simple wildcard +// matcher that gets applied to its fields +// (e.g. include := []string{"obj1.*","obj2.*"}, exclude := []string{"description.*"}). +func (r *SearchRequest) FetchSourceIncludeExclude(include, exclude []string) *SearchRequest { + r.searchSource = r.searchSource.FetchSourceIncludeExclude(include, exclude) + return r +} + +// FetchSourceContext indicates how the _source should be fetched. +func (r *SearchRequest) FetchSourceContext(fsc *FetchSourceContext) *SearchRequest { + r.searchSource = r.searchSource.FetchSourceContext(fsc) + return r +} + +// DocValueField adds a docvalue based field to load and return. +// The field does not have to be stored, but it's recommended to use +// non analyzed or numeric fields. +func (r *SearchRequest) DocValueField(field string) *SearchRequest { + r.searchSource = r.searchSource.DocvalueField(field) + return r +} + +// DocValueFieldWithFormat adds a docvalue based field to load and return. +// The field does not have to be stored, but it's recommended to use +// non analyzed or numeric fields. +func (r *SearchRequest) DocValueFieldWithFormat(field DocvalueField) *SearchRequest { + r.searchSource = r.searchSource.DocvalueFieldWithFormat(field) + return r +} + +// DocValueFields adds one or more docvalue based field to load and return. +// The fields do not have to be stored, but it's recommended to use +// non analyzed or numeric fields. +func (r *SearchRequest) DocValueFields(fields ...string) *SearchRequest { + r.searchSource = r.searchSource.DocvalueFields(fields...) + return r +} + +// DocValueFieldsWithFormat adds one or more docvalue based field to load and return. +// The fields do not have to be stored, but it's recommended to use +// non analyzed or numeric fields. +func (r *SearchRequest) DocValueFieldsWithFormat(fields ...DocvalueField) *SearchRequest { + r.searchSource = r.searchSource.DocvalueFieldsWithFormat(fields...) + return r +} + +// StoredField adds a stored field to load and return +// (note, it must be stored) as part of the search request. +func (r *SearchRequest) StoredField(field string) *SearchRequest { + r.searchSource = r.searchSource.StoredField(field) + return r +} + +// NoStoredFields indicates that no fields should be loaded, +// resulting in only id and type to be returned per field. +func (r *SearchRequest) NoStoredFields() *SearchRequest { + r.searchSource = r.searchSource.NoStoredFields() + return r +} + +// StoredFields adds one or more stored field to load and return +// (note, they must be stored) as part of the search request. +func (r *SearchRequest) StoredFields(fields ...string) *SearchRequest { + r.searchSource = r.searchSource.StoredFields(fields...) + return r +} + +// ScriptField adds a script based field to load and return. +// The field does not have to be stored, but it's recommended +// to use non analyzed or numeric fields. +func (r *SearchRequest) ScriptField(field *ScriptField) *SearchRequest { + r.searchSource = r.searchSource.ScriptField(field) + return r +} + +// ScriptFields adds one or more script based field to load and return. +// The fields do not have to be stored, but it's recommended +// to use non analyzed or numeric fields. +func (r *SearchRequest) ScriptFields(fields ...*ScriptField) *SearchRequest { + r.searchSource = r.searchSource.ScriptFields(fields...) + return r +} + +// Sort adds a sort order. +func (r *SearchRequest) Sort(field string, ascending bool) *SearchRequest { + r.searchSource = r.searchSource.Sort(field, ascending) + return r +} + +// SortWithInfo adds a sort order. +func (r *SearchRequest) SortWithInfo(info SortInfo) *SearchRequest { + r.searchSource = r.searchSource.SortWithInfo(info) + return r +} + +// SortBy adds a sort order. +func (r *SearchRequest) SortBy(sorter ...Sorter) *SearchRequest { + r.searchSource = r.searchSource.SortBy(sorter...) + return r +} + +// SearchAfter sets the sort values that indicates which docs this +// request should "search after". +func (r *SearchRequest) SearchAfter(sortValues ...interface{}) *SearchRequest { + r.searchSource = r.searchSource.SearchAfter(sortValues...) + return r +} + +// Slice allows partitioning the documents in multiple slices. +// It is e.g. used to slice a scroll operation, supported in +// Elasticsearch 5.0 or later. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-scroll.html#sliced-scroll +// for details. +func (r *SearchRequest) Slice(sliceQuery Query) *SearchRequest { + r.searchSource = r.searchSource.Slice(sliceQuery) + return r +} + +// TrackScores is applied when sorting and controls if scores will be +// tracked as well. Defaults to false. +func (r *SearchRequest) TrackScores(trackScores bool) *SearchRequest { + r.searchSource = r.searchSource.TrackScores(trackScores) + return r +} + +// TrackTotalHits indicates if the total hit count for the query should be tracked. +// Defaults to true. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-track-total-hits.html +// for details. +func (r *SearchRequest) TrackTotalHits(trackTotalHits interface{}) *SearchRequest { + r.searchSource = r.searchSource.TrackTotalHits(trackTotalHits) + return r +} + +// Aggregation adds an aggreation to perform as part of the search. +func (r *SearchRequest) Aggregation(name string, aggregation Aggregation) *SearchRequest { + r.searchSource = r.searchSource.Aggregation(name, aggregation) + return r +} + +// Highlight adds highlighting to the search. +func (r *SearchRequest) Highlight(highlight *Highlight) *SearchRequest { + r.searchSource = r.searchSource.Highlight(highlight) + return r +} + +// Suggester adds a suggester to the search. +func (r *SearchRequest) Suggester(suggester Suggester) *SearchRequest { + r.searchSource = r.searchSource.Suggester(suggester) + return r +} + +// Rescorer adds a rescorer to the search. +func (r *SearchRequest) Rescorer(rescore *Rescore) *SearchRequest { + r.searchSource = r.searchSource.Rescorer(rescore) + return r +} + +// ClearRescorers removes all rescorers from the search. +func (r *SearchRequest) ClearRescorers() *SearchRequest { + r.searchSource = r.searchSource.ClearRescorers() + return r +} + +// Profile specifies that this search source should activate the +// Profile API for queries made on it. +func (r *SearchRequest) Profile(profile bool) *SearchRequest { + r.searchSource = r.searchSource.Profile(profile) + return r +} + +// Collapse adds field collapsing. +func (r *SearchRequest) Collapse(collapse *CollapseBuilder) *SearchRequest { + r.searchSource = r.searchSource.Collapse(collapse) + return r +} + +// AllowPartialSearchResults indicates if this request should allow partial +// results. (If method is not called, will default to the cluster level +// setting). +func (r *SearchRequest) AllowPartialSearchResults(allow bool) *SearchRequest { + r.allowPartialSearchResults = &allow + return r +} + +// BatchedReduceSize specifies the number of shard results that should be +// reduced at once on the coordinating node. This value should be used +// as a protection mechanism to reduce the memory overhead per search request +// if the potential number of shards in the request can be large. +func (r *SearchRequest) BatchedReduceSize(size int) *SearchRequest { + r.batchedReduceSize = &size + return r +} + +// MaxConcurrentShardRequests sets the number of shard requests that should +// be executed concurrently. This value should be used as a protection +// mechanism to reduce the number of shard requests fired per high level +// search request. Searches that hit the entire cluster can be throttled +// with this number to reduce the cluster load. The default grows with +// the number of nodes in the cluster but is at most 256. +func (r *SearchRequest) MaxConcurrentShardRequests(size int) *SearchRequest { + r.maxConcurrentShardRequests = &size + return r +} + +// PreFilterShardSize sets a threshold that enforces a pre-filter roundtrip +// to pre-filter search shards based on query rewriting if the number of +// shards the search request expands to exceeds the threshold. +// This filter roundtrip can limit the number of shards significantly if for +// instance a shard can not match any documents based on it's rewrite +// method ie. if date filters are mandatory to match but the shard +// bounds and the query are disjoint. The default is 128. +func (r *SearchRequest) PreFilterShardSize(size int) *SearchRequest { + r.preFilterShardSize = &size + return r +} + +// header is used e.g. by MultiSearch to get information about the search header +// of one SearchRequest. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-multi-search.html +func (r *SearchRequest) header() interface{} { + h := make(map[string]interface{}) + if r.searchType != "" { + h["search_type"] = r.searchType + } + + switch len(r.indices) { + case 0: + case 1: + h["index"] = r.indices[0] + default: + h["indices"] = r.indices + } + + switch len(r.types) { + case 0: + case 1: + h["type"] = r.types[0] + default: + h["types"] = r.types + } + + if r.routing != nil && *r.routing != "" { + h["routing"] = *r.routing + } + if r.preference != nil && *r.preference != "" { + h["preference"] = *r.preference + } + if r.requestCache != nil { + h["request_cache"] = *r.requestCache + } + if r.ignoreUnavailable != nil { + h["ignore_unavailable"] = *r.ignoreUnavailable + } + if r.allowNoIndices != nil { + h["allow_no_indices"] = *r.allowNoIndices + } + if r.expandWildcards != "" { + h["expand_wildcards"] = r.expandWildcards + } + if v := r.allowPartialSearchResults; v != nil { + h["allow_partial_search_results"] = *v + } + if r.scroll != "" { + h["scroll"] = r.scroll + } + + return h +} + +// Body allows to access the search body of the request, as generated by the DSL. +// Notice that Body is read-only. You must not change the request body. +// +// Body is used e.g. by MultiSearch to get information about the search body +// of one SearchRequest. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-multi-search.html +func (r *SearchRequest) Body() (string, error) { + if r.source == nil { + // Default: No custom source specified + src, err := r.searchSource.Source() + if err != nil { + return "", err + } + body, err := json.Marshal(src) + if err != nil { + return "", err + } + return string(body), nil + } + switch t := r.source.(type) { + default: + body, err := json.Marshal(r.source) + if err != nil { + return "", err + } + return string(body), nil + case *SearchSource: + src, err := t.Source() + if err != nil { + return "", err + } + body, err := json.Marshal(src) + if err != nil { + return "", err + } + return string(body), nil + case json.RawMessage: + return string(t), nil + case *json.RawMessage: + return string(*t), nil + case string: + return t, nil + case *string: + if t != nil { + return *t, nil + } + return "{}", nil + } +} + +// source returns the search source. It is used by Reindex. +func (r *SearchRequest) sourceAsMap() (interface{}, error) { + if r.source == nil { + // Default: No custom source specified + return r.searchSource.Source() + } + switch t := r.source.(type) { + default: + body, err := json.Marshal(r.source) + if err != nil { + return "", err + } + return RawStringQuery(body), nil + case *SearchSource: + return t.Source() + case json.RawMessage: + return RawStringQuery(string(t)), nil + case *json.RawMessage: + return RawStringQuery(string(*t)), nil + case string: + return RawStringQuery(t), nil + case *string: + if t != nil { + return RawStringQuery(*t), nil + } + return RawStringQuery("{}"), nil + } +} diff --git a/vendor/github.com/olivere/elastic/v7/search_shards.go b/vendor/github.com/olivere/elastic/v7/search_shards.go new file mode 100644 index 0000000000..6e566ac3f4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_shards.go @@ -0,0 +1,258 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SearchShardsService returns the indices and shards that a search request would be executed against. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-shards.html +type SearchShardsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + routing string + local *bool + preference string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string +} + +// NewSearchShardsService creates a new SearchShardsService. +func NewSearchShardsService(client *Client) *SearchShardsService { + return &SearchShardsService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SearchShardsService) Pretty(pretty bool) *SearchShardsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SearchShardsService) Human(human bool) *SearchShardsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SearchShardsService) ErrorTrace(errorTrace bool) *SearchShardsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SearchShardsService) FilterPath(filterPath ...string) *SearchShardsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SearchShardsService) Header(name string, value string) *SearchShardsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SearchShardsService) Headers(headers http.Header) *SearchShardsService { + s.headers = headers + return s +} + +// Index sets the names of the indices to restrict the results. +func (s *SearchShardsService) Index(index ...string) *SearchShardsService { + s.index = append(s.index, index...) + return s +} + +//A boolean value whether to read the cluster state locally in order to +//determine where shards are allocated instead of using the Master node’s cluster state. +func (s *SearchShardsService) Local(local bool) *SearchShardsService { + s.local = &local + return s +} + +// Routing sets a specific routing value. +func (s *SearchShardsService) Routing(routing string) *SearchShardsService { + s.routing = routing + return s +} + +// Preference specifies the node or shard the operation should be performed on (default: random). +func (s *SearchShardsService) Preference(preference string) *SearchShardsService { + s.preference = preference + return s +} + +// IgnoreUnavailable indicates whether the specified concrete indices +// should be ignored when unavailable (missing or closed). +func (s *SearchShardsService) IgnoreUnavailable(ignoreUnavailable bool) *SearchShardsService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` string +// or when no indices have been specified). +func (s *SearchShardsService) AllowNoIndices(allowNoIndices bool) *SearchShardsService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *SearchShardsService) ExpandWildcards(expandWildcards string) *SearchShardsService { + s.expandWildcards = expandWildcards + return s +} + +// buildURL builds the URL for the operation. +func (s *SearchShardsService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/{index}/_search_shards", map[string]string{ + "index": strings.Join(s.index, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if s.local != nil { + params.Set("local", fmt.Sprintf("%v", *s.local)) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if s.ignoreUnavailable != nil { + params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SearchShardsService) Validate() error { + var invalid []string + if len(s.index) < 1 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SearchShardsService) Do(ctx context.Context) (*SearchShardsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SearchShardsResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SearchShardsResponse is the response of SearchShardsService.Do. +type SearchShardsResponse struct { + Nodes map[string]interface{} `json:"nodes"` + Indices map[string]interface{} `json:"indices"` + Shards [][]*SearchShardsResponseShardsInfo `json:"shards"` +} + +type SearchShardsResponseShardsInfo struct { + Index string `json:"index"` + Node string `json:"node"` + Primary bool `json:"primary"` + Shard uint `json:"shard"` + State string `json:"state"` + AllocationId *AllocationId `json:"allocation_id,omitempty"` + RelocatingNode string `json:"relocating_node"` + ExpectedShardSizeInBytes int64 `json:"expected_shard_size_in_bytes,omitempty"` + RecoverySource *RecoverySource `json:"recovery_source,omitempty"` + UnassignedInfo *UnassignedInfo `json:"unassigned_info,omitempty"` +} + +type RecoverySource struct { + Type string `json:"type"` + // TODO add missing fields here based on the Type +} + +type AllocationId struct { + Id string `json:"id"` + RelocationId string `json:"relocation_id,omitempty"` +} + +type UnassignedInfo struct { + Reason string `json:"reason"` + At *time.Time `json:"at,omitempty"` + FailedAttempts int `json:"failed_attempts,omitempty"` + Delayed bool `json:"delayed"` + Details string `json:"details,omitempty"` + AllocationStatus string `json:"allocation_status"` +} diff --git a/vendor/github.com/olivere/elastic/v7/search_source.go b/vendor/github.com/olivere/elastic/v7/search_source.go new file mode 100644 index 0000000000..724fc1c831 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_source.go @@ -0,0 +1,592 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "fmt" +) + +// SearchSource enables users to build the search source. +// It resembles the SearchSourceBuilder in Elasticsearch. +type SearchSource struct { + query Query // query + postQuery Query // post_filter + sliceQuery Query // slice + from int // from + size int // size + explain *bool // explain + version *bool // version + seqNoAndPrimaryTerm *bool // seq_no_primary_term + sorters []Sorter // sort + trackScores *bool // track_scores + trackTotalHits interface{} // track_total_hits + searchAfterSortValues []interface{} // search_after + minScore *float64 // min_score + timeout string // timeout + terminateAfter *int // terminate_after + storedFieldNames []string // stored_fields + docvalueFields DocvalueFields // docvalue_fields + scriptFields []*ScriptField // script_fields + fetchSourceContext *FetchSourceContext // _source + aggregations map[string]Aggregation // aggregations / aggs + highlight *Highlight // highlight + globalSuggestText string + suggesters []Suggester // suggest + rescores []*Rescore // rescore + defaultRescoreWindowSize *int + indexBoosts map[string]float64 // indices_boost + stats []string // stats + innerHits map[string]*InnerHit + collapse *CollapseBuilder // collapse + profile bool // profile + // TODO extBuilders []SearchExtBuilder // ext +} + +// NewSearchSource initializes a new SearchSource. +func NewSearchSource() *SearchSource { + return &SearchSource{ + from: -1, + size: -1, + aggregations: make(map[string]Aggregation), + indexBoosts: make(map[string]float64), + innerHits: make(map[string]*InnerHit), + } +} + +// Query sets the query to use with this search source. +func (s *SearchSource) Query(query Query) *SearchSource { + s.query = query + return s +} + +// Profile specifies that this search source should activate the +// Profile API for queries made on it. +func (s *SearchSource) Profile(profile bool) *SearchSource { + s.profile = profile + return s +} + +// PostFilter will be executed after the query has been executed and +// only affects the search hits, not the aggregations. +// This filter is always executed as the last filtering mechanism. +func (s *SearchSource) PostFilter(postFilter Query) *SearchSource { + s.postQuery = postFilter + return s +} + +// Slice allows partitioning the documents in multiple slices. +// It is e.g. used to slice a scroll operation, supported in +// Elasticsearch 5.0 or later. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-scroll.html#sliced-scroll +// for details. +func (s *SearchSource) Slice(sliceQuery Query) *SearchSource { + s.sliceQuery = sliceQuery + return s +} + +// From index to start the search from. Defaults to 0. +func (s *SearchSource) From(from int) *SearchSource { + s.from = from + return s +} + +// Size is the number of search hits to return. Defaults to 10. +func (s *SearchSource) Size(size int) *SearchSource { + s.size = size + return s +} + +// MinScore sets the minimum score below which docs will be filtered out. +func (s *SearchSource) MinScore(minScore float64) *SearchSource { + s.minScore = &minScore + return s +} + +// Explain indicates whether each search hit should be returned with +// an explanation of the hit (ranking). +func (s *SearchSource) Explain(explain bool) *SearchSource { + s.explain = &explain + return s +} + +// Version indicates whether each search hit should be returned with +// a version associated to it. +func (s *SearchSource) Version(version bool) *SearchSource { + s.version = &version + return s +} + +// SeqNoAndPrimaryTerm indicates whether SearchHits should be returned with the +// sequence number and primary term of the last modification of the document. +func (s *SearchSource) SeqNoAndPrimaryTerm(enabled bool) *SearchSource { + s.seqNoAndPrimaryTerm = &enabled + return s +} + +// Timeout controls how long a search is allowed to take, e.g. "1s" or "500ms". +func (s *SearchSource) Timeout(timeout string) *SearchSource { + s.timeout = timeout + return s +} + +// TimeoutInMillis controls how many milliseconds a search is allowed +// to take before it is canceled. +func (s *SearchSource) TimeoutInMillis(timeoutInMillis int) *SearchSource { + s.timeout = fmt.Sprintf("%dms", timeoutInMillis) + return s +} + +// TerminateAfter specifies the maximum number of documents to collect for +// each shard, upon reaching which the query execution will terminate early. +func (s *SearchSource) TerminateAfter(terminateAfter int) *SearchSource { + s.terminateAfter = &terminateAfter + return s +} + +// Sort adds a sort order. +func (s *SearchSource) Sort(field string, ascending bool) *SearchSource { + s.sorters = append(s.sorters, SortInfo{Field: field, Ascending: ascending}) + return s +} + +// SortWithInfo adds a sort order. +func (s *SearchSource) SortWithInfo(info SortInfo) *SearchSource { + s.sorters = append(s.sorters, info) + return s +} + +// SortBy adds a sort order. +func (s *SearchSource) SortBy(sorter ...Sorter) *SearchSource { + s.sorters = append(s.sorters, sorter...) + return s +} + +func (s *SearchSource) hasSort() bool { + return len(s.sorters) > 0 +} + +// TrackScores is applied when sorting and controls if scores will be +// tracked as well. Defaults to false. +func (s *SearchSource) TrackScores(trackScores bool) *SearchSource { + s.trackScores = &trackScores + return s +} + +// TrackTotalHits controls how the total number of hits should be tracked. +// Defaults to 10000 which will count the total hit accurately up to 10,000 hits. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-track-total-hits.html +// for details. +func (s *SearchSource) TrackTotalHits(trackTotalHits interface{}) *SearchSource { + s.trackTotalHits = trackTotalHits + return s +} + +// SearchAfter allows a different form of pagination by using a live cursor, +// using the results of the previous page to help the retrieval of the next. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-search-after.html +func (s *SearchSource) SearchAfter(sortValues ...interface{}) *SearchSource { + s.searchAfterSortValues = append(s.searchAfterSortValues, sortValues...) + return s +} + +// Aggregation adds an aggreation to perform as part of the search. +func (s *SearchSource) Aggregation(name string, aggregation Aggregation) *SearchSource { + s.aggregations[name] = aggregation + return s +} + +// DefaultRescoreWindowSize sets the rescore window size for rescores +// that don't specify their window. +func (s *SearchSource) DefaultRescoreWindowSize(defaultRescoreWindowSize int) *SearchSource { + s.defaultRescoreWindowSize = &defaultRescoreWindowSize + return s +} + +// Highlight adds highlighting to the search. +func (s *SearchSource) Highlight(highlight *Highlight) *SearchSource { + s.highlight = highlight + return s +} + +// Highlighter returns the highlighter. +func (s *SearchSource) Highlighter() *Highlight { + if s.highlight == nil { + s.highlight = NewHighlight() + } + return s.highlight +} + +// GlobalSuggestText defines the global text to use with all suggesters. +// This avoids repetition. +func (s *SearchSource) GlobalSuggestText(text string) *SearchSource { + s.globalSuggestText = text + return s +} + +// Suggester adds a suggester to the search. +func (s *SearchSource) Suggester(suggester Suggester) *SearchSource { + s.suggesters = append(s.suggesters, suggester) + return s +} + +// Rescorer adds a rescorer to the search. +func (s *SearchSource) Rescorer(rescore *Rescore) *SearchSource { + s.rescores = append(s.rescores, rescore) + return s +} + +// ClearRescorers removes all rescorers from the search. +func (s *SearchSource) ClearRescorers() *SearchSource { + s.rescores = make([]*Rescore, 0) + return s +} + +// FetchSource indicates whether the response should contain the stored +// _source for every hit. +func (s *SearchSource) FetchSource(fetchSource bool) *SearchSource { + if s.fetchSourceContext == nil { + s.fetchSourceContext = NewFetchSourceContext(fetchSource) + } else { + s.fetchSourceContext.SetFetchSource(fetchSource) + } + return s +} + +// FetchSourceContext indicates how the _source should be fetched. +func (s *SearchSource) FetchSourceContext(fetchSourceContext *FetchSourceContext) *SearchSource { + s.fetchSourceContext = fetchSourceContext + return s +} + +// FetchSourceIncludeExclude specifies that _source should be returned +// with each hit, where "include" and "exclude" serve as a simple wildcard +// matcher that gets applied to its fields +// (e.g. include := []string{"obj1.*","obj2.*"}, exclude := []string{"description.*"}). +func (s *SearchSource) FetchSourceIncludeExclude(include, exclude []string) *SearchSource { + s.fetchSourceContext = NewFetchSourceContext(true). + Include(include...). + Exclude(exclude...) + return s +} + +// NoStoredFields indicates that no fields should be loaded, resulting in only +// id and type to be returned per field. +func (s *SearchSource) NoStoredFields() *SearchSource { + s.storedFieldNames = []string{} + return s +} + +// StoredField adds a single field to load and return (note, must be stored) as +// part of the search request. If none are specified, the source of the +// document will be returned. +func (s *SearchSource) StoredField(storedFieldName string) *SearchSource { + s.storedFieldNames = append(s.storedFieldNames, storedFieldName) + return s +} + +// StoredFields sets the fields to load and return as part of the search request. +// If none are specified, the source of the document will be returned. +func (s *SearchSource) StoredFields(storedFieldNames ...string) *SearchSource { + s.storedFieldNames = append(s.storedFieldNames, storedFieldNames...) + return s +} + +// DocvalueField adds a single field to load from the field data cache +// and return as part of the search request. +func (s *SearchSource) DocvalueField(fieldDataField string) *SearchSource { + s.docvalueFields = append(s.docvalueFields, DocvalueField{Field: fieldDataField}) + return s +} + +// DocvalueField adds a single docvalue field to load from the field data cache +// and return as part of the search request. +func (s *SearchSource) DocvalueFieldWithFormat(fieldDataFieldWithFormat DocvalueField) *SearchSource { + s.docvalueFields = append(s.docvalueFields, fieldDataFieldWithFormat) + return s +} + +// DocvalueFields adds one or more fields to load from the field data cache +// and return as part of the search request. +func (s *SearchSource) DocvalueFields(docvalueFields ...string) *SearchSource { + for _, f := range docvalueFields { + s.docvalueFields = append(s.docvalueFields, DocvalueField{Field: f}) + } + return s +} + +// DocvalueFields adds one or more docvalue fields to load from the field data cache +// and return as part of the search request. +func (s *SearchSource) DocvalueFieldsWithFormat(docvalueFields ...DocvalueField) *SearchSource { + s.docvalueFields = append(s.docvalueFields, docvalueFields...) + return s +} + +// ScriptField adds a single script field with the provided script. +func (s *SearchSource) ScriptField(scriptField *ScriptField) *SearchSource { + s.scriptFields = append(s.scriptFields, scriptField) + return s +} + +// ScriptFields adds one or more script fields with the provided scripts. +func (s *SearchSource) ScriptFields(scriptFields ...*ScriptField) *SearchSource { + s.scriptFields = append(s.scriptFields, scriptFields...) + return s +} + +// IndexBoost sets the boost that a specific index will receive when the +// query is executed against it. +func (s *SearchSource) IndexBoost(index string, boost float64) *SearchSource { + s.indexBoosts[index] = boost + return s +} + +// Stats group this request will be aggregated under. +func (s *SearchSource) Stats(statsGroup ...string) *SearchSource { + s.stats = append(s.stats, statsGroup...) + return s +} + +// InnerHit adds an inner hit to return with the result. +func (s *SearchSource) InnerHit(name string, innerHit *InnerHit) *SearchSource { + s.innerHits[name] = innerHit + return s +} + +// Collapse adds field collapsing. +func (s *SearchSource) Collapse(collapse *CollapseBuilder) *SearchSource { + s.collapse = collapse + return s +} + +// Source returns the serializable JSON for the source builder. +func (s *SearchSource) Source() (interface{}, error) { + source := make(map[string]interface{}) + + if s.from != -1 { + source["from"] = s.from + } + if s.size != -1 { + source["size"] = s.size + } + if s.timeout != "" { + source["timeout"] = s.timeout + } + if s.terminateAfter != nil { + source["terminate_after"] = *s.terminateAfter + } + if s.query != nil { + src, err := s.query.Source() + if err != nil { + return nil, err + } + source["query"] = src + } + if s.postQuery != nil { + src, err := s.postQuery.Source() + if err != nil { + return nil, err + } + source["post_filter"] = src + } + if s.minScore != nil { + source["min_score"] = *s.minScore + } + if s.version != nil { + source["version"] = *s.version + } + if s.explain != nil { + source["explain"] = *s.explain + } + if s.profile { + source["profile"] = s.profile + } + if s.fetchSourceContext != nil { + src, err := s.fetchSourceContext.Source() + if err != nil { + return nil, err + } + source["_source"] = src + } + if s.storedFieldNames != nil { + switch len(s.storedFieldNames) { + case 1: + source["stored_fields"] = s.storedFieldNames[0] + default: + source["stored_fields"] = s.storedFieldNames + } + } + if len(s.docvalueFields) > 0 { + src, err := s.docvalueFields.Source() + if err != nil { + return nil, err + } + source["docvalue_fields"] = src + } + if len(s.scriptFields) > 0 { + sfmap := make(map[string]interface{}) + for _, scriptField := range s.scriptFields { + src, err := scriptField.Source() + if err != nil { + return nil, err + } + sfmap[scriptField.FieldName] = src + } + source["script_fields"] = sfmap + } + if len(s.sorters) > 0 { + var sortarr []interface{} + for _, sorter := range s.sorters { + src, err := sorter.Source() + if err != nil { + return nil, err + } + sortarr = append(sortarr, src) + } + source["sort"] = sortarr + } + if v := s.trackScores; v != nil { + source["track_scores"] = *v + } + if v := s.trackTotalHits; v != nil { + source["track_total_hits"] = v + } + if len(s.searchAfterSortValues) > 0 { + source["search_after"] = s.searchAfterSortValues + } + if s.sliceQuery != nil { + src, err := s.sliceQuery.Source() + if err != nil { + return nil, err + } + source["slice"] = src + } + if len(s.indexBoosts) > 0 { + source["indices_boost"] = s.indexBoosts + } + if len(s.aggregations) > 0 { + aggsMap := make(map[string]interface{}) + for name, aggregate := range s.aggregations { + src, err := aggregate.Source() + if err != nil { + return nil, err + } + aggsMap[name] = src + } + source["aggregations"] = aggsMap + } + if s.highlight != nil { + src, err := s.highlight.Source() + if err != nil { + return nil, err + } + source["highlight"] = src + } + if len(s.suggesters) > 0 { + suggesters := make(map[string]interface{}) + for _, s := range s.suggesters { + src, err := s.Source(false) + if err != nil { + return nil, err + } + suggesters[s.Name()] = src + } + if s.globalSuggestText != "" { + suggesters["text"] = s.globalSuggestText + } + source["suggest"] = suggesters + } + if len(s.rescores) > 0 { + // Strip empty rescores from request + var rescores []*Rescore + for _, r := range s.rescores { + if !r.IsEmpty() { + rescores = append(rescores, r) + } + } + if len(rescores) == 1 { + rescores[0].defaultRescoreWindowSize = s.defaultRescoreWindowSize + src, err := rescores[0].Source() + if err != nil { + return nil, err + } + source["rescore"] = src + } else { + var slice []interface{} + for _, r := range rescores { + r.defaultRescoreWindowSize = s.defaultRescoreWindowSize + src, err := r.Source() + if err != nil { + return nil, err + } + slice = append(slice, src) + } + source["rescore"] = slice + } + } + if len(s.stats) > 0 { + source["stats"] = s.stats + } + // TODO ext builders + + if s.collapse != nil { + src, err := s.collapse.Source() + if err != nil { + return nil, err + } + source["collapse"] = src + } + + if v := s.seqNoAndPrimaryTerm; v != nil { + source["seq_no_primary_term"] = *v + } + + if len(s.innerHits) > 0 { + // Top-level inner hits + // See http://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-request-inner-hits.html#top-level-inner-hits + // "inner_hits": { + // "": { + // "": { + // "": { + // , + // [,"inner_hits" : { []+ } ]? + // } + // } + // }, + // [,"" : { ... } ]* + // } + m := make(map[string]interface{}) + for name, hit := range s.innerHits { + if hit.path != "" { + src, err := hit.Source() + if err != nil { + return nil, err + } + path := make(map[string]interface{}) + path[hit.path] = src + m[name] = map[string]interface{}{ + "path": path, + } + } else if hit.typ != "" { + src, err := hit.Source() + if err != nil { + return nil, err + } + typ := make(map[string]interface{}) + typ[hit.typ] = src + m[name] = map[string]interface{}{ + "type": typ, + } + } else { + // TODO the Java client throws here, because either path or typ must be specified + _ = m + } + } + source["inner_hits"] = m + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/search_terms_lookup.go b/vendor/github.com/olivere/elastic/v7/search_terms_lookup.go new file mode 100644 index 0000000000..f5d6541ac5 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/search_terms_lookup.go @@ -0,0 +1,76 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TermsLookup encapsulates the parameters needed to fetch terms. +// +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-terms-query.html#query-dsl-terms-lookup. +type TermsLookup struct { + index string + typ string + id string + path string + routing string +} + +// NewTermsLookup creates and initializes a new TermsLookup. +func NewTermsLookup() *TermsLookup { + t := &TermsLookup{} + return t +} + +// Index name. +func (t *TermsLookup) Index(index string) *TermsLookup { + t.index = index + return t +} + +// Type name. +// +// Deprecated: Types are in the process of being removed. +func (t *TermsLookup) Type(typ string) *TermsLookup { + t.typ = typ + return t +} + +// Id to look up. +func (t *TermsLookup) Id(id string) *TermsLookup { + t.id = id + return t +} + +// Path to use for lookup. +func (t *TermsLookup) Path(path string) *TermsLookup { + t.path = path + return t +} + +// Routing value. +func (t *TermsLookup) Routing(routing string) *TermsLookup { + t.routing = routing + return t +} + +// Source creates the JSON source of the builder. +func (t *TermsLookup) Source() (interface{}, error) { + src := make(map[string]interface{}) + if t.index != "" { + src["index"] = t.index + } + if t.typ != "" { + src["type"] = t.typ + } + if t.id != "" { + src["id"] = t.id + } + if t.path != "" { + src["path"] = t.path + } + if t.routing != "" { + src["routing"] = t.routing + } + return src, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_create.go b/vendor/github.com/olivere/elastic/v7/snapshot_create.go new file mode 100644 index 0000000000..0c3de06b66 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_create.go @@ -0,0 +1,227 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotCreateService is documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html. +type SnapshotCreateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository string + snapshot string + masterTimeout string + waitForCompletion *bool + bodyJson interface{} + bodyString string +} + +// NewSnapshotCreateService creates a new SnapshotCreateService. +func NewSnapshotCreateService(client *Client) *SnapshotCreateService { + return &SnapshotCreateService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotCreateService) Pretty(pretty bool) *SnapshotCreateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotCreateService) Human(human bool) *SnapshotCreateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotCreateService) ErrorTrace(errorTrace bool) *SnapshotCreateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotCreateService) FilterPath(filterPath ...string) *SnapshotCreateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotCreateService) Header(name string, value string) *SnapshotCreateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotCreateService) Headers(headers http.Header) *SnapshotCreateService { + s.headers = headers + return s +} + +// Repository is the repository name. +func (s *SnapshotCreateService) Repository(repository string) *SnapshotCreateService { + s.repository = repository + return s +} + +// Snapshot is the snapshot name. +func (s *SnapshotCreateService) Snapshot(snapshot string) *SnapshotCreateService { + s.snapshot = snapshot + return s +} + +// MasterTimeout is documented as: Explicit operation timeout for connection to master node. +func (s *SnapshotCreateService) MasterTimeout(masterTimeout string) *SnapshotCreateService { + s.masterTimeout = masterTimeout + return s +} + +// WaitForCompletion is documented as: Should this request wait until the operation has completed before returning. +func (s *SnapshotCreateService) WaitForCompletion(waitForCompletion bool) *SnapshotCreateService { + s.waitForCompletion = &waitForCompletion + return s +} + +// BodyJson is documented as: The snapshot definition. +func (s *SnapshotCreateService) BodyJson(body interface{}) *SnapshotCreateService { + s.bodyJson = body + return s +} + +// BodyString is documented as: The snapshot definition. +func (s *SnapshotCreateService) BodyString(body string) *SnapshotCreateService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotCreateService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_snapshot/{repository}/{snapshot}", map[string]string{ + "snapshot": s.snapshot, + "repository": s.repository, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.waitForCompletion; v != nil { + params.Set("wait_for_completion", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotCreateService) Validate() error { + var invalid []string + if s.repository == "" { + invalid = append(invalid, "Repository") + } + if s.snapshot == "" { + invalid = append(invalid, "Snapshot") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SnapshotCreateService) Do(ctx context.Context) (*SnapshotCreateResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SnapshotCreateResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotShardFailure stores information about failures that occurred during shard snapshotting process. +type SnapshotShardFailure struct { + Index string `json:"index"` + IndexUUID string `json:"index_uuid"` + ShardID int `json:"shard_id"` + Reason string `json:"reason"` + NodeID string `json:"node_id"` + Status string `json:"status"` +} + +// SnapshotCreateResponse is the response of SnapshotCreateService.Do. +type SnapshotCreateResponse struct { + // Accepted indicates whether the request was accepted by elasticsearch. + // It's available when waitForCompletion is false. + Accepted *bool `json:"accepted"` + + // Snapshot is available when waitForCompletion is true. + Snapshot *Snapshot `json:"snapshot"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_create_repository.go b/vendor/github.com/olivere/elastic/v7/snapshot_create_repository.go new file mode 100644 index 0000000000..5976b2adc9 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_create_repository.go @@ -0,0 +1,257 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotCreateRepositoryService creates a snapshot repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html +// for details. +type SnapshotCreateRepositoryService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository string + masterTimeout string + timeout string + verify *bool + typ string + settings map[string]interface{} + bodyJson interface{} + bodyString string +} + +// NewSnapshotCreateRepositoryService creates a new SnapshotCreateRepositoryService. +func NewSnapshotCreateRepositoryService(client *Client) *SnapshotCreateRepositoryService { + return &SnapshotCreateRepositoryService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotCreateRepositoryService) Pretty(pretty bool) *SnapshotCreateRepositoryService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotCreateRepositoryService) Human(human bool) *SnapshotCreateRepositoryService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotCreateRepositoryService) ErrorTrace(errorTrace bool) *SnapshotCreateRepositoryService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotCreateRepositoryService) FilterPath(filterPath ...string) *SnapshotCreateRepositoryService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotCreateRepositoryService) Header(name string, value string) *SnapshotCreateRepositoryService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotCreateRepositoryService) Headers(headers http.Header) *SnapshotCreateRepositoryService { + s.headers = headers + return s +} + +// Repository is the repository name. +func (s *SnapshotCreateRepositoryService) Repository(repository string) *SnapshotCreateRepositoryService { + s.repository = repository + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *SnapshotCreateRepositoryService) MasterTimeout(masterTimeout string) *SnapshotCreateRepositoryService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout is an explicit operation timeout. +func (s *SnapshotCreateRepositoryService) Timeout(timeout string) *SnapshotCreateRepositoryService { + s.timeout = timeout + return s +} + +// Verify indicates whether to verify the repository after creation. +func (s *SnapshotCreateRepositoryService) Verify(verify bool) *SnapshotCreateRepositoryService { + s.verify = &verify + return s +} + +// Type sets the snapshot repository type, e.g. "fs". +func (s *SnapshotCreateRepositoryService) Type(typ string) *SnapshotCreateRepositoryService { + s.typ = typ + return s +} + +// Settings sets all settings of the snapshot repository. +func (s *SnapshotCreateRepositoryService) Settings(settings map[string]interface{}) *SnapshotCreateRepositoryService { + s.settings = settings + return s +} + +// Setting sets a single settings of the snapshot repository. +func (s *SnapshotCreateRepositoryService) Setting(name string, value interface{}) *SnapshotCreateRepositoryService { + if s.settings == nil { + s.settings = make(map[string]interface{}) + } + s.settings[name] = value + return s +} + +// BodyJson is documented as: The repository definition. +func (s *SnapshotCreateRepositoryService) BodyJson(body interface{}) *SnapshotCreateRepositoryService { + s.bodyJson = body + return s +} + +// BodyString is documented as: The repository definition. +func (s *SnapshotCreateRepositoryService) BodyString(body string) *SnapshotCreateRepositoryService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotCreateRepositoryService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_snapshot/{repository}", map[string]string{ + "repository": s.repository, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if v := s.verify; v != nil { + params.Set("verify", fmt.Sprint(*v)) + } + return path, params, nil +} + +// buildBody builds the body for the operation. +func (s *SnapshotCreateRepositoryService) buildBody() (interface{}, error) { + if s.bodyJson != nil { + return s.bodyJson, nil + } + if s.bodyString != "" { + return s.bodyString, nil + } + + body := map[string]interface{}{ + "type": s.typ, + } + if len(s.settings) > 0 { + body["settings"] = s.settings + } + return body, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotCreateRepositoryService) Validate() error { + var invalid []string + if s.repository == "" { + invalid = append(invalid, "Repository") + } + if s.bodyString == "" && s.bodyJson == nil && len(s.settings) == 0 { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SnapshotCreateRepositoryService) Do(ctx context.Context) (*SnapshotCreateRepositoryResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body, err := s.buildBody() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SnapshotCreateRepositoryResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotCreateRepositoryResponse is the response of SnapshotCreateRepositoryService.Do. +type SnapshotCreateRepositoryResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_delete.go b/vendor/github.com/olivere/elastic/v7/snapshot_delete.go new file mode 100644 index 0000000000..74cf09bc8a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_delete.go @@ -0,0 +1,154 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotDeleteService deletes a snapshot from a snapshot repository. +// It is documented at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html. +type SnapshotDeleteService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository string + snapshot string +} + +// NewSnapshotDeleteService creates a new SnapshotDeleteService. +func NewSnapshotDeleteService(client *Client) *SnapshotDeleteService { + return &SnapshotDeleteService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotDeleteService) Pretty(pretty bool) *SnapshotDeleteService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotDeleteService) Human(human bool) *SnapshotDeleteService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotDeleteService) ErrorTrace(errorTrace bool) *SnapshotDeleteService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotDeleteService) FilterPath(filterPath ...string) *SnapshotDeleteService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotDeleteService) Header(name string, value string) *SnapshotDeleteService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotDeleteService) Headers(headers http.Header) *SnapshotDeleteService { + s.headers = headers + return s +} + +// Repository is the repository name. +func (s *SnapshotDeleteService) Repository(repository string) *SnapshotDeleteService { + s.repository = repository + return s +} + +// Snapshot is the snapshot name. +func (s *SnapshotDeleteService) Snapshot(snapshot string) *SnapshotDeleteService { + s.snapshot = snapshot + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotDeleteService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_snapshot/{repository}/{snapshot}", map[string]string{ + "repository": s.repository, + "snapshot": s.snapshot, + }) + if err != nil { + return "", url.Values{}, err + } + return path, url.Values{}, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotDeleteService) Validate() error { + var invalid []string + if s.repository == "" { + invalid = append(invalid, "Repository") + } + if s.snapshot == "" { + invalid = append(invalid, "Snapshot") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SnapshotDeleteService) Do(ctx context.Context) (*SnapshotDeleteResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SnapshotDeleteResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotDeleteResponse is the response of SnapshotDeleteService.Do. +type SnapshotDeleteResponse struct { + Acknowledged bool `json:"acknowledged"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_delete_repository.go b/vendor/github.com/olivere/elastic/v7/snapshot_delete_repository.go new file mode 100644 index 0000000000..15b755c14f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_delete_repository.go @@ -0,0 +1,183 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotDeleteRepositoryService deletes a snapshot repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html +// for details. +type SnapshotDeleteRepositoryService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository []string + masterTimeout string + timeout string +} + +// NewSnapshotDeleteRepositoryService creates a new SnapshotDeleteRepositoryService. +func NewSnapshotDeleteRepositoryService(client *Client) *SnapshotDeleteRepositoryService { + return &SnapshotDeleteRepositoryService{ + client: client, + repository: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotDeleteRepositoryService) Pretty(pretty bool) *SnapshotDeleteRepositoryService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotDeleteRepositoryService) Human(human bool) *SnapshotDeleteRepositoryService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotDeleteRepositoryService) ErrorTrace(errorTrace bool) *SnapshotDeleteRepositoryService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotDeleteRepositoryService) FilterPath(filterPath ...string) *SnapshotDeleteRepositoryService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotDeleteRepositoryService) Header(name string, value string) *SnapshotDeleteRepositoryService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotDeleteRepositoryService) Headers(headers http.Header) *SnapshotDeleteRepositoryService { + s.headers = headers + return s +} + +// Repository is the list of repository names. +func (s *SnapshotDeleteRepositoryService) Repository(repositories ...string) *SnapshotDeleteRepositoryService { + s.repository = append(s.repository, repositories...) + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *SnapshotDeleteRepositoryService) MasterTimeout(masterTimeout string) *SnapshotDeleteRepositoryService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout is an explicit operation timeout. +func (s *SnapshotDeleteRepositoryService) Timeout(timeout string) *SnapshotDeleteRepositoryService { + s.timeout = timeout + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotDeleteRepositoryService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_snapshot/{repository}", map[string]string{ + "repository": strings.Join(s.repository, ","), + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotDeleteRepositoryService) Validate() error { + var invalid []string + if len(s.repository) == 0 { + invalid = append(invalid, "Repository") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SnapshotDeleteRepositoryService) Do(ctx context.Context) (*SnapshotDeleteRepositoryResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SnapshotDeleteRepositoryResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotDeleteRepositoryResponse is the response of SnapshotDeleteRepositoryService.Do. +type SnapshotDeleteRepositoryResponse struct { + Acknowledged bool `json:"acknowledged"` + ShardsAcknowledged bool `json:"shards_acknowledged"` + Index string `json:"index,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_get.go b/vendor/github.com/olivere/elastic/v7/snapshot_get.go new file mode 100644 index 0000000000..02cf99ee83 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_get.go @@ -0,0 +1,225 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotGetService lists the snapshots on a repository +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html +// for details. +type SnapshotGetService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository string + snapshot []string + masterTimeout string + ignoreUnavailable *bool + verbose *bool +} + +// NewSnapshotGetService creates a new SnapshotGetService. +func NewSnapshotGetService(client *Client) *SnapshotGetService { + return &SnapshotGetService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotGetService) Pretty(pretty bool) *SnapshotGetService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotGetService) Human(human bool) *SnapshotGetService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotGetService) ErrorTrace(errorTrace bool) *SnapshotGetService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotGetService) FilterPath(filterPath ...string) *SnapshotGetService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotGetService) Header(name string, value string) *SnapshotGetService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotGetService) Headers(headers http.Header) *SnapshotGetService { + s.headers = headers + return s +} + +// Repository is the repository name. +func (s *SnapshotGetService) Repository(repository string) *SnapshotGetService { + s.repository = repository + return s +} + +// Snapshot is the list of snapshot names. If not set, defaults to all snapshots. +func (s *SnapshotGetService) Snapshot(snapshots ...string) *SnapshotGetService { + s.snapshot = append(s.snapshot, snapshots...) + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *SnapshotGetService) MasterTimeout(masterTimeout string) *SnapshotGetService { + s.masterTimeout = masterTimeout + return s +} + +// IgnoreUnavailable specifies whether to ignore unavailable snapshots, defaults to false +func (s *SnapshotGetService) IgnoreUnavailable(ignoreUnavailable bool) *SnapshotGetService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// Verbose specifies whether to show verbose snapshot info or only show the basic info found in the repository index blob +func (s *SnapshotGetService) Verbose(verbose bool) *SnapshotGetService { + s.verbose = &verbose + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotGetService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.snapshot) > 0 { + path, err = uritemplates.Expand("/_snapshot/{repository}/{snapshot}", map[string]string{ + "repository": s.repository, + "snapshot": strings.Join(s.snapshot, ","), + }) + } else { + path, err = uritemplates.Expand("/_snapshot/{repository}/_all", map[string]string{ + "repository": s.repository, + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.ignoreUnavailable; v != nil { + params.Set("ignore_unavailable", fmt.Sprint(*v)) + } + if v := s.verbose; v != nil { + params.Set("verbose", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotGetService) Validate() error { + var invalid []string + if s.repository == "" { + invalid = append(invalid, "Repository") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SnapshotGetService) Do(ctx context.Context) (*SnapshotGetResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SnapshotGetResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotGetResponse is the response of SnapshotGetService.Do. +type SnapshotGetResponse struct { + Snapshots []*Snapshot `json:"snapshots"` +} + +// Snapshot contains all information about a single snapshot +type Snapshot struct { + Snapshot string `json:"snapshot"` + UUID string `json:"uuid"` + VersionID int `json:"version_id"` + Version string `json:"version"` + Indices []string `json:"indices"` + State string `json:"state"` + Reason string `json:"reason"` + StartTime time.Time `json:"start_time"` + StartTimeInMillis int64 `json:"start_time_in_millis"` + EndTime time.Time `json:"end_time"` + EndTimeInMillis int64 `json:"end_time_in_millis"` + DurationInMillis int64 `json:"duration_in_millis"` + Failures []SnapshotShardFailure `json:"failures"` + Shards *ShardsInfo `json:"shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_get_repository.go b/vendor/github.com/olivere/elastic/v7/snapshot_get_repository.go new file mode 100644 index 0000000000..28603563d8 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_get_repository.go @@ -0,0 +1,185 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotGetRepositoryService reads a snapshot repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html +// for details. +type SnapshotGetRepositoryService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository []string + local *bool + masterTimeout string +} + +// NewSnapshotGetRepositoryService creates a new SnapshotGetRepositoryService. +func NewSnapshotGetRepositoryService(client *Client) *SnapshotGetRepositoryService { + return &SnapshotGetRepositoryService{ + client: client, + repository: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotGetRepositoryService) Pretty(pretty bool) *SnapshotGetRepositoryService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotGetRepositoryService) Human(human bool) *SnapshotGetRepositoryService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotGetRepositoryService) ErrorTrace(errorTrace bool) *SnapshotGetRepositoryService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotGetRepositoryService) FilterPath(filterPath ...string) *SnapshotGetRepositoryService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotGetRepositoryService) Header(name string, value string) *SnapshotGetRepositoryService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotGetRepositoryService) Headers(headers http.Header) *SnapshotGetRepositoryService { + s.headers = headers + return s +} + +// Repository is the list of repository names. +func (s *SnapshotGetRepositoryService) Repository(repositories ...string) *SnapshotGetRepositoryService { + s.repository = append(s.repository, repositories...) + return s +} + +// Local indicates whether to return local information, i.e. do not retrieve the state from master node (default: false). +func (s *SnapshotGetRepositoryService) Local(local bool) *SnapshotGetRepositoryService { + s.local = &local + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *SnapshotGetRepositoryService) MasterTimeout(masterTimeout string) *SnapshotGetRepositoryService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotGetRepositoryService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.repository) > 0 { + path, err = uritemplates.Expand("/_snapshot/{repository}", map[string]string{ + "repository": strings.Join(s.repository, ","), + }) + } else { + path = "/_snapshot" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotGetRepositoryService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *SnapshotGetRepositoryService) Do(ctx context.Context) (SnapshotGetRepositoryResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret SnapshotGetRepositoryResponse + if err := json.Unmarshal(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotGetRepositoryResponse is the response of SnapshotGetRepositoryService.Do. +type SnapshotGetRepositoryResponse map[string]*SnapshotRepositoryMetaData + +// SnapshotRepositoryMetaData contains all information about +// a single snapshot repository. +type SnapshotRepositoryMetaData struct { + Type string `json:"type"` + Settings map[string]interface{} `json:"settings,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_restore.go b/vendor/github.com/olivere/elastic/v7/snapshot_restore.go new file mode 100644 index 0000000000..d148cfa1e4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_restore.go @@ -0,0 +1,294 @@ +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotRestoreService restores a snapshot from a snapshot repository. +// +// It is documented at +// https://www.elastic.co/guide/en/elasticsearch/reference/7.1/modules-snapshots.html#_restore. +type SnapshotRestoreService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository string + snapshot string + masterTimeout string + waitForCompletion *bool + ignoreUnavailable *bool + partial *bool + includeAliases *bool + includeGlobalState *bool + bodyString string + renamePattern string + renameReplacement string + indices []string + indexSettings map[string]interface{} +} + +// NewSnapshotCreateService creates a new SnapshotRestoreService. +func NewSnapshotRestoreService(client *Client) *SnapshotRestoreService { + return &SnapshotRestoreService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotRestoreService) Pretty(pretty bool) *SnapshotRestoreService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotRestoreService) Human(human bool) *SnapshotRestoreService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotRestoreService) ErrorTrace(errorTrace bool) *SnapshotRestoreService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotRestoreService) FilterPath(filterPath ...string) *SnapshotRestoreService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotRestoreService) Header(name string, value string) *SnapshotRestoreService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotRestoreService) Headers(headers http.Header) *SnapshotRestoreService { + s.headers = headers + return s +} + +// Repository name. +func (s *SnapshotRestoreService) Repository(repository string) *SnapshotRestoreService { + s.repository = repository + return s +} + +// Snapshot name. +func (s *SnapshotRestoreService) Snapshot(snapshot string) *SnapshotRestoreService { + s.snapshot = snapshot + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *SnapshotRestoreService) MasterTimeout(masterTimeout string) *SnapshotRestoreService { + s.masterTimeout = masterTimeout + return s +} + +// WaitForCompletion indicates whether this request should wait until the operation has +// completed before returning. +func (s *SnapshotRestoreService) WaitForCompletion(waitForCompletion bool) *SnapshotRestoreService { + s.waitForCompletion = &waitForCompletion + return s +} + +// Indices sets the name of the indices that should be restored from the snapshot. +func (s *SnapshotRestoreService) Indices(indices ...string) *SnapshotRestoreService { + s.indices = indices + return s +} + +// IncludeGlobalState allows the global cluster state to be restored, defaults to false. +func (s *SnapshotRestoreService) IncludeGlobalState(includeGlobalState bool) *SnapshotRestoreService { + s.includeGlobalState = &includeGlobalState + return s +} + +// RenamePattern helps rename indices on restore using regular expressions. +func (s *SnapshotRestoreService) RenamePattern(renamePattern string) *SnapshotRestoreService { + s.renamePattern = renamePattern + return s +} + +// RenameReplacement as RenamePattern, helps rename indices on restore using regular expressions. +func (s *SnapshotRestoreService) RenameReplacement(renameReplacement string) *SnapshotRestoreService { + s.renameReplacement = renameReplacement + return s +} + +// Partial indicates whether to restore indices that where partially snapshoted, defaults to false. +func (s *SnapshotRestoreService) Partial(partial bool) *SnapshotRestoreService { + s.partial = &partial + return s +} + +// BodyString allows the user to specify the body of the HTTP request manually. +func (s *SnapshotRestoreService) BodyString(body string) *SnapshotRestoreService { + s.bodyString = body + return s +} + +// IndexSettings sets the settings to be overwritten during the restore process +func (s *SnapshotRestoreService) IndexSettings(indexSettings map[string]interface{}) *SnapshotRestoreService { + s.indexSettings = indexSettings + return s +} + +// IncludeAliases flags whether indices should be restored with their respective aliases, +// defaults to false. +func (s *SnapshotRestoreService) IncludeAliases(includeAliases bool) *SnapshotRestoreService { + s.includeAliases = &includeAliases + return s +} + +// IgnoreUnavailable specifies whether to ignore unavailable snapshots, defaults to false. +func (s *SnapshotRestoreService) IgnoreUnavailable(ignoreUnavailable bool) *SnapshotRestoreService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// Do executes the operation. +func (s *SnapshotRestoreService) Do(ctx context.Context) (*SnapshotRestoreResponse, error) { + if err := s.Validate(); err != nil { + return nil, err + } + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + var body interface{} + if len(s.bodyString) > 0 { + body = s.bodyString + } else { + body = s.buildBody() + } + + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + ret := new(SnapshotRestoreResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotRestoreService) Validate() error { + var invalid []string + if s.repository == "" { + invalid = append(invalid, "Repository") + } + if s.snapshot == "" { + invalid = append(invalid, "Snapshot") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +func (s *SnapshotRestoreService) buildURL() (string, url.Values, error) { + path, err := uritemplates.Expand("/_snapshot/{repository}/{snapshot}/_restore", map[string]string{ + "snapshot": s.snapshot, + "repository": s.repository, + }) + if err != nil { + return "", url.Values{}, err + } + + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.waitForCompletion; v != nil { + params.Set("wait_for_completion", fmt.Sprint(*v)) + } + if v := s.ignoreUnavailable; v != nil { + params.Set("ignore_unavailable", fmt.Sprint(*v)) + } + return path, params, nil +} + +func (s *SnapshotRestoreService) buildBody() interface{} { + body := map[string]interface{}{} + + if s.includeGlobalState != nil { + body["include_global_state"] = *s.includeGlobalState + } + if s.partial != nil { + body["partial"] = *s.partial + } + if s.includeAliases != nil { + body["include_aliases"] = *s.includeAliases + } + if len(s.indices) > 0 { + body["indices"] = strings.Join(s.indices, ",") + } + if len(s.renamePattern) > 0 { + body["rename_pattern"] = s.renamePattern + } + if len(s.renamePattern) > 0 { + body["rename_replacement"] = s.renameReplacement + } + if len(s.indexSettings) > 0 { + body["index_settings"] = s.indexSettings + } + return body +} + +// SnapshotRestoreResponse represents the response for SnapshotRestoreService.Do +type SnapshotRestoreResponse struct { + // Accepted indicates whether the request was accepted by Elasticsearch. + Accepted *bool `json:"accepted"` + + // Snapshot information. + Snapshot *RestoreInfo `json:"snapshot"` +} + +// RestoreInfo represents information about the restored snapshot. +type RestoreInfo struct { + Snapshot string `json:"snapshot"` + Indices []string `json:"indices"` + Shards ShardsInfo `json:"shards"` +} diff --git a/vendor/github.com/olivere/elastic/v7/snapshot_verify_repository.go b/vendor/github.com/olivere/elastic/v7/snapshot_verify_repository.go new file mode 100644 index 0000000000..ca8eab2dea --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/snapshot_verify_repository.go @@ -0,0 +1,184 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// SnapshotVerifyRepositoryService verifies a snapshop repository. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-snapshots.html +// for details. +type SnapshotVerifyRepositoryService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + repository string + masterTimeout string + timeout string +} + +// NewSnapshotVerifyRepositoryService creates a new SnapshotVerifyRepositoryService. +func NewSnapshotVerifyRepositoryService(client *Client) *SnapshotVerifyRepositoryService { + return &SnapshotVerifyRepositoryService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *SnapshotVerifyRepositoryService) Pretty(pretty bool) *SnapshotVerifyRepositoryService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *SnapshotVerifyRepositoryService) Human(human bool) *SnapshotVerifyRepositoryService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *SnapshotVerifyRepositoryService) ErrorTrace(errorTrace bool) *SnapshotVerifyRepositoryService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *SnapshotVerifyRepositoryService) FilterPath(filterPath ...string) *SnapshotVerifyRepositoryService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *SnapshotVerifyRepositoryService) Header(name string, value string) *SnapshotVerifyRepositoryService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *SnapshotVerifyRepositoryService) Headers(headers http.Header) *SnapshotVerifyRepositoryService { + s.headers = headers + return s +} + +// Repository specifies the repository name. +func (s *SnapshotVerifyRepositoryService) Repository(repository string) *SnapshotVerifyRepositoryService { + s.repository = repository + return s +} + +// MasterTimeout is the explicit operation timeout for connection to master node. +func (s *SnapshotVerifyRepositoryService) MasterTimeout(masterTimeout string) *SnapshotVerifyRepositoryService { + s.masterTimeout = masterTimeout + return s +} + +// Timeout is an explicit operation timeout. +func (s *SnapshotVerifyRepositoryService) Timeout(timeout string) *SnapshotVerifyRepositoryService { + s.timeout = timeout + return s +} + +// buildURL builds the URL for the operation. +func (s *SnapshotVerifyRepositoryService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_snapshot/{repository}/_verify", map[string]string{ + "repository": s.repository, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *SnapshotVerifyRepositoryService) Validate() error { + var invalid []string + if s.repository == "" { + invalid = append(invalid, "Repository") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *SnapshotVerifyRepositoryService) Do(ctx context.Context) (*SnapshotVerifyRepositoryResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(SnapshotVerifyRepositoryResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// SnapshotVerifyRepositoryResponse is the response of SnapshotVerifyRepositoryService.Do. +type SnapshotVerifyRepositoryResponse struct { + Nodes map[string]*SnapshotVerifyRepositoryNode `json:"nodes"` +} + +type SnapshotVerifyRepositoryNode struct { + Name string `json:"name"` +} diff --git a/vendor/github.com/olivere/elastic/v7/sort.go b/vendor/github.com/olivere/elastic/v7/sort.go new file mode 100644 index 0000000000..60cc0a11f7 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/sort.go @@ -0,0 +1,654 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// -- Sorter -- + +// Sorter is an interface for sorting strategies, e.g. ScoreSort or FieldSort. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-sort.html. +type Sorter interface { + Source() (interface{}, error) +} + +// -- SortInfo -- + +// SortInfo contains information about sorting a field. +type SortInfo struct { + Sorter + Field string + Ascending bool + Missing interface{} + IgnoreUnmapped *bool + UnmappedType string + SortMode string + NestedFilter Query // deprecated in 6.1 and replaced by Filter + Filter Query + NestedPath string // deprecated in 6.1 and replaced by Path + Path string + NestedSort *NestedSort // deprecated in 6.1 and replaced by Nested + Nested *NestedSort +} + +func (info SortInfo) Source() (interface{}, error) { + prop := make(map[string]interface{}) + if info.Ascending { + prop["order"] = "asc" + } else { + prop["order"] = "desc" + } + if info.Missing != nil { + prop["missing"] = info.Missing + } + if info.IgnoreUnmapped != nil { + prop["ignore_unmapped"] = *info.IgnoreUnmapped + } + if info.UnmappedType != "" { + prop["unmapped_type"] = info.UnmappedType + } + if info.SortMode != "" { + prop["mode"] = info.SortMode + } + if info.Filter != nil { + src, err := info.Filter.Source() + if err != nil { + return nil, err + } + prop["filter"] = src + } else if info.NestedFilter != nil { + src, err := info.NestedFilter.Source() + if err != nil { + return nil, err + } + prop["nested_filter"] = src // deprecated in 6.1 + } + if info.Path != "" { + prop["path"] = info.Path + } else if info.NestedPath != "" { + prop["nested_path"] = info.NestedPath // deprecated in 6.1 + } + if info.Nested != nil { + src, err := info.Nested.Source() + if err != nil { + return nil, err + } + prop["nested"] = src + } else if info.NestedSort != nil { + src, err := info.NestedSort.Source() + if err != nil { + return nil, err + } + prop["nested"] = src + } + source := make(map[string]interface{}) + source[info.Field] = prop + return source, nil +} + +// -- SortByDoc -- + +// SortByDoc sorts by the "_doc" field, as described in +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-scroll.html. +// +// Example: +// ss := elastic.NewSearchSource() +// ss = ss.SortBy(elastic.SortByDoc{}) +type SortByDoc struct { + Sorter +} + +// Source returns the JSON-serializable data. +func (s SortByDoc) Source() (interface{}, error) { + return "_doc", nil +} + +// -- ScoreSort -- + +// ScoreSort sorts by relevancy score. +type ScoreSort struct { + Sorter + ascending bool +} + +// NewScoreSort creates a new ScoreSort. +func NewScoreSort() *ScoreSort { + return &ScoreSort{ascending: false} // Descending by default! +} + +// Order defines whether sorting ascending (default) or descending. +func (s *ScoreSort) Order(ascending bool) *ScoreSort { + s.ascending = ascending + return s +} + +// Asc sets ascending sort order. +func (s *ScoreSort) Asc() *ScoreSort { + s.ascending = true + return s +} + +// Desc sets descending sort order. +func (s *ScoreSort) Desc() *ScoreSort { + s.ascending = false + return s +} + +// Source returns the JSON-serializable data. +func (s *ScoreSort) Source() (interface{}, error) { + source := make(map[string]interface{}) + x := make(map[string]interface{}) + source["_score"] = x + if s.ascending { + x["order"] = "asc" + } else { + x["order"] = "desc" + } + return source, nil +} + +// -- FieldSort -- + +// FieldSort sorts by a given field. +type FieldSort struct { + Sorter + fieldName string + ascending bool + missing interface{} + unmappedType *string + sortMode *string + filter Query + path *string + nested *NestedSort +} + +// NewFieldSort creates a new FieldSort. +func NewFieldSort(fieldName string) *FieldSort { + return &FieldSort{ + fieldName: fieldName, + ascending: true, + } +} + +// FieldName specifies the name of the field to be used for sorting. +func (s *FieldSort) FieldName(fieldName string) *FieldSort { + s.fieldName = fieldName + return s +} + +// Order defines whether sorting ascending (default) or descending. +func (s *FieldSort) Order(ascending bool) *FieldSort { + s.ascending = ascending + return s +} + +// Asc sets ascending sort order. +func (s *FieldSort) Asc() *FieldSort { + s.ascending = true + return s +} + +// Desc sets descending sort order. +func (s *FieldSort) Desc() *FieldSort { + s.ascending = false + return s +} + +// Missing sets the value to be used when a field is missing in a document. +// You can also use "_last" or "_first" to sort missing last or first +// respectively. +func (s *FieldSort) Missing(missing interface{}) *FieldSort { + s.missing = missing + return s +} + +// UnmappedType sets the type to use when the current field is not mapped +// in an index. +func (s *FieldSort) UnmappedType(typ string) *FieldSort { + s.unmappedType = &typ + return s +} + +// SortMode specifies what values to pick in case a document contains +// multiple values for the targeted sort field. Possible values are: +// min, max, sum, and avg. +func (s *FieldSort) SortMode(sortMode string) *FieldSort { + s.sortMode = &sortMode + return s +} + +// NestedFilter sets a filter that nested objects should match with +// in order to be taken into account for sorting. +// Deprecated: Use Filter instead. +func (s *FieldSort) NestedFilter(nestedFilter Query) *FieldSort { + s.filter = nestedFilter + return s +} + +// Filter sets a filter that nested objects should match with +// in order to be taken into account for sorting. +func (s *FieldSort) Filter(filter Query) *FieldSort { + s.filter = filter + return s +} + +// NestedPath is used if sorting occurs on a field that is inside a +// nested object. +// Deprecated: Use Path instead. +func (s *FieldSort) NestedPath(nestedPath string) *FieldSort { + s.path = &nestedPath + return s +} + +// Path is used if sorting occurs on a field that is inside a +// nested object. +func (s *FieldSort) Path(path string) *FieldSort { + s.path = &path + return s +} + +// NestedSort is available starting with 6.1 and will replace NestedFilter +// and NestedPath. +// Deprecated: Use Nested instead. +func (s *FieldSort) NestedSort(nestedSort *NestedSort) *FieldSort { + s.nested = nestedSort + return s +} + +// Nested is available starting with 6.1 and will replace Filter and Path. +func (s *FieldSort) Nested(nested *NestedSort) *FieldSort { + s.nested = nested + return s +} + +// Source returns the JSON-serializable data. +func (s *FieldSort) Source() (interface{}, error) { + source := make(map[string]interface{}) + x := make(map[string]interface{}) + source[s.fieldName] = x + if s.ascending { + x["order"] = "asc" + } else { + x["order"] = "desc" + } + if s.missing != nil { + x["missing"] = s.missing + } + if s.unmappedType != nil { + x["unmapped_type"] = *s.unmappedType + } + if s.sortMode != nil { + x["mode"] = *s.sortMode + } + if s.filter != nil { + src, err := s.filter.Source() + if err != nil { + return nil, err + } + x["filter"] = src + } + if s.path != nil { + x["path"] = *s.path + } + if s.nested != nil { + src, err := s.nested.Source() + if err != nil { + return nil, err + } + x["nested"] = src + } + return source, nil +} + +// -- GeoDistanceSort -- + +// GeoDistanceSort allows for sorting by geographic distance. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-sort.html#_geo_distance_sorting. +type GeoDistanceSort struct { + Sorter + fieldName string + points []*GeoPoint + geohashes []string + distanceType *string + unit string + ascending bool + sortMode *string + nestedFilter Query + nestedPath *string + nestedSort *NestedSort +} + +// NewGeoDistanceSort creates a new sorter for geo distances. +func NewGeoDistanceSort(fieldName string) *GeoDistanceSort { + return &GeoDistanceSort{ + fieldName: fieldName, + ascending: true, + } +} + +// FieldName specifies the name of the (geo) field to use for sorting. +func (s *GeoDistanceSort) FieldName(fieldName string) *GeoDistanceSort { + s.fieldName = fieldName + return s +} + +// Order defines whether sorting ascending (default) or descending. +func (s *GeoDistanceSort) Order(ascending bool) *GeoDistanceSort { + s.ascending = ascending + return s +} + +// Asc sets ascending sort order. +func (s *GeoDistanceSort) Asc() *GeoDistanceSort { + s.ascending = true + return s +} + +// Desc sets descending sort order. +func (s *GeoDistanceSort) Desc() *GeoDistanceSort { + s.ascending = false + return s +} + +// Point specifies a point to create the range distance aggregations from. +func (s *GeoDistanceSort) Point(lat, lon float64) *GeoDistanceSort { + s.points = append(s.points, GeoPointFromLatLon(lat, lon)) + return s +} + +// Points specifies the geo point(s) to create the range distance aggregations from. +func (s *GeoDistanceSort) Points(points ...*GeoPoint) *GeoDistanceSort { + s.points = append(s.points, points...) + return s +} + +// GeoHashes specifies the geo point to create the range distance aggregations from. +func (s *GeoDistanceSort) GeoHashes(geohashes ...string) *GeoDistanceSort { + s.geohashes = append(s.geohashes, geohashes...) + return s +} + +// Unit specifies the distance unit to use. It defaults to km. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/common-options.html#distance-units +// for details. +func (s *GeoDistanceSort) Unit(unit string) *GeoDistanceSort { + s.unit = unit + return s +} + +// GeoDistance is an alias for DistanceType. +func (s *GeoDistanceSort) GeoDistance(geoDistance string) *GeoDistanceSort { + return s.DistanceType(geoDistance) +} + +// DistanceType describes how to compute the distance, e.g. "arc" or "plane". +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-sort.html#geo-sorting +// for details. +func (s *GeoDistanceSort) DistanceType(distanceType string) *GeoDistanceSort { + s.distanceType = &distanceType + return s +} + +// SortMode specifies what values to pick in case a document contains +// multiple values for the targeted sort field. Possible values are: +// min, max, sum, and avg. +func (s *GeoDistanceSort) SortMode(sortMode string) *GeoDistanceSort { + s.sortMode = &sortMode + return s +} + +// NestedFilter sets a filter that nested objects should match with +// in order to be taken into account for sorting. +func (s *GeoDistanceSort) NestedFilter(nestedFilter Query) *GeoDistanceSort { + s.nestedFilter = nestedFilter + return s +} + +// NestedPath is used if sorting occurs on a field that is inside a +// nested object. +func (s *GeoDistanceSort) NestedPath(nestedPath string) *GeoDistanceSort { + s.nestedPath = &nestedPath + return s +} + +// NestedSort is available starting with 6.1 and will replace NestedFilter +// and NestedPath. +func (s *GeoDistanceSort) NestedSort(nestedSort *NestedSort) *GeoDistanceSort { + s.nestedSort = nestedSort + return s +} + +// Source returns the JSON-serializable data. +func (s *GeoDistanceSort) Source() (interface{}, error) { + source := make(map[string]interface{}) + x := make(map[string]interface{}) + source["_geo_distance"] = x + + // Points + var ptarr []interface{} + for _, pt := range s.points { + ptarr = append(ptarr, pt.Source()) + } + for _, geohash := range s.geohashes { + ptarr = append(ptarr, geohash) + } + x[s.fieldName] = ptarr + + if s.unit != "" { + x["unit"] = s.unit + } + if s.distanceType != nil { + x["distance_type"] = *s.distanceType + } + + if s.ascending { + x["order"] = "asc" + } else { + x["order"] = "desc" + } + if s.sortMode != nil { + x["mode"] = *s.sortMode + } + if s.nestedFilter != nil { + src, err := s.nestedFilter.Source() + if err != nil { + return nil, err + } + x["nested_filter"] = src + } + if s.nestedPath != nil { + x["nested_path"] = *s.nestedPath + } + if s.nestedSort != nil { + src, err := s.nestedSort.Source() + if err != nil { + return nil, err + } + x["nested"] = src + } + return source, nil +} + +// -- ScriptSort -- + +// ScriptSort sorts by a custom script. See +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-scripting.html#modules-scripting +// for details about scripting. +type ScriptSort struct { + Sorter + script *Script + typ string + ascending bool + sortMode *string + nestedFilter Query + nestedPath *string + nestedSort *NestedSort +} + +// NewScriptSort creates and initializes a new ScriptSort. +// You must provide a script and a type, e.g. "string" or "number". +func NewScriptSort(script *Script, typ string) *ScriptSort { + return &ScriptSort{ + script: script, + typ: typ, + ascending: true, + } +} + +// Type sets the script type, which can be either "string" or "number". +func (s *ScriptSort) Type(typ string) *ScriptSort { + s.typ = typ + return s +} + +// Order defines whether sorting ascending (default) or descending. +func (s *ScriptSort) Order(ascending bool) *ScriptSort { + s.ascending = ascending + return s +} + +// Asc sets ascending sort order. +func (s *ScriptSort) Asc() *ScriptSort { + s.ascending = true + return s +} + +// Desc sets descending sort order. +func (s *ScriptSort) Desc() *ScriptSort { + s.ascending = false + return s +} + +// SortMode specifies what values to pick in case a document contains +// multiple values for the targeted sort field. Possible values are: +// min or max. +func (s *ScriptSort) SortMode(sortMode string) *ScriptSort { + s.sortMode = &sortMode + return s +} + +// NestedFilter sets a filter that nested objects should match with +// in order to be taken into account for sorting. +func (s *ScriptSort) NestedFilter(nestedFilter Query) *ScriptSort { + s.nestedFilter = nestedFilter + return s +} + +// NestedPath is used if sorting occurs on a field that is inside a +// nested object. +func (s *ScriptSort) NestedPath(nestedPath string) *ScriptSort { + s.nestedPath = &nestedPath + return s +} + +// NestedSort is available starting with 6.1 and will replace NestedFilter +// and NestedPath. +func (s *ScriptSort) NestedSort(nestedSort *NestedSort) *ScriptSort { + s.nestedSort = nestedSort + return s +} + +// Source returns the JSON-serializable data. +func (s *ScriptSort) Source() (interface{}, error) { + if s.script == nil { + return nil, errors.New("ScriptSort expected a script") + } + source := make(map[string]interface{}) + x := make(map[string]interface{}) + source["_script"] = x + + src, err := s.script.Source() + if err != nil { + return nil, err + } + x["script"] = src + + x["type"] = s.typ + + if s.ascending { + x["order"] = "asc" + } else { + x["order"] = "desc" + } + if s.sortMode != nil { + x["mode"] = *s.sortMode + } + if s.nestedFilter != nil { + src, err := s.nestedFilter.Source() + if err != nil { + return nil, err + } + x["nested_filter"] = src + } + if s.nestedPath != nil { + x["nested_path"] = *s.nestedPath + } + if s.nestedSort != nil { + src, err := s.nestedSort.Source() + if err != nil { + return nil, err + } + x["nested"] = src + } + return source, nil +} + +// -- NestedSort -- + +// NestedSort is used for fields that are inside a nested object. +// It takes a "path" argument and an optional nested filter that the +// nested objects should match with in order to be taken into account +// for sorting. +// +// NestedSort is available from 6.1 and replaces nestedFilter and nestedPath +// in the other sorters. +type NestedSort struct { + Sorter + path string + filter Query + nestedSort *NestedSort +} + +// NewNestedSort creates a new NestedSort. +func NewNestedSort(path string) *NestedSort { + return &NestedSort{path: path} +} + +// Filter sets the filter. +func (s *NestedSort) Filter(filter Query) *NestedSort { + s.filter = filter + return s +} + +// NestedSort embeds another level of nested sorting. +func (s *NestedSort) NestedSort(nestedSort *NestedSort) *NestedSort { + s.nestedSort = nestedSort + return s +} + +// Source returns the JSON-serializable data. +func (s *NestedSort) Source() (interface{}, error) { + source := make(map[string]interface{}) + + if s.path != "" { + source["path"] = s.path + } + if s.filter != nil { + src, err := s.filter.Source() + if err != nil { + return nil, err + } + source["filter"] = src + } + if s.nestedSort != nil { + src, err := s.nestedSort.Source() + if err != nil { + return nil, err + } + source["nested"] = src + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/suggest_field.go b/vendor/github.com/olivere/elastic/v7/suggest_field.go new file mode 100644 index 0000000000..8405a6f9e6 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggest_field.go @@ -0,0 +1,90 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "encoding/json" + "errors" +) + +// SuggestField can be used by the caller to specify a suggest field +// at index time. For a detailed example, see e.g. +// https://www.elastic.co/blog/you-complete-me. +type SuggestField struct { + inputs []string + weight int + contextQueries []SuggesterContextQuery +} + +func NewSuggestField(input ...string) *SuggestField { + return &SuggestField{ + inputs: input, + weight: -1, + } +} + +func (f *SuggestField) Input(input ...string) *SuggestField { + if f.inputs == nil { + f.inputs = make([]string, 0) + } + f.inputs = append(f.inputs, input...) + return f +} + +func (f *SuggestField) Weight(weight int) *SuggestField { + f.weight = weight + return f +} + +func (f *SuggestField) ContextQuery(queries ...SuggesterContextQuery) *SuggestField { + f.contextQueries = append(f.contextQueries, queries...) + return f +} + +// MarshalJSON encodes SuggestField into JSON. +func (f *SuggestField) MarshalJSON() ([]byte, error) { + source := make(map[string]interface{}) + + if f.inputs != nil { + switch len(f.inputs) { + case 1: + source["input"] = f.inputs[0] + default: + source["input"] = f.inputs + } + } + + if f.weight >= 0 { + source["weight"] = f.weight + } + + switch len(f.contextQueries) { + case 0: + case 1: + src, err := f.contextQueries[0].Source() + if err != nil { + return nil, err + } + source["contexts"] = src + default: + ctxq := make(map[string]interface{}) + for _, query := range f.contextQueries { + src, err := query.Source() + if err != nil { + return nil, err + } + m, ok := src.(map[string]interface{}) + if !ok { + return nil, errors.New("SuggesterContextQuery must be of type map[string]interface{}") + } + for k, v := range m { + ctxq[k] = v + } + } + source["contexts"] = ctxq + } + + return json.Marshal(source) +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester.go b/vendor/github.com/olivere/elastic/v7/suggester.go new file mode 100644 index 0000000000..f7dc48f90a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester.go @@ -0,0 +1,15 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// Represents the generic suggester interface. +// A suggester's only purpose is to return the +// source of the query as a JSON-serializable +// object. Returning a map[string]interface{} +// will do. +type Suggester interface { + Name() string + Source(includeName bool) (interface{}, error) +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester_completion.go b/vendor/github.com/olivere/elastic/v7/suggester_completion.go new file mode 100644 index 0000000000..668f0c2e7b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester_completion.go @@ -0,0 +1,358 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// CompletionSuggester is a fast suggester for e.g. type-ahead completion. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-completion.html +// for more details. +type CompletionSuggester struct { + Suggester + name string + text string + prefix string + regex string + field string + analyzer string + size *int + shardSize *int + contextQueries []SuggesterContextQuery + + fuzzyOptions *FuzzyCompletionSuggesterOptions + regexOptions *RegexCompletionSuggesterOptions + skipDuplicates *bool +} + +// Creates a new completion suggester. +func NewCompletionSuggester(name string) *CompletionSuggester { + return &CompletionSuggester{ + name: name, + } +} + +func (q *CompletionSuggester) Name() string { + return q.name +} + +func (q *CompletionSuggester) Text(text string) *CompletionSuggester { + q.text = text + return q +} + +func (q *CompletionSuggester) Prefix(prefix string) *CompletionSuggester { + q.prefix = prefix + return q +} + +func (q *CompletionSuggester) PrefixWithEditDistance(prefix string, editDistance interface{}) *CompletionSuggester { + q.prefix = prefix + q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions().EditDistance(editDistance) + return q +} + +func (q *CompletionSuggester) PrefixWithOptions(prefix string, options *FuzzyCompletionSuggesterOptions) *CompletionSuggester { + q.prefix = prefix + q.fuzzyOptions = options + return q +} + +func (q *CompletionSuggester) FuzzyOptions(options *FuzzyCompletionSuggesterOptions) *CompletionSuggester { + q.fuzzyOptions = options + return q +} + +func (q *CompletionSuggester) Fuzziness(fuzziness interface{}) *CompletionSuggester { + if q.fuzzyOptions == nil { + q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions() + } + q.fuzzyOptions = q.fuzzyOptions.EditDistance(fuzziness) + return q +} + +func (q *CompletionSuggester) Regex(regex string) *CompletionSuggester { + q.regex = regex + return q +} + +func (q *CompletionSuggester) RegexWithOptions(regex string, options *RegexCompletionSuggesterOptions) *CompletionSuggester { + q.regex = regex + q.regexOptions = options + return q +} + +func (q *CompletionSuggester) RegexOptions(options *RegexCompletionSuggesterOptions) *CompletionSuggester { + q.regexOptions = options + return q +} + +func (q *CompletionSuggester) SkipDuplicates(skipDuplicates bool) *CompletionSuggester { + q.skipDuplicates = &skipDuplicates + return q +} + +func (q *CompletionSuggester) Field(field string) *CompletionSuggester { + q.field = field + return q +} + +func (q *CompletionSuggester) Analyzer(analyzer string) *CompletionSuggester { + q.analyzer = analyzer + return q +} + +func (q *CompletionSuggester) Size(size int) *CompletionSuggester { + q.size = &size + return q +} + +func (q *CompletionSuggester) ShardSize(shardSize int) *CompletionSuggester { + q.shardSize = &shardSize + return q +} + +func (q *CompletionSuggester) ContextQuery(query SuggesterContextQuery) *CompletionSuggester { + q.contextQueries = append(q.contextQueries, query) + return q +} + +func (q *CompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) *CompletionSuggester { + q.contextQueries = append(q.contextQueries, queries...) + return q +} + +// completionSuggesterRequest is necessary because the order in which +// the JSON elements are routed to Elasticsearch is relevant. +// We got into trouble when using plain maps because the text element +// needs to go before the completion element. +type completionSuggesterRequest struct { + Text string `json:"text,omitempty"` + Prefix string `json:"prefix,omitempty"` + Regex string `json:"regex,omitempty"` + Completion interface{} `json:"completion,omitempty"` +} + +// Source creates the JSON data for the completion suggester. +func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) { + cs := &completionSuggesterRequest{} + + if q.text != "" { + cs.Text = q.text + } + if q.prefix != "" { + cs.Prefix = q.prefix + } + if q.regex != "" { + cs.Regex = q.regex + } + + suggester := make(map[string]interface{}) + cs.Completion = suggester + + if q.analyzer != "" { + suggester["analyzer"] = q.analyzer + } + if q.field != "" { + suggester["field"] = q.field + } + if q.size != nil { + suggester["size"] = *q.size + } + if q.shardSize != nil { + suggester["shard_size"] = *q.shardSize + } + switch len(q.contextQueries) { + case 0: + case 1: + src, err := q.contextQueries[0].Source() + if err != nil { + return nil, err + } + suggester["contexts"] = src + default: + ctxq := make(map[string]interface{}) + for _, query := range q.contextQueries { + src, err := query.Source() + if err != nil { + return nil, err + } + // Merge the dictionary into ctxq + m, ok := src.(map[string]interface{}) + if !ok { + return nil, errors.New("elastic: context query is not a map") + } + for k, v := range m { + ctxq[k] = v + } + } + suggester["contexts"] = ctxq + } + + // Fuzzy options + if q.fuzzyOptions != nil { + src, err := q.fuzzyOptions.Source() + if err != nil { + return nil, err + } + suggester["fuzzy"] = src + } + + // Regex options + if q.regexOptions != nil { + src, err := q.regexOptions.Source() + if err != nil { + return nil, err + } + suggester["regex"] = src + } + + if q.skipDuplicates != nil { + suggester["skip_duplicates"] = *q.skipDuplicates + } + + // TODO(oe) Add completion-suggester specific parameters here + + if !includeName { + return cs, nil + } + + source := make(map[string]interface{}) + source[q.name] = cs + return source, nil +} + +// -- Fuzzy options -- + +// FuzzyCompletionSuggesterOptions represents the options for fuzzy completion suggester. +type FuzzyCompletionSuggesterOptions struct { + editDistance interface{} + transpositions *bool + minLength *int + prefixLength *int + unicodeAware *bool + maxDeterminizedStates *int +} + +// NewFuzzyCompletionSuggesterOptions initializes a new FuzzyCompletionSuggesterOptions instance. +func NewFuzzyCompletionSuggesterOptions() *FuzzyCompletionSuggesterOptions { + return &FuzzyCompletionSuggesterOptions{} +} + +// EditDistance specifies the maximum number of edits, e.g. a number like "1" or "2" +// or a string like "0..2" or ">5". +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/common-options.html#fuzziness +// for details. +func (o *FuzzyCompletionSuggesterOptions) EditDistance(editDistance interface{}) *FuzzyCompletionSuggesterOptions { + o.editDistance = editDistance + return o +} + +// Transpositions, if set to true, are counted as one change instead of two (defaults to true). +func (o *FuzzyCompletionSuggesterOptions) Transpositions(transpositions bool) *FuzzyCompletionSuggesterOptions { + o.transpositions = &transpositions + return o +} + +// MinLength represents the minimum length of the input before fuzzy suggestions are returned (defaults to 3). +func (o *FuzzyCompletionSuggesterOptions) MinLength(minLength int) *FuzzyCompletionSuggesterOptions { + o.minLength = &minLength + return o +} + +// PrefixLength represents the minimum length of the input, which is not checked for +// fuzzy alternatives (defaults to 1). +func (o *FuzzyCompletionSuggesterOptions) PrefixLength(prefixLength int) *FuzzyCompletionSuggesterOptions { + o.prefixLength = &prefixLength + return o +} + +// UnicodeAware, if true, all measurements (like fuzzy edit distance, transpositions, and lengths) +// are measured in Unicode code points instead of in bytes. This is slightly slower than +// raw bytes, so it is set to false by default. +func (o *FuzzyCompletionSuggesterOptions) UnicodeAware(unicodeAware bool) *FuzzyCompletionSuggesterOptions { + o.unicodeAware = &unicodeAware + return o +} + +// MaxDeterminizedStates is currently undocumented in Elasticsearch. It represents +// the maximum automaton states allowed for fuzzy expansion. +func (o *FuzzyCompletionSuggesterOptions) MaxDeterminizedStates(max int) *FuzzyCompletionSuggesterOptions { + o.maxDeterminizedStates = &max + return o +} + +// Source creates the JSON data. +func (o *FuzzyCompletionSuggesterOptions) Source() (interface{}, error) { + out := make(map[string]interface{}) + + if o.editDistance != nil { + out["fuzziness"] = o.editDistance + } + if o.transpositions != nil { + out["transpositions"] = *o.transpositions + } + if o.minLength != nil { + out["min_length"] = *o.minLength + } + if o.prefixLength != nil { + out["prefix_length"] = *o.prefixLength + } + if o.unicodeAware != nil { + out["unicode_aware"] = *o.unicodeAware + } + if o.maxDeterminizedStates != nil { + out["max_determinized_states"] = *o.maxDeterminizedStates + } + + return out, nil +} + +// -- Regex options -- + +// RegexCompletionSuggesterOptions represents the options for regex completion suggester. +type RegexCompletionSuggesterOptions struct { + flags interface{} // string or int + maxDeterminizedStates *int +} + +// NewRegexCompletionSuggesterOptions initializes a new RegexCompletionSuggesterOptions instance. +func NewRegexCompletionSuggesterOptions() *RegexCompletionSuggesterOptions { + return &RegexCompletionSuggesterOptions{} +} + +// Flags represents internal regex flags. +// Possible flags are ALL (default), ANYSTRING, COMPLEMENT, EMPTY, INTERSECTION, INTERVAL, or NONE. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-completion.html#regex +// for details. +func (o *RegexCompletionSuggesterOptions) Flags(flags interface{}) *RegexCompletionSuggesterOptions { + o.flags = flags + return o +} + +// MaxDeterminizedStates represents the maximum automaton states allowed for regex expansion. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-completion.html#regex +// for details. +func (o *RegexCompletionSuggesterOptions) MaxDeterminizedStates(max int) *RegexCompletionSuggesterOptions { + o.maxDeterminizedStates = &max + return o +} + +// Source creates the JSON data. +func (o *RegexCompletionSuggesterOptions) Source() (interface{}, error) { + out := make(map[string]interface{}) + + if o.flags != nil { + out["flags"] = o.flags + } + if o.maxDeterminizedStates != nil { + out["max_determinized_states"] = *o.maxDeterminizedStates + } + + return out, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester_context.go b/vendor/github.com/olivere/elastic/v7/suggester_context.go new file mode 100644 index 0000000000..fa6e445254 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester_context.go @@ -0,0 +1,124 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import "errors" + +// SuggesterContextQuery is used to define context information within +// a suggestion request. +type SuggesterContextQuery interface { + Source() (interface{}, error) +} + +// ContextSuggester is a fast suggester for e.g. type-ahead completion that supports filtering and boosting based on contexts. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/suggester-context.html +// for more details. +type ContextSuggester struct { + Suggester + name string + prefix string + field string + size *int + contextQueries []SuggesterContextQuery +} + +// Creates a new context suggester. +func NewContextSuggester(name string) *ContextSuggester { + return &ContextSuggester{ + name: name, + contextQueries: make([]SuggesterContextQuery, 0), + } +} + +func (q *ContextSuggester) Name() string { + return q.name +} + +func (q *ContextSuggester) Prefix(prefix string) *ContextSuggester { + q.prefix = prefix + return q +} + +func (q *ContextSuggester) Field(field string) *ContextSuggester { + q.field = field + return q +} + +func (q *ContextSuggester) Size(size int) *ContextSuggester { + q.size = &size + return q +} + +func (q *ContextSuggester) ContextQuery(query SuggesterContextQuery) *ContextSuggester { + q.contextQueries = append(q.contextQueries, query) + return q +} + +func (q *ContextSuggester) ContextQueries(queries ...SuggesterContextQuery) *ContextSuggester { + q.contextQueries = append(q.contextQueries, queries...) + return q +} + +// contextSuggesterRequest is necessary because the order in which +// the JSON elements are routed to Elasticsearch is relevant. +// We got into trouble when using plain maps because the text element +// needs to go before the completion element. +type contextSuggesterRequest struct { + Prefix string `json:"prefix"` + Completion interface{} `json:"completion"` +} + +// Creates the source for the context suggester. +func (q *ContextSuggester) Source(includeName bool) (interface{}, error) { + cs := &contextSuggesterRequest{} + + if q.prefix != "" { + cs.Prefix = q.prefix + } + + suggester := make(map[string]interface{}) + cs.Completion = suggester + + if q.field != "" { + suggester["field"] = q.field + } + if q.size != nil { + suggester["size"] = *q.size + } + switch len(q.contextQueries) { + case 0: + case 1: + src, err := q.contextQueries[0].Source() + if err != nil { + return nil, err + } + suggester["contexts"] = src + default: + ctxq := make(map[string]interface{}) + for _, query := range q.contextQueries { + src, err := query.Source() + if err != nil { + return nil, err + } + // Merge the dictionary into ctxq + m, ok := src.(map[string]interface{}) + if !ok { + return nil, errors.New("elastic: context query is not a map") + } + for k, v := range m { + ctxq[k] = v + } + } + suggester["contexts"] = ctxq + } + + if !includeName { + return cs, nil + } + + source := make(map[string]interface{}) + source[q.name] = cs + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester_context_category.go b/vendor/github.com/olivere/elastic/v7/suggester_context_category.go new file mode 100644 index 0000000000..ccca3018a4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester_context_category.go @@ -0,0 +1,154 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// -- SuggesterCategoryMapping -- + +// SuggesterCategoryMapping provides a mapping for a category context in a suggester. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/suggester-context.html#_category_mapping. +type SuggesterCategoryMapping struct { + name string + fieldName string + defaultValues []string +} + +// NewSuggesterCategoryMapping creates a new SuggesterCategoryMapping. +func NewSuggesterCategoryMapping(name string) *SuggesterCategoryMapping { + return &SuggesterCategoryMapping{ + name: name, + defaultValues: make([]string, 0), + } +} + +func (q *SuggesterCategoryMapping) DefaultValues(values ...string) *SuggesterCategoryMapping { + q.defaultValues = append(q.defaultValues, values...) + return q +} + +func (q *SuggesterCategoryMapping) FieldName(fieldName string) *SuggesterCategoryMapping { + q.fieldName = fieldName + return q +} + +// Source returns a map that will be used to serialize the context query as JSON. +func (q *SuggesterCategoryMapping) Source() (interface{}, error) { + source := make(map[string]interface{}) + + x := make(map[string]interface{}) + source[q.name] = x + + x["type"] = "category" + + switch len(q.defaultValues) { + case 0: + x["default"] = q.defaultValues + case 1: + x["default"] = q.defaultValues[0] + default: + x["default"] = q.defaultValues + } + + if q.fieldName != "" { + x["path"] = q.fieldName + } + return source, nil +} + +// -- SuggesterCategoryQuery -- + +// SuggesterCategoryQuery provides querying a category context in a suggester. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/suggester-context.html#_category_query. +type SuggesterCategoryQuery struct { + name string + values map[string]*int +} + +// NewSuggesterCategoryQuery creates a new SuggesterCategoryQuery. +func NewSuggesterCategoryQuery(name string, values ...string) *SuggesterCategoryQuery { + q := &SuggesterCategoryQuery{ + name: name, + values: make(map[string]*int), + } + + if len(values) > 0 { + q.Values(values...) + } + return q +} + +func (q *SuggesterCategoryQuery) Value(val string) *SuggesterCategoryQuery { + q.values[val] = nil + return q +} + +func (q *SuggesterCategoryQuery) ValueWithBoost(val string, boost int) *SuggesterCategoryQuery { + q.values[val] = &boost + return q +} + +func (q *SuggesterCategoryQuery) Values(values ...string) *SuggesterCategoryQuery { + for _, val := range values { + q.values[val] = nil + } + return q +} + +// Source returns a map that will be used to serialize the context query as JSON. +func (q *SuggesterCategoryQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + + switch len(q.values) { + case 0: + source[q.name] = make([]string, 0) + default: + contexts := make([]interface{}, 0) + for val, boost := range q.values { + context := make(map[string]interface{}) + context["context"] = val + if boost != nil { + context["boost"] = *boost + } + contexts = append(contexts, context) + } + source[q.name] = contexts + } + + return source, nil +} + +type SuggesterCategoryIndex struct { + name string + values []string +} + +// NewSuggesterCategoryIndex creates a new SuggesterCategoryIndex. +func NewSuggesterCategoryIndex(name string, values ...string) *SuggesterCategoryIndex { + q := &SuggesterCategoryIndex{ + name: name, + values: values, + } + return q +} + +func (q *SuggesterCategoryIndex) Values(values ...string) *SuggesterCategoryIndex { + q.values = append(q.values, values...) + return q +} + +// Source returns a map that will be used to serialize the context query as JSON. +func (q *SuggesterCategoryIndex) Source() (interface{}, error) { + source := make(map[string]interface{}) + + switch len(q.values) { + case 0: + source[q.name] = make([]string, 0) + case 1: + source[q.name] = q.values[0] + default: + source[q.name] = q.values + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester_context_geo.go b/vendor/github.com/olivere/elastic/v7/suggester_context_geo.go new file mode 100644 index 0000000000..82ddd57358 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester_context_geo.go @@ -0,0 +1,178 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// -- SuggesterGeoMapping -- + +// SuggesterGeoMapping provides a mapping for a geolocation context in a suggester. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/suggester-context.html#_geo_location_mapping. +type SuggesterGeoMapping struct { + name string + defaultLocations []*GeoPoint + precision []string + neighbors *bool + fieldName string +} + +// NewSuggesterGeoMapping creates a new SuggesterGeoMapping. +func NewSuggesterGeoMapping(name string) *SuggesterGeoMapping { + return &SuggesterGeoMapping{ + name: name, + } +} + +func (q *SuggesterGeoMapping) DefaultLocations(locations ...*GeoPoint) *SuggesterGeoMapping { + q.defaultLocations = append(q.defaultLocations, locations...) + return q +} + +func (q *SuggesterGeoMapping) Precision(precision ...string) *SuggesterGeoMapping { + q.precision = append(q.precision, precision...) + return q +} + +func (q *SuggesterGeoMapping) Neighbors(neighbors bool) *SuggesterGeoMapping { + q.neighbors = &neighbors + return q +} + +func (q *SuggesterGeoMapping) FieldName(fieldName string) *SuggesterGeoMapping { + q.fieldName = fieldName + return q +} + +// Source returns a map that will be used to serialize the context query as JSON. +func (q *SuggesterGeoMapping) Source() (interface{}, error) { + source := make(map[string]interface{}) + + x := make(map[string]interface{}) + source[q.name] = x + + x["type"] = "geo" + + if len(q.precision) > 0 { + x["precision"] = q.precision + } + if q.neighbors != nil { + x["neighbors"] = *q.neighbors + } + + switch len(q.defaultLocations) { + case 0: + case 1: + x["default"] = q.defaultLocations[0].Source() + default: + var arr []interface{} + for _, p := range q.defaultLocations { + arr = append(arr, p.Source()) + } + x["default"] = arr + } + + if q.fieldName != "" { + x["path"] = q.fieldName + } + return source, nil +} + +// -- SuggesterGeoQuery -- + +// SuggesterGeoQuery provides querying a geolocation context in a suggester. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/suggester-context.html#_geo_location_query +type SuggesterGeoQuery struct { + name string + location *GeoPoint + precision string + neighbours []string + boost *int +} + +// NewSuggesterGeoQuery creates a new SuggesterGeoQuery. +func NewSuggesterGeoQuery(name string, location *GeoPoint) *SuggesterGeoQuery { + return &SuggesterGeoQuery{ + name: name, + location: location, + neighbours: make([]string, 0), + } +} + +func (q *SuggesterGeoQuery) Precision(precision string) *SuggesterGeoQuery { + q.precision = precision + return q +} + +func (q *SuggesterGeoQuery) Neighbours(neighbours ...string) *SuggesterGeoQuery { + q.neighbours = append(q.neighbours, neighbours...) + return q +} + +func (q *SuggesterGeoQuery) Boost(boost int) *SuggesterGeoQuery { + q.boost = &boost + return q +} + +// Source returns a map that will be used to serialize the context query as JSON. +func (q *SuggesterGeoQuery) Source() (interface{}, error) { + source := make(map[string]interface{}) + + x := make(map[string]interface{}) + source[q.name] = x + + if q.location != nil { + x["context"] = q.location.Source() + } + if q.precision != "" { + x["precision"] = q.precision + } + if q.boost != nil { + x["boost"] = q.boost + } + switch len(q.neighbours) { + case 0: + case 1: + x["neighbours"] = q.neighbours[0] + default: + x["neighbours"] = q.neighbours + } + + return source, nil +} + +type SuggesterGeoIndex struct { + name string + locations []*GeoPoint +} + +// NewSuggesterGeoQuery creates a new SuggesterGeoQuery. +func NewSuggesterGeoIndex(name string) *SuggesterGeoIndex { + return &SuggesterGeoIndex{ + name: name, + } +} + +func (q *SuggesterGeoIndex) Locations(locations ...*GeoPoint) *SuggesterGeoIndex { + q.locations = append(q.locations, locations...) + return q +} + +// Source returns a map that will be used to serialize the context query as JSON. +func (q *SuggesterGeoIndex) Source() (interface{}, error) { + source := make(map[string]interface{}) + + switch len(q.locations) { + case 0: + source[q.name] = make([]string, 0) + case 1: + source[q.name] = q.locations[0].Source() + default: + var arr []interface{} + for _, p := range q.locations { + arr = append(arr, p.Source()) + } + source[q.name] = arr + } + + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester_phrase.go b/vendor/github.com/olivere/elastic/v7/suggester_phrase.go new file mode 100644 index 0000000000..82ce443b70 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester_phrase.go @@ -0,0 +1,550 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// PhraseSuggester provides an API to access word alternatives +// on a per token basis within a certain string distance. +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-phrase.html. +type PhraseSuggester struct { + Suggester + name string + text string + field string + analyzer string + size *int + shardSize *int + contextQueries []SuggesterContextQuery + + // fields specific to a phrase suggester + maxErrors *float64 + separator *string + realWordErrorLikelihood *float64 + confidence *float64 + generators map[string][]CandidateGenerator + gramSize *int + smoothingModel SmoothingModel + forceUnigrams *bool + tokenLimit *int + preTag, postTag *string + collateQuery *Script + collatePreference *string + collateParams map[string]interface{} + collatePrune *bool +} + +// NewPhraseSuggester creates a new PhraseSuggester. +func NewPhraseSuggester(name string) *PhraseSuggester { + return &PhraseSuggester{ + name: name, + collateParams: make(map[string]interface{}), + } +} + +func (q *PhraseSuggester) Name() string { + return q.name +} + +func (q *PhraseSuggester) Text(text string) *PhraseSuggester { + q.text = text + return q +} + +func (q *PhraseSuggester) Field(field string) *PhraseSuggester { + q.field = field + return q +} + +func (q *PhraseSuggester) Analyzer(analyzer string) *PhraseSuggester { + q.analyzer = analyzer + return q +} + +func (q *PhraseSuggester) Size(size int) *PhraseSuggester { + q.size = &size + return q +} + +func (q *PhraseSuggester) ShardSize(shardSize int) *PhraseSuggester { + q.shardSize = &shardSize + return q +} + +func (q *PhraseSuggester) ContextQuery(query SuggesterContextQuery) *PhraseSuggester { + q.contextQueries = append(q.contextQueries, query) + return q +} + +func (q *PhraseSuggester) ContextQueries(queries ...SuggesterContextQuery) *PhraseSuggester { + q.contextQueries = append(q.contextQueries, queries...) + return q +} + +func (q *PhraseSuggester) GramSize(gramSize int) *PhraseSuggester { + if gramSize >= 1 { + q.gramSize = &gramSize + } + return q +} + +func (q *PhraseSuggester) MaxErrors(maxErrors float64) *PhraseSuggester { + q.maxErrors = &maxErrors + return q +} + +func (q *PhraseSuggester) Separator(separator string) *PhraseSuggester { + q.separator = &separator + return q +} + +func (q *PhraseSuggester) RealWordErrorLikelihood(realWordErrorLikelihood float64) *PhraseSuggester { + q.realWordErrorLikelihood = &realWordErrorLikelihood + return q +} + +func (q *PhraseSuggester) Confidence(confidence float64) *PhraseSuggester { + q.confidence = &confidence + return q +} + +func (q *PhraseSuggester) CandidateGenerator(generator CandidateGenerator) *PhraseSuggester { + if q.generators == nil { + q.generators = make(map[string][]CandidateGenerator) + } + typ := generator.Type() + if _, found := q.generators[typ]; !found { + q.generators[typ] = make([]CandidateGenerator, 0) + } + q.generators[typ] = append(q.generators[typ], generator) + return q +} + +func (q *PhraseSuggester) CandidateGenerators(generators ...CandidateGenerator) *PhraseSuggester { + for _, g := range generators { + q = q.CandidateGenerator(g) + } + return q +} + +func (q *PhraseSuggester) ClearCandidateGenerator() *PhraseSuggester { + q.generators = nil + return q +} + +func (q *PhraseSuggester) ForceUnigrams(forceUnigrams bool) *PhraseSuggester { + q.forceUnigrams = &forceUnigrams + return q +} + +func (q *PhraseSuggester) SmoothingModel(smoothingModel SmoothingModel) *PhraseSuggester { + q.smoothingModel = smoothingModel + return q +} + +func (q *PhraseSuggester) TokenLimit(tokenLimit int) *PhraseSuggester { + q.tokenLimit = &tokenLimit + return q +} + +func (q *PhraseSuggester) Highlight(preTag, postTag string) *PhraseSuggester { + q.preTag = &preTag + q.postTag = &postTag + return q +} + +func (q *PhraseSuggester) CollateQuery(collateQuery *Script) *PhraseSuggester { + q.collateQuery = collateQuery + return q +} + +func (q *PhraseSuggester) CollatePreference(collatePreference string) *PhraseSuggester { + q.collatePreference = &collatePreference + return q +} + +func (q *PhraseSuggester) CollateParams(collateParams map[string]interface{}) *PhraseSuggester { + q.collateParams = collateParams + return q +} + +func (q *PhraseSuggester) CollatePrune(collatePrune bool) *PhraseSuggester { + q.collatePrune = &collatePrune + return q +} + +// phraseSuggesterRequest is necessary because the order in which +// the JSON elements are routed to Elasticsearch is relevant. +// We got into trouble when using plain maps because the text element +// needs to go before the simple_phrase element. +type phraseSuggesterRequest struct { + Text string `json:"text"` + Phrase interface{} `json:"phrase"` +} + +// Source generates the source for the phrase suggester. +func (q *PhraseSuggester) Source(includeName bool) (interface{}, error) { + ps := &phraseSuggesterRequest{} + + if q.text != "" { + ps.Text = q.text + } + + suggester := make(map[string]interface{}) + ps.Phrase = suggester + + if q.analyzer != "" { + suggester["analyzer"] = q.analyzer + } + if q.field != "" { + suggester["field"] = q.field + } + if q.size != nil { + suggester["size"] = *q.size + } + if q.shardSize != nil { + suggester["shard_size"] = *q.shardSize + } + switch len(q.contextQueries) { + case 0: + case 1: + src, err := q.contextQueries[0].Source() + if err != nil { + return nil, err + } + suggester["contexts"] = src + default: + var ctxq []interface{} + for _, query := range q.contextQueries { + src, err := query.Source() + if err != nil { + return nil, err + } + ctxq = append(ctxq, src) + } + suggester["contexts"] = ctxq + } + + // Phase-specified parameters + if q.realWordErrorLikelihood != nil { + suggester["real_word_error_likelihood"] = *q.realWordErrorLikelihood + } + if q.confidence != nil { + suggester["confidence"] = *q.confidence + } + if q.separator != nil { + suggester["separator"] = *q.separator + } + if q.maxErrors != nil { + suggester["max_errors"] = *q.maxErrors + } + if q.gramSize != nil { + suggester["gram_size"] = *q.gramSize + } + if q.forceUnigrams != nil { + suggester["force_unigrams"] = *q.forceUnigrams + } + if q.tokenLimit != nil { + suggester["token_limit"] = *q.tokenLimit + } + if q.generators != nil && len(q.generators) > 0 { + for typ, generators := range q.generators { + var arr []interface{} + for _, g := range generators { + src, err := g.Source() + if err != nil { + return nil, err + } + arr = append(arr, src) + } + suggester[typ] = arr + } + } + if q.smoothingModel != nil { + src, err := q.smoothingModel.Source() + if err != nil { + return nil, err + } + x := make(map[string]interface{}) + x[q.smoothingModel.Type()] = src + suggester["smoothing"] = x + } + if q.preTag != nil { + hl := make(map[string]string) + hl["pre_tag"] = *q.preTag + if q.postTag != nil { + hl["post_tag"] = *q.postTag + } + suggester["highlight"] = hl + } + if q.collateQuery != nil { + collate := make(map[string]interface{}) + suggester["collate"] = collate + if q.collateQuery != nil { + src, err := q.collateQuery.Source() + if err != nil { + return nil, err + } + collate["query"] = src + } + if q.collatePreference != nil { + collate["preference"] = *q.collatePreference + } + if len(q.collateParams) > 0 { + collate["params"] = q.collateParams + } + if q.collatePrune != nil { + collate["prune"] = *q.collatePrune + } + } + + if !includeName { + return ps, nil + } + + source := make(map[string]interface{}) + source[q.name] = ps + return source, nil +} + +// -- Smoothing models -- + +type SmoothingModel interface { + Type() string + Source() (interface{}, error) +} + +// StupidBackoffSmoothingModel implements a stupid backoff smoothing model. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-phrase.html#_smoothing_models +// for details about smoothing models. +type StupidBackoffSmoothingModel struct { + discount float64 +} + +func NewStupidBackoffSmoothingModel(discount float64) *StupidBackoffSmoothingModel { + return &StupidBackoffSmoothingModel{ + discount: discount, + } +} + +func (sm *StupidBackoffSmoothingModel) Type() string { + return "stupid_backoff" +} + +func (sm *StupidBackoffSmoothingModel) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["discount"] = sm.discount + return source, nil +} + +// -- + +// LaplaceSmoothingModel implements a laplace smoothing model. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-phrase.html#_smoothing_models +// for details about smoothing models. +type LaplaceSmoothingModel struct { + alpha float64 +} + +func NewLaplaceSmoothingModel(alpha float64) *LaplaceSmoothingModel { + return &LaplaceSmoothingModel{ + alpha: alpha, + } +} + +func (sm *LaplaceSmoothingModel) Type() string { + return "laplace" +} + +func (sm *LaplaceSmoothingModel) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["alpha"] = sm.alpha + return source, nil +} + +// -- + +// LinearInterpolationSmoothingModel implements a linear interpolation +// smoothing model. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-phrase.html#_smoothing_models +// for details about smoothing models. +type LinearInterpolationSmoothingModel struct { + trigramLamda float64 + bigramLambda float64 + unigramLambda float64 +} + +func NewLinearInterpolationSmoothingModel(trigramLamda, bigramLambda, unigramLambda float64) *LinearInterpolationSmoothingModel { + return &LinearInterpolationSmoothingModel{ + trigramLamda: trigramLamda, + bigramLambda: bigramLambda, + unigramLambda: unigramLambda, + } +} + +func (sm *LinearInterpolationSmoothingModel) Type() string { + return "linear_interpolation" +} + +func (sm *LinearInterpolationSmoothingModel) Source() (interface{}, error) { + source := make(map[string]interface{}) + source["trigram_lambda"] = sm.trigramLamda + source["bigram_lambda"] = sm.bigramLambda + source["unigram_lambda"] = sm.unigramLambda + return source, nil +} + +// -- CandidateGenerator -- + +type CandidateGenerator interface { + Type() string + Source() (interface{}, error) +} + +// DirectCandidateGenerator implements a direct candidate generator. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-phrase.html#_smoothing_models +// for details about smoothing models. +type DirectCandidateGenerator struct { + field string + preFilter *string + postFilter *string + suggestMode *string + accuracy *float64 + size *int + sort *string + stringDistance *string + maxEdits *int + maxInspections *int + maxTermFreq *float64 + prefixLength *int + minWordLength *int + minDocFreq *float64 +} + +func NewDirectCandidateGenerator(field string) *DirectCandidateGenerator { + return &DirectCandidateGenerator{ + field: field, + } +} + +func (g *DirectCandidateGenerator) Type() string { + return "direct_generator" +} + +func (g *DirectCandidateGenerator) Field(field string) *DirectCandidateGenerator { + g.field = field + return g +} + +func (g *DirectCandidateGenerator) PreFilter(preFilter string) *DirectCandidateGenerator { + g.preFilter = &preFilter + return g +} + +func (g *DirectCandidateGenerator) PostFilter(postFilter string) *DirectCandidateGenerator { + g.postFilter = &postFilter + return g +} + +func (g *DirectCandidateGenerator) SuggestMode(suggestMode string) *DirectCandidateGenerator { + g.suggestMode = &suggestMode + return g +} + +func (g *DirectCandidateGenerator) Accuracy(accuracy float64) *DirectCandidateGenerator { + g.accuracy = &accuracy + return g +} + +func (g *DirectCandidateGenerator) Size(size int) *DirectCandidateGenerator { + g.size = &size + return g +} + +func (g *DirectCandidateGenerator) Sort(sort string) *DirectCandidateGenerator { + g.sort = &sort + return g +} + +func (g *DirectCandidateGenerator) StringDistance(stringDistance string) *DirectCandidateGenerator { + g.stringDistance = &stringDistance + return g +} + +func (g *DirectCandidateGenerator) MaxEdits(maxEdits int) *DirectCandidateGenerator { + g.maxEdits = &maxEdits + return g +} + +func (g *DirectCandidateGenerator) MaxInspections(maxInspections int) *DirectCandidateGenerator { + g.maxInspections = &maxInspections + return g +} + +func (g *DirectCandidateGenerator) MaxTermFreq(maxTermFreq float64) *DirectCandidateGenerator { + g.maxTermFreq = &maxTermFreq + return g +} + +func (g *DirectCandidateGenerator) PrefixLength(prefixLength int) *DirectCandidateGenerator { + g.prefixLength = &prefixLength + return g +} + +func (g *DirectCandidateGenerator) MinWordLength(minWordLength int) *DirectCandidateGenerator { + g.minWordLength = &minWordLength + return g +} + +func (g *DirectCandidateGenerator) MinDocFreq(minDocFreq float64) *DirectCandidateGenerator { + g.minDocFreq = &minDocFreq + return g +} + +func (g *DirectCandidateGenerator) Source() (interface{}, error) { + source := make(map[string]interface{}) + if g.field != "" { + source["field"] = g.field + } + if g.suggestMode != nil { + source["suggest_mode"] = *g.suggestMode + } + if g.accuracy != nil { + source["accuracy"] = *g.accuracy + } + if g.size != nil { + source["size"] = *g.size + } + if g.sort != nil { + source["sort"] = *g.sort + } + if g.stringDistance != nil { + source["string_distance"] = *g.stringDistance + } + if g.maxEdits != nil { + source["max_edits"] = *g.maxEdits + } + if g.maxInspections != nil { + source["max_inspections"] = *g.maxInspections + } + if g.maxTermFreq != nil { + source["max_term_freq"] = *g.maxTermFreq + } + if g.prefixLength != nil { + source["prefix_length"] = *g.prefixLength + } + if g.minWordLength != nil { + source["min_word_length"] = *g.minWordLength + } + if g.minDocFreq != nil { + source["min_doc_freq"] = *g.minDocFreq + } + if g.preFilter != nil { + source["pre_filter"] = *g.preFilter + } + if g.postFilter != nil { + source["post_filter"] = *g.postFilter + } + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/suggester_term.go b/vendor/github.com/olivere/elastic/v7/suggester_term.go new file mode 100644 index 0000000000..ba7d8b6087 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/suggester_term.go @@ -0,0 +1,233 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +// TermSuggester suggests terms based on edit distance. +// For more details, see +// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-suggesters-term.html. +type TermSuggester struct { + Suggester + name string + text string + field string + analyzer string + size *int + shardSize *int + contextQueries []SuggesterContextQuery + + // fields specific to term suggester + suggestMode string + accuracy *float64 + sort string + stringDistance string + maxEdits *int + maxInspections *int + maxTermFreq *float64 + prefixLength *int + minWordLength *int + minDocFreq *float64 +} + +// NewTermSuggester creates a new TermSuggester. +func NewTermSuggester(name string) *TermSuggester { + return &TermSuggester{ + name: name, + } +} + +func (q *TermSuggester) Name() string { + return q.name +} + +func (q *TermSuggester) Text(text string) *TermSuggester { + q.text = text + return q +} + +func (q *TermSuggester) Field(field string) *TermSuggester { + q.field = field + return q +} + +func (q *TermSuggester) Analyzer(analyzer string) *TermSuggester { + q.analyzer = analyzer + return q +} + +func (q *TermSuggester) Size(size int) *TermSuggester { + q.size = &size + return q +} + +func (q *TermSuggester) ShardSize(shardSize int) *TermSuggester { + q.shardSize = &shardSize + return q +} + +func (q *TermSuggester) ContextQuery(query SuggesterContextQuery) *TermSuggester { + q.contextQueries = append(q.contextQueries, query) + return q +} + +func (q *TermSuggester) ContextQueries(queries ...SuggesterContextQuery) *TermSuggester { + q.contextQueries = append(q.contextQueries, queries...) + return q +} + +func (q *TermSuggester) SuggestMode(suggestMode string) *TermSuggester { + q.suggestMode = suggestMode + return q +} + +func (q *TermSuggester) Accuracy(accuracy float64) *TermSuggester { + q.accuracy = &accuracy + return q +} + +func (q *TermSuggester) Sort(sort string) *TermSuggester { + q.sort = sort + return q +} + +func (q *TermSuggester) StringDistance(stringDistance string) *TermSuggester { + q.stringDistance = stringDistance + return q +} + +func (q *TermSuggester) MaxEdits(maxEdits int) *TermSuggester { + q.maxEdits = &maxEdits + return q +} + +func (q *TermSuggester) MaxInspections(maxInspections int) *TermSuggester { + q.maxInspections = &maxInspections + return q +} + +func (q *TermSuggester) MaxTermFreq(maxTermFreq float64) *TermSuggester { + q.maxTermFreq = &maxTermFreq + return q +} + +func (q *TermSuggester) PrefixLength(prefixLength int) *TermSuggester { + q.prefixLength = &prefixLength + return q +} + +func (q *TermSuggester) MinWordLength(minWordLength int) *TermSuggester { + q.minWordLength = &minWordLength + return q +} + +func (q *TermSuggester) MinDocFreq(minDocFreq float64) *TermSuggester { + q.minDocFreq = &minDocFreq + return q +} + +// termSuggesterRequest is necessary because the order in which +// the JSON elements are routed to Elasticsearch is relevant. +// We got into trouble when using plain maps because the text element +// needs to go before the term element. +type termSuggesterRequest struct { + Text string `json:"text"` + Term interface{} `json:"term"` +} + +// Source generates the source for the term suggester. +func (q *TermSuggester) Source(includeName bool) (interface{}, error) { + // "suggest" : { + // "my-suggest-1" : { + // "text" : "the amsterdma meetpu", + // "term" : { + // "field" : "body" + // } + // }, + // "my-suggest-2" : { + // "text" : "the rottredam meetpu", + // "term" : { + // "field" : "title", + // } + // } + // } + ts := &termSuggesterRequest{} + if q.text != "" { + ts.Text = q.text + } + + suggester := make(map[string]interface{}) + ts.Term = suggester + + if q.analyzer != "" { + suggester["analyzer"] = q.analyzer + } + if q.field != "" { + suggester["field"] = q.field + } + if q.size != nil { + suggester["size"] = *q.size + } + if q.shardSize != nil { + suggester["shard_size"] = *q.shardSize + } + switch len(q.contextQueries) { + case 0: + case 1: + src, err := q.contextQueries[0].Source() + if err != nil { + return nil, err + } + suggester["contexts"] = src + default: + ctxq := make([]interface{}, len(q.contextQueries)) + for i, query := range q.contextQueries { + src, err := query.Source() + if err != nil { + return nil, err + } + ctxq[i] = src + } + suggester["contexts"] = ctxq + } + + // Specific to term suggester + if q.suggestMode != "" { + suggester["suggest_mode"] = q.suggestMode + } + if q.accuracy != nil { + suggester["accuracy"] = *q.accuracy + } + if q.sort != "" { + suggester["sort"] = q.sort + } + if q.stringDistance != "" { + suggester["string_distance"] = q.stringDistance + } + if q.maxEdits != nil { + suggester["max_edits"] = *q.maxEdits + } + if q.maxInspections != nil { + suggester["max_inspections"] = *q.maxInspections + } + if q.maxTermFreq != nil { + suggester["max_term_freq"] = *q.maxTermFreq + } + if q.prefixLength != nil { + suggester["prefix_length"] = *q.prefixLength + } + if q.minWordLength != nil { + suggester["min_word_len"] = *q.minWordLength + } + if q.minDocFreq != nil { + suggester["min_doc_freq"] = *q.minDocFreq + } + + if !includeName { + return ts, nil + } + + source := make(map[string]interface{}) + source[q.name] = ts + return source, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/tasks_cancel.go b/vendor/github.com/olivere/elastic/v7/tasks_cancel.go new file mode 100644 index 0000000000..1294004443 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/tasks_cancel.go @@ -0,0 +1,207 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// TasksCancelService can cancel long-running tasks. +// It is supported as of Elasticsearch 2.3.0. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/tasks.html#task-cancellation +// for details. +type TasksCancelService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + taskId string + actions []string + nodeId []string + parentTaskId string +} + +// NewTasksCancelService creates a new TasksCancelService. +func NewTasksCancelService(client *Client) *TasksCancelService { + return &TasksCancelService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *TasksCancelService) Pretty(pretty bool) *TasksCancelService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *TasksCancelService) Human(human bool) *TasksCancelService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *TasksCancelService) ErrorTrace(errorTrace bool) *TasksCancelService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *TasksCancelService) FilterPath(filterPath ...string) *TasksCancelService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *TasksCancelService) Header(name string, value string) *TasksCancelService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *TasksCancelService) Headers(headers http.Header) *TasksCancelService { + s.headers = headers + return s +} + +// TaskId specifies the task to cancel. Notice that the caller is responsible +// for using the correct format, i.e. node_id:task_number, as specified in +// the REST API. +func (s *TasksCancelService) TaskId(taskId string) *TasksCancelService { + s.taskId = taskId + return s +} + +// TaskIdFromNodeAndId specifies the task to cancel. Set id to -1 for all tasks. +func (s *TasksCancelService) TaskIdFromNodeAndId(nodeId string, id int64) *TasksCancelService { + if id != -1 { + s.taskId = fmt.Sprintf("%s:%d", nodeId, id) + } + return s +} + +// Actions is a list of actions that should be cancelled. Leave empty to cancel all. +func (s *TasksCancelService) Actions(actions ...string) *TasksCancelService { + s.actions = append(s.actions, actions...) + return s +} + +// NodeId is a list of node IDs or names to limit the returned information; +// use `_local` to return information from the node you're connecting to, +// leave empty to get information from all nodes. +func (s *TasksCancelService) NodeId(nodeId ...string) *TasksCancelService { + s.nodeId = append(s.nodeId, nodeId...) + return s +} + +// ParentTaskId specifies to cancel tasks with specified parent task id. +// Notice that the caller is responsible for using the correct format, +// i.e. node_id:task_number, as specified in the REST API. +func (s *TasksCancelService) ParentTaskId(parentTaskId string) *TasksCancelService { + s.parentTaskId = parentTaskId + return s +} + +// ParentTaskIdFromNodeAndId specifies to cancel tasks with specified parent task id. +func (s *TasksCancelService) ParentTaskIdFromNodeAndId(nodeId string, id int64) *TasksCancelService { + if id != -1 { + s.parentTaskId = fmt.Sprintf("%s:%d", nodeId, id) + } + return s +} + +// buildURL builds the URL for the operation. +func (s *TasksCancelService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if s.taskId != "" { + path, err = uritemplates.Expand("/_tasks/{task_id}/_cancel", map[string]string{ + "task_id": s.taskId, + }) + } else { + path = "/_tasks/_cancel" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.actions) > 0 { + params.Set("actions", strings.Join(s.actions, ",")) + } + if len(s.nodeId) > 0 { + params.Set("nodes", strings.Join(s.nodeId, ",")) + } + if s.parentTaskId != "" { + params.Set("parent_task_id", s.parentTaskId) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *TasksCancelService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *TasksCancelService) Do(ctx context.Context) (*TasksListResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(TasksListResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/tasks_get_task.go b/vendor/github.com/olivere/elastic/v7/tasks_get_task.go new file mode 100644 index 0000000000..f0123d1df4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/tasks_get_task.go @@ -0,0 +1,168 @@ +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// TasksGetTaskService retrieves the state of a task in the cluster. It is part of the Task Management API +// documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/tasks.html#_current_tasks_information. +type TasksGetTaskService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + taskId string + waitForCompletion *bool +} + +// NewTasksGetTaskService creates a new TasksGetTaskService. +func NewTasksGetTaskService(client *Client) *TasksGetTaskService { + return &TasksGetTaskService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *TasksGetTaskService) Pretty(pretty bool) *TasksGetTaskService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *TasksGetTaskService) Human(human bool) *TasksGetTaskService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *TasksGetTaskService) ErrorTrace(errorTrace bool) *TasksGetTaskService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *TasksGetTaskService) FilterPath(filterPath ...string) *TasksGetTaskService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *TasksGetTaskService) Header(name string, value string) *TasksGetTaskService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *TasksGetTaskService) Headers(headers http.Header) *TasksGetTaskService { + s.headers = headers + return s +} + +// TaskId specifies the task to return. Notice that the caller is responsible +// for using the correct format, i.e. node_id:task_number, as specified in +// the REST API. +func (s *TasksGetTaskService) TaskId(taskId string) *TasksGetTaskService { + s.taskId = taskId + return s +} + +// TaskIdFromNodeAndId indicates to return the task on the given node with specified id. +func (s *TasksGetTaskService) TaskIdFromNodeAndId(nodeId string, id int64) *TasksGetTaskService { + s.taskId = fmt.Sprintf("%s:%d", nodeId, id) + return s +} + +// WaitForCompletion indicates whether to wait for the matching tasks +// to complete (default: false). +func (s *TasksGetTaskService) WaitForCompletion(waitForCompletion bool) *TasksGetTaskService { + s.waitForCompletion = &waitForCompletion + return s +} + +// buildURL builds the URL for the operation. +func (s *TasksGetTaskService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_tasks/{task_id}", map[string]string{ + "task_id": s.taskId, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.waitForCompletion; v != nil { + params.Set("wait_for_completion", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *TasksGetTaskService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *TasksGetTaskService) Do(ctx context.Context) (*TasksGetTaskResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(TasksGetTaskResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + ret.Header = res.Header + return ret, nil +} + +type TasksGetTaskResponse struct { + Header http.Header `json:"-"` + Completed bool `json:"completed"` + Task *TaskInfo `json:"task,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/tasks_list.go b/vendor/github.com/olivere/elastic/v7/tasks_list.go new file mode 100644 index 0000000000..dc1ad495cf --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/tasks_list.go @@ -0,0 +1,280 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// TasksListService retrieves the list of currently executing tasks +// on one ore more nodes in the cluster. It is part of the Task Management API +// documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/tasks.html. +// +// It is supported as of Elasticsearch 2.3.0. +type TasksListService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + taskId []string + actions []string + detailed *bool + nodeId []string + parentTaskId string + waitForCompletion *bool + groupBy string +} + +// NewTasksListService creates a new TasksListService. +func NewTasksListService(client *Client) *TasksListService { + return &TasksListService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *TasksListService) Pretty(pretty bool) *TasksListService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *TasksListService) Human(human bool) *TasksListService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *TasksListService) ErrorTrace(errorTrace bool) *TasksListService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *TasksListService) FilterPath(filterPath ...string) *TasksListService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *TasksListService) Header(name string, value string) *TasksListService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *TasksListService) Headers(headers http.Header) *TasksListService { + s.headers = headers + return s +} + +// TaskId indicates to returns the task(s) with specified id(s). +// Notice that the caller is responsible for using the correct format, +// i.e. node_id:task_number, as specified in the REST API. +func (s *TasksListService) TaskId(taskId ...string) *TasksListService { + s.taskId = append(s.taskId, taskId...) + return s +} + +// Actions is a list of actions that should be returned. Leave empty to return all. +func (s *TasksListService) Actions(actions ...string) *TasksListService { + s.actions = append(s.actions, actions...) + return s +} + +// Detailed indicates whether to return detailed task information (default: false). +func (s *TasksListService) Detailed(detailed bool) *TasksListService { + s.detailed = &detailed + return s +} + +// NodeId is a list of node IDs or names to limit the returned information; +// use `_local` to return information from the node you're connecting to, +// leave empty to get information from all nodes. +func (s *TasksListService) NodeId(nodeId ...string) *TasksListService { + s.nodeId = append(s.nodeId, nodeId...) + return s +} + +// ParentTaskId returns tasks with specified parent task id. +// Notice that the caller is responsible for using the correct format, +// i.e. node_id:task_number, as specified in the REST API. +func (s *TasksListService) ParentTaskId(parentTaskId string) *TasksListService { + s.parentTaskId = parentTaskId + return s +} + +// WaitForCompletion indicates whether to wait for the matching tasks +// to complete (default: false). +func (s *TasksListService) WaitForCompletion(waitForCompletion bool) *TasksListService { + s.waitForCompletion = &waitForCompletion + return s +} + +// GroupBy groups tasks by nodes or parent/child relationships. +// As of now, it can either be "nodes" (default) or "parents" or "none". +func (s *TasksListService) GroupBy(groupBy string) *TasksListService { + s.groupBy = groupBy + return s +} + +// buildURL builds the URL for the operation. +func (s *TasksListService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.taskId) > 0 { + path, err = uritemplates.Expand("/_tasks/{task_id}", map[string]string{ + "task_id": strings.Join(s.taskId, ","), + }) + } else { + path = "/_tasks" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.actions) > 0 { + params.Set("actions", strings.Join(s.actions, ",")) + } + if v := s.detailed; v != nil { + params.Set("detailed", fmt.Sprint(*v)) + } + if len(s.nodeId) > 0 { + params.Set("nodes", strings.Join(s.nodeId, ",")) + } + if s.parentTaskId != "" { + params.Set("parent_task_id", s.parentTaskId) + } + if v := s.waitForCompletion; v != nil { + params.Set("wait_for_completion", fmt.Sprint(*v)) + } + if s.groupBy != "" { + params.Set("group_by", s.groupBy) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *TasksListService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *TasksListService) Do(ctx context.Context) (*TasksListResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(TasksListResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + ret.Header = res.Header + return ret, nil +} + +// TasksListResponse is the response of TasksListService.Do. +type TasksListResponse struct { + Header http.Header `json:"-"` + TaskFailures []*TaskOperationFailure `json:"task_failures"` + NodeFailures []*FailedNodeException `json:"node_failures"` + // Nodes returns the tasks per node. The key is the node id. + Nodes map[string]*DiscoveryNode `json:"nodes"` +} + +type TaskOperationFailure struct { + TaskId int64 `json:"task_id"` // this is a long in the Java source + NodeId string `json:"node_id"` + Status string `json:"status"` + Reason *ErrorDetails `json:"reason"` +} + +type FailedNodeException struct { + *ErrorDetails + NodeId string `json:"node_id"` +} + +type DiscoveryNode struct { + Name string `json:"name"` + TransportAddress string `json:"transport_address"` + Host string `json:"host"` + IP string `json:"ip"` + Roles []string `json:"roles"` // "master", "data", or "ingest" + Attributes map[string]interface{} `json:"attributes"` + // Tasks returns the tasks by its id (as a string). + Tasks map[string]*TaskInfo `json:"tasks"` +} + +// TaskInfo represents information about a currently running task. +type TaskInfo struct { + Node string `json:"node"` + Id int64 `json:"id"` // the task id (yes, this is a long in the Java source) + Type string `json:"type"` + Action string `json:"action"` + Status interface{} `json:"status"` // has separate implementations of Task.Status in Java for reindexing, replication, and "RawTaskStatus" + Description interface{} `json:"description"` // same as Status + StartTime string `json:"start_time"` + StartTimeInMillis int64 `json:"start_time_in_millis"` + RunningTime string `json:"running_time"` + RunningTimeInNanos int64 `json:"running_time_in_nanos"` + Cancellable bool `json:"cancellable"` + ParentTaskId string `json:"parent_task_id"` // like "YxJnVYjwSBm_AUbzddTajQ:12356" + Headers map[string]string `json:"headers"` +} + +// StartTaskResult is used in cases where a task gets started asynchronously and +// the operation simply returnes a TaskID to watch for via the Task Management API. +type StartTaskResult struct { + Header http.Header `json:"-"` + TaskId string `json:"task"` +} diff --git a/vendor/github.com/olivere/elastic/v7/termvectors.go b/vendor/github.com/olivere/elastic/v7/termvectors.go new file mode 100644 index 0000000000..a196810633 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/termvectors.go @@ -0,0 +1,520 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// TermvectorsService returns information and statistics on terms in the +// fields of a particular document. The document could be stored in the +// index or artificially provided by the user. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-termvectors.html +// for documentation. +type TermvectorsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + index string + typ string + dfs *bool + doc interface{} + fieldStatistics *bool + fields []string + filter *TermvectorsFilterSettings + perFieldAnalyzer map[string]string + offsets *bool + parent string + payloads *bool + positions *bool + preference string + realtime *bool + routing string + termStatistics *bool + version interface{} + versionType string + bodyJson interface{} + bodyString string +} + +// NewTermvectorsService creates a new TermvectorsService. +func NewTermvectorsService(client *Client) *TermvectorsService { + return &TermvectorsService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *TermvectorsService) Pretty(pretty bool) *TermvectorsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *TermvectorsService) Human(human bool) *TermvectorsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *TermvectorsService) ErrorTrace(errorTrace bool) *TermvectorsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *TermvectorsService) FilterPath(filterPath ...string) *TermvectorsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *TermvectorsService) Header(name string, value string) *TermvectorsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *TermvectorsService) Headers(headers http.Header) *TermvectorsService { + s.headers = headers + return s +} + +// Index in which the document resides. +func (s *TermvectorsService) Index(index string) *TermvectorsService { + s.index = index + return s +} + +// Type of the document. +// +// Deprecated: Types are in the process of being removed. +func (s *TermvectorsService) Type(typ string) *TermvectorsService { + s.typ = typ + return s +} + +// Id of the document. +func (s *TermvectorsService) Id(id string) *TermvectorsService { + s.id = id + return s +} + +// Dfs specifies if distributed frequencies should be returned instead +// shard frequencies. +func (s *TermvectorsService) Dfs(dfs bool) *TermvectorsService { + s.dfs = &dfs + return s +} + +// Doc is the document to analyze. +func (s *TermvectorsService) Doc(doc interface{}) *TermvectorsService { + s.doc = doc + return s +} + +// FieldStatistics specifies if document count, sum of document frequencies +// and sum of total term frequencies should be returned. +func (s *TermvectorsService) FieldStatistics(fieldStatistics bool) *TermvectorsService { + s.fieldStatistics = &fieldStatistics + return s +} + +// Fields a list of fields to return. +func (s *TermvectorsService) Fields(fields ...string) *TermvectorsService { + if s.fields == nil { + s.fields = make([]string, 0) + } + s.fields = append(s.fields, fields...) + return s +} + +// Filter adds terms filter settings. +func (s *TermvectorsService) Filter(filter *TermvectorsFilterSettings) *TermvectorsService { + s.filter = filter + return s +} + +// PerFieldAnalyzer allows to specify a different analyzer than the one +// at the field. +func (s *TermvectorsService) PerFieldAnalyzer(perFieldAnalyzer map[string]string) *TermvectorsService { + s.perFieldAnalyzer = perFieldAnalyzer + return s +} + +// Offsets specifies if term offsets should be returned. +func (s *TermvectorsService) Offsets(offsets bool) *TermvectorsService { + s.offsets = &offsets + return s +} + +// Parent id of documents. +func (s *TermvectorsService) Parent(parent string) *TermvectorsService { + s.parent = parent + return s +} + +// Payloads specifies if term payloads should be returned. +func (s *TermvectorsService) Payloads(payloads bool) *TermvectorsService { + s.payloads = &payloads + return s +} + +// Positions specifies if term positions should be returned. +func (s *TermvectorsService) Positions(positions bool) *TermvectorsService { + s.positions = &positions + return s +} + +// Preference specify the node or shard the operation +// should be performed on (default: random). +func (s *TermvectorsService) Preference(preference string) *TermvectorsService { + s.preference = preference + return s +} + +// Realtime specifies if request is real-time as opposed to +// near-real-time (default: true). +func (s *TermvectorsService) Realtime(realtime bool) *TermvectorsService { + s.realtime = &realtime + return s +} + +// Routing is a specific routing value. +func (s *TermvectorsService) Routing(routing string) *TermvectorsService { + s.routing = routing + return s +} + +// TermStatistics specifies if total term frequency and document frequency +// should be returned. +func (s *TermvectorsService) TermStatistics(termStatistics bool) *TermvectorsService { + s.termStatistics = &termStatistics + return s +} + +// Version an explicit version number for concurrency control. +func (s *TermvectorsService) Version(version interface{}) *TermvectorsService { + s.version = version + return s +} + +// VersionType specifies a version type ("internal", "external", or "external_gte"). +func (s *TermvectorsService) VersionType(versionType string) *TermvectorsService { + s.versionType = versionType + return s +} + +// BodyJson defines the body parameters. See documentation. +func (s *TermvectorsService) BodyJson(body interface{}) *TermvectorsService { + s.bodyJson = body + return s +} + +// BodyString defines the body parameters as a string. See documentation. +func (s *TermvectorsService) BodyString(body string) *TermvectorsService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *TermvectorsService) buildURL() (string, url.Values, error) { + var pathParam = map[string]string{ + "index": s.index, + } + path := "/{index}" + var err error + + if s.typ != "" { + pathParam["type"] = s.typ + path += "/{type}" + } else { + path += "/_termvectors" + } + if s.id != "" { + pathParam["id"] = s.id + path += "/{id}" + } + if s.typ != "" { + path += "/_termvectors" + } + + path, err = uritemplates.Expand(path, pathParam) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.dfs; v != nil { + params.Set("dfs", fmt.Sprint(*v)) + } + if v := s.fieldStatistics; v != nil { + params.Set("field_statistics", fmt.Sprint(*v)) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + if v := s.offsets; v != nil { + params.Set("offsets", fmt.Sprint(*v)) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if v := s.payloads; v != nil { + params.Set("payloads", fmt.Sprint(*v)) + } + if v := s.positions; v != nil { + params.Set("positions", fmt.Sprint(*v)) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if v := s.realtime; v != nil { + params.Set("realtime", fmt.Sprint(*v)) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if v := s.termStatistics; v != nil { + params.Set("term_statistics", fmt.Sprint(*v)) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%v", s.version)) + } + if s.versionType != "" { + params.Set("version_type", s.versionType) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *TermvectorsService) Validate() error { + var invalid []string + if s.index == "" { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *TermvectorsService) Do(ctx context.Context) (*TermvectorsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else if s.bodyString != "" { + body = s.bodyString + } else { + data := make(map[string]interface{}) + if s.doc != nil { + data["doc"] = s.doc + } + if len(s.perFieldAnalyzer) > 0 { + data["per_field_analyzer"] = s.perFieldAnalyzer + } + if s.filter != nil { + src, err := s.filter.Source() + if err != nil { + return nil, err + } + data["filter"] = src + } + if len(data) > 0 { + body = data + } + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(TermvectorsResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// -- Filter settings -- + +// TermvectorsFilterSettings adds additional filters to a Termsvector request. +// It allows to filter terms based on their tf-idf scores. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-termvectors.html#_terms_filtering +// for more information. +type TermvectorsFilterSettings struct { + maxNumTerms *int64 + minTermFreq *int64 + maxTermFreq *int64 + minDocFreq *int64 + maxDocFreq *int64 + minWordLength *int64 + maxWordLength *int64 +} + +// NewTermvectorsFilterSettings creates and initializes a new TermvectorsFilterSettings struct. +func NewTermvectorsFilterSettings() *TermvectorsFilterSettings { + return &TermvectorsFilterSettings{} +} + +// MaxNumTerms specifies the maximum number of terms the must be returned per field. +func (fs *TermvectorsFilterSettings) MaxNumTerms(value int64) *TermvectorsFilterSettings { + fs.maxNumTerms = &value + return fs +} + +// MinTermFreq ignores words with less than this frequency in the source doc. +func (fs *TermvectorsFilterSettings) MinTermFreq(value int64) *TermvectorsFilterSettings { + fs.minTermFreq = &value + return fs +} + +// MaxTermFreq ignores words with more than this frequency in the source doc. +func (fs *TermvectorsFilterSettings) MaxTermFreq(value int64) *TermvectorsFilterSettings { + fs.maxTermFreq = &value + return fs +} + +// MinDocFreq ignores terms which do not occur in at least this many docs. +func (fs *TermvectorsFilterSettings) MinDocFreq(value int64) *TermvectorsFilterSettings { + fs.minDocFreq = &value + return fs +} + +// MaxDocFreq ignores terms which occur in more than this many docs. +func (fs *TermvectorsFilterSettings) MaxDocFreq(value int64) *TermvectorsFilterSettings { + fs.maxDocFreq = &value + return fs +} + +// MinWordLength specifies the minimum word length below which words will be ignored. +func (fs *TermvectorsFilterSettings) MinWordLength(value int64) *TermvectorsFilterSettings { + fs.minWordLength = &value + return fs +} + +// MaxWordLength specifies the maximum word length above which words will be ignored. +func (fs *TermvectorsFilterSettings) MaxWordLength(value int64) *TermvectorsFilterSettings { + fs.maxWordLength = &value + return fs +} + +// Source returns JSON for the query. +func (fs *TermvectorsFilterSettings) Source() (interface{}, error) { + source := make(map[string]interface{}) + if fs.maxNumTerms != nil { + source["max_num_terms"] = *fs.maxNumTerms + } + if fs.minTermFreq != nil { + source["min_term_freq"] = *fs.minTermFreq + } + if fs.maxTermFreq != nil { + source["max_term_freq"] = *fs.maxTermFreq + } + if fs.minDocFreq != nil { + source["min_doc_freq"] = *fs.minDocFreq + } + if fs.maxDocFreq != nil { + source["max_doc_freq"] = *fs.maxDocFreq + } + if fs.minWordLength != nil { + source["min_word_length"] = *fs.minWordLength + } + if fs.maxWordLength != nil { + source["max_word_length"] = *fs.maxWordLength + } + return source, nil +} + +// -- Response types -- + +type TokenInfo struct { + StartOffset int64 `json:"start_offset"` + EndOffset int64 `json:"end_offset"` + Position int64 `json:"position"` + Payload string `json:"payload"` +} + +type TermsInfo struct { + DocFreq int64 `json:"doc_freq"` + Score float64 `json:"score"` + TermFreq int64 `json:"term_freq"` + Ttf int64 `json:"ttf"` + Tokens []TokenInfo `json:"tokens"` +} + +type FieldStatistics struct { + DocCount int64 `json:"doc_count"` + SumDocFreq int64 `json:"sum_doc_freq"` + SumTtf int64 `json:"sum_ttf"` +} + +type TermVectorsFieldInfo struct { + FieldStatistics FieldStatistics `json:"field_statistics"` + Terms map[string]TermsInfo `json:"terms"` +} + +// TermvectorsResponse is the response of TermvectorsService.Do. +type TermvectorsResponse struct { + Index string `json:"_index"` + Type string `json:"_type"` + Id string `json:"_id,omitempty"` + Version int `json:"_version"` + Found bool `json:"found"` + Took int64 `json:"took"` + TermVectors map[string]TermVectorsFieldInfo `json:"term_vectors"` +} diff --git a/vendor/github.com/olivere/elastic/v7/update.go b/vendor/github.com/olivere/elastic/v7/update.go new file mode 100644 index 0000000000..468032e6a0 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/update.go @@ -0,0 +1,412 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// UpdateService updates a document in Elasticsearch. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-update.html +// for details. +type UpdateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index string + typ string + id string + routing string + parent string + script *Script + fields []string + fsc *FetchSourceContext + version *int64 + versionType string + retryOnConflict *int + refresh string + waitForActiveShards string + upsert interface{} + scriptedUpsert *bool + docAsUpsert *bool + detectNoop *bool + doc interface{} + timeout string + ifSeqNo *int64 + ifPrimaryTerm *int64 +} + +// NewUpdateService creates the service to update documents in Elasticsearch. +func NewUpdateService(client *Client) *UpdateService { + return &UpdateService{ + client: client, + typ: "_doc", + fields: make([]string, 0), + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *UpdateService) Pretty(pretty bool) *UpdateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *UpdateService) Human(human bool) *UpdateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *UpdateService) ErrorTrace(errorTrace bool) *UpdateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *UpdateService) FilterPath(filterPath ...string) *UpdateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *UpdateService) Header(name string, value string) *UpdateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *UpdateService) Headers(headers http.Header) *UpdateService { + s.headers = headers + return s +} + +// Index is the name of the Elasticsearch index (required). +func (s *UpdateService) Index(name string) *UpdateService { + s.index = name + return s +} + +// Type is the type of the document. +// +// Deprecated: Types are in the process of being removed. +func (s *UpdateService) Type(typ string) *UpdateService { + s.typ = typ + return s +} + +// Id is the identifier of the document to update (required). +func (s *UpdateService) Id(id string) *UpdateService { + s.id = id + return s +} + +// Routing specifies a specific routing value. +func (s *UpdateService) Routing(routing string) *UpdateService { + s.routing = routing + return s +} + +// Parent sets the id of the parent document. +func (s *UpdateService) Parent(parent string) *UpdateService { + s.parent = parent + return s +} + +// Script is the script definition. +func (s *UpdateService) Script(script *Script) *UpdateService { + s.script = script + return s +} + +// RetryOnConflict specifies how many times the operation should be retried +// when a conflict occurs (default: 0). +func (s *UpdateService) RetryOnConflict(retryOnConflict int) *UpdateService { + s.retryOnConflict = &retryOnConflict + return s +} + +// Fields is a list of fields to return in the response. +func (s *UpdateService) Fields(fields ...string) *UpdateService { + s.fields = make([]string, 0, len(fields)) + s.fields = append(s.fields, fields...) + return s +} + +// Version defines the explicit version number for concurrency control. +func (s *UpdateService) Version(version int64) *UpdateService { + s.version = &version + return s +} + +// VersionType is e.g. "internal". +func (s *UpdateService) VersionType(versionType string) *UpdateService { + s.versionType = versionType + return s +} + +// Refresh the index after performing the update. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *UpdateService) Refresh(refresh string) *UpdateService { + s.refresh = refresh + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active before +// proceeding with the update operation. Defaults to 1, meaning the primary shard only. +// Set to `all` for all shard copies, otherwise set to any non-negative value less than +// or equal to the total number of copies for the shard (number of replicas + 1). +func (s *UpdateService) WaitForActiveShards(waitForActiveShards string) *UpdateService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// Doc allows for updating a partial document. +func (s *UpdateService) Doc(doc interface{}) *UpdateService { + s.doc = doc + return s +} + +// Upsert can be used to index the document when it doesn't exist yet. +// Use this e.g. to initialize a document with a default value. +func (s *UpdateService) Upsert(doc interface{}) *UpdateService { + s.upsert = doc + return s +} + +// DocAsUpsert can be used to insert the document if it doesn't already exist. +func (s *UpdateService) DocAsUpsert(docAsUpsert bool) *UpdateService { + s.docAsUpsert = &docAsUpsert + return s +} + +// DetectNoop will instruct Elasticsearch to check if changes will occur +// when updating via Doc. It there aren't any changes, the request will +// turn into a no-op. +func (s *UpdateService) DetectNoop(detectNoop bool) *UpdateService { + s.detectNoop = &detectNoop + return s +} + +// ScriptedUpsert should be set to true if the referenced script +// (defined in Script or ScriptId) should be called to perform an insert. +// The default is false. +func (s *UpdateService) ScriptedUpsert(scriptedUpsert bool) *UpdateService { + s.scriptedUpsert = &scriptedUpsert + return s +} + +// Timeout is an explicit timeout for the operation, e.g. "1000", "1s" or "500ms". +func (s *UpdateService) Timeout(timeout string) *UpdateService { + s.timeout = timeout + return s +} + +// IfSeqNo indicates to only perform the update operation if the last +// operation that has changed the document has the specified sequence number. +func (s *UpdateService) IfSeqNo(seqNo int64) *UpdateService { + s.ifSeqNo = &seqNo + return s +} + +// IfPrimaryTerm indicates to only perform the update operation if the +// last operation that has changed the document has the specified primary term. +func (s *UpdateService) IfPrimaryTerm(primaryTerm int64) *UpdateService { + s.ifPrimaryTerm = &primaryTerm + return s +} + +// FetchSource asks Elasticsearch to return the updated _source in the response. +func (s *UpdateService) FetchSource(fetchSource bool) *UpdateService { + if s.fsc == nil { + s.fsc = NewFetchSourceContext(fetchSource) + } else { + s.fsc.SetFetchSource(fetchSource) + } + return s +} + +// FetchSourceContext indicates that _source should be returned in the response, +// allowing wildcard patterns to be defined via FetchSourceContext. +func (s *UpdateService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *UpdateService { + s.fsc = fetchSourceContext + return s +} + +// url returns the URL part of the document request. +func (s *UpdateService) url() (string, url.Values, error) { + // Build url + var path string + var err error + if s.typ == "" || s.typ == "_doc" { + path, err = uritemplates.Expand("/{index}/_update/{id}", map[string]string{ + "index": s.index, + "id": s.id, + }) + } else { + path, err = uritemplates.Expand("/{index}/{type}/{id}/_update", map[string]string{ + "index": s.index, + "type": s.typ, + "id": s.id, + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.routing != "" { + params.Set("routing", s.routing) + } + if s.parent != "" { + params.Set("parent", s.parent) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + if len(s.fields) > 0 { + params.Set("fields", strings.Join(s.fields, ",")) + } + if s.version != nil { + params.Set("version", fmt.Sprintf("%d", *s.version)) + } + if s.versionType != "" { + params.Set("version_type", s.versionType) + } + if s.retryOnConflict != nil { + params.Set("retry_on_conflict", fmt.Sprintf("%v", *s.retryOnConflict)) + } + if v := s.ifSeqNo; v != nil { + params.Set("if_seq_no", fmt.Sprintf("%d", *v)) + } + if v := s.ifPrimaryTerm; v != nil { + params.Set("if_primary_term", fmt.Sprintf("%d", *v)) + } + return path, params, nil +} + +// body returns the body part of the document request. +func (s *UpdateService) body() (interface{}, error) { + source := make(map[string]interface{}) + + if s.script != nil { + src, err := s.script.Source() + if err != nil { + return nil, err + } + source["script"] = src + } + + if v := s.scriptedUpsert; v != nil { + source["scripted_upsert"] = *v + } + + if s.upsert != nil { + source["upsert"] = s.upsert + } + + if s.doc != nil { + source["doc"] = s.doc + } + if v := s.docAsUpsert; v != nil { + source["doc_as_upsert"] = *v + } + if v := s.detectNoop; v != nil { + source["detect_noop"] = *v + } + if s.fsc != nil { + src, err := s.fsc.Source() + if err != nil { + return nil, err + } + source["_source"] = src + } + + return source, nil +} + +// Do executes the update operation. +func (s *UpdateService) Do(ctx context.Context) (*UpdateResponse, error) { + path, params, err := s.url() + if err != nil { + return nil, err + } + + // Get body of the request + body, err := s.body() + if err != nil { + return nil, err + } + + // Get response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return result + ret := new(UpdateResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// UpdateResponse is the result of updating a document in Elasticsearch. +type UpdateResponse struct { + Index string `json:"_index,omitempty"` + Type string `json:"_type,omitempty"` + Id string `json:"_id,omitempty"` + Version int64 `json:"_version,omitempty"` + Result string `json:"result,omitempty"` + Shards *ShardsInfo `json:"_shards,omitempty"` + SeqNo int64 `json:"_seq_no,omitempty"` + PrimaryTerm int64 `json:"_primary_term,omitempty"` + Status int `json:"status,omitempty"` + ForcedRefresh bool `json:"forced_refresh,omitempty"` + GetResult *GetResult `json:"get,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/update_by_query.go b/vendor/github.com/olivere/elastic/v7/update_by_query.go new file mode 100644 index 0000000000..8fdffd349f --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/update_by_query.go @@ -0,0 +1,773 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// UpdateByQueryService is documented at https://www.elastic.co/guide/en/elasticsearch/plugins/master/plugins-reindex.html. +type UpdateByQueryService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + typ []string + script *Script + query Query + body interface{} + xSource []string + xSourceExclude []string + xSourceInclude []string + allowNoIndices *bool + analyzeWildcard *bool + analyzer string + conflicts string + defaultOperator string + docvalueFields []string + df string + expandWildcards string + explain *bool + fielddataFields []string + from *int + ignoreUnavailable *bool + lenient *bool + lowercaseExpandedTerms *bool + pipeline string + preference string + q string + refresh string + requestCache *bool + requestsPerSecond *int + routing []string + scroll string + scrollSize *int + searchTimeout string + searchType string + size *int + slices interface{} + sort []string + stats []string + storedFields []string + suggestField string + suggestMode string + suggestSize *int + suggestText string + terminateAfter *int + timeout string + trackScores *bool + version *bool + versionType *bool + waitForActiveShards string + waitForCompletion *bool +} + +// NewUpdateByQueryService creates a new UpdateByQueryService. +func NewUpdateByQueryService(client *Client) *UpdateByQueryService { + return &UpdateByQueryService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *UpdateByQueryService) Pretty(pretty bool) *UpdateByQueryService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *UpdateByQueryService) Human(human bool) *UpdateByQueryService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *UpdateByQueryService) ErrorTrace(errorTrace bool) *UpdateByQueryService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *UpdateByQueryService) FilterPath(filterPath ...string) *UpdateByQueryService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *UpdateByQueryService) Header(name string, value string) *UpdateByQueryService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *UpdateByQueryService) Headers(headers http.Header) *UpdateByQueryService { + s.headers = headers + return s +} + +// Index is a list of index names to search; use `_all` or empty string to +// perform the operation on all indices. +func (s *UpdateByQueryService) Index(index ...string) *UpdateByQueryService { + s.index = append(s.index, index...) + return s +} + +// Type is a list of document types to search; leave empty to perform +// the operation on all types. +func (s *UpdateByQueryService) Type(typ ...string) *UpdateByQueryService { + s.typ = append(s.typ, typ...) + return s +} + +// Script sets an update script. +func (s *UpdateByQueryService) Script(script *Script) *UpdateByQueryService { + s.script = script + return s +} + +// Body specifies the body of the request. It overrides data being specified via +// SearchService or Script. +func (s *UpdateByQueryService) Body(body string) *UpdateByQueryService { + s.body = body + return s +} + +// XSource is true or false to return the _source field or not, +// or a list of fields to return. +func (s *UpdateByQueryService) XSource(xSource ...string) *UpdateByQueryService { + s.xSource = append(s.xSource, xSource...) + return s +} + +// XSourceExclude represents a list of fields to exclude from the returned _source field. +func (s *UpdateByQueryService) XSourceExclude(xSourceExclude ...string) *UpdateByQueryService { + s.xSourceExclude = append(s.xSourceExclude, xSourceExclude...) + return s +} + +// XSourceInclude represents a list of fields to extract and return from the _source field. +func (s *UpdateByQueryService) XSourceInclude(xSourceInclude ...string) *UpdateByQueryService { + s.xSourceInclude = append(s.xSourceInclude, xSourceInclude...) + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices expression +// resolves into no concrete indices. (This includes `_all` string or when +// no indices have been specified). +func (s *UpdateByQueryService) AllowNoIndices(allowNoIndices bool) *UpdateByQueryService { + s.allowNoIndices = &allowNoIndices + return s +} + +// AnalyzeWildcard specifies whether wildcard and prefix queries should be +// analyzed (default: false). +func (s *UpdateByQueryService) AnalyzeWildcard(analyzeWildcard bool) *UpdateByQueryService { + s.analyzeWildcard = &analyzeWildcard + return s +} + +// Analyzer specifies the analyzer to use for the query string. +func (s *UpdateByQueryService) Analyzer(analyzer string) *UpdateByQueryService { + s.analyzer = analyzer + return s +} + +// Conflicts indicates what to do when the process detects version conflicts. +// Possible values are "proceed" and "abort". +func (s *UpdateByQueryService) Conflicts(conflicts string) *UpdateByQueryService { + s.conflicts = conflicts + return s +} + +// AbortOnVersionConflict aborts the request on version conflicts. +// It is an alias to setting Conflicts("abort"). +func (s *UpdateByQueryService) AbortOnVersionConflict() *UpdateByQueryService { + s.conflicts = "abort" + return s +} + +// ProceedOnVersionConflict aborts the request on version conflicts. +// It is an alias to setting Conflicts("proceed"). +func (s *UpdateByQueryService) ProceedOnVersionConflict() *UpdateByQueryService { + s.conflicts = "proceed" + return s +} + +// DefaultOperator is the default operator for query string query (AND or OR). +func (s *UpdateByQueryService) DefaultOperator(defaultOperator string) *UpdateByQueryService { + s.defaultOperator = defaultOperator + return s +} + +// DF specifies the field to use as default where no field prefix is given in the query string. +func (s *UpdateByQueryService) DF(df string) *UpdateByQueryService { + s.df = df + return s +} + +// DocvalueFields specifies the list of fields to return as the docvalue representation of a field for each hit. +func (s *UpdateByQueryService) DocvalueFields(docvalueFields ...string) *UpdateByQueryService { + s.docvalueFields = docvalueFields + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *UpdateByQueryService) ExpandWildcards(expandWildcards string) *UpdateByQueryService { + s.expandWildcards = expandWildcards + return s +} + +// Explain specifies whether to return detailed information about score +// computation as part of a hit. +func (s *UpdateByQueryService) Explain(explain bool) *UpdateByQueryService { + s.explain = &explain + return s +} + +// FielddataFields is a list of fields to return as the field data +// representation of a field for each hit. +func (s *UpdateByQueryService) FielddataFields(fielddataFields ...string) *UpdateByQueryService { + s.fielddataFields = append(s.fielddataFields, fielddataFields...) + return s +} + +// From is the starting offset (default: 0). +func (s *UpdateByQueryService) From(from int) *UpdateByQueryService { + s.from = &from + return s +} + +// IgnoreUnavailable indicates whether specified concrete indices should be +// ignored when unavailable (missing or closed). +func (s *UpdateByQueryService) IgnoreUnavailable(ignoreUnavailable bool) *UpdateByQueryService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// Lenient specifies whether format-based query failures +// (such as providing text to a numeric field) should be ignored. +func (s *UpdateByQueryService) Lenient(lenient bool) *UpdateByQueryService { + s.lenient = &lenient + return s +} + +// LowercaseExpandedTerms specifies whether query terms should be lowercased. +func (s *UpdateByQueryService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *UpdateByQueryService { + s.lowercaseExpandedTerms = &lowercaseExpandedTerms + return s +} + +// Pipeline specifies the ingest pipeline to set on index requests made by this action (default: none). +func (s *UpdateByQueryService) Pipeline(pipeline string) *UpdateByQueryService { + s.pipeline = pipeline + return s +} + +// Preference specifies the node or shard the operation should be performed on +// (default: random). +func (s *UpdateByQueryService) Preference(preference string) *UpdateByQueryService { + s.preference = preference + return s +} + +// Q specifies the query in the Lucene query string syntax. +func (s *UpdateByQueryService) Q(q string) *UpdateByQueryService { + s.q = q + return s +} + +// Query sets a query definition using the Query DSL. +func (s *UpdateByQueryService) Query(query Query) *UpdateByQueryService { + s.query = query + return s +} + +// Refresh indicates whether the effected indexes should be refreshed. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-refresh.html +// for details. +func (s *UpdateByQueryService) Refresh(refresh string) *UpdateByQueryService { + s.refresh = refresh + return s +} + +// RequestCache specifies if request cache should be used for this request +// or not, defaults to index level setting. +func (s *UpdateByQueryService) RequestCache(requestCache bool) *UpdateByQueryService { + s.requestCache = &requestCache + return s +} + +// RequestsPerSecond sets the throttle on this request in sub-requests per second. +// -1 means set no throttle as does "unlimited" which is the only non-float this accepts. +func (s *UpdateByQueryService) RequestsPerSecond(requestsPerSecond int) *UpdateByQueryService { + s.requestsPerSecond = &requestsPerSecond + return s +} + +// Routing is a list of specific routing values. +func (s *UpdateByQueryService) Routing(routing ...string) *UpdateByQueryService { + s.routing = append(s.routing, routing...) + return s +} + +// Scroll specifies how long a consistent view of the index should be maintained +// for scrolled search. +func (s *UpdateByQueryService) Scroll(scroll string) *UpdateByQueryService { + s.scroll = scroll + return s +} + +// ScrollSize is the size on the scroll request powering the update_by_query. +func (s *UpdateByQueryService) ScrollSize(scrollSize int) *UpdateByQueryService { + s.scrollSize = &scrollSize + return s +} + +// SearchTimeout defines an explicit timeout for each search request. +// Defaults to no timeout. +func (s *UpdateByQueryService) SearchTimeout(searchTimeout string) *UpdateByQueryService { + s.searchTimeout = searchTimeout + return s +} + +// SearchType is the search operation type. Possible values are +// "query_then_fetch" and "dfs_query_then_fetch". +func (s *UpdateByQueryService) SearchType(searchType string) *UpdateByQueryService { + s.searchType = searchType + return s +} + +// Size represents the number of hits to return (default: 10). +func (s *UpdateByQueryService) Size(size int) *UpdateByQueryService { + s.size = &size + return s +} + +// Slices represents the number of slices (default: 1). +// It used to be a number, but can be set to "auto" as of 6.7. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/docs-update-by-query.html#docs-update-by-query-slice +// for details. +func (s *UpdateByQueryService) Slices(slices interface{}) *UpdateByQueryService { + s.slices = slices + return s +} + +// Sort is a list of : pairs. +func (s *UpdateByQueryService) Sort(sort ...string) *UpdateByQueryService { + s.sort = append(s.sort, sort...) + return s +} + +// SortByField adds a sort order. +func (s *UpdateByQueryService) SortByField(field string, ascending bool) *UpdateByQueryService { + if ascending { + s.sort = append(s.sort, fmt.Sprintf("%s:asc", field)) + } else { + s.sort = append(s.sort, fmt.Sprintf("%s:desc", field)) + } + return s +} + +// Stats specifies specific tag(s) of the request for logging and statistical purposes. +func (s *UpdateByQueryService) Stats(stats ...string) *UpdateByQueryService { + s.stats = append(s.stats, stats...) + return s +} + +// StoredFields specifies the list of stored fields to return as part of a hit. +func (s *UpdateByQueryService) StoredFields(storedFields ...string) *UpdateByQueryService { + s.storedFields = storedFields + return s +} + +// SuggestField specifies which field to use for suggestions. +func (s *UpdateByQueryService) SuggestField(suggestField string) *UpdateByQueryService { + s.suggestField = suggestField + return s +} + +// SuggestMode specifies the suggest mode. Possible values are +// "missing", "popular", and "always". +func (s *UpdateByQueryService) SuggestMode(suggestMode string) *UpdateByQueryService { + s.suggestMode = suggestMode + return s +} + +// SuggestSize specifies how many suggestions to return in response. +func (s *UpdateByQueryService) SuggestSize(suggestSize int) *UpdateByQueryService { + s.suggestSize = &suggestSize + return s +} + +// SuggestText specifies the source text for which the suggestions should be returned. +func (s *UpdateByQueryService) SuggestText(suggestText string) *UpdateByQueryService { + s.suggestText = suggestText + return s +} + +// TerminateAfter indicates the maximum number of documents to collect +// for each shard, upon reaching which the query execution will terminate early. +func (s *UpdateByQueryService) TerminateAfter(terminateAfter int) *UpdateByQueryService { + s.terminateAfter = &terminateAfter + return s +} + +// Timeout is the time each individual bulk request should wait for shards +// that are unavailable. +func (s *UpdateByQueryService) Timeout(timeout string) *UpdateByQueryService { + s.timeout = timeout + return s +} + +// TimeoutInMillis sets the timeout in milliseconds. +func (s *UpdateByQueryService) TimeoutInMillis(timeoutInMillis int) *UpdateByQueryService { + s.timeout = fmt.Sprintf("%dms", timeoutInMillis) + return s +} + +// TrackScores indicates whether to calculate and return scores even if +// they are not used for sorting. +func (s *UpdateByQueryService) TrackScores(trackScores bool) *UpdateByQueryService { + s.trackScores = &trackScores + return s +} + +// Version specifies whether to return document version as part of a hit. +func (s *UpdateByQueryService) Version(version bool) *UpdateByQueryService { + s.version = &version + return s +} + +// VersionType indicates if the document increment the version number (internal) +// on hit or not (reindex). +func (s *UpdateByQueryService) VersionType(versionType bool) *UpdateByQueryService { + s.versionType = &versionType + return s +} + +// WaitForActiveShards sets the number of shard copies that must be active before proceeding +// with the update by query operation. Defaults to 1, meaning the primary shard only. +// Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal +// to the total number of copies for the shard (number of replicas + 1). +func (s *UpdateByQueryService) WaitForActiveShards(waitForActiveShards string) *UpdateByQueryService { + s.waitForActiveShards = waitForActiveShards + return s +} + +// WaitForCompletion indicates if the request should block until the reindex is complete. +func (s *UpdateByQueryService) WaitForCompletion(waitForCompletion bool) *UpdateByQueryService { + s.waitForCompletion = &waitForCompletion + return s +} + +// buildURL builds the URL for the operation. +func (s *UpdateByQueryService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.typ) > 0 { + path, err = uritemplates.Expand("/{index}/{type}/_update_by_query", map[string]string{ + "index": strings.Join(s.index, ","), + "type": strings.Join(s.typ, ","), + }) + } else { + path, err = uritemplates.Expand("/{index}/_update_by_query", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if len(s.xSource) > 0 { + params.Set("_source", strings.Join(s.xSource, ",")) + } + if len(s.xSourceExclude) > 0 { + params.Set("_source_excludes", strings.Join(s.xSourceExclude, ",")) + } + if len(s.xSourceInclude) > 0 { + params.Set("_source_includes", strings.Join(s.xSourceInclude, ",")) + } + if s.allowNoIndices != nil { + params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) + } + if s.analyzer != "" { + params.Set("analyzer", s.analyzer) + } + if v := s.analyzeWildcard; v != nil { + params.Set("analyze_wildcard", fmt.Sprint(*v)) + } + if s.conflicts != "" { + params.Set("conflicts", s.conflicts) + } + if s.defaultOperator != "" { + params.Set("default_operator", s.defaultOperator) + } + if s.df != "" { + params.Set("df", s.df) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if v := s.explain; v != nil { + params.Set("explain", fmt.Sprint(*v)) + } + if len(s.storedFields) > 0 { + params.Set("stored_fields", strings.Join(s.storedFields, ",")) + } + if len(s.docvalueFields) > 0 { + params.Set("docvalue_fields", strings.Join(s.docvalueFields, ",")) + } + if len(s.fielddataFields) > 0 { + params.Set("fielddata_fields", strings.Join(s.fielddataFields, ",")) + } + if s.from != nil { + params.Set("from", fmt.Sprintf("%d", *s.from)) + } + if v := s.ignoreUnavailable; v != nil { + params.Set("ignore_unavailable", fmt.Sprint(*v)) + } + if v := s.lenient; v != nil { + params.Set("lenient", fmt.Sprint(*v)) + } + if v := s.lowercaseExpandedTerms; v != nil { + params.Set("lowercase_expanded_terms", fmt.Sprint(*v)) + } + if s.pipeline != "" { + params.Set("pipeline", s.pipeline) + } + if s.preference != "" { + params.Set("preference", s.preference) + } + if s.q != "" { + params.Set("q", s.q) + } + if s.refresh != "" { + params.Set("refresh", s.refresh) + } + if v := s.requestCache; v != nil { + params.Set("request_cache", fmt.Sprint(*v)) + } + if len(s.routing) > 0 { + params.Set("routing", strings.Join(s.routing, ",")) + } + if s.scroll != "" { + params.Set("scroll", s.scroll) + } + if s.scrollSize != nil { + params.Set("scroll_size", fmt.Sprintf("%d", *s.scrollSize)) + } + if s.searchTimeout != "" { + params.Set("search_timeout", s.searchTimeout) + } + if s.searchType != "" { + params.Set("search_type", s.searchType) + } + if s.size != nil { + params.Set("size", fmt.Sprintf("%d", *s.size)) + } + if s.slices != nil { + params.Set("slices", fmt.Sprintf("%v", s.slices)) + } + if len(s.sort) > 0 { + params.Set("sort", strings.Join(s.sort, ",")) + } + if len(s.stats) > 0 { + params.Set("stats", strings.Join(s.stats, ",")) + } + if s.suggestField != "" { + params.Set("suggest_field", s.suggestField) + } + if s.suggestMode != "" { + params.Set("suggest_mode", s.suggestMode) + } + if s.suggestSize != nil { + params.Set("suggest_size", fmt.Sprintf("%v", *s.suggestSize)) + } + if s.suggestText != "" { + params.Set("suggest_text", s.suggestText) + } + if s.terminateAfter != nil { + params.Set("terminate_after", fmt.Sprintf("%v", *s.terminateAfter)) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if v := s.trackScores; v != nil { + params.Set("track_scores", fmt.Sprint(*v)) + } + if v := s.version; v != nil { + params.Set("version", fmt.Sprint(*v)) + } + if v := s.versionType; v != nil { + params.Set("version_type", fmt.Sprint(*v)) + } + if s.waitForActiveShards != "" { + params.Set("wait_for_active_shards", s.waitForActiveShards) + } + if v := s.waitForCompletion; v != nil { + params.Set("wait_for_completion", fmt.Sprint(*v)) + } + if s.requestsPerSecond != nil { + params.Set("requests_per_second", fmt.Sprintf("%v", *s.requestsPerSecond)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *UpdateByQueryService) Validate() error { + var invalid []string + if len(s.index) == 0 { + invalid = append(invalid, "Index") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// getBody returns the body part of the document request. +func (s *UpdateByQueryService) getBody() (interface{}, error) { + if s.body != nil { + return s.body, nil + } + source := make(map[string]interface{}) + if s.script != nil { + src, err := s.script.Source() + if err != nil { + return nil, err + } + source["script"] = src + } + if s.query != nil { + src, err := s.query.Source() + if err != nil { + return nil, err + } + source["query"] = src + } + return source, nil +} + +// Do executes the operation. +func (s *UpdateByQueryService) Do(ctx context.Context) (*BulkIndexByScrollResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body, err := s.getBody() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + IgnoreErrors: []int{http.StatusConflict}, + }) + if err != nil { + return nil, err + } + + // Return operation response (BulkIndexByScrollResponse is defined in DeleteByQuery) + ret := new(BulkIndexByScrollResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// DoAsync executes the update-by-query operation asynchronously by starting a new task. +// Callers need to use the Task Management API to watch the outcome of the reindexing +// operation. +func (s *UpdateByQueryService) DoAsync(ctx context.Context) (*StartTaskResult, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // DoAsync only makes sense with WaitForCompletion set to true + if s.waitForCompletion != nil && *s.waitForCompletion { + return nil, fmt.Errorf("cannot start a task with WaitForCompletion set to true") + } + f := false + s.waitForCompletion = &f + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + body, err := s.getBody() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + IgnoreErrors: []int{http.StatusConflict}, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(StartTaskResult) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} diff --git a/vendor/github.com/olivere/elastic/v7/uritemplates/LICENSE b/vendor/github.com/olivere/elastic/v7/uritemplates/LICENSE new file mode 100644 index 0000000000..de9c88cb65 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/uritemplates/LICENSE @@ -0,0 +1,18 @@ +Copyright (c) 2013 Joshua Tacoma + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/olivere/elastic/v7/uritemplates/uritemplates.go b/vendor/github.com/olivere/elastic/v7/uritemplates/uritemplates.go new file mode 100644 index 0000000000..8a84813fe5 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/uritemplates/uritemplates.go @@ -0,0 +1,359 @@ +// Copyright 2013 Joshua Tacoma. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package uritemplates is a level 4 implementation of RFC 6570 (URI +// Template, http://tools.ietf.org/html/rfc6570). +// +// To use uritemplates, parse a template string and expand it with a value +// map: +// +// template, _ := uritemplates.Parse("https://api.github.com/repos{/user,repo}") +// values := make(map[string]interface{}) +// values["user"] = "jtacoma" +// values["repo"] = "uritemplates" +// expanded, _ := template.ExpandString(values) +// fmt.Printf(expanded) +// +package uritemplates + +import ( + "bytes" + "errors" + "fmt" + "reflect" + "regexp" + "strconv" + "strings" +) + +var ( + unreserved = regexp.MustCompile("[^A-Za-z0-9\\-._~]") + reserved = regexp.MustCompile("[^A-Za-z0-9\\-._~:/?#[\\]@!$&'()*+,;=]") + validname = regexp.MustCompile("^([A-Za-z0-9_\\.]|%[0-9A-Fa-f][0-9A-Fa-f])+$") + hex = []byte("0123456789ABCDEF") +) + +func pctEncode(src []byte) []byte { + dst := make([]byte, len(src)*3) + for i, b := range src { + buf := dst[i*3 : i*3+3] + buf[0] = 0x25 + buf[1] = hex[b/16] + buf[2] = hex[b%16] + } + return dst +} + +func escape(s string, allowReserved bool) (escaped string) { + if allowReserved { + escaped = string(reserved.ReplaceAllFunc([]byte(s), pctEncode)) + } else { + escaped = string(unreserved.ReplaceAllFunc([]byte(s), pctEncode)) + } + return escaped +} + +// A UriTemplate is a parsed representation of a URI template. +type UriTemplate struct { + raw string + parts []templatePart +} + +// Parse parses a URI template string into a UriTemplate object. +func Parse(rawtemplate string) (template *UriTemplate, err error) { + template = new(UriTemplate) + template.raw = rawtemplate + split := strings.Split(rawtemplate, "{") + template.parts = make([]templatePart, len(split)*2-1) + for i, s := range split { + if i == 0 { + if strings.Contains(s, "}") { + err = errors.New("unexpected }") + break + } + template.parts[i].raw = s + } else { + subsplit := strings.Split(s, "}") + if len(subsplit) != 2 { + err = errors.New("malformed template") + break + } + expression := subsplit[0] + template.parts[i*2-1], err = parseExpression(expression) + if err != nil { + break + } + template.parts[i*2].raw = subsplit[1] + } + } + if err != nil { + template = nil + } + return template, err +} + +type templatePart struct { + raw string + terms []templateTerm + first string + sep string + named bool + ifemp string + allowReserved bool +} + +type templateTerm struct { + name string + explode bool + truncate int +} + +func parseExpression(expression string) (result templatePart, err error) { + switch expression[0] { + case '+': + result.sep = "," + result.allowReserved = true + expression = expression[1:] + case '.': + result.first = "." + result.sep = "." + expression = expression[1:] + case '/': + result.first = "/" + result.sep = "/" + expression = expression[1:] + case ';': + result.first = ";" + result.sep = ";" + result.named = true + expression = expression[1:] + case '?': + result.first = "?" + result.sep = "&" + result.named = true + result.ifemp = "=" + expression = expression[1:] + case '&': + result.first = "&" + result.sep = "&" + result.named = true + result.ifemp = "=" + expression = expression[1:] + case '#': + result.first = "#" + result.sep = "," + result.allowReserved = true + expression = expression[1:] + default: + result.sep = "," + } + rawterms := strings.Split(expression, ",") + result.terms = make([]templateTerm, len(rawterms)) + for i, raw := range rawterms { + result.terms[i], err = parseTerm(raw) + if err != nil { + break + } + } + return result, err +} + +func parseTerm(term string) (result templateTerm, err error) { + if strings.HasSuffix(term, "*") { + result.explode = true + term = term[:len(term)-1] + } + split := strings.Split(term, ":") + if len(split) == 1 { + result.name = term + } else if len(split) == 2 { + result.name = split[0] + var parsed int64 + parsed, err = strconv.ParseInt(split[1], 10, 0) + result.truncate = int(parsed) + } else { + err = errors.New("multiple colons in same term") + } + if !validname.MatchString(result.name) { + err = errors.New("not a valid name: " + result.name) + } + if result.explode && result.truncate > 0 { + err = errors.New("both explode and prefix modifers on same term") + } + return result, err +} + +// Expand expands a URI template with a set of values to produce a string. +func (self *UriTemplate) Expand(value interface{}) (string, error) { + values, ismap := value.(map[string]interface{}) + if !ismap { + if m, ismap := struct2map(value); !ismap { + return "", errors.New("expected map[string]interface{}, struct, or pointer to struct.") + } else { + return self.Expand(m) + } + } + var buf bytes.Buffer + for _, p := range self.parts { + err := p.expand(&buf, values) + if err != nil { + return "", err + } + } + return buf.String(), nil +} + +func (self *templatePart) expand(buf *bytes.Buffer, values map[string]interface{}) error { + if len(self.raw) > 0 { + buf.WriteString(self.raw) + return nil + } + var zeroLen = buf.Len() + buf.WriteString(self.first) + var firstLen = buf.Len() + for _, term := range self.terms { + value, exists := values[term.name] + if !exists { + continue + } + if buf.Len() != firstLen { + buf.WriteString(self.sep) + } + switch v := value.(type) { + case string: + self.expandString(buf, term, v) + case []interface{}: + self.expandArray(buf, term, v) + case map[string]interface{}: + if term.truncate > 0 { + return errors.New("cannot truncate a map expansion") + } + self.expandMap(buf, term, v) + default: + if m, ismap := struct2map(value); ismap { + if term.truncate > 0 { + return errors.New("cannot truncate a map expansion") + } + self.expandMap(buf, term, m) + } else { + str := fmt.Sprintf("%v", value) + self.expandString(buf, term, str) + } + } + } + if buf.Len() == firstLen { + original := buf.Bytes()[:zeroLen] + buf.Reset() + buf.Write(original) + } + return nil +} + +func (self *templatePart) expandName(buf *bytes.Buffer, name string, empty bool) { + if self.named { + buf.WriteString(name) + if empty { + buf.WriteString(self.ifemp) + } else { + buf.WriteString("=") + } + } +} + +func (self *templatePart) expandString(buf *bytes.Buffer, t templateTerm, s string) { + if len(s) > t.truncate && t.truncate > 0 { + s = s[:t.truncate] + } + self.expandName(buf, t.name, len(s) == 0) + buf.WriteString(escape(s, self.allowReserved)) +} + +func (self *templatePart) expandArray(buf *bytes.Buffer, t templateTerm, a []interface{}) { + if len(a) == 0 { + return + } else if !t.explode { + self.expandName(buf, t.name, false) + } + for i, value := range a { + if t.explode && i > 0 { + buf.WriteString(self.sep) + } else if i > 0 { + buf.WriteString(",") + } + var s string + switch v := value.(type) { + case string: + s = v + default: + s = fmt.Sprintf("%v", v) + } + if len(s) > t.truncate && t.truncate > 0 { + s = s[:t.truncate] + } + if self.named && t.explode { + self.expandName(buf, t.name, len(s) == 0) + } + buf.WriteString(escape(s, self.allowReserved)) + } +} + +func (self *templatePart) expandMap(buf *bytes.Buffer, t templateTerm, m map[string]interface{}) { + if len(m) == 0 { + return + } + if !t.explode { + self.expandName(buf, t.name, len(m) == 0) + } + var firstLen = buf.Len() + for k, value := range m { + if firstLen != buf.Len() { + if t.explode { + buf.WriteString(self.sep) + } else { + buf.WriteString(",") + } + } + var s string + switch v := value.(type) { + case string: + s = v + default: + s = fmt.Sprintf("%v", v) + } + if t.explode { + buf.WriteString(escape(k, self.allowReserved)) + buf.WriteRune('=') + buf.WriteString(escape(s, self.allowReserved)) + } else { + buf.WriteString(escape(k, self.allowReserved)) + buf.WriteRune(',') + buf.WriteString(escape(s, self.allowReserved)) + } + } +} + +func struct2map(v interface{}) (map[string]interface{}, bool) { + value := reflect.ValueOf(v) + switch value.Type().Kind() { + case reflect.Ptr: + return struct2map(value.Elem().Interface()) + case reflect.Struct: + m := make(map[string]interface{}) + for i := 0; i < value.NumField(); i++ { + tag := value.Type().Field(i).Tag + var name string + if strings.Contains(string(tag), ":") { + name = tag.Get("uri") + } else { + name = strings.TrimSpace(string(tag)) + } + if len(name) == 0 { + name = value.Type().Field(i).Name + } + m[name] = value.Field(i).Interface() + } + return m, true + } + return nil, false +} diff --git a/vendor/github.com/olivere/elastic/v7/uritemplates/utils.go b/vendor/github.com/olivere/elastic/v7/uritemplates/utils.go new file mode 100644 index 0000000000..399ef46236 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/uritemplates/utils.go @@ -0,0 +1,13 @@ +package uritemplates + +func Expand(path string, expansions map[string]string) (string, error) { + template, err := Parse(path) + if err != nil { + return "", err + } + values := make(map[string]interface{}) + for k, v := range expansions { + values[k] = v + } + return template.Expand(values) +} diff --git a/vendor/github.com/olivere/elastic/v7/validate.go b/vendor/github.com/olivere/elastic/v7/validate.go new file mode 100644 index 0000000000..7ed9158c33 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/validate.go @@ -0,0 +1,339 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// ValidateService allows a user to validate a potentially +// expensive query without executing it. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-validate.html. +type ValidateService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + index []string + typ []string + q string + explain *bool + rewrite *bool + allShards *bool + lenient *bool + analyzer string + df string + analyzeWildcard *bool + defaultOperator string + ignoreUnavailable *bool + allowNoIndices *bool + expandWildcards string + bodyJson interface{} + bodyString string +} + +// NewValidateService creates a new ValidateService. +func NewValidateService(client *Client) *ValidateService { + return &ValidateService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *ValidateService) Pretty(pretty bool) *ValidateService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *ValidateService) Human(human bool) *ValidateService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *ValidateService) ErrorTrace(errorTrace bool) *ValidateService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *ValidateService) FilterPath(filterPath ...string) *ValidateService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *ValidateService) Header(name string, value string) *ValidateService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *ValidateService) Headers(headers http.Header) *ValidateService { + s.headers = headers + return s +} + +// Index sets the names of the indices to use for search. +func (s *ValidateService) Index(index ...string) *ValidateService { + s.index = append(s.index, index...) + return s +} + +// Type adds search restrictions for a list of types. +// +// Deprecated: Types are in the process of being removed. Instead of using a type, prefer to +// filter on a field on the document. +func (s *ValidateService) Type(typ ...string) *ValidateService { + s.typ = append(s.typ, typ...) + return s +} + +// Lenient specifies whether format-based query failures +// (such as providing text to a numeric field) should be ignored. +func (s *ValidateService) Lenient(lenient bool) *ValidateService { + s.lenient = &lenient + return s +} + +// Query in the Lucene query string syntax. +func (s *ValidateService) Q(q string) *ValidateService { + s.q = q + return s +} + +// An explain parameter can be specified to get more detailed information about why a query failed. +func (s *ValidateService) Explain(explain *bool) *ValidateService { + s.explain = explain + return s +} + +// Provide a more detailed explanation showing the actual Lucene query that will be executed. +func (s *ValidateService) Rewrite(rewrite *bool) *ValidateService { + s.rewrite = rewrite + return s +} + +// Execute validation on all shards instead of one random shard per index. +func (s *ValidateService) AllShards(allShards *bool) *ValidateService { + s.allShards = allShards + return s +} + +// AnalyzeWildcard specifies whether wildcards and prefix queries +// in the query string query should be analyzed (default: false). +func (s *ValidateService) AnalyzeWildcard(analyzeWildcard bool) *ValidateService { + s.analyzeWildcard = &analyzeWildcard + return s +} + +// Analyzer is the analyzer for the query string query. +func (s *ValidateService) Analyzer(analyzer string) *ValidateService { + s.analyzer = analyzer + return s +} + +// Df is the default field for query string query (default: _all). +func (s *ValidateService) Df(df string) *ValidateService { + s.df = df + return s +} + +// DefaultOperator is the default operator for query string query (AND or OR). +func (s *ValidateService) DefaultOperator(defaultOperator string) *ValidateService { + s.defaultOperator = defaultOperator + return s +} + +// Query sets a query definition using the Query DSL. +func (s *ValidateService) Query(query Query) *ValidateService { + src, err := query.Source() + if err != nil { + // Do nothing in case of an error + return s + } + body := make(map[string]interface{}) + body["query"] = src + s.bodyJson = body + return s +} + +// IgnoreUnavailable indicates whether the specified concrete indices +// should be ignored when unavailable (missing or closed). +func (s *ValidateService) IgnoreUnavailable(ignoreUnavailable bool) *ValidateService { + s.ignoreUnavailable = &ignoreUnavailable + return s +} + +// AllowNoIndices indicates whether to ignore if a wildcard indices +// expression resolves into no concrete indices. (This includes `_all` string +// or when no indices have been specified). +func (s *ValidateService) AllowNoIndices(allowNoIndices bool) *ValidateService { + s.allowNoIndices = &allowNoIndices + return s +} + +// ExpandWildcards indicates whether to expand wildcard expression to +// concrete indices that are open, closed or both. +func (s *ValidateService) ExpandWildcards(expandWildcards string) *ValidateService { + s.expandWildcards = expandWildcards + return s +} + +// BodyJson sets the query definition using the Query DSL. +func (s *ValidateService) BodyJson(body interface{}) *ValidateService { + s.bodyJson = body + return s +} + +// BodyString sets the query definition using the Query DSL as a string. +func (s *ValidateService) BodyString(body string) *ValidateService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *ValidateService) buildURL() (string, url.Values, error) { + var err error + var path string + // Build URL + if len(s.index) > 0 && len(s.typ) > 0 { + path, err = uritemplates.Expand("/{index}/{type}/_validate/query", map[string]string{ + "index": strings.Join(s.index, ","), + "type": strings.Join(s.typ, ","), + }) + } else if len(s.index) > 0 { + path, err = uritemplates.Expand("/{index}/_validate/query", map[string]string{ + "index": strings.Join(s.index, ","), + }) + } else { + path, err = uritemplates.Expand("/_validate/query", map[string]string{ + "type": strings.Join(s.typ, ","), + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.explain != nil { + params.Set("explain", fmt.Sprintf("%v", *s.explain)) + } + if s.rewrite != nil { + params.Set("rewrite", fmt.Sprintf("%v", *s.rewrite)) + } + if s.allShards != nil { + params.Set("all_shards", fmt.Sprintf("%v", *s.allShards)) + } + if s.defaultOperator != "" { + params.Set("default_operator", s.defaultOperator) + } + if v := s.lenient; v != nil { + params.Set("lenient", fmt.Sprint(*v)) + } + if s.q != "" { + params.Set("q", s.q) + } + if v := s.analyzeWildcard; v != nil { + params.Set("analyze_wildcard", fmt.Sprint(*v)) + } + if s.analyzer != "" { + params.Set("analyzer", s.analyzer) + } + if s.df != "" { + params.Set("df", s.df) + } + if v := s.allowNoIndices; v != nil { + params.Set("allow_no_indices", fmt.Sprint(*v)) + } + if s.expandWildcards != "" { + params.Set("expand_wildcards", s.expandWildcards) + } + if v := s.ignoreUnavailable; v != nil { + params.Set("ignore_unavailable", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *ValidateService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *ValidateService) Do(ctx context.Context) (*ValidateResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(ValidateResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// ValidateResponse is the response of ValidateService.Do. +type ValidateResponse struct { + Valid bool `json:"valid"` + Shards map[string]interface{} `json:"_shards"` + Explanations []interface{} `json:"explanations"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_ilm_delete_lifecycle.go b/vendor/github.com/olivere/elastic/v7/xpack_ilm_delete_lifecycle.go new file mode 100644 index 0000000000..bd60fde504 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_ilm_delete_lifecycle.go @@ -0,0 +1,194 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// See the documentation at +// https://www.elastic.co/guide/en/elasticsearch/reference/6.7/ilm-get-lifecycle.html. +type XPackIlmDeleteLifecycleService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + policy string + timeout string + masterTimeout string + flatSettings *bool + local *bool +} + +// NewXPackIlmDeleteLifecycleService creates a new XPackIlmDeleteLifecycleService. +func NewXPackIlmDeleteLifecycleService(client *Client) *XPackIlmDeleteLifecycleService { + return &XPackIlmDeleteLifecycleService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackIlmDeleteLifecycleService) Pretty(pretty bool) *XPackIlmDeleteLifecycleService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackIlmDeleteLifecycleService) Human(human bool) *XPackIlmDeleteLifecycleService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackIlmDeleteLifecycleService) ErrorTrace(errorTrace bool) *XPackIlmDeleteLifecycleService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackIlmDeleteLifecycleService) FilterPath(filterPath ...string) *XPackIlmDeleteLifecycleService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackIlmDeleteLifecycleService) Header(name string, value string) *XPackIlmDeleteLifecycleService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackIlmDeleteLifecycleService) Headers(headers http.Header) *XPackIlmDeleteLifecycleService { + s.headers = headers + return s +} + +// Policy is the name of the index lifecycle policy. +func (s *XPackIlmDeleteLifecycleService) Policy(policy string) *XPackIlmDeleteLifecycleService { + s.policy = policy + return s +} + +// Timeout is an explicit operation timeout. +func (s *XPackIlmDeleteLifecycleService) Timeout(timeout string) *XPackIlmDeleteLifecycleService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *XPackIlmDeleteLifecycleService) MasterTimeout(masterTimeout string) *XPackIlmDeleteLifecycleService { + s.masterTimeout = masterTimeout + return s +} + +// FlatSettings is returns settings in flat format (default: false). +func (s *XPackIlmDeleteLifecycleService) FlatSettings(flatSettings bool) *XPackIlmDeleteLifecycleService { + s.flatSettings = &flatSettings + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackIlmDeleteLifecycleService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + path, err = uritemplates.Expand("/_ilm/policy/{policy}", map[string]string{ + "policy": s.policy, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.flatSettings; v != nil { + params.Set("flat_settings", fmt.Sprint(*v)) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackIlmDeleteLifecycleService) Validate() error { + var invalid []string + if s.policy == "" { + invalid = append(invalid, "Policy") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackIlmDeleteLifecycleService) Do(ctx context.Context) (*XPackIlmDeleteLifecycleResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Delete URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Delete HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackIlmDeleteLifecycleResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackIlmDeleteLifecycleResponse is the response of XPackIlmDeleteLifecycleService.Do. +type XPackIlmDeleteLifecycleResponse struct { + Acknowledged bool `json:"acknowledged"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_ilm_get_lifecycle.go b/vendor/github.com/olivere/elastic/v7/xpack_ilm_get_lifecycle.go new file mode 100644 index 0000000000..3a52c36d5d --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_ilm_get_lifecycle.go @@ -0,0 +1,193 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// See the documentation at +// https://www.elastic.co/guide/en/elasticsearch/reference/6.7/ilm-get-lifecycle.html. +type XPackIlmGetLifecycleService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + policy []string + timeout string + masterTimeout string + flatSettings *bool + local *bool +} + +// NewXPackIlmGetLifecycleService creates a new XPackIlmGetLifecycleService. +func NewXPackIlmGetLifecycleService(client *Client) *XPackIlmGetLifecycleService { + return &XPackIlmGetLifecycleService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackIlmGetLifecycleService) Pretty(pretty bool) *XPackIlmGetLifecycleService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackIlmGetLifecycleService) Human(human bool) *XPackIlmGetLifecycleService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackIlmGetLifecycleService) ErrorTrace(errorTrace bool) *XPackIlmGetLifecycleService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackIlmGetLifecycleService) FilterPath(filterPath ...string) *XPackIlmGetLifecycleService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackIlmGetLifecycleService) Header(name string, value string) *XPackIlmGetLifecycleService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackIlmGetLifecycleService) Headers(headers http.Header) *XPackIlmGetLifecycleService { + s.headers = headers + return s +} + +// Policy is the name of the index lifecycle policy. +func (s *XPackIlmGetLifecycleService) Policy(policies ...string) *XPackIlmGetLifecycleService { + s.policy = append(s.policy, policies...) + return s +} + +// Timeout is an explicit operation timeout. +func (s *XPackIlmGetLifecycleService) Timeout(timeout string) *XPackIlmGetLifecycleService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *XPackIlmGetLifecycleService) MasterTimeout(masterTimeout string) *XPackIlmGetLifecycleService { + s.masterTimeout = masterTimeout + return s +} + +// FlatSettings is returns settings in flat format (default: false). +func (s *XPackIlmGetLifecycleService) FlatSettings(flatSettings bool) *XPackIlmGetLifecycleService { + s.flatSettings = &flatSettings + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackIlmGetLifecycleService) buildURL() (string, url.Values, error) { + // Build URL + var err error + var path string + if len(s.policy) > 0 { + path, err = uritemplates.Expand("/_ilm/policy/{policy}", map[string]string{ + "policy": strings.Join(s.policy, ","), + }) + } else { + path = "/_ilm/policy" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.flatSettings; v != nil { + params.Set("flat_settings", fmt.Sprint(*v)) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.local; v != nil { + params.Set("local", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackIlmGetLifecycleService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *XPackIlmGetLifecycleService) Do(ctx context.Context) (map[string]*XPackIlmGetLifecycleResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + var ret map[string]*XPackIlmGetLifecycleResponse + if err := s.client.decoder.Decode(res.Body, &ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackIlmGetLifecycleResponse is the response of XPackIlmGetLifecycleService.Do. +type XPackIlmGetLifecycleResponse struct { + Version int `json:"version,omitempty"` + ModifiedDate string `json:"modified_date,omitempty"` // e.g. "2019-10-03T17:43:42.720Z" + Policy map[string]interface{} `json:"policy,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_ilm_put_lifecycle.go b/vendor/github.com/olivere/elastic/v7/xpack_ilm_put_lifecycle.go new file mode 100644 index 0000000000..f2e8ba1020 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_ilm_put_lifecycle.go @@ -0,0 +1,214 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// See the documentation at +// https://www.elastic.co/guide/en/elasticsearch/reference/6.7/ilm-put-lifecycle.html +type XPackIlmPutLifecycleService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + policy string + timeout string + masterTimeout string + flatSettings *bool + bodyJson interface{} + bodyString string +} + +// NewXPackIlmPutLifecycleService creates a new XPackIlmPutLifecycleService. +func NewXPackIlmPutLifecycleService(client *Client) *XPackIlmPutLifecycleService { + return &XPackIlmPutLifecycleService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackIlmPutLifecycleService) Pretty(pretty bool) *XPackIlmPutLifecycleService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackIlmPutLifecycleService) Human(human bool) *XPackIlmPutLifecycleService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackIlmPutLifecycleService) ErrorTrace(errorTrace bool) *XPackIlmPutLifecycleService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackIlmPutLifecycleService) FilterPath(filterPath ...string) *XPackIlmPutLifecycleService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackIlmPutLifecycleService) Header(name string, value string) *XPackIlmPutLifecycleService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackIlmPutLifecycleService) Headers(headers http.Header) *XPackIlmPutLifecycleService { + s.headers = headers + return s +} + +// Policy is the name of the index lifecycle policy. +func (s *XPackIlmPutLifecycleService) Policy(policy string) *XPackIlmPutLifecycleService { + s.policy = policy + return s +} + +// Timeout is an explicit operation timeout. +func (s *XPackIlmPutLifecycleService) Timeout(timeout string) *XPackIlmPutLifecycleService { + s.timeout = timeout + return s +} + +// MasterTimeout specifies the timeout for connection to master. +func (s *XPackIlmPutLifecycleService) MasterTimeout(masterTimeout string) *XPackIlmPutLifecycleService { + s.masterTimeout = masterTimeout + return s +} + +// FlatSettings indicates whether to return settings in flat format (default: false). +func (s *XPackIlmPutLifecycleService) FlatSettings(flatSettings bool) *XPackIlmPutLifecycleService { + s.flatSettings = &flatSettings + return s +} + +// BodyJson is documented as: The template definition. +func (s *XPackIlmPutLifecycleService) BodyJson(body interface{}) *XPackIlmPutLifecycleService { + s.bodyJson = body + return s +} + +// BodyString is documented as: The template definition. +func (s *XPackIlmPutLifecycleService) BodyString(body string) *XPackIlmPutLifecycleService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackIlmPutLifecycleService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_ilm/policy/{policy}", map[string]string{ + "policy": s.policy, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.timeout != "" { + params.Set("timeout", s.timeout) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.flatSettings; v != nil { + params.Set("flat_settings", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackIlmPutLifecycleService) Validate() error { + var invalid []string + if s.policy == "" { + invalid = append(invalid, "Policy") + } + if s.bodyString == "" && s.bodyJson == nil { + invalid = append(invalid, "BodyJson") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackIlmPutLifecycleService) Do(ctx context.Context) (*XPackIlmPutLifecycleResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackIlmPutLifecycleResponse) + if err := s.client.decoder.Decode(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackIlmPutLifecycleSResponse is the response of XPackIlmPutLifecycleService.Do. +type XPackIlmPutLifecycleResponse struct { + Acknowledged bool `json:"acknowledged"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_info.go b/vendor/github.com/olivere/elastic/v7/xpack_info.go new file mode 100644 index 0000000000..d9cbeedcec --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_info.go @@ -0,0 +1,225 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackInfoService retrieves xpack info. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/info-api.html. +type XPackInfoService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers +} + +// NewXPackInfoService creates a new XPackInfoService. +func NewXPackInfoService(client *Client) *XPackInfoService { + return &XPackInfoService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackInfoService) Pretty(pretty bool) *XPackInfoService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackInfoService) Human(human bool) *XPackInfoService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackInfoService) ErrorTrace(errorTrace bool) *XPackInfoService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackInfoService) FilterPath(filterPath ...string) *XPackInfoService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackInfoService) Header(name string, value string) *XPackInfoService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackInfoService) Headers(headers http.Header) *XPackInfoService { + s.headers = headers + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackInfoService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_xpack", map[string]string{}) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackInfoService) Validate() error { + var invalid []string + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackInfoService) Do(ctx context.Context) (*XPackInfoServiceResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := XPackInfoServiceResponse{} + if err := json.Unmarshal(res.Body, &ret); err != nil { + return nil, err + } + return &ret, nil +} + +// XPackInfoServiceResponse is the response of XPackInfoService.Do. +type XPackInfoServiceResponse struct { + Build XPackInfoBuild `json:"build"` + License XPackInfoLicense `json:"license"` + Features XPackInfoFeatures `json:"features"` + Tagline string `json:"tagline"` +} + +// XPackInfoBuild is the xpack build info +type XPackInfoBuild struct { + Hash string `json:"hash"` + Date string `json:"date"` +} + +// XPackInfoLicense is the xpack license info +type XPackInfoLicense struct { + UID string `json:"uid"` + Type string `json:"type"` + Mode string `json:"mode"` + Status string `json:"status"` + ExpiryMilis int `json:"expiry_date_in_millis"` +} + +// XPackInfoFeatures is the xpack feature info object +type XPackInfoFeatures struct { + Graph XPackInfoGraph `json:"graph"` + Logstash XPackInfoLogstash `json:"logstash"` + MachineLearning XPackInfoML `json:"ml"` + Monitoring XPackInfoMonitoring `json:"monitoring"` + Rollup XPackInfoRollup `json:"rollup"` + Security XPackInfoSecurity `json:"security"` + Watcher XPackInfoWatcher `json:"watcher"` +} + +// XPackInfoGraph is the xpack graph plugin info +type XPackInfoGraph struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` +} + +// XPackInfoLogstash is the xpack logstash plugin info +type XPackInfoLogstash struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` +} + +// XPackInfoML is the xpack machine learning plugin info +type XPackInfoML struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` + NativeCodeInfo map[string]string `json:"native_code_info"` +} + +// XPackInfoMonitoring is the xpack monitoring plugin info +type XPackInfoMonitoring struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` +} + +// XPackInfoRollup is the xpack rollup plugin info +type XPackInfoRollup struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` +} + +// XPackInfoSecurity is the xpack security plugin info +type XPackInfoSecurity struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` +} + +// XPackInfoWatcher is the xpack watcher plugin info +type XPackInfoWatcher struct { + Description string `json:"description"` + Available bool `json:"available"` + Enabled bool `json:"enabled"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_change_password.go b/vendor/github.com/olivere/elastic/v7/xpack_security_change_password.go new file mode 100644 index 0000000000..34174a15c5 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_change_password.go @@ -0,0 +1,201 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityChangePasswordService changes a native user's password. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.1/security-api-change-password.html. +type XPackSecurityChangePasswordService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + username string + password string + refresh string + body interface{} +} + +// NewXPackSecurityChangePasswordService creates a new XPackSecurityChangePasswordService. +func NewXPackSecurityChangePasswordService(client *Client) *XPackSecurityChangePasswordService { + return &XPackSecurityChangePasswordService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityChangePasswordService) Pretty(pretty bool) *XPackSecurityChangePasswordService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityChangePasswordService) Human(human bool) *XPackSecurityChangePasswordService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityChangePasswordService) ErrorTrace(errorTrace bool) *XPackSecurityChangePasswordService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityChangePasswordService) FilterPath(filterPath ...string) *XPackSecurityChangePasswordService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityChangePasswordService) Header(name string, value string) *XPackSecurityChangePasswordService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityChangePasswordService) Headers(headers http.Header) *XPackSecurityChangePasswordService { + s.headers = headers + return s +} + +// Username is name of the user to change. +func (s *XPackSecurityChangePasswordService) Username(username string) *XPackSecurityChangePasswordService { + s.username = username + return s +} + +// Password is the new value of the password. +func (s *XPackSecurityChangePasswordService) Password(password string) *XPackSecurityChangePasswordService { + s.password = password + return s +} + +// Refresh, if "true" (the default), refreshes the affected shards to make this operation +// visible to search, if "wait_for" then wait for a refresh to make this operation visible +// to search, if "false" then do nothing with refreshes. +func (s *XPackSecurityChangePasswordService) Refresh(refresh string) *XPackSecurityChangePasswordService { + s.refresh = refresh + return s +} + +// Body specifies the password. Use a string or a type that will get serialized as JSON. +func (s *XPackSecurityChangePasswordService) Body(body interface{}) *XPackSecurityChangePasswordService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityChangePasswordService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_xpack/security/user/{username}/_password", map[string]string{ + "username": s.username, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.refresh; v != "" { + params.Set("refresh", v) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityChangePasswordService) Validate() error { + var invalid []string + if s.username == "" { + invalid = append(invalid, "Userame") + } + if s.password == "" && s.body == nil { + invalid = append(invalid, "Body") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityChangePasswordService) Do(ctx context.Context) (*XPackSecurityChangeUserPasswordResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + var body interface{} + if s.body != nil { + body = s.body + } else { + body = map[string]interface{}{ + "password": s.password, + } + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityChangeUserPasswordResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityChangeUserPasswordResponse is the response of +// XPackSecurityChangePasswordService.Do. +// +// A successful call returns an empty JSON structure: {}. +type XPackSecurityChangeUserPasswordResponse struct { +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_delete_role.go b/vendor/github.com/olivere/elastic/v7/xpack_security_delete_role.go new file mode 100644 index 0000000000..d9c7cb9a0b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_delete_role.go @@ -0,0 +1,159 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityDeleteRoleService delete a role by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-delete-role.html. +type XPackSecurityDeleteRoleService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string +} + +// NewXPackSecurityDeleteRoleService creates a new XPackSecurityDeleteRoleService. +func NewXPackSecurityDeleteRoleService(client *Client) *XPackSecurityDeleteRoleService { + return &XPackSecurityDeleteRoleService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityDeleteRoleService) Pretty(pretty bool) *XPackSecurityDeleteRoleService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityDeleteRoleService) Human(human bool) *XPackSecurityDeleteRoleService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityDeleteRoleService) ErrorTrace(errorTrace bool) *XPackSecurityDeleteRoleService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityDeleteRoleService) FilterPath(filterPath ...string) *XPackSecurityDeleteRoleService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityDeleteRoleService) Header(name string, value string) *XPackSecurityDeleteRoleService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityDeleteRoleService) Headers(headers http.Header) *XPackSecurityDeleteRoleService { + s.headers = headers + return s +} + +// Name is name of the role to delete. +func (s *XPackSecurityDeleteRoleService) Name(name string) *XPackSecurityDeleteRoleService { + s.name = name + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityDeleteRoleService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/role/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityDeleteRoleService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityDeleteRoleService) Do(ctx context.Context) (*XPackSecurityDeleteRoleResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityDeleteRoleResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityDeleteRoleResponse is the response of XPackSecurityDeleteRoleService.Do. +type XPackSecurityDeleteRoleResponse struct { + Found bool `json:"found"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_delete_role_mapping.go b/vendor/github.com/olivere/elastic/v7/xpack_security_delete_role_mapping.go new file mode 100644 index 0000000000..05b564b456 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_delete_role_mapping.go @@ -0,0 +1,159 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityDeleteRoleMappingService delete a role mapping by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-delete-role-mapping.html. +type XPackSecurityDeleteRoleMappingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string +} + +// NewXPackSecurityDeleteRoleMappingService creates a new XPackSecurityDeleteRoleMappingService. +func NewXPackSecurityDeleteRoleMappingService(client *Client) *XPackSecurityDeleteRoleMappingService { + return &XPackSecurityDeleteRoleMappingService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityDeleteRoleMappingService) Pretty(pretty bool) *XPackSecurityDeleteRoleMappingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityDeleteRoleMappingService) Human(human bool) *XPackSecurityDeleteRoleMappingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityDeleteRoleMappingService) ErrorTrace(errorTrace bool) *XPackSecurityDeleteRoleMappingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityDeleteRoleMappingService) FilterPath(filterPath ...string) *XPackSecurityDeleteRoleMappingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityDeleteRoleMappingService) Header(name string, value string) *XPackSecurityDeleteRoleMappingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityDeleteRoleMappingService) Headers(headers http.Header) *XPackSecurityDeleteRoleMappingService { + s.headers = headers + return s +} + +// Name is name of the role mapping to delete. +func (s *XPackSecurityDeleteRoleMappingService) Name(name string) *XPackSecurityDeleteRoleMappingService { + s.name = name + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityDeleteRoleMappingService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/role_mapping/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityDeleteRoleMappingService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityDeleteRoleMappingService) Do(ctx context.Context) (*XPackSecurityDeleteRoleMappingResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityDeleteRoleMappingResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityDeleteRoleMappingResponse is the response of XPackSecurityDeleteRoleMappingService.Do. +type XPackSecurityDeleteRoleMappingResponse struct { + Found bool `json:"found"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_delete_user.go b/vendor/github.com/olivere/elastic/v7/xpack_security_delete_user.go new file mode 100644 index 0000000000..1e079f73a8 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_delete_user.go @@ -0,0 +1,170 @@ +// Copyright 2012-2019 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityDeleteUserService delete a user by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/security-api-delete-user.html. +type XPackSecurityDeleteUserService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + username string + refresh string +} + +// NewXPackSecurityDeleteUserService creates a new XPackSecurityDeleteUserService. +func NewXPackSecurityDeleteUserService(client *Client) *XPackSecurityDeleteUserService { + return &XPackSecurityDeleteUserService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityDeleteUserService) Pretty(pretty bool) *XPackSecurityDeleteUserService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityDeleteUserService) Human(human bool) *XPackSecurityDeleteUserService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityDeleteUserService) ErrorTrace(errorTrace bool) *XPackSecurityDeleteUserService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityDeleteUserService) FilterPath(filterPath ...string) *XPackSecurityDeleteUserService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityDeleteUserService) Header(name string, value string) *XPackSecurityDeleteUserService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityDeleteUserService) Headers(headers http.Header) *XPackSecurityDeleteUserService { + s.headers = headers + return s +} + +// Username is name of the user to delete. +func (s *XPackSecurityDeleteUserService) Username(username string) *XPackSecurityDeleteUserService { + s.username = username + return s +} + +// Refresh specifies if and how to wait for refreshing the shards after the request. +// Possible values are "true" (default), "false" and "wait_for", all of type string. +func (s *XPackSecurityDeleteUserService) Refresh(refresh string) *XPackSecurityDeleteUserService { + s.refresh = refresh + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityDeleteUserService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/user/{username}", map[string]string{ + "username": s.username, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.refresh; v != "" { + params.Set("refresh", v) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityDeleteUserService) Validate() error { + var invalid []string + if s.username == "" { + invalid = append(invalid, "Username") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityDeleteUserService) Do(ctx context.Context) (*XPackSecurityDeleteUserResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityDeleteUserResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityDeleteUserResponse is the response of XPackSecurityDeleteUserService.Do. +type XPackSecurityDeleteUserResponse struct { + Found bool `json:"found"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_disable_user.go b/vendor/github.com/olivere/elastic/v7/xpack_security_disable_user.go new file mode 100644 index 0000000000..846c0e2851 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_disable_user.go @@ -0,0 +1,168 @@ +// Copyright 2012-2019 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityDisableUserService retrieves a user by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-get-user.html. +type XPackSecurityDisableUserService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + username string + refresh string +} + +// NewXPackSecurityDisableUserService creates a new XPackSecurityDisableUserService. +func NewXPackSecurityDisableUserService(client *Client) *XPackSecurityDisableUserService { + return &XPackSecurityDisableUserService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityDisableUserService) Pretty(pretty bool) *XPackSecurityDisableUserService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityDisableUserService) Human(human bool) *XPackSecurityDisableUserService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityDisableUserService) ErrorTrace(errorTrace bool) *XPackSecurityDisableUserService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityDisableUserService) FilterPath(filterPath ...string) *XPackSecurityDisableUserService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityDisableUserService) Header(name string, value string) *XPackSecurityDisableUserService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityDisableUserService) Headers(headers http.Header) *XPackSecurityDisableUserService { + s.headers = headers + return s +} + +// Username is name of the user to disable. +func (s *XPackSecurityDisableUserService) Username(username string) *XPackSecurityDisableUserService { + s.username = username + return s +} + +// Refresh specifies if and how to wait for refreshing the shards after the request. +// Possible values are "true" (default), "false" and "wait_for", all of type string. +func (s *XPackSecurityDisableUserService) Refresh(refresh string) *XPackSecurityDisableUserService { + s.refresh = refresh + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityDisableUserService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/user/{username}/_disable", map[string]string{ + "username": s.username, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.refresh; v != "" { + params.Set("refresh", v) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityDisableUserService) Validate() error { + var invalid []string + if s.username == "" { + invalid = append(invalid, "Username") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityDisableUserService) Do(ctx context.Context) (*XPackSecurityDisableUserResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityDisableUserResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityDisableUserResponse is the response of XPackSecurityDisableUserService.Do. +type XPackSecurityDisableUserResponse struct { +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_enable_user.go b/vendor/github.com/olivere/elastic/v7/xpack_security_enable_user.go new file mode 100644 index 0000000000..c04b917331 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_enable_user.go @@ -0,0 +1,168 @@ +// Copyright 2012-2019 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityEnableUserService retrieves a user by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-get-user.html. +type XPackSecurityEnableUserService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + username string + refresh string +} + +// NewXPackSecurityEnableUserService creates a new XPackSecurityEnableUserService. +func NewXPackSecurityEnableUserService(client *Client) *XPackSecurityEnableUserService { + return &XPackSecurityEnableUserService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityEnableUserService) Pretty(pretty bool) *XPackSecurityEnableUserService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityEnableUserService) Human(human bool) *XPackSecurityEnableUserService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityEnableUserService) ErrorTrace(errorTrace bool) *XPackSecurityEnableUserService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityEnableUserService) FilterPath(filterPath ...string) *XPackSecurityEnableUserService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityEnableUserService) Header(name string, value string) *XPackSecurityEnableUserService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityEnableUserService) Headers(headers http.Header) *XPackSecurityEnableUserService { + s.headers = headers + return s +} + +// Username is name of the user to enable. +func (s *XPackSecurityEnableUserService) Username(username string) *XPackSecurityEnableUserService { + s.username = username + return s +} + +// Refresh specifies if and how to wait for refreshing the shards after the request. +// Possible values are "true" (default), "false" and "wait_for", all of type string. +func (s *XPackSecurityEnableUserService) Refresh(refresh string) *XPackSecurityEnableUserService { + s.refresh = refresh + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityEnableUserService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/user/{username}/_enable", map[string]string{ + "username": s.username, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.refresh; v != "" { + params.Set("refresh", v) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityEnableUserService) Validate() error { + var invalid []string + if s.username == "" { + invalid = append(invalid, "Username") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityEnableUserService) Do(ctx context.Context) (*XPackSecurityEnableUserResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityEnableUserResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityEnableUserResponse is the response of XPackSecurityEnableUserService.Do. +type XPackSecurityEnableUserResponse struct { +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_get_role.go b/vendor/github.com/olivere/elastic/v7/xpack_security_get_role.go new file mode 100644 index 0000000000..fcb64ab85a --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_get_role.go @@ -0,0 +1,186 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityGetRoleService retrieves a role by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-get-role.html. +type XPackSecurityGetRoleService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string +} + +// NewXPackSecurityGetRoleService creates a new XPackSecurityGetRoleService. +func NewXPackSecurityGetRoleService(client *Client) *XPackSecurityGetRoleService { + return &XPackSecurityGetRoleService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityGetRoleService) Pretty(pretty bool) *XPackSecurityGetRoleService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityGetRoleService) Human(human bool) *XPackSecurityGetRoleService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityGetRoleService) ErrorTrace(errorTrace bool) *XPackSecurityGetRoleService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityGetRoleService) FilterPath(filterPath ...string) *XPackSecurityGetRoleService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityGetRoleService) Header(name string, value string) *XPackSecurityGetRoleService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityGetRoleService) Headers(headers http.Header) *XPackSecurityGetRoleService { + s.headers = headers + return s +} + +// Name is name of the role to retrieve. +func (s *XPackSecurityGetRoleService) Name(name string) *XPackSecurityGetRoleService { + s.name = name + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityGetRoleService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/role/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityGetRoleService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityGetRoleService) Do(ctx context.Context) (*XPackSecurityGetRoleResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := XPackSecurityGetRoleResponse{} + if err := json.Unmarshal(res.Body, &ret); err != nil { + return nil, err + } + return &ret, nil +} + +// XPackSecurityGetRoleResponse is the response of XPackSecurityGetRoleService.Do. +type XPackSecurityGetRoleResponse map[string]XPackSecurityRole + +// XPackSecurityRole is the role object. +// +// The Java source for this struct is defined here: +// https://github.com/elastic/elasticsearch/blob/6.7/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java +type XPackSecurityRole struct { + Cluster []string `json:"cluster"` + Indices []XPackSecurityIndicesPermissions `json:"indices"` + Applications []XPackSecurityApplicationPrivileges `json:"applications"` + RunAs []string `json:"run_as"` + Global map[string]interface{} `json:"global"` + Metadata map[string]interface{} `json:"metadata"` + TransientMetadata map[string]interface{} `json:"transient_metadata"` +} + +// XPackSecurityApplicationPrivileges is the application privileges object +type XPackSecurityApplicationPrivileges struct { + Application string `json:"application"` + Privileges []string `json:"privileges"` + Ressources []string `json:"resources"` +} + +// XPackSecurityIndicesPermissions is the indices permission object +type XPackSecurityIndicesPermissions struct { + Names []string `json:"names"` + Privileges []string `json:"privileges"` + FieldSecurity interface{} `json:"field_security"` + Query string `json:"query"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_get_role_mapping.go b/vendor/github.com/olivere/elastic/v7/xpack_security_get_role_mapping.go new file mode 100644 index 0000000000..24230be339 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_get_role_mapping.go @@ -0,0 +1,165 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityGetRoleMappingService retrieves a role mapping by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-get-role-mapping.html. +type XPackSecurityGetRoleMappingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string +} + +// NewXPackSecurityGetRoleMappingService creates a new XPackSecurityGetRoleMappingService. +func NewXPackSecurityGetRoleMappingService(client *Client) *XPackSecurityGetRoleMappingService { + return &XPackSecurityGetRoleMappingService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityGetRoleMappingService) Pretty(pretty bool) *XPackSecurityGetRoleMappingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityGetRoleMappingService) Human(human bool) *XPackSecurityGetRoleMappingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityGetRoleMappingService) ErrorTrace(errorTrace bool) *XPackSecurityGetRoleMappingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityGetRoleMappingService) FilterPath(filterPath ...string) *XPackSecurityGetRoleMappingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityGetRoleMappingService) Header(name string, value string) *XPackSecurityGetRoleMappingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityGetRoleMappingService) Headers(headers http.Header) *XPackSecurityGetRoleMappingService { + s.headers = headers + return s +} + +// Name is name of the role mapping to retrieve. +func (s *XPackSecurityGetRoleMappingService) Name(name string) *XPackSecurityGetRoleMappingService { + s.name = name + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityGetRoleMappingService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/role_mapping/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityGetRoleMappingService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityGetRoleMappingService) Do(ctx context.Context) (*XPackSecurityGetRoleMappingResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := XPackSecurityGetRoleMappingResponse{} + if err := json.Unmarshal(res.Body, &ret); err != nil { + return nil, err + } + return &ret, nil +} + +// XPackSecurityGetRoleMappingResponse is the response of XPackSecurityGetRoleMappingService.Do. +type XPackSecurityGetRoleMappingResponse map[string]XPackSecurityRoleMapping + +// XPackSecurityRoleMapping is the role mapping object +type XPackSecurityRoleMapping struct { + Enabled bool `json:"enabled"` + Roles []string `json:"roles"` + Rules map[string]interface{} `json:"rules"` + Metadata interface{} `json:"metadata"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_get_user.go b/vendor/github.com/olivere/elastic/v7/xpack_security_get_user.go new file mode 100644 index 0000000000..50a179b194 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_get_user.go @@ -0,0 +1,176 @@ +// Copyright 2012-2019 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityGetUserService retrieves a user by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-get-user.html. +type XPackSecurityGetUserService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + usernames []string +} + +// NewXPackSecurityGetUserService creates a new XPackSecurityGetUserService. +func NewXPackSecurityGetUserService(client *Client) *XPackSecurityGetUserService { + return &XPackSecurityGetUserService{ + client: client, + } +} + +// Pretty indicates that the JSON response be indented and human readable. +func (s *XPackSecurityGetUserService) Pretty(pretty bool) *XPackSecurityGetUserService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityGetUserService) Human(human bool) *XPackSecurityGetUserService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityGetUserService) ErrorTrace(errorTrace bool) *XPackSecurityGetUserService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityGetUserService) FilterPath(filterPath ...string) *XPackSecurityGetUserService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityGetUserService) Header(name string, value string) *XPackSecurityGetUserService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityGetUserService) Headers(headers http.Header) *XPackSecurityGetUserService { + s.headers = headers + return s +} + +// Usernames are the names of one or more users to retrieve. +func (s *XPackSecurityGetUserService) Usernames(usernames ...string) *XPackSecurityGetUserService { + for _, username := range usernames { + if v := strings.TrimSpace(username); v != "" { + s.usernames = append(s.usernames, v) + } + } + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityGetUserService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + if len(s.usernames) > 0 { + path, err = uritemplates.Expand("/_security/user/{username}", map[string]string{ + "username": strings.Join(s.usernames, ","), + }) + } else { + path = "/_security/user" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityGetUserService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *XPackSecurityGetUserService) Do(ctx context.Context) (*XPackSecurityGetUserResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := XPackSecurityGetUserResponse{} + if err := json.Unmarshal(res.Body, &ret); err != nil { + return nil, err + } + return &ret, nil +} + +// XPackSecurityGetUserResponse is the response of XPackSecurityGetUserService.Do. +type XPackSecurityGetUserResponse map[string]XPackSecurityUser + +// XPackSecurityUser is the user object. +// +// The Java source for this struct is defined here: +// https://github.com/elastic/elasticsearch/blob/7.3/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/User.java +type XPackSecurityUser struct { + Username string `json:"username"` + Roles []string `json:"roles"` + Fullname string `json:"full_name"` + Email string `json:"email"` + Metadata map[string]interface{} `json:"metadata"` + Enabled bool `json:"enabled"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_put_role.go b/vendor/github.com/olivere/elastic/v7/xpack_security_put_role.go new file mode 100644 index 0000000000..f3c869f898 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_put_role.go @@ -0,0 +1,174 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityPutRoleService retrieves a role by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-put-role.html. +type XPackSecurityPutRoleService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string + body interface{} +} + +// NewXPackSecurityPutRoleService creates a new XPackSecurityPutRoleService. +func NewXPackSecurityPutRoleService(client *Client) *XPackSecurityPutRoleService { + return &XPackSecurityPutRoleService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityPutRoleService) Pretty(pretty bool) *XPackSecurityPutRoleService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityPutRoleService) Human(human bool) *XPackSecurityPutRoleService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityPutRoleService) ErrorTrace(errorTrace bool) *XPackSecurityPutRoleService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityPutRoleService) FilterPath(filterPath ...string) *XPackSecurityPutRoleService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityPutRoleService) Header(name string, value string) *XPackSecurityPutRoleService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityPutRoleService) Headers(headers http.Header) *XPackSecurityPutRoleService { + s.headers = headers + return s +} + +// Name is name of the role to create. +func (s *XPackSecurityPutRoleService) Name(name string) *XPackSecurityPutRoleService { + s.name = name + return s +} + +// Body specifies the role. Use a string or a type that will get serialized as JSON. +func (s *XPackSecurityPutRoleService) Body(body interface{}) *XPackSecurityPutRoleService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityPutRoleService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/role/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityPutRoleService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if s.body == nil { + invalid = append(invalid, "Body") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityPutRoleService) Do(ctx context.Context) (*XPackSecurityPutRoleResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: s.body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityPutRoleResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityPutRoleResponse is the response of XPackSecurityPutRoleService.Do. +type XPackSecurityPutRoleResponse struct { + Role XPackSecurityPutRole +} + +type XPackSecurityPutRole struct { + Created bool `json:"created"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_put_role_mapping.go b/vendor/github.com/olivere/elastic/v7/xpack_security_put_role_mapping.go new file mode 100644 index 0000000000..0850d5e56b --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_put_role_mapping.go @@ -0,0 +1,174 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityPutRoleMappingService create or update a role mapping by its name. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/security-api-put-role-mapping.html. +type XPackSecurityPutRoleMappingService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + name string + body interface{} +} + +// NewXPackSecurityPutRoleMappingService creates a new XPackSecurityPutRoleMappingService. +func NewXPackSecurityPutRoleMappingService(client *Client) *XPackSecurityPutRoleMappingService { + return &XPackSecurityPutRoleMappingService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityPutRoleMappingService) Pretty(pretty bool) *XPackSecurityPutRoleMappingService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityPutRoleMappingService) Human(human bool) *XPackSecurityPutRoleMappingService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityPutRoleMappingService) ErrorTrace(errorTrace bool) *XPackSecurityPutRoleMappingService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityPutRoleMappingService) FilterPath(filterPath ...string) *XPackSecurityPutRoleMappingService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityPutRoleMappingService) Header(name string, value string) *XPackSecurityPutRoleMappingService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityPutRoleMappingService) Headers(headers http.Header) *XPackSecurityPutRoleMappingService { + s.headers = headers + return s +} + +// Name is name of the role mapping to create/update. +func (s *XPackSecurityPutRoleMappingService) Name(name string) *XPackSecurityPutRoleMappingService { + s.name = name + return s +} + +// Body specifies the role mapping. Use a string or a type that will get serialized as JSON. +func (s *XPackSecurityPutRoleMappingService) Body(body interface{}) *XPackSecurityPutRoleMappingService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityPutRoleMappingService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/role_mapping/{name}", map[string]string{ + "name": s.name, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityPutRoleMappingService) Validate() error { + var invalid []string + if s.name == "" { + invalid = append(invalid, "Name") + } + if s.body == nil { + invalid = append(invalid, "Body") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityPutRoleMappingService) Do(ctx context.Context) (*XPackSecurityPutRoleMappingResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: s.body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityPutRoleMappingResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityPutRoleMappingResponse is the response of XPackSecurityPutRoleMappingService.Do. +type XPackSecurityPutRoleMappingResponse struct { + Role_Mapping XPackSecurityPutRoleMapping +} + +type XPackSecurityPutRoleMapping struct { + Created bool `json:"created"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_security_put_user.go b/vendor/github.com/olivere/elastic/v7/xpack_security_put_user.go new file mode 100644 index 0000000000..f7ab77e8c8 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_security_put_user.go @@ -0,0 +1,215 @@ +// Copyright 2012-2019 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackSecurityPutUserService adds a user. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/security-api-put-user.html. +type XPackSecurityPutUserService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + username string + refresh string + + user *XPackSecurityPutUserRequest + body interface{} +} + +// NewXPackSecurityPutUserService creates a new XPackSecurityPutUserService. +func NewXPackSecurityPutUserService(client *Client) *XPackSecurityPutUserService { + return &XPackSecurityPutUserService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackSecurityPutUserService) Pretty(pretty bool) *XPackSecurityPutUserService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackSecurityPutUserService) Human(human bool) *XPackSecurityPutUserService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackSecurityPutUserService) ErrorTrace(errorTrace bool) *XPackSecurityPutUserService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackSecurityPutUserService) FilterPath(filterPath ...string) *XPackSecurityPutUserService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackSecurityPutUserService) Header(name string, value string) *XPackSecurityPutUserService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackSecurityPutUserService) Headers(headers http.Header) *XPackSecurityPutUserService { + s.headers = headers + return s +} + +// Username is the name of the user to add. +func (s *XPackSecurityPutUserService) Username(username string) *XPackSecurityPutUserService { + s.username = username + return s +} + +// User specifies the data of the new user. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.4/security-api-put-user.html +// for details. +func (s *XPackSecurityPutUserService) User(user *XPackSecurityPutUserRequest) *XPackSecurityPutUserService { + s.user = user + return s +} + +// Refresh specifies if and how to wait for refreshing the shards after the request. +// Possible values are "true" (default), "false" and "wait_for", all of type string. +func (s *XPackSecurityPutUserService) Refresh(refresh string) *XPackSecurityPutUserService { + s.refresh = refresh + return s +} + +// Body specifies the user. Use a string or a type that will get serialized as JSON. +func (s *XPackSecurityPutUserService) Body(body interface{}) *XPackSecurityPutUserService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackSecurityPutUserService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_security/user/{username}", map[string]string{ + "username": s.username, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.refresh; v != "" { + params.Set("refresh", v) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackSecurityPutUserService) Validate() error { + var invalid []string + if s.username == "" { + invalid = append(invalid, "Username") + } + if s.user == nil && s.body == nil { + invalid = append(invalid, "User") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackSecurityPutUserService) Do(ctx context.Context) (*XPackSecurityPutUserResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + var body interface{} + if s.user != nil { + body = s.user + } else { + body = s.body + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackSecurityPutUserResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackSecurityPutUserRequest specifies the data required/allowed to add +// a new user. +type XPackSecurityPutUserRequest struct { + Enabled bool `json:"enabled"` + Email string `json:"email,omitempty"` + FullName string `json:"full_name,omitempty"` + Metadata map[string]interface{} `json:"metadata,omitempty"` + Password string `json:"password,omitempty"` + PasswordHash string `json:"password_hash,omitempty"` + Roles []string `json:"roles"` +} + +// XPackSecurityPutUserResponse is the response of XPackSecurityPutUserService.Do. +type XPackSecurityPutUserResponse struct { + User XPackSecurityPutUser `json:"user"` +} + +// XPackSecurityPutUser is the response containing the creation information +type XPackSecurityPutUser struct { + Created bool `json:"created"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_ack_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_ack_watch.go new file mode 100644 index 0000000000..d1ae9253ba --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_ack_watch.go @@ -0,0 +1,198 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherAckWatchService enables you to manually throttle execution of the watch’s actions. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-ack-watch.html. +type XPackWatcherAckWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + watchId string + actionId []string + masterTimeout string +} + +// NewXPackWatcherAckWatchService creates a new XPackWatcherAckWatchService. +func NewXPackWatcherAckWatchService(client *Client) *XPackWatcherAckWatchService { + return &XPackWatcherAckWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherAckWatchService) Pretty(pretty bool) *XPackWatcherAckWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherAckWatchService) Human(human bool) *XPackWatcherAckWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherAckWatchService) ErrorTrace(errorTrace bool) *XPackWatcherAckWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherAckWatchService) FilterPath(filterPath ...string) *XPackWatcherAckWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherAckWatchService) Header(name string, value string) *XPackWatcherAckWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherAckWatchService) Headers(headers http.Header) *XPackWatcherAckWatchService { + s.headers = headers + return s +} + +// WatchId is the unique ID of the watch. +func (s *XPackWatcherAckWatchService) WatchId(watchId string) *XPackWatcherAckWatchService { + s.watchId = watchId + return s +} + +// ActionId is a slice of action ids to be acked. +func (s *XPackWatcherAckWatchService) ActionId(actionId ...string) *XPackWatcherAckWatchService { + s.actionId = append(s.actionId, actionId...) + return s +} + +// MasterTimeout indicates an explicit operation timeout for +// connection to master node. +func (s *XPackWatcherAckWatchService) MasterTimeout(masterTimeout string) *XPackWatcherAckWatchService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherAckWatchService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + if len(s.actionId) > 0 { + path, err = uritemplates.Expand("/_watcher/watch/{watch_id}/_ack/{action_id}", map[string]string{ + "watch_id": s.watchId, + "action_id": strings.Join(s.actionId, ","), + }) + } else { + path, err = uritemplates.Expand("/_watcher/watch/{watch_id}/_ack", map[string]string{ + "watch_id": s.watchId, + }) + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherAckWatchService) Validate() error { + var invalid []string + if s.watchId == "" { + invalid = append(invalid, "WatchId") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackWatcherAckWatchService) Do(ctx context.Context) (*XPackWatcherAckWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherAckWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherAckWatchResponse is the response of XPackWatcherAckWatchService.Do. +type XPackWatcherAckWatchResponse struct { + Status *XPackWatcherAckWatchStatus `json:"status"` +} + +// XPackWatcherAckWatchStatus is the status of a XPackWatcherAckWatchResponse. +type XPackWatcherAckWatchStatus struct { + State map[string]interface{} `json:"state"` + LastChecked string `json:"last_checked"` + LastMetCondition string `json:"last_met_condition"` + Actions map[string]map[string]interface{} `json:"actions"` + ExecutionState string `json:"execution_state"` + Version int `json:"version"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_activate_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_activate_watch.go new file mode 100644 index 0000000000..9a49605320 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_activate_watch.go @@ -0,0 +1,169 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherActivateWatchService enables you to activate a currently inactive watch. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-activate-watch.html. +type XPackWatcherActivateWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + watchId string + masterTimeout string +} + +// NewXPackWatcherActivateWatchService creates a new XPackWatcherActivateWatchService. +func NewXPackWatcherActivateWatchService(client *Client) *XPackWatcherActivateWatchService { + return &XPackWatcherActivateWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherActivateWatchService) Pretty(pretty bool) *XPackWatcherActivateWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherActivateWatchService) Human(human bool) *XPackWatcherActivateWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherActivateWatchService) ErrorTrace(errorTrace bool) *XPackWatcherActivateWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherActivateWatchService) FilterPath(filterPath ...string) *XPackWatcherActivateWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherActivateWatchService) Header(name string, value string) *XPackWatcherActivateWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherActivateWatchService) Headers(headers http.Header) *XPackWatcherActivateWatchService { + s.headers = headers + return s +} + +// WatchId is the ID of the watch to activate. +func (s *XPackWatcherActivateWatchService) WatchId(watchId string) *XPackWatcherActivateWatchService { + s.watchId = watchId + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *XPackWatcherActivateWatchService) MasterTimeout(masterTimeout string) *XPackWatcherActivateWatchService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherActivateWatchService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_watcher/watch/{watch_id}/_activate", map[string]string{ + "watch_id": s.watchId, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherActivateWatchService) Validate() error { + var invalid []string + if s.watchId == "" { + invalid = append(invalid, "WatchId") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackWatcherActivateWatchService) Do(ctx context.Context) (*XPackWatcherActivateWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherActivateWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherActivateWatchResponse is the response of XPackWatcherActivateWatchService.Do. +type XPackWatcherActivateWatchResponse struct { + Status *XPackWatchStatus `json:"status"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_deactivate_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_deactivate_watch.go new file mode 100644 index 0000000000..42c5e55663 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_deactivate_watch.go @@ -0,0 +1,169 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherDeactivateWatchService enables you to deactivate a currently active watch. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-deactivate-watch.html. +type XPackWatcherDeactivateWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + watchId string + masterTimeout string +} + +// NewXPackWatcherDeactivateWatchService creates a new XPackWatcherDeactivateWatchService. +func NewXPackWatcherDeactivateWatchService(client *Client) *XPackWatcherDeactivateWatchService { + return &XPackWatcherDeactivateWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherDeactivateWatchService) Pretty(pretty bool) *XPackWatcherDeactivateWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherDeactivateWatchService) Human(human bool) *XPackWatcherDeactivateWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherDeactivateWatchService) ErrorTrace(errorTrace bool) *XPackWatcherDeactivateWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherDeactivateWatchService) FilterPath(filterPath ...string) *XPackWatcherDeactivateWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherDeactivateWatchService) Header(name string, value string) *XPackWatcherDeactivateWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherDeactivateWatchService) Headers(headers http.Header) *XPackWatcherDeactivateWatchService { + s.headers = headers + return s +} + +// WatchId is the ID of the watch to deactivate. +func (s *XPackWatcherDeactivateWatchService) WatchId(watchId string) *XPackWatcherDeactivateWatchService { + s.watchId = watchId + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *XPackWatcherDeactivateWatchService) MasterTimeout(masterTimeout string) *XPackWatcherDeactivateWatchService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherDeactivateWatchService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_watcher/watch/{watch_id}/_deactivate", map[string]string{ + "watch_id": s.watchId, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherDeactivateWatchService) Validate() error { + var invalid []string + if s.watchId == "" { + invalid = append(invalid, "WatchId") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackWatcherDeactivateWatchService) Do(ctx context.Context) (*XPackWatcherDeactivateWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherDeactivateWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherDeactivateWatchResponse is the response of XPackWatcherDeactivateWatchService.Do. +type XPackWatcherDeactivateWatchResponse struct { + Status *XPackWatchStatus `json:"status"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_delete_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_delete_watch.go new file mode 100644 index 0000000000..dc7e7f9595 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_delete_watch.go @@ -0,0 +1,171 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherDeleteWatchService removes a watch. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-delete-watch.html. +type XPackWatcherDeleteWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + masterTimeout string +} + +// NewXPackWatcherDeleteWatchService creates a new XPackWatcherDeleteWatchService. +func NewXPackWatcherDeleteWatchService(client *Client) *XPackWatcherDeleteWatchService { + return &XPackWatcherDeleteWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherDeleteWatchService) Pretty(pretty bool) *XPackWatcherDeleteWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherDeleteWatchService) Human(human bool) *XPackWatcherDeleteWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherDeleteWatchService) ErrorTrace(errorTrace bool) *XPackWatcherDeleteWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherDeleteWatchService) FilterPath(filterPath ...string) *XPackWatcherDeleteWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherDeleteWatchService) Header(name string, value string) *XPackWatcherDeleteWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherDeleteWatchService) Headers(headers http.Header) *XPackWatcherDeleteWatchService { + s.headers = headers + return s +} + +// Id of the watch to delete. +func (s *XPackWatcherDeleteWatchService) Id(id string) *XPackWatcherDeleteWatchService { + s.id = id + return s +} + +// MasterTimeout specifies an explicit operation timeout for connection to master node. +func (s *XPackWatcherDeleteWatchService) MasterTimeout(masterTimeout string) *XPackWatcherDeleteWatchService { + s.masterTimeout = masterTimeout + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherDeleteWatchService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_watcher/watch/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherDeleteWatchService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackWatcherDeleteWatchService) Do(ctx context.Context) (*XPackWatcherDeleteWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "DELETE", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherDeleteWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherDeleteWatchResponse is the response of XPackWatcherDeleteWatchService.Do. +type XPackWatcherDeleteWatchResponse struct { + Found bool `json:"found"` + Id string `json:"_id"` + Version int `json:"_version"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_execute_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_execute_watch.go new file mode 100644 index 0000000000..9a31c51ee3 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_execute_watch.go @@ -0,0 +1,214 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherExecuteWatchService forces the execution of a stored watch. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-execute-watch.html. +type XPackWatcherExecuteWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + debug *bool + bodyJson interface{} + bodyString string +} + +// NewXPackWatcherExecuteWatchService creates a new XPackWatcherExecuteWatchService. +func NewXPackWatcherExecuteWatchService(client *Client) *XPackWatcherExecuteWatchService { + return &XPackWatcherExecuteWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherExecuteWatchService) Pretty(pretty bool) *XPackWatcherExecuteWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherExecuteWatchService) Human(human bool) *XPackWatcherExecuteWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherExecuteWatchService) ErrorTrace(errorTrace bool) *XPackWatcherExecuteWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherExecuteWatchService) FilterPath(filterPath ...string) *XPackWatcherExecuteWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherExecuteWatchService) Header(name string, value string) *XPackWatcherExecuteWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherExecuteWatchService) Headers(headers http.Header) *XPackWatcherExecuteWatchService { + s.headers = headers + return s +} + +// Id of the watch to execute on. +func (s *XPackWatcherExecuteWatchService) Id(id string) *XPackWatcherExecuteWatchService { + s.id = id + return s +} + +// Debug indicates whether the watch should execute in debug mode. +func (s *XPackWatcherExecuteWatchService) Debug(debug bool) *XPackWatcherExecuteWatchService { + s.debug = &debug + return s +} + +// BodyJson is documented as: Execution control. +func (s *XPackWatcherExecuteWatchService) BodyJson(body interface{}) *XPackWatcherExecuteWatchService { + s.bodyJson = body + return s +} + +// BodyString is documented as: Execution control. +func (s *XPackWatcherExecuteWatchService) BodyString(body string) *XPackWatcherExecuteWatchService { + s.bodyString = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherExecuteWatchService) buildURL() (string, url.Values, error) { + // Build URL + var ( + path string + err error + ) + if s.id != "" { + path, err = uritemplates.Expand("/_watcher/watch/{id}/_execute", map[string]string{ + "id": s.id, + }) + } else { + path = "/_watcher/watch/_execute" + } + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.debug; v != nil { + params.Set("debug", fmt.Sprint(*v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherExecuteWatchService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *XPackWatcherExecuteWatchService) Do(ctx context.Context) (*XPackWatcherExecuteWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Setup HTTP request body + var body interface{} + if s.bodyJson != nil { + body = s.bodyJson + } else { + body = s.bodyString + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherExecuteWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherExecuteWatchResponse is the response of XPackWatcherExecuteWatchService.Do. +type XPackWatcherExecuteWatchResponse struct { + Id string `json:"_id"` + WatchRecord *XPackWatchRecord `json:"watch_record"` +} + +type XPackWatchRecord struct { + WatchId string `json:"watch_id"` + Node string `json:"node"` + Messages []string `json:"messages"` + State string `json:"state"` + Status *XPackWatchRecordStatus `json:"status"` + Input map[string]map[string]interface{} `json:"input"` + Condition map[string]map[string]interface{} `json:"condition"` + Result map[string]interface{} `json:"Result"` +} + +type XPackWatchRecordStatus struct { + Version int `json:"version"` + State map[string]interface{} `json:"state"` + LastChecked string `json:"last_checked"` + LastMetCondition string `json:"last_met_condition"` + Actions map[string]map[string]interface{} `json:"actions"` + ExecutionState string `json:"execution_state"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_get_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_get_watch.go new file mode 100644 index 0000000000..b0ce092883 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_get_watch.go @@ -0,0 +1,214 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherGetWatchService retrieves a watch by its ID. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-get-watch.html. +type XPackWatcherGetWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string +} + +// NewXPackWatcherGetWatchService creates a new XPackWatcherGetWatchService. +func NewXPackWatcherGetWatchService(client *Client) *XPackWatcherGetWatchService { + return &XPackWatcherGetWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherGetWatchService) Pretty(pretty bool) *XPackWatcherGetWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherGetWatchService) Human(human bool) *XPackWatcherGetWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherGetWatchService) ErrorTrace(errorTrace bool) *XPackWatcherGetWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherGetWatchService) FilterPath(filterPath ...string) *XPackWatcherGetWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherGetWatchService) Header(name string, value string) *XPackWatcherGetWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherGetWatchService) Headers(headers http.Header) *XPackWatcherGetWatchService { + s.headers = headers + return s +} + +// Id is ID of the watch to retrieve. +func (s *XPackWatcherGetWatchService) Id(id string) *XPackWatcherGetWatchService { + s.id = id + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherGetWatchService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_watcher/watch/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherGetWatchService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackWatcherGetWatchService) Do(ctx context.Context) (*XPackWatcherGetWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherGetWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherGetWatchResponse is the response of XPackWatcherGetWatchService.Do. +type XPackWatcherGetWatchResponse struct { + Found bool `json:"found"` + Id string `json:"_id"` + Version int64 `json:"_version,omitempty"` + Status *XPackWatchStatus `json:"status,omitempty"` + Watch *XPackWatch `json:"watch,omitempty"` +} + +type XPackWatchStatus struct { + State *XPackWatchExecutionState `json:"state,omitempty"` + LastChecked *time.Time `json:"last_checked,omitempty"` + LastMetCondition *time.Time `json:"last_met_condition,omitempty"` + Actions map[string]*XPackWatchActionStatus `json:"actions,omitempty"` + ExecutionState string `json:"execution_state,omitempty"` + Headers map[string]string `json:"headers,omitempty"` + Version int64 `json:"version"` +} + +type XPackWatchExecutionState struct { + Active bool `json:"active"` + Timestamp time.Time `json:"timestamp"` +} + +type XPackWatchActionStatus struct { + AckStatus *XPackWatchActionAckStatus `json:"ack"` + LastExecution *XPackWatchActionExecutionState `json:"last_execution,omitempty"` + LastSuccessfulExecution *XPackWatchActionExecutionState `json:"last_successful_execution,omitempty"` + LastThrottle *XPackWatchActionThrottle `json:"last_throttle,omitempty"` +} + +type XPackWatchActionAckStatus struct { + Timestamp time.Time `json:"timestamp"` + AckStatusState string `json:"ack_status_state"` +} + +type XPackWatchActionExecutionState struct { + Timestamp time.Time `json:"timestamp"` + Successful bool `json:"successful"` + Reason string `json:"reason,omitempty"` +} + +type XPackWatchActionThrottle struct { + Timestamp time.Time `json:"timestamp"` + Reason string `json:"reason,omitempty"` +} + +type XPackWatch struct { + Trigger map[string]map[string]interface{} `json:"trigger"` + Input map[string]map[string]interface{} `json:"input"` + Condition map[string]map[string]interface{} `json:"condition"` + Transform map[string]interface{} `json:"transform,omitempty"` + ThrottlePeriod string `json:"throttle_period,omitempty"` + ThrottlePeriodInMillis int64 `json:"throttle_period_in_millis,omitempty"` + Actions map[string]*XPackWatchActionStatus `json:"actions"` + Metadata map[string]interface{} `json:"metadata,omitempty"` + Status *XPackWatchStatus `json:"status,omitempty"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_put_watch.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_put_watch.go new file mode 100644 index 0000000000..03298253a4 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_put_watch.go @@ -0,0 +1,212 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/olivere/elastic/v7/uritemplates" +) + +// XPackWatcherPutWatchService either registers a new watch in Watcher +// or update an existing one. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-put-watch.html. +type XPackWatcherPutWatchService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + id string + active *bool + masterTimeout string + ifSeqNo *int64 + ifPrimaryTerm *int64 + body interface{} +} + +// NewXPackWatcherPutWatchService creates a new XPackWatcherPutWatchService. +func NewXPackWatcherPutWatchService(client *Client) *XPackWatcherPutWatchService { + return &XPackWatcherPutWatchService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherPutWatchService) Pretty(pretty bool) *XPackWatcherPutWatchService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherPutWatchService) Human(human bool) *XPackWatcherPutWatchService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherPutWatchService) ErrorTrace(errorTrace bool) *XPackWatcherPutWatchService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherPutWatchService) FilterPath(filterPath ...string) *XPackWatcherPutWatchService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherPutWatchService) Header(name string, value string) *XPackWatcherPutWatchService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherPutWatchService) Headers(headers http.Header) *XPackWatcherPutWatchService { + s.headers = headers + return s +} + +// Id of the watch to upsert. +func (s *XPackWatcherPutWatchService) Id(id string) *XPackWatcherPutWatchService { + s.id = id + return s +} + +// Active specifies whether the watch is in/active by default. +func (s *XPackWatcherPutWatchService) Active(active bool) *XPackWatcherPutWatchService { + s.active = &active + return s +} + +// MasterTimeout is an explicit operation timeout for connection to master node. +func (s *XPackWatcherPutWatchService) MasterTimeout(masterTimeout string) *XPackWatcherPutWatchService { + s.masterTimeout = masterTimeout + return s +} + +// IfSeqNo indicates to update the watch only if the last operation that +// has changed the watch has the specified sequence number. +func (s *XPackWatcherPutWatchService) IfSeqNo(seqNo int64) *XPackWatcherPutWatchService { + s.ifSeqNo = &seqNo + return s +} + +// IfPrimaryTerm indicates to update the watch only if the last operation that +// has changed the watch has the specified primary term. +func (s *XPackWatcherPutWatchService) IfPrimaryTerm(primaryTerm int64) *XPackWatcherPutWatchService { + s.ifPrimaryTerm = &primaryTerm + return s +} + +// Body specifies the watch. Use a string or a type that will get serialized as JSON. +func (s *XPackWatcherPutWatchService) Body(body interface{}) *XPackWatcherPutWatchService { + s.body = body + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherPutWatchService) buildURL() (string, url.Values, error) { + // Build URL + path, err := uritemplates.Expand("/_watcher/watch/{id}", map[string]string{ + "id": s.id, + }) + if err != nil { + return "", url.Values{}, err + } + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.active; v != nil { + params.Set("active", fmt.Sprint(*v)) + } + if s.masterTimeout != "" { + params.Set("master_timeout", s.masterTimeout) + } + if v := s.ifSeqNo; v != nil { + params.Set("if_seq_no", fmt.Sprintf("%d", *v)) + } + if v := s.ifPrimaryTerm; v != nil { + params.Set("if_primary_term", fmt.Sprintf("%d", *v)) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherPutWatchService) Validate() error { + var invalid []string + if s.id == "" { + invalid = append(invalid, "Id") + } + if s.body == nil { + invalid = append(invalid, "Body") + } + if len(invalid) > 0 { + return fmt.Errorf("missing required fields: %v", invalid) + } + return nil +} + +// Do executes the operation. +func (s *XPackWatcherPutWatchService) Do(ctx context.Context) (*XPackWatcherPutWatchResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "PUT", + Path: path, + Params: params, + Body: s.body, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherPutWatchResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherPutWatchResponse is the response of XPackWatcherPutWatchService.Do. +type XPackWatcherPutWatchResponse struct { +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_start.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_start.go new file mode 100644 index 0000000000..3fb29a7ac3 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_start.go @@ -0,0 +1,137 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" +) + +// XPackWatcherStartService starts the watcher service if it is not already running. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-start.html. +type XPackWatcherStartService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers +} + +// NewXPackWatcherStartService creates a new XPackWatcherStartService. +func NewXPackWatcherStartService(client *Client) *XPackWatcherStartService { + return &XPackWatcherStartService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherStartService) Pretty(pretty bool) *XPackWatcherStartService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherStartService) Human(human bool) *XPackWatcherStartService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherStartService) ErrorTrace(errorTrace bool) *XPackWatcherStartService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherStartService) FilterPath(filterPath ...string) *XPackWatcherStartService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherStartService) Header(name string, value string) *XPackWatcherStartService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherStartService) Headers(headers http.Header) *XPackWatcherStartService { + s.headers = headers + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherStartService) buildURL() (string, url.Values, error) { + // Build URL path + path := "/_watcher/_start" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherStartService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *XPackWatcherStartService) Do(ctx context.Context) (*XPackWatcherStartResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherStartResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherStartResponse is the response of XPackWatcherStartService.Do. +type XPackWatcherStartResponse struct { + Acknowledged bool `json:"acknowledged"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_stats.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_stats.go new file mode 100644 index 0000000000..9c615c2ec1 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_stats.go @@ -0,0 +1,165 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" +) + +// XPackWatcherStatsService returns the current watcher metrics. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-stats.html. +type XPackWatcherStatsService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers + + metric string + emitStacktraces *bool +} + +// NewXPackWatcherStatsService creates a new XPackWatcherStatsService. +func NewXPackWatcherStatsService(client *Client) *XPackWatcherStatsService { + return &XPackWatcherStatsService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherStatsService) Pretty(pretty bool) *XPackWatcherStatsService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherStatsService) Human(human bool) *XPackWatcherStatsService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherStatsService) ErrorTrace(errorTrace bool) *XPackWatcherStatsService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherStatsService) FilterPath(filterPath ...string) *XPackWatcherStatsService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherStatsService) Header(name string, value string) *XPackWatcherStatsService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherStatsService) Headers(headers http.Header) *XPackWatcherStatsService { + s.headers = headers + return s +} + +// Metric controls what additional stat metrics should be include in the response. +func (s *XPackWatcherStatsService) Metric(metric string) *XPackWatcherStatsService { + s.metric = metric + return s +} + +// EmitStacktraces, if enabled, emits stack traces of currently running watches. +func (s *XPackWatcherStatsService) EmitStacktraces(emitStacktraces bool) *XPackWatcherStatsService { + s.emitStacktraces = &emitStacktraces + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherStatsService) buildURL() (string, url.Values, error) { + // Build URL + path := "/_watcher/stats" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + if v := s.emitStacktraces; v != nil { + params.Set("emit_stacktraces", fmt.Sprint(*v)) + } + if s.metric != "" { + params.Set("metric", s.metric) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherStatsService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *XPackWatcherStatsService) Do(ctx context.Context) (*XPackWatcherStatsResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "GET", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherStatsResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherStatsResponse is the response of XPackWatcherStatsService.Do. +type XPackWatcherStatsResponse struct { + Stats []XPackWatcherStats `json:"stats"` +} + +// XPackWatcherStats represents the stats used in XPackWatcherStatsResponse. +type XPackWatcherStats struct { + WatcherState string `json:"watcher_state"` + WatchCount int `json:"watch_count"` + ExecutionThreadPool map[string]interface{} `json:"execution_thread_pool"` +} diff --git a/vendor/github.com/olivere/elastic/v7/xpack_watcher_stop.go b/vendor/github.com/olivere/elastic/v7/xpack_watcher_stop.go new file mode 100644 index 0000000000..d5fe9d09b2 --- /dev/null +++ b/vendor/github.com/olivere/elastic/v7/xpack_watcher_stop.go @@ -0,0 +1,137 @@ +// Copyright 2012-2018 Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" +) + +// XPackWatcherStopService stops the watcher service if it is running. +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/watcher-api-stop.html. +type XPackWatcherStopService struct { + client *Client + + pretty *bool // pretty format the returned JSON response + human *bool // return human readable values for statistics + errorTrace *bool // include the stack trace of returned errors + filterPath []string // list of filters used to reduce the response + headers http.Header // custom request-level HTTP headers +} + +// NewXPackWatcherStopService creates a new XPackWatcherStopService. +func NewXPackWatcherStopService(client *Client) *XPackWatcherStopService { + return &XPackWatcherStopService{ + client: client, + } +} + +// Pretty tells Elasticsearch whether to return a formatted JSON response. +func (s *XPackWatcherStopService) Pretty(pretty bool) *XPackWatcherStopService { + s.pretty = &pretty + return s +} + +// Human specifies whether human readable values should be returned in +// the JSON response, e.g. "7.5mb". +func (s *XPackWatcherStopService) Human(human bool) *XPackWatcherStopService { + s.human = &human + return s +} + +// ErrorTrace specifies whether to include the stack trace of returned errors. +func (s *XPackWatcherStopService) ErrorTrace(errorTrace bool) *XPackWatcherStopService { + s.errorTrace = &errorTrace + return s +} + +// FilterPath specifies a list of filters used to reduce the response. +func (s *XPackWatcherStopService) FilterPath(filterPath ...string) *XPackWatcherStopService { + s.filterPath = filterPath + return s +} + +// Header adds a header to the request. +func (s *XPackWatcherStopService) Header(name string, value string) *XPackWatcherStopService { + if s.headers == nil { + s.headers = http.Header{} + } + s.headers.Add(name, value) + return s +} + +// Headers specifies the headers of the request. +func (s *XPackWatcherStopService) Headers(headers http.Header) *XPackWatcherStopService { + s.headers = headers + return s +} + +// buildURL builds the URL for the operation. +func (s *XPackWatcherStopService) buildURL() (string, url.Values, error) { + // Build URL path + path := "/_watcher/_stop" + + // Add query string parameters + params := url.Values{} + if v := s.pretty; v != nil { + params.Set("pretty", fmt.Sprint(*v)) + } + if v := s.human; v != nil { + params.Set("human", fmt.Sprint(*v)) + } + if v := s.errorTrace; v != nil { + params.Set("error_trace", fmt.Sprint(*v)) + } + if len(s.filterPath) > 0 { + params.Set("filter_path", strings.Join(s.filterPath, ",")) + } + return path, params, nil +} + +// Validate checks if the operation is valid. +func (s *XPackWatcherStopService) Validate() error { + return nil +} + +// Do executes the operation. +func (s *XPackWatcherStopService) Do(ctx context.Context) (*XPackWatcherStopResponse, error) { + // Check pre-conditions + if err := s.Validate(); err != nil { + return nil, err + } + + // Get URL for request + path, params, err := s.buildURL() + if err != nil { + return nil, err + } + + // Get HTTP response + res, err := s.client.PerformRequest(ctx, PerformRequestOptions{ + Method: "POST", + Path: path, + Params: params, + Headers: s.headers, + }) + if err != nil { + return nil, err + } + + // Return operation response + ret := new(XPackWatcherStopResponse) + if err := json.Unmarshal(res.Body, ret); err != nil { + return nil, err + } + return ret, nil +} + +// XPackWatcherStopResponse is the response of XPackWatcherStopService.Do. +type XPackWatcherStopResponse struct { + Acknowledged bool `json:"acknowledged"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index d5de9130b3..a6fb0ad5e3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -294,6 +294,7 @@ github.com/lunny/nodb/store/goleveldb # github.com/magiconair/properties v1.8.1 github.com/magiconair/properties # github.com/mailru/easyjson v0.7.0 +github.com/mailru/easyjson github.com/mailru/easyjson/buffer github.com/mailru/easyjson/jlexer github.com/mailru/easyjson/jwriter @@ -336,6 +337,10 @@ github.com/nfnt/resize github.com/niklasfasching/go-org/org # github.com/oliamb/cutter v0.2.2 github.com/oliamb/cutter +# github.com/olivere/elastic/v7 v7.0.9 +github.com/olivere/elastic/v7 +github.com/olivere/elastic/v7/config +github.com/olivere/elastic/v7/uritemplates # github.com/pelletier/go-toml v1.4.0 github.com/pelletier/go-toml # github.com/philhofer/fwd v1.0.0