# Query Across multiple classes

**URL:** <https://forum.weaviate.io/t/query-across-multiple-classes/2872>\
**Category:** General\
**Created:** [June 30, 2024, 2:16pm UTC](https://forum.weaviate.io/t/query-across-multiple-classes/2872 "2024-06-30T14:16:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ansh\_Gaur](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/ansh_gaur/32/197_2.png) [@Ansh\_Gaur](https://forum.weaviate.io/u/Ansh_Gaur)\
**Post date:** [June 30, 2024, 2:16pm UTC](https://forum.weaviate.io/t/query-across-multiple-classes/2872/1 "2024-06-30T14:16:14Z")

</div>

I have a use case where I might have required data distributed across multiple classes and I want to use this data from multiple classes in a RAG chain to generate answers for a query .

Assuming I have a limit of 8k Tokens for the LLM model used in RAG chain what are some good ways to get the top tokens combined from these classes (top k tokens across the classes and not individually).

I know of one methods I found :-

```
def fetch_and_combine_results(query: str, classes: list, per_class_limit: int = 4096) -> list:
 combined_results = []

 for class_name in classes:
    response = (
        client.query
        .get(class_name, ["link", "scraped_text"])
        .with_hybrid(query, alpha=0.8)
        .with_limit(per_class_limit)
        .with_additional(["distance", "id"])
        .do()
    )

    if response and response['data']['Get'][class_name]:
        class_results = response['data']['Get'][class_name]
        combined_results.extend(class_results)

return combined_results

def sort_and_limit_results(results: list, token_limit: int = 4096) -> list:

results.sort(key=lambda x: x['_additional']['distance'])

limited_results = []
total_tokens = 0

for result in results:
    text = result['scraped_text']
    tokens = Total_tokens(text)

    if total_tokens + tokens > token_limit:
        break

    limited_results.append(result)
    total_tokens += tokens

return limited_results

```

Are there any other better ways to do this ?  
Thank you for your help!!

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [July 1, 2024, 7:48pm UTC](https://forum.weaviate.io/t/query-across-multiple-classes/2872/2 "2024-07-01T19:48:22Z")

</div>

hi @Ansh_Gaur !

Welcome to our community 🤗

Unless you use [cross references](https://weaviate.io/developers/weaviate/manage-data/cross-references) (and maybe [ref2vec](https://weaviate.io/developers/weaviate/modules/retriever-vectorizer-modules/ref2vec-centroid)?) and some modelling, you will not be able to query the two collections at once and get a single score/distance.

You will, off course, be able to get two+ separate queries, as you are doing.

Now, you need to keep in mind that, when doing a **hybrid** search, the [fusion algorithm](https://weaviate.io/blog/hybrid-search-fusion-algorithms) will kick in.

So you are not resorting the two queries results by cosine vector distance, but by a normalized score ranking from the first result on each query (the score of the first object in each query is 1), as explained in that blog post.

Maybe the distance (or doing a nearText with outcut instead of hybrid) can get you better different sorting. Or not hehehe

So something to explore 🙂

Also, I see you are using the v3 client. We strongly suggest using the python v4 client.

With that said… 😉 there is a way of getting the two results with only one http query, by using graphql raw queries 🙂

Check here for more on that:

> [@GraphQL: Use Common Interface for Classes](https://forum.weaviate.io/t/graphql-use-common-interface-for-classes/726/2):
>
> Hi @Tobias_Schweizer ! Welcome to our community hugs Happy to know that you are experimenting and liking Weaviate! AFAIK, there isn’t a way to implement such an Interface. While you can use [cross reference to create relationships between those classes](https://weaviate.io/developers/weaviate/manage-data/cross-references), you will need to query each class with it’s properties separately. There is a way to do multiple queries at the same request payload by leveraging GraphQL alias, like so: { Get { Q1: MyClass(tenant: "rep-1") { name } …

Let me know if that helps 🙂
