added ability to search through another user's files

This commit is contained in:
Martina 2021-11-08 17:17:09 -08:00
parent 84c25d0026
commit 8accb20f1c
4 changed files with 74 additions and 39 deletions

View File

@ -152,6 +152,7 @@ export default function ApplicationHeader({ viewer, page, data, onAction }) {
//TODO(amine): plug in the right values
//for more context, look at node_common/managers/search/search.js
//userId: (optional) the id of the user whose stuff we are searching through. If specified, globalSearch is disregarded since the search will be limited to that user's public items. Does not apply when searching for type USER
//types: leaving it null searches everything. Doing ["SLATE"] searches just slates, doing ["USER", "FILE"] searches users and files.
//globalSearch: whether you are just searching the user's files/slates or global files/slates. This option doesn't exist for searching users since there is no notion of public or private users
//tagIds: only applies when searching files. the ids of the tags (aka collections) you are searching within. aka if you only want to search for files in a given slate, provide that slate's id. If no tag ids are provided, it searches all files

View File

@ -5,12 +5,15 @@ import {
filesIndex,
} from "~/node_common/managers/search/search-client";
import * as Logging from "~/common/logging";
//NOTE(martina): types specifies which object types to search through. Leaving it null or empty will search ALL object types
// E.g. ["USER", "SLATE", "FILE"]
//NOTE(martina): grouped = true will separate results by object type rather than mixing them togther.
// E.g. { users: [user1, user2, ...], slates: [slate1, slate2, ...], files: [file1, file2, ...]}
export const searchMultiple = async ({
query,
ownerId,
userId,
grouped = false,
types,
@ -19,13 +22,22 @@ export const searchMultiple = async ({
let body = [];
let keys = [];
let ownerQuery = {
bool: {
should: [{ term: { ownerId: userId } }],
},
};
if (globalSearch) {
ownerQuery.bool.should.push({ term: { isPublic: true } });
let ownerQuery;
if (userId) {
ownerQuery = {
bool: {
must: [{ term: { ownerId: userId } }, { term: { isPublic: true } }],
},
};
} else {
ownerQuery = {
bool: {
should: [{ term: { ownerId } }],
},
};
if (globalSearch) {
ownerQuery.bool.should.push({ term: { isPublic: true } });
}
}
if (!types?.length || types.includes("USER")) {
@ -131,7 +143,6 @@ export const searchMultiple = async ({
const group = returned[i];
cleaned[keys[i]] = group.map((result) => result._source);
}
console.log(cleaned);
return cleaned;
} else {
let cleaned = [];
@ -139,7 +150,6 @@ export const searchMultiple = async ({
cleaned.push(...group);
}
cleaned.sort((a, b) => b._score - a._score).map((result) => result._source);
console.log(cleaned);
return cleaned;
}
} catch (e) {
@ -176,14 +186,13 @@ export const searchUser = async ({ query }) => {
const hits = result?.body?.hits?.hits;
let users = hits.map((hit) => hit._source);
console.log(users);
return users;
} catch (e) {
console.log(e);
}
};
export const searchSlate = async ({ query, userId, globalSearch = false }) => {
export const searchSlate = async ({ query, ownerId, userId, globalSearch = false }) => {
try {
const must = [
{
@ -197,13 +206,22 @@ export const searchSlate = async ({ query, userId, globalSearch = false }) => {
},
];
let ownerQuery = {
bool: {
should: [{ term: { ownerId: userId } }],
},
};
if (globalSearch) {
ownerQuery.bool.should.push({ term: { isPublic: true } });
let ownerQuery;
if (userId) {
ownerQuery = {
bool: {
must: [{ term: { ownerId: userId } }, { term: { isPublic: true } }],
},
};
} else {
ownerQuery = {
bool: {
should: [{ term: { ownerId } }],
},
};
if (globalSearch) {
ownerQuery.bool.should.push({ term: { isPublic: true } });
}
}
must.push(ownerQuery);
@ -222,14 +240,13 @@ export const searchSlate = async ({ query, userId, globalSearch = false }) => {
const hits = result?.body?.hits?.hits;
let slates = hits.map((hit) => hit._source);
console.log(slates);
return slates;
} catch (e) {
console.log(e);
}
};
export const searchFile = async ({ query, userId, globalSearch = false, tagIds = [] }) => {
export const searchFile = async ({ query, ownerId, userId, globalSearch = false, tagIds = [] }) => {
try {
const must = [
{
@ -266,13 +283,22 @@ export const searchFile = async ({ query, userId, globalSearch = false, tagIds =
must.push(tagQuery);
}
let ownerQuery = {
bool: {
should: [{ term: { ownerId: userId } }],
},
};
if (globalSearch) {
ownerQuery.bool.should.push({ term: { isPublic: true } });
let ownerQuery;
if (userId) {
ownerQuery = {
bool: {
must: [{ term: { ownerId: userId } }, { term: { isPublic: true } }],
},
};
} else {
ownerQuery = {
bool: {
should: [{ term: { ownerId } }],
},
};
if (globalSearch) {
ownerQuery.bool.should.push({ term: { isPublic: true } });
}
}
must.push(ownerQuery);
@ -338,7 +364,6 @@ export const searchFile = async ({ query, userId, globalSearch = false, tagIds =
const hits = result?.body?.hits?.hits;
let files = hits.map((hit) => hit._source);
console.log(files);
return files;
} catch (e) {
console.log(e);
@ -347,18 +372,15 @@ export const searchFile = async ({ query, userId, globalSearch = false, tagIds =
export const getUser = async ({ id }) => {
const { body } = await searchClient.get({ index: usersIndex, id });
console.log(body);
return body;
};
export const getSlate = async ({ id }) => {
const { body } = await searchClient.get({ index: slatesIndex, id });
console.log(body);
return body;
};
export const getFile = async ({ id }) => {
const { body } = await searchClient.get({ index: filesIndex, id });
console.log(body);
return body;
};

View File

@ -2,6 +2,7 @@ import * as RequestUtilities from "~/node_common/request-utilities";
import SearchManager from "~/node_common/managers/search";
//NOTE(martina): props
// userId: (optional) the id of the user whose stuff we are searching through. If specified, globalSearch is disregarded since the search will be limited to that user's public items. Does not apply when searching for type USER
// globalSearch: Whether we are searching all public stuff plus the user's own stuff (true) or just the user's stuff (false)
// query: The search query
// types: Which object types to search for. Can be a string or array of strings. Leaving it null or empty will search ALL object types. Potential values are: "USER", "SLATE", "FILE"
@ -16,7 +17,7 @@ export default async (req, res) => {
res.status(400).send({ decorator: "SERVER_SEARCH_DATA_NOT_INCLUDED", error: true });
}
const searchProps = { ...req.body.data, userId: id };
const searchProps = { ...req.body.data, ownerId: id };
if (!searchProps.types) {
searchProps.types = [];

View File

@ -141,21 +141,32 @@ async function update() {
}
async function search() {
// await SearchManager.searchUser({ query: "image" });
// await SearchManager.searchSlate({
let result = null;
// result = await SearchManager.searchUser({ query: "image" });
// result = await SearchManager.searchSlate({
// query: "my slate",
// ownerId: "5172dd8b-6b11-40d3-8c9f-b4cbaa0eb8e7",
// userId: "5172dd8b-6b11-40d3-8c9f-b4cbaa0eb8e7",
// globalSearch: true,
// });
// await SearchManager.searchFile({
// query: "chocolate",
// userId: "f9cc7b00-ce59-4b49-abd1-c7ef7253e258",
// result = await SearchManager.searchFile({
// query: "slate",
// ownerId: "5172dd8b-6b11-40d3-8c9f-b4cbaa0eb8e7",
// // userId: "5172dd8b-6b11-40d3-8c9f-b4cbaa0eb8e7",
// globalSearch: true,
// tagIds: [],
// // tagIds: ["d82fbc78-88de-4015-adec-a7ea832fc922", "0824a3cb-e839-4246-8ff4-d919919e1487"],
// });
// await SearchManager.searchAll({ query: "slate", userId: "5172dd8b-6b11-40d3-8c9f-b4cbaa0eb8e7" });
// await SearchManager.getFile({ id: "2892b652-5034-4e0f-b3b2-0352e0d64e17" });
// result = await SearchManager.searchMultiple({
// query: "slate",
// ownerId: "02b5f36f-2ce3-46f3-8b95-9bd996658e22",
// userId: "5172dd8b-6b11-40d3-8c9f-b4cbaa0eb8e7",
// grouped: true,
// types: ["SLATE", "FILE"],
// });
// result = await SearchManager.getFile({ id: "2892b652-5034-4e0f-b3b2-0352e0d64e17" });
console.log(result);
}
async function setUpIndex() {