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
    }
  }
}

Hello @felixthekraut, @jphwang
I have nested json like yours my data example like this

{
  "data": {
    "Get": {
      "Adata": [
        {
          "code": "prd002",
          "description": "Savory Caesar Salad",
          "ingredients": [
            {
              "code": "xxx",
              "name": "Romaine lettuce",
              "quantity": "100",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Grilled chicken",
              "quantity": "100",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Parmesan cheese",
              "quantity": "50",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Croutons",
              "quantity": "30",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Caesar dressing",
              "quantity": "20",
              "unit": "gr"
            }
          ],
          "name": "Savory Caesar Salad",
          "properties": [
            {
              "color": "#a02",
              "region": "#c02",
              "viscosity": "#b02"
            }
          ]
        },
        {
          "code": "prd001",
          "description": "Sweet Strawberry Spinach Salad",
          "ingredients": [
            {
              "code": "xxx",
              "name": "Baby spinach",
              "quantity": "100",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Strawberries",
              "quantity": "100",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Goat cheese",
              "quantity": "50",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Candied pecans",
              "quantity": "30",
              "unit": "gr"
            },
            {
              "code": "xxx",
              "name": "Balsamic glaze",
              "quantity": "20",
              "unit": "gr"
            }
          ],
          "name": "Sweet Strawberry Spinach Salad",
          "properties": [
            {
              "color": "#a01",
              "region": "#c01",
              "viscosity": "#b01"
            }
          ]
        }
      ]
    }
  }
}

I want to create a GraphQL search using the ‘name’ and ‘quantity’ from the ‘ingredients’ key, and also use the ‘color’ value from the ‘properties’ key.
How can I achieve this?

Thanks for help and share