@@ -38,13 +38,23 @@ async function executeKeywordSearch(params: {
3838} ) : Promise < KeywordSearchResponse > {
3939 const { query_text, namespace, top_k, metadata_filter, fields } = params ;
4040
41- if ( ! query_text . trim ( ) ) {
41+ const normalizedQuery = query_text . trim ( ) ;
42+ const normalizedNamespace = namespace ?. trim ( ) ?? '' ;
43+
44+ if ( ! normalizedQuery ) {
4245 return {
4346 status : 'error' ,
4447 message : 'Query text cannot be empty' ,
4548 } ;
4649 }
4750
51+ if ( ! normalizedNamespace ) {
52+ return {
53+ status : 'error' ,
54+ message : 'Namespace cannot be empty' ,
55+ } ;
56+ }
57+
4858 if ( metadata_filter ) {
4959 const filterError = validateMetadataFilter ( metadata_filter ) ;
5060 if ( filterError ) {
@@ -54,19 +64,21 @@ async function executeKeywordSearch(params: {
5464
5565 const client = getPineconeClient ( ) ;
5666 const results = await client . keywordSearch ( {
57- query : query_text . trim ( ) ,
58- namespace,
67+ query : normalizedQuery ,
68+ namespace : normalizedNamespace ,
5969 topK : top_k ,
6070 metadataFilter : metadata_filter ,
6171 fields : fields ?. length ? fields : undefined ,
6272 } ) ;
6373
64- const formattedResults = formatQueryResultRows ( results , { namespace } ) ;
74+ const formattedResults = formatQueryResultRows ( results , {
75+ namespace : normalizedNamespace ,
76+ } ) ;
6577
6678 const response : KeywordSearchResponse = {
6779 status : 'success' ,
68- query : query_text ,
69- namespace,
80+ query : normalizedQuery ,
81+ namespace : normalizedNamespace ,
7082 index : client . getSparseIndexName ( ) ,
7183 metadata_filter : metadata_filter ,
7284 result_count : formattedResults . length ,
0 commit comments