Skip to content

Commit 54d214d

Browse files
fix: use sparse index namespaces for Test 5 keyword search
fix: use sparse index namespaces for Test 5 keyword search - Add PineconeClient.listNamespacesFromKeywordIndex() to list namespaces from the keyword (sparse) index - Test 5 now uses a namespace from the sparse index for keywordSearch; skip with clear message if sparse index has no namespaces - Avoids using dense-derived testNamespace for sparse-only query (CodeRabbit feedback)
1 parent 6457376 commit 54d214d

2 files changed

Lines changed: 58 additions & 22 deletions

File tree

scripts/test-search.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -152,35 +152,48 @@ async function test() {
152152
);
153153
}
154154

155-
// Test 5: Keyword (sparse-only) search on pinecone-rag-sparse
155+
// Test 5: Keyword (sparse-only) search — use namespace from sparse index, not dense
156156
let duration5: number | undefined;
157157
let test5Skipped = false;
158-
console.log(`\n🔤 Test 5: Keyword search (sparse-only index)`);
159-
console.log(` Namespace: "${testNamespace}"`);
160-
console.log(` Query: "test query"`);
161-
console.log(` Top K: 3`);
162-
try {
163-
const startTime5 = Date.now();
164-
const results5 = await client.keywordSearch({
165-
query: 'test query',
166-
namespace: testNamespace,
167-
topK: 3,
168-
});
169-
duration5 = Date.now() - startTime5;
170-
console.log(`✅ Keyword search returned ${results5.length} result(s) in ${duration5}ms`);
171-
if (results5.length > 0) {
172-
console.log(
173-
` First result score: ${results5[0].score.toFixed(4)}, reranked: ${results5[0].reranked}`
174-
);
175-
}
176-
} catch (kwError) {
158+
const sparseNamespaces = await client.listNamespacesFromKeywordIndex();
159+
if (sparseNamespaces.length === 0) {
177160
test5Skipped = true;
161+
console.log(`\n🔤 Test 5: Keyword search (sparse-only index)`);
178162
console.log(
179-
`⚠️ Keyword search skipped: ${kwError instanceof Error ? kwError.message : String(kwError)}`
163+
`⚠️ Keyword search skipped: sparse index has no namespaces (or index unavailable).`
180164
);
181165
console.log(
182-
` Ensure PINECONE_SPARSE_INDEX_NAME (e.g. pinecone-rag-sparse) exists and has data.`
166+
` Ensure PINECONE_SPARSE_INDEX_NAME (e.g. pinecone-rag-sparse or rag-hybrid-sparse) exists and has data.`
183167
);
168+
} else {
169+
const sparseTestNamespace = sparseNamespaces[0].namespace;
170+
console.log(`\n🔤 Test 5: Keyword search (sparse-only index)`);
171+
console.log(` Namespace: "${sparseTestNamespace}" (from sparse index)`);
172+
console.log(` Query: "test query"`);
173+
console.log(` Top K: 3`);
174+
try {
175+
const startTime5 = Date.now();
176+
const results5 = await client.keywordSearch({
177+
query: 'test query',
178+
namespace: sparseTestNamespace,
179+
topK: 3,
180+
});
181+
duration5 = Date.now() - startTime5;
182+
console.log(`✅ Keyword search returned ${results5.length} result(s) in ${duration5}ms`);
183+
if (results5.length > 0) {
184+
console.log(
185+
` First result score: ${results5[0].score.toFixed(4)}, reranked: ${results5[0].reranked}`
186+
);
187+
}
188+
} catch (kwError) {
189+
test5Skipped = true;
190+
console.log(
191+
`⚠️ Keyword search skipped: ${kwError instanceof Error ? kwError.message : String(kwError)}`
192+
);
193+
console.log(
194+
` Ensure PINECONE_SPARSE_INDEX_NAME exists and namespace "${sparseTestNamespace}" has data.`
195+
);
196+
}
184197
}
185198

186199
console.log('\n✨ All tests completed successfully!');

src/pinecone-client.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ export class PineconeClient {
157157
return index;
158158
}
159159

160+
/**
161+
* List namespaces present on the keyword (sparse) index used for keyword_search.
162+
* Use this to choose a namespace for sparse-only queries instead of the dense index list.
163+
*/
164+
async listNamespacesFromKeywordIndex(): Promise<
165+
Array<{ namespace: string; recordCount: number }>
166+
> {
167+
try {
168+
const keywordIndex = await this.ensureKeywordIndex();
169+
const stats = keywordIndex.describeIndexStats
170+
? await keywordIndex.describeIndexStats()
171+
: undefined;
172+
const namespaces = stats?.namespaces ?? {};
173+
return Object.entries(namespaces).map(([namespace, info]) => ({
174+
namespace,
175+
recordCount: info?.recordCount ?? 0,
176+
}));
177+
} catch (error) {
178+
logError('Error listing namespaces from keyword index', error);
179+
return [];
180+
}
181+
}
182+
160183
/**
161184
* List all available namespaces with their metadata information
162185
*

0 commit comments

Comments
 (0)