Skip to content
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

Allow _source_includes and _source_excludes for GET search requests #268

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
14 changes: 14 additions & 0 deletions docs/docs/spec/types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ components:
description: Comma-delimited list of fields to sort search results (e.g. "create_date:asc,modified_date:desc")
schema:
type: string
_source_excludes:
name: _source_excludes
in: query
required: false
description: Comma-delimited list of fields to exclude from search results (e.g. "embedding,embedding_text_length")
schema:
type: string
_source_includes:
name: _source_includes
in: query
required: false
description: Comma-delimited list of fields to include in search results (e.g. "title,accession_number")
schema:
type: string
as:
name: as
in: query
Expand Down
21 changes: 20 additions & 1 deletion node/src/handlers/search-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ const constructSearchContext = async (event) => {
searchContext.size = queryStringParameters.size || searchContext.size || 10;
searchContext.from = queryStringParameters.from || searchContext.from || 0;

if (
queryStringParameters?._source_excludes ||
searchContext._source?.exclude
) {
searchContext._source = searchContext._source || {};
searchContext._source.exclude =
queryStringParameters?._source_excludes.split(",") ||
searchContext._source.exclude;
}

if (
queryStringParameters?._source_includes ||
searchContext._source?.include
) {
searchContext._source = searchContext._source || {};
searchContext._source.include =
queryStringParameters?._source_includes.split(",") ||
searchContext._source.include;
}

if (queryStringParameters?.sort || searchContext.sort)
searchContext.sort =
parseSortParameter(queryStringParameters) || searchContext.sort;
Expand All @@ -104,7 +124,6 @@ const constructSearchContext = async (event) => {
const page = Number(queryStringParameters.page || 1);
searchContext.from = (page - 1) * searchContext.size;
}

return searchContext;
};

Expand Down
50 changes: 50 additions & 0 deletions node/test/integration/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,55 @@ describe("Search routes", () => {
"?sort=create_date%3Aasc%2Cmodified_date%3Adesc"
);
});

it("allows excluding fields via query string parameters for GET requests", async () => {
const originalQuery = {
query: { query_string: { query: "*" } },
_source: { exclude: ["title"] },
};
const event = helpers
.mockEvent("GET", "/search")
.queryParams({ _source_excludes: "title" })
.render();
const authQuery = new RequestPipeline(originalQuery)
.authFilter(helpers.preprocess(event))
.toJson();

mock
.post("/dc-v2-work/_search", authQuery)
.reply(200, helpers.testFixture("mocks/search.json"));

const result = await handler(event);
expect(result.statusCode).to.eq(200);
const resultBody = JSON.parse(result.body);
expect(resultBody.pagination.query_url).to.contain(
"?_source_excludes=title"
);
});

it("allows including fields via query string parameters for GET requests", async () => {
const originalQuery = {
query: { query_string: { query: "*" } },
_source: { include: ["title", "accession_number"] },
};
const event = helpers
.mockEvent("GET", "/search")
.queryParams({ _source_includes: "title,accession_number" })
.render();
const authQuery = new RequestPipeline(originalQuery)
.authFilter(helpers.preprocess(event))
.toJson();

mock
.post("/dc-v2-work/_search", authQuery)
.reply(200, helpers.testFixture("mocks/search.json"));

const result = await handler(event);
expect(result.statusCode).to.eq(200);
const resultBody = JSON.parse(result.body);
expect(resultBody.pagination.query_url).to.contain(
"?_source_includes=title%2Caccession_number"
);
});
});
});
Loading