Multiple metadata filters not working

When I hardcode my filters, everything works fine. When I put in variables, my filter always returns empty. Am I going about this wrong?

if ‘query’ in data: # Check if the request contains a ‘query’ field
query = data[‘query’]
# Capture the namespace
namespace_query = data.get(‘namespace_query’)
# Capture company name, type, story, and client name if they are provided in the request data
company_query = [data[‘company_query’]
] if ‘company_query’ in data else
type_query = [data[‘type_query’]] if ‘type_query’ in data else
story_query = [data[‘story_query’]] if ‘story_query’ in data else
client_query = [data[‘client_query’]] if ‘client_query’ in data else

    my_inp_embed = get_embedding(query, engine="text-embedding-ada-002")

    # Define the filter for the Pinecone query
    filter = {}

    if company_query[0]:
        filter["company name"] = {"$in": company_query}
    if type_query[0]:
        filter["type of story"] = {"$in": type_query}
    if story_query[0]:
        filter["story title"] = {"$in": story_query}
    if client_query[0]:
        filter["client name"] = {"$in": client_query}

    print(f"Using filter: {filter}")  # print out the filter for debugging

    res = index.query(
        vector=my_inp_embed,
        top_k=3,
        filter=filter,
        include_metadata=True,
        include_values=False,
        namespace=namespace_query
    ) ; My HTML matches up, I've debugged every possible way, but my filter is always empty.

Hi @terrence,

Thanks for your question, and I’m sorry you’re encountering this issue.

After reviewing your code and the metadata filtering documentation, I have a couple of recommendations to try out - hopefully one of these helps shake something loose:

  • Checking for Non-Empty Lists: Try replacing the index-based check if company_query[0]: with if company_query: to prevent potential IndexError exceptions and to check for non-empty lists.
  • Debugging: Print the data object to ensure it contains the expected values. This would help verify whether the variables are being populated as intended before being used to construct the filter expressions.
  • Key Names: Ensure that the key names used in the filter object exactly match the key names used when inserting metadata into the Pinecone index. You’ve probably already done this, but any mismatch in key names will result in an empty filter.
  • Null Metadata Values: Ensure that the fields in the request data do not contain null values for some unexpected reason, as this could lead to unexpected behavior

I hope this is helpful! Let me know if that changes anything and if you have any follow-up questions.