Skip to content

Commit

Permalink
fix: disjunctive token search
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-murphy committed Jul 16, 2024
1 parent c935ae6 commit c43d5c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion scripts/addQueryTokensToPubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function updatePubsWithTokens(db: DB, pubs: Pubs) {
const pubData = pub.data() as { title: string; author: string; updatedAt: number };
const newData = {
...pubData,
updatedAt: Timestamp.fromDate(new Date(pubData.updatedAt)),
//updatedAt: Timestamp.fromDate(new Date(pubData.updatedAt)),
tokens: createPublicationTokens(pubData),
};
return db.doc(`publications/${pub.id}`).set(newData);
Expand Down
9 changes: 4 additions & 5 deletions src/components/PublicationsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,19 @@ export function PublicationsTable() {
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([]);

React.useEffect(() => {
console.log({ columnFilters });
const authorFilters = columnFilters.filter((filter) => filter.id === 'author').pop();
const titleFilters = columnFilters.filter((filter) => filter.id === 'title').pop();
const yearFilters = columnFilters.filter((filter) => filter.id === 'year').pop();
if (authorFilters !== undefined) {
setAuthorFilters(cleanTokenString(authorFilters.value as string));
}
} else setAuthorFilters([]);
if (titleFilters !== undefined) {
setTitleFilters(cleanTokenString(titleFilters.value as string));
}
} else setTitleFilters([]);
if (yearFilters !== undefined) {
const [yearMin, yearMax] = yearFilters.value as [number | undefined, number | undefined];
if (yearMin !== undefined) setYearMin(yearMin);
if (yearMax !== undefined) setYearMax(yearMax);
if (yearMin !== undefined) setYearMin(Number(yearMin));
if (yearMax !== undefined) setYearMax(Number(yearMax));
}
}, [columnFilters, setAuthorFilters, setTitleFilters, setYearMax, setYearMin]);

Expand Down
36 changes: 18 additions & 18 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,17 @@ export const makePubsSnapshot = (
} = filterOpts;
const orderConstraint = orderBy(field, dir);
const constraints = [
filters.title.length !== 0
? where('tokens.title', 'array-contains-any', filters.title)
: undefined,
filters.author.length !== 0
? where('tokens.author', 'array-contains-any', filters.author)
: undefined,
filters.year.min !== undefined ? where('year', '>=', filters.year.min) : undefined,
filters.year.max !== undefined ? where('year', '<=', filters.year.max) : undefined,
].filter((constraint) => constraint !== undefined);
...(filters.title.length + filters.author.length > 0
? [
where('tokens', 'array-contains-any', [
...filters.title.map((titleFilter) => `title:${titleFilter}`),
...filters.author.map((authorFilter) => `author:${authorFilter}`),
]),
]
: []),
...(filters.year.min !== undefined ? [where('year', '>=', filters.year.min)] : []),
...(filters.year.max !== undefined ? [where('year', '<=', filters.year.max)] : []),
];
const queryConditions = (
constraints.length === 0 ? [orderConstraint] : [and(...constraints), orderConstraint]
) as QueryConstraint[];
Expand All @@ -197,11 +199,6 @@ export const makePubsSnapshot = (
newCheckpoints[pagination.pageIndex + 1] = snapshot.docs[snapshot.docs.length - 1];
setPubsCheckpoints(newCheckpoints);
const publications = snapshot.docs.map((doc) => doc.data());
console.log({ publications });
/**
* QueryDocumentSnapshot<DocumentData, DocumentData>
* QueryDocumentSnapshot<DocumentData, DocumentData>
*/
setPubs(publications);
},
(error) => {
Expand Down Expand Up @@ -232,11 +229,14 @@ export const cleanTokenString = (tokenString: string) => {
* @param publication
*/
export const createPublicationTokens = ({ title, author }: { title: string; author: string }) => {
return {
title: cleanTokenString(title),
// Split Title and Author into a list of individual tokens, and combine into a single token list.
return [
...cleanTokenString(title).map((token) => `title:${token}`),
// It's common to have middle initials -- these dont narrow a search field much, and are trimmed for the
author: cleanTokenString(author).filter((token) => token.length > 1),
};
...cleanTokenString(author)
.filter((token) => token.length > 1)
.map((token) => `author:${token}`),
];
};

export const addPublication = async (publication) => {
Expand Down

0 comments on commit c43d5c7

Please sign in to comment.