#community-help

Grouping Multiple Collections for Federated Search

TLDR Kyle wanted to use federated search to group results from multiple collections. He was confused about how to selectively show tags in search context. Jason explained a way to achieve this by setting one filter for each tag and querying dynamically.

Powered by Struct AI

1

1

Jul 07, 2021 (31 months ago)
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
06:44 PM
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?
06:44
Kyle
06:44 PM
e.g.
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)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:47 PM
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)?
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
06:47 PM
multiple tags (and user created)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:51 PM
One way you could do this is by creating a collection with a schema like this:
06:53
Jason
06:53 PM
{
  fields: [
    ...
    {name: "userids_tagged_.*", type: "int32[]", optional: true}
  ]
}
06:55
Jason
06:55 PM
Then when indexing the movie, you'd send it as:

{
  description: "...",
  userids_tagged_favorite: [1, 2, 3, 4],
  userids_tagged_tagx: [1, 2, 10, 42],
}
06:59
Jason
06:59 PM
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
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
06:59 PM
in that scenario, how would the user be able to select from the tags only they have used?
07:00
Kyle
07:00 PM
(thank you for helping me on this)

1

Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:00 PM
You also want to use a Scoped API Key and embed these filters in it, along with excludefields, so `userids_tagged*` fields are returned in the response...
07:01
Jason
07:01 PM
> 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?
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
08:26 PM
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 ?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:44 PM
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?
Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
09:18 PM
movie results from the movies they've tagged, but would be able to change which tags they're filtering results by
Jul 08, 2021 (31 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
12:31 AM
Oops, missed your message.

So if say a movie like this exists:
12:31
Jason
12:31 AM
{
  description: "...",
  userids_tagged_favorite: [1, 2, 3, 4],
  userids_tagged_tagx: [1, 2, 10, 42],
}
12:33
Jason
12:33 AM
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
12:37
Jason
12:37 AM
So in terms of Typesense queries, that would translate to a multi_query:

[
  {
    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
]
12:40
Jason
12:40 AM
So say UserID 1 doesn't exist in userids_tagged_tagz, then the 3rd query above will return 0 results

1

Kyle
Photo of md5-634dde0508c7b907c4bc5a5c9b5fe23a
Kyle
04:07 PM
thanks Jason!

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community

Similar Threads

User-Specific Tagging and Filtering in UI

bnfd asked for the best way to create user-specific tags available on the UI. Jason suggested using personalized filters and creating a separate collection for each user's movies. The duo clarified the use of 'tags' in schemas and the refinementList widget in instantsearch. They also discussed various approaches to import and search large document collections.

1

46
29mo

Querying and Indexing Multiple Elements Issues

Krish queried fields with multiple elements, which Kishore Nallan suggested checking `drop_tokens_threshold`. Krish wished to force OR mode for token, but Kishore Nallan admitted the feature was missing. Krish was able to resolve the issue with url encoding.

34
12mo

Methods for Fetching, Querying, and Modifying Collections in Typesense

Bill inquired about performing OR queries, querying empty arrays and modifying collections in Typesense. Kishore Nallan explained the current limitations and provided workarounds and recommendations for each case. The conversation also touched upon the usage of cache in Typesense and the workings of the _eval function.

5

52
10mo

Querying with Not-in in Typesense

Masahiro inquired about using not-in queries in Typesense. Kishore Nallan explained how to conduct such queries by using the "-" operator in the query string, and assisted Masahiro with issues stemming from a high number of exclusion tokens. The problem was eventually resolved by switching to the `multi_search` endpoint.

4

49
34mo

Moving from Algolia to Typesense: Questions and Answers

Juan sought advice from Kishore Nallan about moving from Algolia to Typesense, handling MultiSearch, setting parameters, checking imported documents, and a specific syntax query.

1

22
22mo