Skip to content

Commit 10c3872

Browse files
committed
add filters in moreLikeThis
1 parent 48c4927 commit 10c3872

File tree

7 files changed

+38
-20
lines changed

7 files changed

+38
-20
lines changed

client/src/api/authors/more-like-this/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { authorsIndex, postHeaders } from "../../../config/api";
22
import { LIGHT_SOURCE } from "../_utils/constants";
33

4-
export async function getMoreAuthorsLikeThis(id: string) {
4+
export async function getMoreAuthorsLikeThis(id: string, filters?: any[]) {
55
const body = JSON.stringify({
66
_source: LIGHT_SOURCE,
77
size: 3,
88
query: {
99
bool: {
10+
...(filters?.length && { filter: filters }),
1011
must: [{
1112
more_like_this: {
1213
fields: ["domains.label.*"],
@@ -18,7 +19,8 @@ export async function getMoreAuthorsLikeThis(id: string) {
1819
}
1920
}
2021
})
22+
console.log(filters, body)
2123
const res = await fetch(`${authorsIndex}/_search`, { method: 'POST', body, headers: postHeaders })
2224
const data = await res.json();
2325
return data?.hits?.hits?.map(({ _source }) => _source) || []
24-
}
26+
}

client/src/api/organizations/more-like-this/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { organizationsIndex, postHeaders } from "../../../config/api";
22
import { DEFAULT_FILTERS, LIGHT_SOURCE } from "../_utils/constants";
33

4-
export async function getMoreOrganizationsLikeThis(id: string) {
4+
export async function getMoreOrganizationsLikeThis(id: string, filters?: any) {
55
const body = JSON.stringify({
66
_source: LIGHT_SOURCE,
77
size: 3,
88
query: {
99
bool: {
10-
filter: DEFAULT_FILTERS,
10+
filter: [...DEFAULT_FILTERS, ...filters],
1111
must: [{
1212
more_like_this: {
1313
fields: ["publications.title.*", "web_content", "patents.title.*", "projects.label.*", "description.*"],
@@ -22,4 +22,4 @@ export async function getMoreOrganizationsLikeThis(id: string) {
2222
const res = await fetch(`${organizationsIndex}/_search`, { method: 'POST', body, headers: postHeaders })
2323
const data = await res.json();
2424
return data?.hits?.hits?.map(({ _source }) => _source) || []
25-
}
25+
}

client/src/api/projects/more-like-this/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { projectsIndex, postHeaders } from "../../../config/api";
22
import { LIGHT_SOURCE } from "../_utils/constants";
33

4-
export async function getMoreProjectsLikeThis(id: string) {
4+
export async function getMoreProjectsLikeThis(id: string, filters?: any[]) {
55
const body = JSON.stringify({
66
_source: LIGHT_SOURCE,
77
size: 3,
88
query: {
99
bool: {
10+
...(filters?.length && {filter: filters}),
1011
must: [{
1112
more_like_this: {
1213
fields: ["label.*", "publications.title.*", "description.*"],
@@ -21,4 +22,4 @@ export async function getMoreProjectsLikeThis(id: string) {
2122
const res = await fetch(`${projectsIndex}/_search`, { method: 'POST', body, headers: postHeaders })
2223
const data = await res.json();
2324
return data?.hits?.hits?.map(({ _source }) => _source) || []
24-
}
25+
}

client/src/api/publications/more-like-this/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ import { publicationsIndex, postHeaders } from "../../../config/api";
22
import { LIGHT_SOURCE } from "../_utils/constants";
33

44

5-
export async function getMorePublicationsLikeThis(id: string) {
5+
export async function getMorePublicationsLikeThis(id: string, filters?: any[]) {
66
const body = JSON.stringify({
77
_source: LIGHT_SOURCE,
88
size: 3,
99
query: {
10-
more_like_this: {
11-
fields: ["title.default", "domains.label.*"],
12-
like: [{ _id: id }],
13-
min_term_freq: 1,
14-
max_query_terms: 12,
10+
bool: {
11+
...(filters?.length && { filter: filters }),
12+
must: [{
13+
more_like_this: {
14+
fields: ["title.default", "domains.label.*"],
15+
like: [{ _id: id }],
16+
min_term_freq: 1,
17+
max_query_terms: 12,
18+
},
19+
}],
1520
},
1621
}
1722
})
1823
const res = await fetch(`${publicationsIndex}/_search`, { method: 'POST', body, headers: postHeaders })
1924
const data = await res.json();
2025
return data?.hits?.hits?.map(({ _source }) => _source) || []
21-
}
26+
}

client/src/components/more-like-this/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ const API_MAPPER = {
4949
type API = keyof typeof API_MAPPER;
5050

5151

52-
export default function MoreLikeThis({ id, api }: { id: string, api: API }) {
52+
export default function MoreLikeThis({ id, api, filters = [] }: { id: string, api: API, filters?: any[] }) {
5353
const { locale } = useDSFRConfig();
5454
const intl = createIntl({ locale, messages: messages[locale] })
5555
const Component = API_MAPPER[api].item;
5656
const { data: moreLikeThis, isLoading, isError } = useQuery({
57-
queryKey: ["moreLike", api, id],
58-
queryFn: () => API_MAPPER[api].fn(id),
57+
queryKey: ["moreLike", api, id, filters],
58+
queryFn: () => API_MAPPER[api].fn(id, filters),
5959
});
6060

6161
if (isError) return (
@@ -76,4 +76,4 @@ export default function MoreLikeThis({ id, api }: { id: string, api: API }) {
7676
) : <Text className="fr-card__detail fr-text--md">{intl.formatMessage({ id: "more-like-this.empty" })}</Text>}
7777
</>
7878
)
79-
}
79+
}

client/src/pages/authors/[id]/components/author.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ export default function AuthorPage({ data }: { data: Author }) {
183183
icon="user-line"
184184
show
185185
>
186-
<MoreLikeThis id={data._id} api="authors" />
186+
<MoreLikeThis
187+
id={data._id}
188+
api="authors"
189+
filters={[
190+
{bool: {must_not: {terms: {"id.keyword": coAuthors.map((author) => author.value)}}}}
191+
]}
192+
/>
187193
</PageSection>
188194
<PageSection
189195
title="Data JSON"

client/src/pages/organizations/[id]/components/organization.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,11 @@ export default function OrganizationPresentation({ data }: { data: Organization
264264
icon="building-line"
265265
show
266266
>
267-
<MoreLikeThis id={data._id} api="organizations" />
267+
<MoreLikeThis
268+
id={data._id}
269+
api="organizations"
270+
filters={ [{ term: { "level.keyword": data.level } }] }
271+
/>
268272
</PageSection>
269273
<PageSection title="Data JSON" description="" show={import.meta.env.DEV}>
270274
<div>

0 commit comments

Comments
 (0)