Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAD-1195 search published #626

Draft
wants to merge 1 commit into
base: feature/v2.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ export class CaptureModelRepository extends BaseRepository<'capture_model_api_mi
derivedFrom,
all,
target,
searchQuery,
}: {
page?: number;
perPage?: number;
allDerivatives?: boolean;
derivedFrom?: string;
all?: boolean;
target?: string;
searchQuery?: string;
},
site_id: number
) {
Expand All @@ -161,6 +163,14 @@ export class CaptureModelRepository extends BaseRepository<'capture_model_api_mi
})}::jsonb) is not null`
: sql``;

const search = searchQuery
? `and cm.id in (${CaptureModelRepository.queries.documentSearch(
searchQuery,
{ allDerivatives, derivedFrom, target },
site_id
)})`
: sql``;

return sql<CaptureModelSnippetRow>`
select cm.id,
cm.derived_from,
Expand All @@ -173,6 +183,7 @@ export class CaptureModelRepository extends BaseRepository<'capture_model_api_mi
where cm.site_id = ${site_id}
${derivedQuery}
${targetQuery}
${search}
${paging}
`;
},
Expand Down Expand Up @@ -201,6 +212,34 @@ export class CaptureModelRepository extends BaseRepository<'capture_model_api_mi
${paging}
`;
},

documentSearch(query: string, { derivedFrom, allDerivatives, target }: any, site_id: number) {
const derivedQuery = derivedFrom
? sql`and cmn.derived_from = ${derivedFrom}`
: allDerivatives
? sql``
: sql`and cmn.derived_from is null`;

const targetQuery = target
? sql`
and jsonb_path_query_first(cmn.target::jsonb, '$[*] ? (@.id == $target)', ${sql.json({
target,
})}::jsonb) is not null`
: sql``;

return sql<{ id: string }>`
select r.id
from capture_model_document r left join capture_model cmn on r.id = cmn.document_id,
jsonb_to_recordset(jsonb_path_query_array(document_data -> 'properties',
'$.*[0] ? (@.type == "entity")."properties".*[0]')) as entity(id text, value text, type text),
jsonb_to_recordset(jsonb_path_query_array(document_data -> 'properties',
'$.*[0] ? (@.value != null)')) as field(id text, value text)
where (field.value::text ilike ${`%${query}%`} or entity.value::text ilike ${`%${query}%`})
and cmn.site_id = ${site_id}
${derivedQuery}
${targetQuery}
`;
},
};
static mutations = {
createDocument: (doc: CaptureModel['document'], site_id: number, target?: CaptureModel['target']) => {
Expand Down Expand Up @@ -611,6 +650,7 @@ export class CaptureModelRepository extends BaseRepository<'capture_model_api_mi
derivedFrom?: string;
all?: boolean;
target?: string;
searchQuery?: string;
},
siteId: number
): Promise<CaptureModelSnippet[]> {
Expand Down Expand Up @@ -1108,7 +1148,42 @@ export class CaptureModelRepository extends BaseRepository<'capture_model_api_mi
});
}

async searchPublished(siteId: number) {
async searchPublished(
siteId: number,
query: string,
options: {
manifest?: string;
canvas?: string;
collection?: string;
field_type?: string;
parent_property?: string;
selector_type?: string;
capture_model_id?: string;
} = {}
) {
// Is this the correct return type?
// return this.listCaptureModels(
// {
// page: 0,
// perPage: 100,
// all: true,
// target: options.manifest || options.canvas,
// derivedFrom: options.capture_model_id,
// searchQuery: query,
// },
// siteId
// );

// options.manifest / options.canvas / options.collection
// | JSON query on the target field, can be grabbed from existing codebase
//
// options.field_type / options.parent_property / options.selector_type
// | No easy way to achieve this at the moment. It could be shallow - and use a query, otherwise it would
// | Have to be a post-filter.
//
// options.capture_model_id
// | Simple enough field check.

// @todo this does need to be re-implemented for the search manifest functionality to work
// however this might be a good time to use the actual search service for that instead.
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@ import { optionalUserWithScope } from '../../utility/user-with-scope';
export const searchPublished: RouteMiddleware = async context => {
const { siteId } = optionalUserWithScope(context, ['models.view_published']);

// const { q: query, manifest, canvas, collection, field_type, parent_property, selector_type, capture_model_id } =
// (ctx.request.query as any) || {};
//
// const queryBlock: any = {};
//
// if (manifest) {
// queryBlock.manifest = manifest;
// }
// if (canvas) {
// queryBlock.canvas = canvas;
// }
// if (collection) {
// queryBlock.collection = collection;
// }
// if (field_type) {
// queryBlock.field_type = field_type;
// }
// if (parent_property) {
// queryBlock.parent_property = parent_property;
// }
// if (selector_type) {
// queryBlock.selector_type = selector_type;
// }
// if (capture_model_id) {
// queryBlock.capture_model_id = capture_model_id;
// }
const { q: query, manifest, canvas, collection, field_type, parent_property, selector_type, capture_model_id } =
(context.query as any) || {};

const queryBlock: {
manifest?: string;
canvas?: string;
collection?: string;
field_type?: string;
parent_property?: string;
selector_type?: string;
capture_model_id?: string;
} = {};

if (manifest) {
queryBlock.manifest = manifest;
}
if (canvas) {
queryBlock.canvas = canvas;
}
if (collection) {
queryBlock.collection = collection;
}
if (field_type) {
queryBlock.field_type = field_type;
}
if (parent_property) {
queryBlock.parent_property = parent_property;
}
if (selector_type) {
queryBlock.selector_type = selector_type;
}
if (capture_model_id) {
queryBlock.capture_model_id = capture_model_id;
}

context.response.body = {
results: await context.captureModels.searchPublished(siteId),
results: await context.captureModels.searchPublished(siteId, query, queryBlock),
};
};