Is it possible to use federated search to group re...
# community-help
k
Is it possible to use federated search to group results from multiple collections? e.g. say I want users to be able to search movie descriptions from movies they've tagged (but not be able to use other users tags). Is there a better way to do this?
e.g.
Copy code
let searchRequests = {
  'searches': [
    {
      'collection': 'movies',
      'q': 'matrix',
      'query_by': 'description'
    },
    {
      'collection': 'user_tags',
      'facetFilters': [ "tag:favorite"]
    }
  ]
}

// Search parameters that are common to all searches go here
let commonSearchParams =  {
    'group_by': 'movie_id',
}

client.multiSearch.perform(searchRequests, commonSearchParams)
j
Question: just to make sure, can users tag a movie with multiple tags, or can users only favorite movies (apply a single favorite tag to a movie)?
k
multiple tags (and user created)
j
One way you could do this is by creating a collection with a schema like this:
Copy code
{
  fields: [
    ...
    {name: "userids_tagged_.*", type: "int32[]", optional: true}
  ]
}
Then when indexing the movie, you'd send it as:
Copy code
{
  description: "...",
  userids_tagged_favorite: [1, 2, 3, 4],
  userids_tagged_tagx: [1, 2, 10, 42],
}
Then, if you have a list of all tags created, you could add one filter for each tag ever created, and only get the records which have the give user id in that field
k
in that scenario, how would the user be able to select from the tags only they have used?
(thank you for helping me on this)
šŸ‘ 1
j
You also want to use a Scoped API Key and embed these filters in it, along with exclude_fields, so
userids_tagged_*
fields are returned in the response...
how would the user be able to select from the tags only they have used?
If you filter on movies that only have that particular user's ID in any of the
userids_tagged_*
fields, that achieves this right?
k
ah, I'm still learning typesense so maybe missing this piece. If a userid is present in say
userids_tagged_tagx
,
userids_tagged_tagy
but not
userids_tagged_tagz
, how would the user see
x
,
y
available in the current search context but not
z
?
j
May be I'm not understanding the question:
how would the user seeĀ x,yĀ available in the current search context but notĀ z
Are users searching for both movie results and tag results? Or are they searching only for movie results from the movies that they've tagged?
k
movie results from the movies they've tagged, but would be able to change which tags they're filtering results by
j
Oops, missed your message. So if say a movie like this exists:
Copy code
{
  description: "...",
  userids_tagged_favorite: [1, 2, 3, 4],
  userids_tagged_tagx: [1, 2, 10, 42],
}
And UserID 1 is searching for movies, if you tell Typesense, get me all movies that have UserID 1 in: - the list of users who've tagged a movie with tag favorite - the list of users who've tagged a movie with tag tagx etc
So in terms of Typesense queries, that would translate to a multi_query:
Copy code
[
  {
    collection: "movies",
    q: "thriller",
    query_by: "description",
    filter_by: "userids_tagged_favorite:1"
  },
  {
    collection: "movies",
    q: "thriller",
    query_by: "description",
    filter_by: "userids_tagged_tagx:1"
  },
  {
    collection: "movies",
    q: "thriller",
    query_by: "description",
    filter_by: "userids_tagged_tagz:1"
  },
  // One query per tag - you'd have to maintain all the tags ever created on your side and use that to construct the query dynamically
]
So say UserID 1 doesn't exist in
userids_tagged_tagz
, then the 3rd query above will return 0 results
šŸ™Œ 1
k
thanks @Jason Bosco!