run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
33.51k stars 4.7k forks source link

[Bug]: #14438

Open sunilsamsonsuresh opened 2 weeks ago

sunilsamsonsuresh commented 2 weeks ago

Bug Description

The code in _python3.10/site-packages/llama_index/vectorstores/mongodb/base.py

The below code only retains the last filter condition by replacing the existing key value pair.

def _to_mongodb_filter(standard_filters: MetadataFilters) -> Dict:
    """Convert from standard dataclass to filter dict."""
    filters = {}
    for filter in standard_filters.legacy_filters():
        filters[filter.key] = filter.value
    return filters

Version

0.10.44

Steps to Reproduce

Apply multiple filters to reproduce this

country_filter = MetadataFilters(
    filters=[
        ExactMatchFilter(key="metadata.country_code", value=f'{country_code}'),
        ExactMatchFilter(key="metadata.country_code", value='Global'),
    ],
    condition='or'
)

Relevant Logs/Tracbacks

No response

dosubot[bot] commented 2 weeks ago

@sunilsamsonsuresh Hello there! 👋 I'm here to help you with any bugs, questions, or contributions you have in mind.

To fix the _to_mongodb_filter function to handle multiple filter conditions without replacing the existing key-value pairs, you can modify the function to use the $and operator. This way, each filter condition is added as a separate dictionary within an array, ensuring that multiple conditions are combined correctly.

Here is the updated _to_mongodb_filter function:

def _to_mongodb_filter(standard_filters: MetadataFilters) -> Dict:
    """Convert from standard dataclass to filter dict."""
    filters = []
    for filter in standard_filters.legacy_filters():
        filters.append({filter.key: filter.value})
    return {"$and": filters} if filters else {}

This modification ensures that multiple filter conditions are combined using the $and operator, allowing MongoDB to handle them correctly without overwriting existing key-value pairs [1][2].

To continue talking to Dosu, mention @dosu.