# Not well Filtering in Node.js please I need help!

**URL:** <https://community.pinecone.io/t/not-well-filtering-in-node-js-please-i-need-help/6759>\
**Category:** General\
**Tags:** embeddings, index\
**Created:** [October 9, 2024, 9:31am UTC](https://community.pinecone.io/t/not-well-filtering-in-node-js-please-i-need-help/6759 "2024-10-09T09:31:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aparrilla](https://sea2.discourse-cdn.com/flex020/user_avatar/community.pinecone.io/aparrilla/32/3173_2.png) [@aparrilla](https://community.pinecone.io/u/aparrilla)\
**Post date:** [October 9, 2024, 9:31am UTC](https://community.pinecone.io/t/not-well-filtering-in-node-js-please-i-need-help/6759/1 "2024-10-09T09:31:48Z")

</div>

Having this code:

```auto
async function searchInPinecone(indexName, prompt, userId) {
    const embeddings = new OpenAIEmbeddings({ apiKey: openAiApiKey });
    const index = client.Index(indexName);

    // Get the embedding of the prompt
    const queryEmbedding = await embeddings.embedQuery(prompt);

    // Log the UserId
    console.log('UserId used in the search:', userId);

    try {
        // Perform the search in Pinecone with the filter by UserId
        const queryResponse = await index.query({
            vector: queryEmbedding,
            topK: 20, // Number of closest results to return
            includeMetadata: true,
            filter: {
                UserId: userId.toLowerCase() // Filter to return only results with the provided UserId
            }
        });

        // Log the complete query response
        console.log('Query response:', queryResponse);

        // Log the metadata used in the search
        console.log('Metadata used in the search:', queryResponse.matches.map(match => match.metadata));

        // Check if any matches were found
        if (queryResponse.matches.length === 0) {
            console.log('No matches found for the provided UserId.');
            return ['No context found'];
        }

        // Sort results by the value of {i} in the ID `${doc.id}_chunk_${i}`
        const sortedMatches = queryResponse.matches.sort((a, b) => {
            const aChunkIndex = parseInt(a.id.split('_chunk_').pop());
            const bChunkIndex = parseInt(b.id.split('_chunk_').pop());
            return aChunkIndex - bChunkIndex; // Sort by the numeric value of {i}
        });

        // Extract the relevant context from the sorted results
        const fullContext = sortedMatches.map(match => match.metadata ? match.metadata.content : 'No metadata').join('\n\n');

        // Split the full context into manageable-sized chunks
        const contextChunks = splitContext(fullContext);

        return contextChunks.length > 0 ? contextChunks : ['No context found']; // Return the chunks or a default context if empty
    } catch (error) {
        console.error('Error searching in Pinecone:', error.message);
        throw new Error(`Pinecone search failed: ${error.message}`);
    }
}

```

It doesn’t filter well and I don’t know what else I can do. Searching at the docs, I thought that It was like that, but It seems to be not right?? I’m getting this at the logs: Query response: { matches: , namespace: ‘’, usage: { readUnits: 5 } } UserId used in the search: {whatever} I’m creating the metadata like this: `return { id: `${doc.id}_chunk_${i}`, values: vector, metadata: { content: chunk, UserId: userId.toLowerCase() } }; ` That thing is doing it well, I have “content” and “UserId” as it should be, so I don’t know why It is not filtering!! Please Help ):

---

<div class="post-metadata">

**Author:** ![aparrilla](https://sea2.discourse-cdn.com/flex020/user_avatar/community.pinecone.io/aparrilla/32/3173_2.png) [@aparrilla](https://community.pinecone.io/u/aparrilla)\
**Post date:** [October 9, 2024, 9:38am UTC](https://community.pinecone.io/t/not-well-filtering-in-node-js-please-i-need-help/6759/2 "2024-10-09T09:38:41Z")

</div>

Also, when I’m not filtering:

```auto
const queryResponse = await index.query({
            vector: queryEmbedding,
            topK: 20, // Number of closest results to return
            includeMetadata: true,
        });

```

It does it well, but using all the metadata I have.
