Skip to content

Commit

Permalink
fix(mentions): Correct mentions count
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Oct 31, 2024
1 parent 8a35f35 commit a825aea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
5 changes: 2 additions & 3 deletions client/src/pages/mentions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ export default function Mentions() {
useEffect(() => {
const getData = async () => {
setLoading(true);
setMentions([]);
setTotalRecords(0);
if (urlSearchParams?.search?.length > 0) {
const data = await getMentions(urlSearchParams);
const mentionsTmp = data?.mentions ?? [];
Expand All @@ -309,9 +311,6 @@ export default function Mentions() {
}),
);
setTotalRecords(data?.count ?? 0);
} else {
setMentions([]);
setTotalRecords(0);
}
setLoading(false);
};
Expand Down
42 changes: 33 additions & 9 deletions server/src/routes/works.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ router.route('/works').post(async (req, res) => {
}
});

const getMentions = async ({ options }) => {
const getMentionsQuery = ({ options }) => {
const {
from, search, size, sortBy, sortOrder, type,
} = options;
Expand All @@ -250,7 +250,7 @@ const getMentions = async ({ options }) => {
} else if (type === 'datasets') {
types = ['dataset-implicit', 'dataset-name'];
}
let body = {
const body = {
from,
size,
query: {
Expand Down Expand Up @@ -316,10 +316,13 @@ const getMentions = async ({ options }) => {
body.sort = [];
sortFields.map((sortField) => body.sort.push({ [sortField]: sortOrder }));
}
body = JSON.stringify(body);
return body;
};

const getMentions = async ({ query }) => {
const url = `${process.env.ES_URL}/${process.env.ES_INDEX_MENTIONS}/_search`;
const params = {
body,
body: JSON.stringify(query),
method: 'POST',
headers: {
Authorization: process.env.ES_AUTH,
Expand All @@ -328,7 +331,6 @@ const getMentions = async ({ options }) => {
};
const response = await fetch(url, params);
const data = await response.json();
const count = data?.hits?.total?.value ?? 0;
const mentions = (data?.hits?.hits ?? []).map((mention) => ({
...mention._source,
affiliations: [
Expand All @@ -350,9 +352,29 @@ const getMentions = async ({ options }) => {
mention._source?.['software-name']?.rawForm
?? mention._source?.['dataset-name']?.rawForm,
type: mention._source?.type === 'software' ? 'software' : 'dataset',
type_original: mention._source?.type === 'software' ? 'software' : 'dataset',
type_original:
mention._source?.type === 'software' ? 'software' : 'dataset',
}));
return { count, mentions };
return mentions;
};

const getMentionsCount = async ({ query }) => {
['_source', 'from', 'highlight', 'size'].forEach((item) => {
// eslint-disable-next-line no-param-reassign
delete query?.[item];
});
const url = `${process.env.ES_URL}/${process.env.ES_INDEX_MENTIONS}/_count`;
const params = {
body: JSON.stringify(query),
method: 'POST',
headers: {
Authorization: process.env.ES_AUTH,
'content-type': 'application/json',
},
};
const response = await fetch(url, params);
const data = await response.json();
return data?.count ?? 0;
};

router.route('/mentions').post(async (req, res) => {
Expand All @@ -363,8 +385,10 @@ router.route('/mentions').post(async (req, res) => {
.status(400)
.json({ message: 'Type should be either "datasets" or "software".' });
} else {
const result = await getMentions({ options });
res.status(200).json(result);
const query = getMentionsQuery({ options });
const mentions = await getMentions({ query });
const count = await getMentionsCount({ query });
res.status(200).json({ count, mentions });
}
} catch (err) {
console.error(err);
Expand Down

0 comments on commit a825aea

Please sign in to comment.