-
Notifications
You must be signed in to change notification settings - Fork 11
support ES 9.2.0 #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
support ES 9.2.0 #102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| # Changelog | ||
| ## Unreleased | ||
| ## v0.1.3 | ||
| **Features** | ||
| Added support for ES 9.2.0 | ||
|
|
||
| ## v0.11.0 | ||
| **Features** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,19 +265,32 @@ def construct_es_query_term(filter_key, value) | |
| query: value, | ||
| fields: all_fields ? nil : fields, | ||
| default_operator: "and", | ||
| all_fields: all_fields | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deprecated in 6.0.0 and since removed |
||
| }.compact | ||
| } | ||
| when /common(,[0-9.]+){,2}/ | ||
| ensure_single_field_for "common", fields do |field| | ||
| flag, cutoff, min_match = flag.split(",") | ||
| cutoff = cutoff or @configuration[:common_terms_cutoff_frequency] | ||
| term = { | ||
| common: { | ||
| field => { query: value, cutoff_frequency: cutoff } | ||
| # common was deprecated and removed in favor of match | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see deprecation message here https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-common-terms-query.html |
||
| # cutoff_frequency for match was also removed and is no longer used | ||
| match: { | ||
| field => { query: value } | ||
| } | ||
| } | ||
| term["minimum_should_match"] = min_match if min_match | ||
| term[:match][field]["minimum_should_match"] = min_match if min_match | ||
| term | ||
| end | ||
| when /match(,[0-9.]+){,1}/ | ||
| ensure_single_field_for "match", fields do |field| | ||
| flag, min_match = flag.split(",") | ||
| term = { | ||
| # common was deprecated and removed in favor of match | ||
| # cutoff_frequency for match was also removed and is no longer used | ||
| match: { | ||
| field => { query: value } | ||
| } | ||
| } | ||
| term[:match][field]["minimum_should_match"] = min_match if min_match | ||
| term | ||
| end | ||
| else | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,25 +3,34 @@ | |
| require 'connection_pool' | ||
|
|
||
| # monkeypatch "authentic product check"" in client | ||
| module Elasticsearch | ||
| class Client | ||
| alias original_verify_with_version_or_header verify_with_version_or_header | ||
| module ElasticsearchMonkeyPatch | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new version of the monkeypatch was needed |
||
| private | ||
|
|
||
| def verify_with_version_or_header(...) | ||
| original_verify_with_version_or_header(...) | ||
| rescue Elasticsearch::UnsupportedProductError | ||
| # silenty ignore this error | ||
| def verify_elasticsearch(*args, &block) | ||
| while not @verified do | ||
| sleep 1 | ||
| begin | ||
| response = @transport.perform_request(*args, &block) | ||
| response.headers['x-elastic-product'] = 'Elasticsearch' | ||
| @verified = true | ||
| rescue StandardError => e | ||
| Mu::log.debug("SETUP") { "no reaction from elastic, retrying..." } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when creating the connection pool, the library already performs this check, but elastic isn't there yet, this causes an error and if we don't rescue, the service crashes.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is before our check on if elastic is up (which is still needed because the cluster may not be healthy yet even if the response gives a 200). |
||
| next | ||
| end | ||
| end | ||
| response | ||
| end | ||
| end | ||
|
|
||
| Elasticsearch::Client.prepend(ElasticsearchMonkeyPatch) | ||
|
|
||
| # A wrapper around elasticsearch client for backwards compatiblity | ||
| # see https://rubydoc.info/gems/elasticsearch-api/Elasticsearch | ||
| # and https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/examples.html | ||
| # for docs on the client api | ||
| ## | ||
| module MuSearch | ||
| class Elastic | ||
| class ElasticWrapper | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to avoid conflict with the Elastic::Transport that replaces Elasticsearch::Transport |
||
| # Sets up the ElasticSearch connection pool | ||
| def initialize(size:) | ||
| MuSearch::ElasticConnectionPool.setup(size: size) | ||
|
|
@@ -32,9 +41,11 @@ def initialize(size:) | |
| # | ||
| # Executes a health check and accepts either "green" or "yellow". | ||
| def up? | ||
| Mu::log.debug("SETUP") { "Checking if Elasticsearch is up..." } | ||
| MuSearch::ElasticConnectionPool.with_client do |es_client| | ||
| begin | ||
| health = es_client.cluster.health | ||
| Mu::log.debug("SETUP") { "Elasticsearch cluster health: #{health["status"]}" } | ||
| health["status"] == "yellow" or health["status"] == "green" | ||
| rescue | ||
| false | ||
|
|
@@ -68,7 +79,7 @@ def create_index(index, mappings = nil, settings = nil) | |
| MuSearch::ElasticConnectionPool.with_client do |es_client| | ||
| begin | ||
| es_client.indices.create(index: index, body: { settings: settings, mappings: mappings}) | ||
| rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e | ||
| rescue Elastic::Transport::Transport::Errors::BadRequest => e | ||
| error_message = e.message | ||
| if error_message.include?("resource_already_exists_exception") | ||
| @logger.warn("ELASTICSEARCH") {"Failed to create index #{index}, because it already exists" } | ||
|
|
@@ -102,7 +113,7 @@ def delete_index(index) | |
| es_client.indices.delete(index: index) | ||
| @logger.debug("ELASTICSEARCH") { "Successfully deleted index #{index}" } | ||
| true | ||
| rescue Elasticsearch::Transport::Transport::Errors::NotFound => e | ||
| rescue Elastic::Transport::Transport::Errors::NotFound => e | ||
| @logger.debug("ELASTICSEARCH") { "Index #{index} doesn't exist and cannot be deleted." } | ||
| false | ||
| rescue StandardError => e | ||
|
|
@@ -129,7 +140,7 @@ def refresh_index(index) | |
| es_client.indices.refresh(index: index) | ||
| @logger.debug("ELASTICSEARCH") { "Successfully refreshed index #{index}" } | ||
| true | ||
| rescue Elasticsearch::Transport::Transport::Errors::NotFound => e | ||
| rescue Elastic::Transport::Transport::Errors::NotFound => e | ||
| @logger.warn("ELASTICSEARCH") { "Index #{index} does not exist, cannot refresh." } | ||
| false | ||
| rescue StandardError => e | ||
|
|
@@ -149,7 +160,7 @@ def clear_index(index) | |
| es_client.delete_by_query(index: index, body: { query: { match_all: {} } }) | ||
| @logger.debug("ELASTICSEARCH") { "Successfully cleared all documents from index #{index}" } | ||
| true | ||
| rescue Elasticsearch::Transport::Transport::Errors::NotFound => e | ||
| rescue Elastic::Transport::Transport::Errors::NotFound => e | ||
| @logger.warn("ELASTICSEARCH") { "Index #{index} does not exist, cannot clear documents." } | ||
| false | ||
| rescue StandardError => e | ||
|
|
@@ -167,7 +178,7 @@ def get_document(index, id) | |
| MuSearch::ElasticConnectionPool.with_client do |es_client| | ||
| begin | ||
| es_client.get(index: index, id: id) | ||
| rescue Elasticsearch::Transport::Transport::Errors::NotFound => e | ||
| rescue Elastic::Transport::Transport::Errors::NotFound => e | ||
| @logger.debug("ELASTICSEARCH") { "Document #{id} not found in index #{index}" } | ||
| nil | ||
| rescue StandardError => e | ||
|
|
@@ -208,7 +219,7 @@ def update_document(index, id, document) | |
| body = es_client.update(index: index, id: id, body: {doc: document}) | ||
| @logger.debug("ELASTICSEARCH") { "Updated document #{id} in index #{index}" } | ||
| body | ||
| rescue Elasticsearch::Transport::Transport::Errors::NotFound => e | ||
| rescue Elastic::Transport::Transport::Errors::NotFound => e | ||
| @logger.info("ELASTICSEARCH") { "Cannot update document #{id} in index #{index} because it doesn't exist" } | ||
| nil | ||
| rescue StandardError => e | ||
|
|
@@ -246,7 +257,7 @@ def delete_document(index, id) | |
| es_client.delete(index: index, id: id) | ||
| @logger.debug("ELASTICSEARCH") { "Successfully deleted document #{id} in index #{index}" } | ||
| true | ||
| rescue Elasticsearch::Transport::Transport::Errors::NotFound => e | ||
| rescue Elastic::Transport::Transport::Errors::NotFound => e | ||
| @logger.debug("ELASTICSEARCH") { "Document #{id} doesn't exist in index #{index} and cannot be deleted." } | ||
| false | ||
| rescue StandardError => e | ||
|
|
@@ -264,7 +275,7 @@ def search_documents(indexes:, query: nil) | |
| begin | ||
| @logger.debug("SEARCH") { "Searching Elasticsearch index(es) #{indexes} with body #{req_body}" } | ||
| es_client.search(index: indexes, body: query) | ||
| rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e | ||
| rescue Elastic::Transport::Transport::Errors::BadRequest => e | ||
| raise ArgumentError, "Invalid search query #{query}" | ||
| rescue StandardError => e | ||
| @logger.error("SEARCH") { "Searching documents in index(es) #{indexes} failed.\n Error: #{e.full_message}" } | ||
|
|
@@ -282,7 +293,7 @@ def count_documents(indexes:, query: nil) | |
| @logger.debug("SEARCH") { "Count search results in index(es) #{indexes} for body #{query.inspect}" } | ||
| response = es_client.count(index: indexes, body: query) | ||
| response["count"] | ||
| rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e | ||
| rescue Elastic::Transport::Transport::Errors::BadRequest => e | ||
| @logger.error("SEARCH") { "Counting search results in index(es) #{indexes} failed.\n Error: #{e.full_message}" } | ||
| raise ArgumentError, "Invalid count query #{query}" | ||
| rescue StandardError => e | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ES put the cluster status to yellow if we don't specify that we really only want a single node.