Add functions & pipeline for partial tiploca match which requires skipping end stations

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2025-03-06 21:37:33 +00:00
parent 4d2262f349
commit a561357fbe
2 changed files with 68 additions and 2 deletions

View File

@ -78,8 +78,12 @@ export async function findByTiplocArray(
if (partialEnd) {
return convertDocument(partialEnd, "first");
} else {
// Here, I should search for a partialStart match. For now return null.
return null;
const partialStart = await findPartialStartMatchByTiploc(tiplocArray);
if (partialStart) {
return convertDocument(partialEnd, "last");
} else {
return null;
}
}
}
} catch (err) {
@ -103,6 +107,13 @@ async function findPartialEndMatchByTiploc(array: string[]): Promise<Document> {
return res[0];
}
// Uses a pipeline to find a partial match - supporting codes starting with the correct stops.
async function findPartialStartMatchByTiploc(array: string[]): Promise<Document> {
const pipeline = getPartialStartTiplocMatchPipeline(array);
const res = await queryAggregate("pis", pipeline);
return res[0];
}
function convertDocument(doc: Document, skipType: string): OB_Pis_SimpleObject {
return {
code: doc.code.toString(),

View File

@ -75,6 +75,61 @@ export function getPartialEndTiplocMatchPipeline(query: string[]) {
];
}
export function getPartialStartTiplocMatchPipeline(query: string[]) {
return [
{
'$match': {
'tiplocs': {
'$all': query
}
}
}, {
'$addFields': {
'query': query
}
}, {
'$match': {
'$expr': {
'$eq': [
{
'$slice': [
'$tiplocs', {
'$size': '$query'
}
]
}, '$query'
]
}
}
}, {
'$addFields': {
'skipStops': {
'$subtract': [
{
'$size': '$tiplocs'
}, {
'$size': '$query'
}
]
}
}
}, {
'$sort': {
'skipStops': 1
}
}, {
'$limit': 1
}, {
'$project': {
'code': 1,
'skipStops': 1,
'toc': 1,
'_id': 0
}
}
]
}
export function getFullTiplocMatchPipeline(query: string[]) {
return [
{