Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #159 from leonardosfl/multiple-terms
Browse files Browse the repository at this point in the history
Allow multiple terms to be used on search on the COVID-19 Dashboard component
  • Loading branch information
harumhelmy authored Aug 5, 2020
2 parents 99405da + 930e111 commit 154fa57
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
19 changes: 10 additions & 9 deletions src/components/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ export default function Dashboard() {
location.state && location.state.bookmark
);

const covidTerms = ['COVID-19', 'Coronavirus', 'SARS-CoV-2'];
const params = new URLSearchParams(location.search);

useEffect(() => {
if (location.search === "") {
history.replace({ search: "q=COVID-19" })
history.replace({ search: createPreprintQs({ text: covidTerms }, location.search) });
}
}, [apiQs]);

const [preprints, fetchResultsProgress] = usePreprintSearchResults(apiQs);

const [hoveredSortOption, setHoveredSortOption] = useState(null);

/**
Expand Down Expand Up @@ -189,7 +190,7 @@ export default function Dashboard() {
onChange={e => {
const search = createPreprintQs(
{
text: 'COVID-19',
text: covidTerms,
hasOthersRec: e.target.checked || null
},
location.search
Expand All @@ -216,7 +217,7 @@ export default function Dashboard() {
onChange={e => {
const search = createPreprintQs(
{
text: 'COVID-19',
text: covidTerms,
hasPeerRec: e.target.checked || null
},
location.search
Expand All @@ -243,7 +244,7 @@ export default function Dashboard() {
onChange={e => {
const search = createPreprintQs(
{
text: 'COVID-19',
text: covidTerms,
hasData: e.target.checked || null
},
location.search
Expand All @@ -270,7 +271,7 @@ export default function Dashboard() {
onChange={e => {
const search = createPreprintQs(
{
text: 'COVID-19',
text: covidTerms,
hasCode: e.target.checked || null
},
location.search
Expand Down Expand Up @@ -298,7 +299,7 @@ export default function Dashboard() {
) => {
const search = createPreprintQs(
{
text: 'COVID-19' || 'coronavirus' || 'SARS-CoV2',
text: covidTerms,
sort: nextSortOption
},
location.search
Expand Down Expand Up @@ -346,7 +347,7 @@ export default function Dashboard() {
onClick={() => {
history.push({
pathname: location.pathname,
search: createPreprintQs({ text: params.get('q') }, location.search)
search: createPreprintQs({ text: params.getAll('q') }, location.search)
});
}}
>
Expand All @@ -363,7 +364,7 @@ export default function Dashboard() {
onClick={() => {
history.push({
pathname: location.pathname,
search: createPreprintQs({ text: params.get('q') }, location.search),
search: createPreprintQs({ text: params.getAll('q') }, location.search),
state: { bookmark: preprints.bookmark }
});
}}
Expand Down
54 changes: 33 additions & 21 deletions src/utils/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export function createPreprintQs(
const ui = new URLSearchParams(uiQs);

if (text != null && text != '') {
ui.set('q', text);
if (Array.isArray(text)) {
ui.delete('q');
text.forEach(term => ui.append('q', term));
} else {
ui.set('q', text);
}
} else {
ui.delete('q');
}
Expand Down Expand Up @@ -112,26 +117,10 @@ export function apifyPreprintQs(uiQs = '', bookmark) {
const drilldown = [];

if (ui.has('q')) {
const q = ui.get('q');
const terms = q.split(/[+|\s]/).filter(Boolean);

const ored = [`name:"${escapeLucene(q)}"`];

if (terms.length > 1) {
ored.push(`name:"${escapeLucene(q)}"~5`);
} else if (terms.length === 1) {
ored.push(`name:${escapeLucene(terms[0])}*`);
}

const doiMatched = q.match(doiRegex());
if (doiMatched) {
ored.push(...doiMatched.map(doi => `doi:"${doi}"`));
}

const arXivIds = identifiersArxiv.extract(q);
if (arXivIds && arXivIds.length) {
ored.push(...arXivIds.map(arXivId => `arXivId:"${arXivId}"`));
}
const searchTerms = ui.getAll('q');
const ored = searchTerms.length > 1
? searchTerms.map(q => buildLuceneQueryForTerm(q)).reduce((prev, curr) => prev.concat(curr))
: buildLuceneQueryForTerm(searchTerms[0]);

anded.push(ored.length == 1 ? ored[0] : `(${ored.join(' OR ')})`);
}
Expand Down Expand Up @@ -398,3 +387,26 @@ export function createBlockedRolesQs({ bookmark }) {
function escapeLucene(term) {
return term.replace(/([+&|!(){}[\]^"~*?:\\\/-])/g, '\\$1');
}

function buildLuceneQueryForTerm(q) {
const result = [`name:"${escapeLucene(q)}"`];
const words = q.split(/[+|\s]/).filter(Boolean);

if (words.length > 1) {
result.push(`name:"${escapeLucene(q)}"~5`);
} else if (words.length === 1) {
result.push(`name:${escapeLucene(words[0])}*`);
}

const doiMatched = q.match(doiRegex());
if (doiMatched) {
result.push(...doiMatched.map(doi => `doi:"${doi}"`));
}

const arXivIds = identifiersArxiv.extract(q);
if (arXivIds && arXivIds.length) {
result.push(...arXivIds.map(arXivId => `arXivId:"${arXivId}"`));
}

return result;
}
9 changes: 9 additions & 0 deletions test/test-search-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ describe('search utils', () => {
);
});

it('should allow multiple terms to be used', () => {
const ui = createPreprintQs({ text: ['text1', 'text2'] });

assert.equal(ui, '?q=text1&q=text2');

const p = querystring.parse(apifyPreprintQs(ui).substring(1));
assert.equal(p.q, '(name:"text1" OR name:text1* OR name:"text2" OR name:text2*)' );
});

it('should handle DOI and arXivId', () => {
const ui = createPreprintQs({
text: `text ${arXivId} ${crossrefDoi} ${openAireDoi}`
Expand Down

0 comments on commit 154fa57

Please sign in to comment.