# AND/OR query logic - is this a BUG?

**URL:** https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268
**Category:** General
**Created:** [September 24, 2024, 7:45am UTC](https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268 "2024-09-24T07:45:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Analitiq](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Analitiq](https://forum.weaviate.io/u/Analitiq)
#### Post date: [September 24, 2024, 7:45am UTC](https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268/1 "2024-09-24T07:45:02Z")

</div>

I have been trying to wrap my head around this for a while and thought of asking the community if this is a BUG and needs to be reported.

Let’s imagine we have a simple set of documents in weaviate collection, that can be defined for simplicity as this list:

```auto
test_documents = [
    {"document_name": "test_document_1.txt", "content": "This is the first test document."},
    {"document_name": "test_document_2.txt", "content": "Another document for testing."},
    {"document_name": "project_plan.txt", "content": "This document contains the project plan."},
    {"document_name": "summary_report.txt", "content": "The summary of all reports."},
    {"document_name": "test_document_3.txt", "content": "This document is for additional tests."},
]

```

We then have this GraphQL query:

```auto
{
  Aggregate {
    Test_collection(
      where: {
        operator: And, 
        operands: [
          {
            operator: Or, 
            operands: [
              {path: ["document_name"], operator: Like, valueText: "*test*"}, 
              {path: ["content"], operator: NotEqual, valueText: "This is the first test document."}
            ]
          }, 
          {
            operator: Or, 
            operands: [
              {path: ["document_name"], operator: Equal, valueText: "project_plan.txt"}, 
              {path: ["content"], operator: Like, valueText: "*project*"}
            ]
          }
        ]
      }, 
      tenant: "test_collection"
    ) {
      meta {
        count
      }
    }
  }
}

```

The query should be returning 1 document (project\_plan.txt) but it is returning 0.

I took This logical approach when breaking down the query and running the operands separately.

The first part matches all 5 documents:

```auto
operator: Or, 
            operands: [
              {path: ["document_name"], operator: Like, valueText: "*test*"}, 
              {path: ["content"], operator: NotEqual, valueText: "This is the first test document."}
            ]

```

The second part matches only 1 document:

```auto
operator: Or, 
            operands: [
              {path: ["document_name"], operator: Equal, valueText: "project_plan.txt"}, 
              {path: ["content"], operator: Like, valueText: "*project*"}
            ]

```

The AND operator between the filters for all 5 documents and 1 document should be returning 1 document as a result.

Am I missing something here?

---

<div class="post-metadata">

### Author: ![Shahin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/shahin/32/7944_2.png) [@Shahin](https://forum.weaviate.io/u/Shahin)
#### Post date: [September 24, 2024, 8:58am UTC](https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268/2 "2024-09-24T08:58:39Z")

</div>

Good morning @Analitiq,

Thank you so much for the details in this details.

Would you mind sharing your schema creation method as well?

I will look into it and replicate if needed, but I would like to understand the schema behind the scenes.

Thanks!

---

<div class="post-metadata">

### Author: ![Analitiq](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Analitiq](https://forum.weaviate.io/u/Analitiq)
#### Post date: [September 25, 2024, 12:43pm UTC](https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268/3 "2024-09-25T12:43:10Z")

</div>

@Shahin Thank you for your reply. The shcema is created autmatically.

I chunk the data like this:

```auto
    Chunk(
        content=chunk_of_text,
        source=metadata["source"],
        document_type=metadata["document_type"],
        document_name=metadata["document_name"],
        document_num_char=len(chunk),
        chunk_num_char=len(chunk),
        date_loaded=datetime.now(timezone.utc),
        content_kw=keyword_extractions.extract_keywords(chunk),
    )

```

one the chunk is created, I pass it to Weavaite cloud:

```auto
collection = self.__get_tenant_collection_object(collection_name, tenant_name)

                with collection.batch.dynamic() as batch:
                    for chunk in chunks:
                        uuid = generate_uuid5(chunk.model_dump())
                        hf_vector = self.vectorizer.vectorize(chunk.content)
                        try:
                            response = batch.add_object(
                                properties=chunk.model_dump(),
                                uuid=uuid,
                                vector=hf_vector,
                            )
                            logger.info(response)
                            chunks_loaded += 1
                        except Exception as e:
                            raise e

```

I am running latest Weaviate 4.8.1 and python 3.10

---

<div class="post-metadata">

### Author: ![Analitiq](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Analitiq](https://forum.weaviate.io/u/Analitiq)
#### Post date: [September 25, 2024, 12:44pm UTC](https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268/4 "2024-09-25T12:44:16Z")

</div>

Here is how the schema looks in the Weaviate cloud.

 ![Screenshot 2024-09-25 at 14.43.47](https://canada1.discourse-cdn.com/flex027/uploads/weaviate/original/2X/8/8ca7e53788434ec9045b6ea88f01b1f87310a475.png)

---

<div class="post-metadata">

### Author: ![Shahin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/shahin/32/7944_2.png) [@Shahin](https://forum.weaviate.io/u/Shahin)
#### Post date: [October 2, 2024, 12:18pm UTC](https://forum.weaviate.io/t/and-or-query-logic-is-this-a-bug/4268/5 "2024-10-02T12:18:25Z")

</div>

Hey @Analitiq,

Let’s break this down. In your query:

1. First Condition OR matches documents where document\_name contains “test” OR content is not equal to “This is the first test document.” **This is broad and will match almost all documents**.

2. Second Condition OR matches documents where document\_name equals “project\_plan.txt” OR content contains “project.” This is more specific and applies to just one document.

I think you expected that combining these with **AND** would return “project\_plan.txt,” but because the first condition is too broad and the second is narrow, it’s unlikely that both will be true for the same document. The **AND** operator requires both sets of conditions to match a single document.

If your goal is to find documents related to “test” or “project,” I suggest adjusting the **OR** conditions to focus on those specific terms, while grouping them together in a single **OR** condition with more refined filters, right?

Have you tried to switch the main operator AND to **OR** then the two OR conditions two **ANDs**?
