Skip to content

Commit

Permalink
Merge pull request #268 from nulib/5230-no-embedding
Browse files Browse the repository at this point in the history
Allow _source_includes and _source_excludes for GET search requests
  • Loading branch information
kdid authored Oct 9, 2024
2 parents ec2a371 + 298ceb3 commit 7ba44d6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
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"
);
});
});
});

0 comments on commit 7ba44d6

Please sign in to comment.