Query filters - filtering out objects with references

Description

I’m trying to set up a web app with restaurant dishes. I have a question about filters.

  • I have classes for ‘Dish’ and ‘Allergen’. I’m using references to link them together (eg. a ‘Dish’ has a property ‘allergens’ which is a reference to ‘Allergens’)
  • I’m trying to work how to query ‘Dishes’ while filtering out certain certain allergens. For example, I want to return dishes that match a certain search query (eg. vector query / hybrid query) while making sure that the ‘Dish’ objects do not contain links to certain ‘Allergen’ objects.
  • It feels like I need a way to either:
    • return dishes where none of the disallowed allergen IDs are linked.
    • create an ‘allowed allergen IDs’ list, and then query dishes where the allergens are within that list, but not any others.

Thinking about this a bit more, I suppose I could:

  • make a list of all the dish IDs
  • make a list of all the dishes that include the ‘disallowed allergens’ by doing a query with the ContainsAny filter
  • filter out the disallowed dish IDs to create an ‘allowed dish IDs’ array
  • use the ‘allowed dish IDs’ array somehow?

Has anyone had any experience with something similar? Thanks :slight_smile:

Server Setup Information

  • Weaviate Server Version: 1.23.0
  • Deployment Method: embedded
  • Multi Node? Number of Running Nodes: N/A
  • Client Language and Version: weaviate-ts-embedded 1.2.0

Any additional Information

Hi @larryhudson !! Welcome to our community :hugs:

You can filter out objects based on cross references:

If you need filtering from “both sides” of the cross reference, then you will need to implement a two way cross reference:

Let me know if this helps!

Thanks!

Hi @DudaNogueira, thanks for your reply.

  • I have set up a class for ‘Dish’ and another class for ‘Ingredient’, and I am using a two-way reference to link them together. Here’s how my classes are set up:
    • Class: Dish
      • name: [“text”]
      • description: [“text”]
      • ingredients: [“Ingredient”]
    • Class: Ingredient
      • name: [“text”]
      • description: [“text”]
      • dishes: [“Dish”]
  • I am struggling to work out how to use filters to exclude ‘disallowed’ ingredients. I’ve tried to explain that in my post above.

Say I have these objects in my database:

  • Dish 1 [ID: dish1]:
    • name: Spaghetti Bolognese
    • ingredients: [“ingredient1”]
  • Dish 2 [ID: dish2]:
    • name: Mushroom Pizza
    • ingredients: [“ingredient2”]
  • Ingredient 1 [ID: ingredient1]:
    • name: Beef
    • dishes: [“dish1”]
  • Ingredient 2 [ID: ingredient2]:
    • name: Mushroom
    • ingredients: [“dish2”]

I want to do a query for ‘dish’ objects that exclude the ingredient with ID “ingredient1”. As I said above, it doesn’t look like there is a query filter for this.

Am I missing something? If I am, could you send through an example of how I could achieve this?

Thanks again for your help.

Hi @larryhudson

You are right. Weaviate doesn’t currently have a filter for ContainsNone, which would allow you to search for “give me dishes that do not contain cheese”.

I tried to find a way to do it with cross-references, but I couldn’t get it to work.

GitHub issue

We have a GitHub issue prepared for this.
Please give it an upvote, and feel free to add your example as well :wink: