Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions elastic-utils-lib/src/cmr/elastic_utils/search/es_index.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require
[cheshire.core :as json]
[clojure.set :as set]
[clojure.string :as string]
[clojurewerkz.elastisch.rest.index :as esri]
[cmr.common.lifecycle :as lifecycle]
[cmr.common.log :refer [debug info warnf]]
Expand Down Expand Up @@ -62,9 +63,9 @@
in a system using the :search-index key."
[context es-cluster-name]
(cond
(= es-cluster-name cmr.elastic-utils.config/non-gran-elastic-name) (get-in context [:system :non-gran-search-index])
(= es-cluster-name cmr.elastic-utils.config/gran-elastic-name) (get-in context [:system :gran-search-index])
:else (throw (Exception. (str "expected specific elastic name but got " es-cluster-name " instead.")))))
(= es-cluster-name cmr.elastic-utils.config/non-gran-elastic-name) (get-in context [:system :non-gran-search-index])
(= es-cluster-name cmr.elastic-utils.config/gran-elastic-name) (get-in context [:system :gran-search-index])
:else (throw (Exception. (str "expected specific elastic name but got " es-cluster-name " instead.")))))

(defn context->conn
"Returns the connection given a context. This assumes that the search index is always located in
Expand Down Expand Up @@ -158,18 +159,25 @@
;; TODO 10636 this is hardcoded to index name...could it be better? Will these rules always be true?
;; TODO unit test this -- need a sys test as well, so that if any index is created or found, we will auto warn that something could break with this
(defn get-es-cluster-name-from-index-name
"Returns the Elasticsearch cluster name based on the index name."
[index-name]
;; NOTE: expecting index-name to represent only one index-name as a string
;(println "10636- INSIDE get-es-cluster-from-index-name. Given index-name = " index-name)
(if
(and (not (= index-name "collection_search_alias"))
(and (not (= index-name "1_collections_v2"))
(or (clojure.string/starts-with? index-name "1_c")
(= index-name "1_small_collections")
(= index-name "1_deleted_granules")
(= index-name (str cmr.elastic-utils.config/gran-elastic-name "-index-sets")))))
cmr.elastic-utils.config/gran-elastic-name
cmr.elastic-utils.config/non-gran-elastic-name))
(let [gran-cluster cmr.elastic-utils.config/gran-elastic-name
non-gran-cluster cmr.elastic-utils.config/non-gran-elastic-name
gran-index-set-name (str gran-cluster "-index-sets")

excluded-indices #{"collection_search_alias" "1_collections_v2"}
gran-specific-indices #{"1_small_collections" "1_deleted_granules"}

uses-gran-cluster? (and
(not (excluded-indices index-name))
(or (string/starts-with? index-name "1_c")
Comment thread
jceaser marked this conversation as resolved.
(gran-specific-indices index-name)
(= index-name gran-index-set-name)))]
(if uses-gran-cluster?
gran-cluster
non-gran-cluster)))

(defn get-es-cluster-name-by-index-info-type-name
[index-info]
Expand Down Expand Up @@ -286,10 +294,10 @@
(set/rename-keys {:search-after :search_after})
util/remove-nil-keys)]
(debug "Executing against indexes [" (:index-name index-info) "] the elastic query:"
(pr-str elastic-query)
"with sort" (pr-str sort-params)
"with aggregations" (pr-str aggregations)
"and highlights" (pr-str highlights))
(pr-str elastic-query)
"with sort" (pr-str sort-params)
"with aggregations" (pr-str aggregations)
"and highlights" (pr-str highlights))
(when-let [scroll-id (:scroll-id query-map)]
(debug "Using scroll-id" scroll-id))
(when-let [search-after (:search_after query-map)]
Expand Down Expand Up @@ -348,7 +356,7 @@
(if (or (nil? search-after-values)
(>= (count accumulated-hits) total-hits))
;; We've got all results
(do
(do
(debug "Returning all results with total hits:" total-hits
"and took time:" took-total
"timed out:" timed-out)
Expand All @@ -360,7 +368,7 @@

(let [next-response (send-query-to-elastic
context
(assoc query-with-sort
(assoc query-with-sort
:page-size batch-size
:search-after search-after-values))
new-hits (get-in next-response [:hits :hits])]
Expand Down
28 changes: 25 additions & 3 deletions elastic-utils-lib/test/cmr/elastic_utils/test/es_index.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
(ns cmr.elastic-utils.test.es-index
"Tests for the cmr.elastic-utils.search.es-index namespace"
(:require
(:require
[clojure.test :refer [deftest is testing]]
[cmr.elastic-utils.search.es-index :as es-index]
[cmr.common.services.search.query-model :as qm]
[cmr.elastic-utils.search.es-group-query-conditions :as gc]))
[cmr.elastic-utils.config :as config]
[cmr.elastic-utils.search.es-group-query-conditions :as gc]
[cmr.elastic-utils.search.es-index :as es-index]))

(def gran-cluster cmr.elastic-utils.config/gran-elastic-name)
(def non-gran-cluster config/non-gran-elastic-name)

(deftest test-query->execution-params
(let [query->execution-params #'es-index/query->execution-params
Expand All @@ -27,3 +31,21 @@
execution-params (query->execution-params query)]
(is (not (= false
(:_source execution-params))))))))

(deftest test-get-es-cluster-name-from-index-name
(testing "All excluded indices always return non-gran cluster regardless of other patterns"
(let [excluded #{"collection_search_alias" "1_collections_v2"}]
(doseq [excluded-index excluded]
(is (= non-gran-cluster
(es-index/get-es-cluster-name-from-index-name excluded-index))))))

(testing "All indices starting with '1_c' (but not excluded) return gran cluster"
(let [test-indices ["1_c" "1_ca" "1_collections" "1_custom" "1_c123"]]
(doseq [index test-indices]
(is (= gran-cluster
(es-index/get-es-cluster-name-from-index-name index))))))

(testing "Random/default indices should return non-gran cluster"
(let [random #{"some_index" "users" "metadata" ""}]
(doseq [index random]
(is (= non-gran-cluster (es-index/get-es-cluster-name-from-index-name index)))))))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have a missing new line. Did you use vscode and the calva plugin?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, vscode and calva

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure to which newline you are referring

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the one at the end of the file, the (-).

69 changes: 69 additions & 0 deletions resources/shell/cmr.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# file: cmr.fish

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, there is another fish user on the project?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this file for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, this is a command completions file for using the cmr command in the fish shell. It's not important to your branch, I just used it as an opportunity to add it as mentioned in the README

(If you use a system shell not compatible with Bash, we accept Pull Requests for
new shells with auto-complete.)

# cmr (sub)command-completion for fish shell
# copy this file to ~/.config/fish/completions

set -l apps access-control bootstrap dev-system indexer ingest metadata-db search virtual-product
set -l libs acl-lib common-app-lib common-lib elastic-utils-lib es-spatial-plugin message-queue-lib oracle-lib orbits-lib redis-lib spatial-lib transmit-lib umm-lib umm-spec-lib vdd-spatial-viz
set -l apps_libs $apps $libs

# Level 1
set -l top_commands build clean git install setup show start status stop test
set -l top_dashed -h -v --help --version

# Level 2
set -l setup_level local db dev help profile
set -l build_level docker help uberdocker uberjar uberjars
set -l clean_level all es-data help $apps_libs
set -l install_level all docs help jars 'jars,docs' local orbits-gems oracle-libs
set -l show_level help log log-tail log-test log-tests port-process sqs-queues
set -l start_level all docker help local repl uberdocker uberjar $apps
set -l status_level docker help sqs-sns uberdocker uberjar
set -l stop_level all docker help local uberdocker uberjar $apps
set -l test_level all cicd dep-tree dep-trees help lint versions
set -l git_level branches help log-latest log-short log-graph tag

# Level 3
set -l setup_db_level create-users do-migrations help
set -l star_docker_level all help $apps
set -l star_local_level spatial_plugin sqs-sns help
set -l uberdocker_level separate together

# Main completion function
complete -c cmr -f

# Level 1 completions
complete -c cmr -n __fish_use_subcommand -a "$top_commands"
complete -c cmr -n __fish_use_subcommand -a "$top_dashed"

# Level 2 completions
complete -c cmr -n '__fish_seen_subcommand_from build' -a "$build_level"
complete -c cmr -n '__fish_seen_subcommand_from clean' -a "$clean_level"
complete -c cmr -n '__fish_seen_subcommand_from git' -a "$git_level"
complete -c cmr -n '__fish_seen_subcommand_from help' -a "$top_commands"
complete -c cmr -n '__fish_seen_subcommand_from install' -a "$install_level"
complete -c cmr -n '__fish_seen_subcommand_from setup' -a "$setup_level"
complete -c cmr -n '__fish_seen_subcommand_from show' -a "$show_level"
complete -c cmr -n '__fish_seen_subcommand_from start' -a "$start_level"
complete -c cmr -n '__fish_seen_subcommand_from status' -a "$status_level"
complete -c cmr -n '__fish_seen_subcommand_from stop' -a "$stop_level"
complete -c cmr -n '__fish_seen_subcommand_from test' -a "$test_level"

# Level 3 completions
complete -c cmr -n '__fish_seen_subcommand_from setup; and __fish_seen_subcommand_from db' -a "$setup_db_level"
complete -c cmr -n '__fish_seen_subcommand_from test; and __fish_seen_subcommand_from dep-tree' -a "$apps_libs"
complete -c cmr -n '__fish_seen_subcommand_from start; and __fish_seen_subcommand_from docker' -a "$star_docker_level"
complete -c cmr -n '__fish_seen_subcommand_from stop; and __fish_seen_subcommand_from docker' -a "$star_docker_level"
complete -c cmr -n '__fish_seen_subcommand_from status; and __fish_seen_subcommand_from docker' -a "$star_docker_level"
complete -c cmr -n '__fish_seen_subcommand_from test; and __fish_seen_subcommand_from lint' -a "$apps_libs"
complete -c cmr -n '__fish_seen_subcommand_from start; and __fish_seen_subcommand_from local' -a "$star_local_level"
complete -c cmr -n '__fish_seen_subcommand_from stop; and __fish_seen_subcommand_from local' -a "$star_local_level"
complete -c cmr -n '__fish_seen_subcommand_from show; and __fish_seen_subcommand_from log' -a "$apps"
complete -c cmr -n '__fish_seen_subcommand_from show; and __fish_seen_subcommand_from log-tail' -a "$apps"
complete -c cmr -n '__fish_seen_subcommand_from show; and __fish_seen_subcommand_from log-test' -a "$apps"
complete -c cmr -n '__fish_seen_subcommand_from start; and __fish_seen_subcommand_from uberdocker' -a "$uberdocker_level"
complete -c cmr -n '__fish_seen_subcommand_from stop; and __fish_seen_subcommand_from uberdocker' -a "$uberdocker_level"
complete -c cmr -n '__fish_seen_subcommand_from status; and __fish_seen_subcommand_from uberdocker' -a "$uberdocker_level"
complete -c cmr -n '__fish_seen_subcommand_from start; and __fish_seen_subcommand_from uberjar' -a "$apps"
complete -c cmr -n '__fish_seen_subcommand_from stop; and __fish_seen_subcommand_from uberjar' -a "$apps"
complete -c cmr -n '__fish_seen_subcommand_from build; and __fish_seen_subcommand_from uberjar' -a "$apps"
complete -c cmr -n '__fish_seen_subcommand_from test; and __fish_seen_subcommand_from versions' -a "$apps_libs"