From 7cae0b80176617ece5ecb9966be0f171252e3587 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Mon, 29 Jul 2024 12:52:29 -0500 Subject: [PATCH] Fix: _update_by_query with a term. (#451) Signed-off-by: dblock --- CHANGELOG.md | 1 + spec/schemas/_common.query_dsl.yaml | 4 +- spec/schemas/_common.yaml | 3 +- tests/_core/reindex.yaml | 71 ------------------------ tests/_core/reindex/pipeline.yaml | 84 +++++++++++++++++++++++++++++ tests/indices/update_by_query.yaml | 72 +++++++++++++++++++++++++ 6 files changed, 161 insertions(+), 74 deletions(-) create mode 100644 tests/_core/reindex/pipeline.yaml create mode 100644 tests/indices/update_by_query.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 19de5fc50..a76163f31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fixed `/_mapping` with `index` in query ([#385](https://github.com/opensearch-project/opensearch-api-specification/pull/385)) - Fixed duplicate `/_nodes/{node_id}` path ([#416](https://github.com/opensearch-project/opensearch-api-specification/pull/416)) - Fixed `_source` accepting an array of fields in `/_search` ([#430](https://github.com/opensearch-project/opensearch-api-specification/pull/430)) +- Fixed `_update_by_query` with a simple term ([451](https://github.com/opensearch-project/opensearch-api-specification/pull/451)) ### Security diff --git a/spec/schemas/_common.query_dsl.yaml b/spec/schemas/_common.query_dsl.yaml index df09be4fd..414272b8b 100644 --- a/spec/schemas/_common.query_dsl.yaml +++ b/spec/schemas/_common.query_dsl.yaml @@ -182,7 +182,9 @@ components: To return a document, the query term must exactly match the queried field's value, including whitespace and capitalization. type: object additionalProperties: - $ref: '#/components/schemas/TermQuery' + anyOf: + - $ref: '#/components/schemas/TermQuery' + - $ref: '_common.yaml#/components/schemas/FieldValue' minProperties: 1 maxProperties: 1 terms: diff --git a/spec/schemas/_common.yaml b/spec/schemas/_common.yaml index 365c8826d..81c69f230 100644 --- a/spec/schemas/_common.yaml +++ b/spec/schemas/_common.yaml @@ -416,8 +416,7 @@ components: Specifies any named parameters that are passed into the script as variables. Use parameters instead of hard-coded values to decrease compile time. type: object - additionalProperties: - type: object + additionalProperties: true StoredScriptId: allOf: - $ref: '#/components/schemas/ScriptBase' diff --git a/tests/_core/reindex.yaml b/tests/_core/reindex.yaml index f0dbfe87d..1cf82fde1 100644 --- a/tests/_core/reindex.yaml +++ b/tests/_core/reindex.yaml @@ -11,28 +11,7 @@ epilogues: - path: /videos method: DELETE status: [200, 404] - - path: /_ingest/pipeline/transform-and-count - method: DELETE - status: [200, 404] prologues: - - path: /_ingest/pipeline/transform-and-count - method: PUT - request_body: - payload: - description: | - Splits the `title`` field into a `words` list. - Computes the length of the title field and stores it in a new `length` field. - Removes the `year` field. - processors: - - split: - field: title - separator: '\s+' - target_field: words - - script: - lang: painless - source: ctx.length = ctx.words.length - - remove: - field: year - path: /{index}/_doc method: POST parameters: @@ -135,56 +114,6 @@ chapters: status: 200 payload: total: 1 - - synopsis: Transform documents using a pipeline. - path: /_reindex - method: POST - request_body: - payload: - source: - index: movies - dest: - index: videos - pipeline: transform-and-count - response: - status: 200 - - synopsis: Refresh the index. - path: /{index}/_refresh - method: POST - parameters: - index: videos - - synopsis: Get all videos. - path: /{index}/_search - method: POST - parameters: - index: videos - request_body: - payload: - query: - match_all: {} - response: - status: 200 - payload: - hits: - hits: - - _index: videos - _source: - words: - - Beauty - - and - - the - # eslint-disable-next-line yml/sort-sequence-values - - Beast - length: 4 - title: Beauty and the Beast - - synopsis: Update documents in the current index. - path: /{index}/_update_by_query - method: POST - parameters: - index: videos - response: - status: 200 - payload: - updated: 1 - synopsis: Reindex from movies to films with all options. path: /_reindex method: POST diff --git a/tests/_core/reindex/pipeline.yaml b/tests/_core/reindex/pipeline.yaml new file mode 100644 index 000000000..aff582e48 --- /dev/null +++ b/tests/_core/reindex/pipeline.yaml @@ -0,0 +1,84 @@ +$schema: ../../../json_schemas/test_story.schema.yaml + +description: Test reindex with a Search pipeline. +epilogues: + - path: /movies + method: DELETE + status: [200, 404] + - path: /videos + method: DELETE + status: [200, 404] + - path: /_ingest/pipeline/transform-and-count + method: DELETE + status: [200, 404] +prologues: + - path: /_ingest/pipeline/transform-and-count + method: PUT + request_body: + payload: + description: | + Splits the `title`` field into a `words` list. + Computes the length of the title field and stores it in a new `length` field. + Removes the `year` field. + processors: + - split: + field: title + separator: '\s+' + target_field: words + - script: + lang: painless + source: ctx.length = ctx.words.length + - remove: + field: year + - path: /{index}/_doc + method: POST + parameters: + index: movies + refresh: true + request_body: + payload: + title: Beauty and the Beast + year: 91 + status: [201] +chapters: + - synopsis: Transform documents using a pipeline. + path: /_reindex + method: POST + request_body: + payload: + source: + index: movies + dest: + index: videos + pipeline: transform-and-count + response: + status: 200 + - synopsis: Refresh the index. + path: /{index}/_refresh + method: POST + parameters: + index: videos + - synopsis: Get all videos. + path: /{index}/_search + method: POST + parameters: + index: videos + request_body: + payload: + query: + match_all: {} + response: + status: 200 + payload: + hits: + hits: + - _index: videos + _source: + words: + - Beauty + - and + - the + # eslint-disable-next-line yml/sort-sequence-values + - Beast + length: 4 + title: Beauty and the Beast diff --git a/tests/indices/update_by_query.yaml b/tests/indices/update_by_query.yaml new file mode 100644 index 000000000..4043dc496 --- /dev/null +++ b/tests/indices/update_by_query.yaml @@ -0,0 +1,72 @@ +$schema: ../../json_schemas/test_story.schema.yaml + +description: Test reindex. +epilogues: + - path: /books + method: DELETE + status: [200, 404] +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request_body: + content_type: application/x-ndjson + payload: + - {create: {_index: books, _id: book_1392214}} + - {author: Harper Lee, title: To Kill a Mockingbird, year: 60} + - {create: {_index: books, _id: book_1392215}} + - {author: Elizabeth Rudnick, title: Beauty and the Beast, year: 91} +chapters: + - synopsis: Update documents in the index. + path: /{index}/_update_by_query + method: POST + parameters: + index: books + refresh: true + response: + status: 200 + payload: + updated: 2 + - synopsis: Update documents in the index (full query term, script). + path: /{index}/_update_by_query + method: POST + parameters: + index: books + request_body: + payload: + query: + term: + title: + _name: title + value: beauty + case_insensitive: true + boost: 1 + script: + source: ctx._source.year += params.century + lang: painless + params: + century: 1900 + response: + status: 200 + payload: + updated: 1 + - synopsis: Update documents in the index (simple term, script). + path: /{index}/_update_by_query + method: POST + parameters: + index: books + request_body: + payload: + query: + term: + year: 60 + script: + source: ctx._source.year += params.century + lang: painless + params: + century: 1900 + response: + status: 200 + payload: + updated: 1