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

236 - Fixes DOI searches by escaping special characters in search terms #246

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
2 changes: 1 addition & 1 deletion .eslint-ratchet
Original file line number Diff line number Diff line change
@@ -1 +1 @@
274
200
2 changes: 1 addition & 1 deletion .tsc-ratchet
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125
113
34 changes: 34 additions & 0 deletions api/lambdas/search-by-keywords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ describe('search-by-keywords.handler', () => {
const uuid2 = randomUUID();
const uuid3 = randomUUID();

const doiUUID = randomUUID();

beforeAll(async () => {
const doc1 = {
uuid: uuid1,
Expand All @@ -79,6 +81,10 @@ describe('search-by-keywords.handler', () => {
uuid: uuid3,
dc_title: 'This is a document with author text.',
dc_contributor_author: 'Ocean Sea',
dc_identifier_uri: [
'http://hdl.handle.net/11329/1029',
'http://dx.doi.org/10.25607/OBP-561',
],
};

await osClient.addDocument(esUrl, documentsIndexName, doc1);
Expand All @@ -88,6 +94,12 @@ describe('search-by-keywords.handler', () => {
});

afterAll(async () => {
await osClient.deleteByQuery(
esUrl,
documentsIndexName,
{ match: { uuid: doiUUID } }
);

await osClient.deleteByQuery(esUrl, documentsIndexName, { match: { uuid: uuid1 } });
await osClient.deleteByQuery(esUrl, documentsIndexName, { match: { uuid: uuid2 } });
await osClient.deleteByQuery(esUrl, documentsIndexName, { match: { uuid: uuid3 } });
Expand Down Expand Up @@ -184,6 +196,28 @@ describe('search-by-keywords.handler', () => {
);
});

test('should find documents with the DOI metadata field', (done) => {
const proxyEvent = {
queryStringParameters: {
keywords: ':dc_identifier_uri:10.25607/OBP-561',
},
};

searchHandler(
proxyEvent,
(results) => {
expect(results.hits.total.value).toEqual(1);

const uuids = results.hits.hits.map(
(h) => h._source.uuid
);

expect(uuids).toEqual([uuid3]);
},
done
);
});

describe('and using boolean operators', () => {
test('should find matching documents using the OR boolean operator', (done) => {
const proxyEvent = {
Expand Down
159 changes: 159 additions & 0 deletions api/lib/search-query-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,164 @@ describe('search-document-builder', () => {
test.todo('should boost the dc_title keyword field if searching all fields');

test.todo('shoudl boost the dc_description_abstract field if searching all fields');

describe('when escaping query string special characters', () => {
// + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
test('should escape + in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a + in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\+ in it)');
});

test('should escape - in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a - in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\- in it)');
});

test('should escape = in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a = in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\= in it)');
});

test('should escape && in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a && in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\&& in it)');
});

test('should escape || in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a || in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\|| in it)');
});

test('should escape ! in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a ! in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\! in it)');
});

test('should escape ( and ) in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a ( and ) in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\( and \\) in it)');
});

test('should escape { and } in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a { and } in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\{ and \\} in it)');
});

test('should escape [ and ] in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a [ and ] in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\[ and \\] in it)');
});

test('should escape : in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a : in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\: in it)');
});

test('should escape / in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a / in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\/ in it)');
});

test('should escape \\ in the query string term component', () => {
const keywordComps = [
{
operator: '',
field: '*',
term: 'term with a \\ in it',
},
];

const result = formatQueryString(keywordComps);
expect(result).toEqual('*:(term with a \\\\ in it)');
});
});
});
});
14 changes: 12 additions & 2 deletions api/lib/search-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export const nestedQuery = (termPhrase: unknown) => ({
},
});

// Elasticsearch's query_string query has a list of special characters. We don't
// want to necessarily escape them all (e.g. the user can use a wildcard if they want)
// but there are a few obvious ones we need to escape.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
// All special characters: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
// Characters we currently want to escape: + - = && || ! ( ) { } [ ] : \ /
const queryStringSpecialCharacters = /\+|-|=|&{2}|\|{2}|!|\(|\)|{|}|\[|]|:|\/|\\/g;
const encodeQueryStringTermComp = (term: string): string => term.replace(queryStringSpecialCharacters, '\\$&');

const formatKeywordComp = (keywordComp: SearchKeywordComps) => {
let openSearchOperator;

Expand All @@ -43,7 +52,8 @@ const formatKeywordComp = (keywordComp: SearchKeywordComps) => {
openSearchOperator = 'OR';
}

return `${openSearchOperator} ${keywordComp.field}:(${keywordComp.term})`;
const escapedKeywordCompTerm = encodeQueryStringTermComp(keywordComp.term);
return `${openSearchOperator} ${keywordComp.field}:(${escapedKeywordCompTerm})`;
};

/**
Expand Down Expand Up @@ -90,7 +100,7 @@ export const buildSort = (sortParams: string[] = []) => {
* Helper function that builds the `query` field of the Elasticsearch search
* document.
*
* @param keywords An array of search keyword components.
* @param keywordComps An array of search keyword components.
* @param terms An array of terms that will be used as filters in the
* query.
* @param termURIs A list of term URIs (ontology URIs) that can be
Expand Down
33 changes: 0 additions & 33 deletions ingest/downloader/bulk-items-helper.rb

This file was deleted.

23 changes: 0 additions & 23 deletions ingest/downloader/bulk-items-template.yaml

This file was deleted.

Loading