llama_index: [Bug]: Multiple MetaDataFilters not working even with a condition set.

Bug Description

query_engine = index.as_query_engine(
    filters=MetadataFilters(
        filters=[
            MetadataFilter(
                key="color",
                value="red",
            ),
           MetadataFilter(
                key="alpha",
                value="1",
            ),
       ],
        condition=FilterCondition.OR,
)

Only when i use either of the filters it gives a index. When i do buth it returns: Empty response

Version

0.9.44

Steps to Reproduce

Make a vectore store index and query it

index= VectorStoreIndex.from_vector_store(
   vector_store=jobs_vector_store, service_context=service_context
)

Im using Qdrant.

Relevant Logs/Tracbacks

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 17 (1 by maintainers)

Most upvoted comments

🤖

Based on the context provided, it seems like you’re trying to use multiple MetadataFilters with a FilterCondition.OR condition. This should return nodes that match any of the filters, not all of them. However, you mentioned that you’re getting an empty response.

The code you’ve provided seems correct, but I suspect the issue might be with the data you’re querying. If there are no nodes that match either the “color” filter or the “alpha” filter, the query would indeed return an empty response.

Here’s a modified version of your code that should work as expected:

from llama_index.vector_stores.types import (
    FilterOperator,
    FilterCondition,
)

filters = MetadataFilters(
    filters=[
        MetadataFilter(key="color", value="red"),
        MetadataFilter(key="alpha", value="1"),
    ],
    condition=FilterCondition.OR,  # Change AND to OR
)

query_engine = index.as_query_engine(filters=filters)

In this code, the FilterCondition.OR condition is used, meaning that the query will return nodes that match either the “color” filter or the “alpha” filter, not necessarily both.

If you’re still getting an empty response, I would recommend checking the data you’re querying to ensure there are nodes that match either of the filters. You can do this by running separate queries for each filter and checking if they return any results.

For more information on how to use MetadataFilters with multiple filters and a FilterCondition.OR condition, you can refer to the Qdrant metadata filter example in the LlamaIndex documentation.

If you’re still having issues after trying these steps, please provide more information about the data you’re querying and I’ll be happy to help further.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

Somehow got it working after upgrading the qdrant image (v1.11.0) and llama-index (v0.11.1) docker pull qdrant/qdrant:v1.11.0 pip install llama-index-core>0.11.1 llama-index-vector-stores-qdrant>0.3.0 --upgrade Example usage:

# Define your filter:
doc_list=['doc1','doc2','doc3']

from llama_index.core.vector_stores import (
    MetadataFilter,
    MetadataFilters,
    FilterOperator,
)

# Define your filter(s)
from llama_index.core.vector_stores import FilterOperator, FilterCondition
filters = MetadataFilters(
    filters=[
        MetadataFilter(key="metadata_key_name", value=doc, operator=FilterOperator.EQ) for doc in doc_list
    ],
    condition=FilterCondition.OR,
)
retriever_filtered = index.as_retriever(filters=filters, similarity_top_k=5)

# Test your retriever with filter
result=retriever_filtered.retrieve("Testing Filter")
set([res.metadata['metadata_key_name'] for res in result])