Nest AND and OR in where filters

Is it possible to nest ANDs and ORs in where filters?

I would like to fetch documents where wordCount is between 20 and 500 and myId is in a given list, e.g. 1, 2, 3. In SQL I would use the following WHERE clause: WHERE wordCount >= 20 AND wordCount <= 500 AND myId IN (1, 2, 3)

At this point I have the following where filter, but I can’t figure out how to add the myId filter.

where: {
    operator: And,
    operands: [{
        path: ["wordCount"],
        operator: GreaterThanEqual,
        valueInt: 20
    }, {
        path: ["wordCount"],
        operator: LessThanEqual,
        valueInt: 500
    }]
}

Thanks for the help!

Hey again @felixthekraut :slight_smile:

You can absolutely nest filters - take a look at this example (Filters | Weaviate - vector database).

I personally like to test individual filters to make sure they work and then combine them. Let me know how you go with this page - we recently added it so we’d love to hear what you think!

1 Like

Perfect thank you for your help. That page was very helpful. I think we need to find a good way to link to it from GraphQL - Conditional filters | Weaviate - vector database so it is easier to discover it. Otherwise I really like that new page, it is short and to the point and builds well on top of the other Conditional Filters page.

Yeah for sure. Thanks Felix - we’re in the process of updating those sets of pages, and that’s a great idea.

Would you mind sharing the query filter structure you ended up with? I’m trying to do something similar, but still struggling to get it right.

Thanks!

I ended up with something like this:

{
  Get {
    MyClass(limit: 50, nearText: {
        concepts: ["life insurance"],
        certainty: 0.80
        },
        where: {
            operator: And,
            operands: [{
                path: ["wordCount"],
                operator: GreaterThanEqual,
                valueInt: 50
            }, {
                path: ["wordCount"],
                operator: LessThanEqual,
                valueInt: 6000
            },
            {
                operator: Or,
                operands: [
                    {
                        path: ["myId"],
                        operator: Equal,
                        valueInt: 1
                    },
                    {
                        path: ["myId"],
                        operator: Equal,
                        valueInt: 2
                    }
                ]
            }]
        }
        ) {
      _additional {
          id
          certainty
        }
      myId
      wordCount
    }
  }
}