Skip to content

Commit

Permalink
Hacky fix for DOI searches.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpilone committed Aug 24, 2022
1 parent 48fce09 commit 6eeef70
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
23 changes: 12 additions & 11 deletions api/lambdas/search-by-keywords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ describe('search-by-keywords.handler', () => {
const uuid1 = randomUUID();
const uuid2 = randomUUID();
const uuid3 = randomUUID();

const doiUUID = randomUUID();
const uuid4 = randomUUID();

beforeAll(async () => {
const doc1 = {
Expand All @@ -81,28 +80,30 @@ 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',
dc_identifier_doi: [
'http://dx.doi.org/10.25607/OBP-561',
],
};

const doc4 = {
uuid: uuid4,
dc_identifier_doi: [
'http://dx.doi.org/10.25607/OBP-765',
],
};

await osClient.addDocument(esUrl, documentsIndexName, doc1);
await osClient.addDocument(esUrl, documentsIndexName, doc2);
await osClient.addDocument(esUrl, documentsIndexName, doc3);
await osClient.addDocument(esUrl, documentsIndexName, doc4);
await osClient.refreshIndex(esUrl, documentsIndexName);
});

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 } });
await osClient.deleteByQuery(esUrl, documentsIndexName, { match: { uuid: uuid4 } });
await osClient.refreshIndex(esUrl, documentsIndexName);
});

Expand Down Expand Up @@ -199,7 +200,7 @@ 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',
keywords: ':dc_identifier_doi:10.25607/OBP-561',
},
};

Expand Down
14 changes: 11 additions & 3 deletions api/lib/search-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export const nestedQuery = (termPhrase: unknown) => ({
// All special characters: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
// Characters we currently want to escape: + - = && || ! ( ) { } [ ] : \ /
const queryStringSpecialCharacters = /\+|-|=|&{2}|\|{2}|!|\(|\)|{|}|\[|]|:|\/|\\/g;
const encodeQueryStringTermComp = (term: string): string => term.replace(queryStringSpecialCharacters, '\\$&');
const encodeQueryStringTerm = (term: string, field: string): string => {
const encodedTerm = term.replace(queryStringSpecialCharacters, '\\$&');

// FIXME: This is a hack to make doi searches work. Fix this better please.
return field === 'dc_identifier_doi' ? `*${encodedTerm}` : encodedTerm;
};

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

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

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/documents-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const documentsMapping = {
type: 'text',
analyzer: 'english',
},
dc_identifier_doi: {
type: 'keyword',
},
},
},
};
3 changes: 3 additions & 0 deletions lib/open-search-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ describe('open-search-client', () => {
uuid: {
type: 'keyword',
},
dc_identifier_doi: {
type: 'keyword',
},
},
});
});
Expand Down

0 comments on commit 6eeef70

Please sign in to comment.