Browse Source

added optional ef argument to knn function at the SphinxQL and JSON interfaces

Stas Klinov 1 year ago
parent
commit
74b44de181
5 changed files with 13 additions and 4 deletions
  1. 1 1
      src/knnmisc.cpp
  2. 4 2
      src/searchdsql.cpp
  3. 1 0
      src/sphinx.h
  4. 1 0
      src/sphinxjsonquery.cpp
  5. 6 1
      src/sphinxql.y

+ 1 - 1
src/knnmisc.cpp

@@ -384,7 +384,7 @@ std::pair<RowidIterator_i *, bool> CreateKNNIterator ( knn::KNN_i * pKNN, const
 		NormalizeVec(dPoint);
 
 	std::string sErrorSTL;
-	knn::Iterator_i * pIterator = pKNN->CreateIterator ( pKNNAttr->m_sName.cstr(), { dPoint.Begin(), (size_t)dPoint.GetLength() }, tQuery.m_iKNNK, sErrorSTL );
+	knn::Iterator_i * pIterator = pKNN->CreateIterator ( pKNNAttr->m_sName.cstr(), { dPoint.Begin(), (size_t)dPoint.GetLength() }, tQuery.m_iKNNK, tQuery.m_iKnnEf, sErrorSTL );
 	if ( !pIterator )
 	{
 		sError = sErrorSTL.c_str();

+ 4 - 2
src/searchdsql.cpp

@@ -293,7 +293,7 @@ public:
 	bool			AddSchemaItem ( SqlNode_t * pNode );
 	bool			SetMatch ( const SqlNode_t & tValue );
 	bool			AddMatch ( const SqlNode_t & tValue, const SqlNode_t & tIndex );
-	bool			SetKNN ( const SqlNode_t & tAttr, const SqlNode_t & tK, const SqlNode_t & tValues );
+	bool			SetKNN ( const SqlNode_t & tAttr, const SqlNode_t & tK, const SqlNode_t & tValues, const SqlNode_t * pEf );
 	void			AddConst ( int iList, const SqlNode_t& tValue );
 	void			SetLocalStatement ( const SqlNode_t & tName );
 	bool			AddFloatRangeFilter ( const SqlNode_t & tAttr, float fMin, float fMax, bool bHasEqual, bool bExclude=false );
@@ -1236,10 +1236,12 @@ bool SqlParser_c::AddMatch ( const SqlNode_t & tValue, const SqlNode_t & tIndex
 	return true;
 }
 
-bool SqlParser_c::SetKNN ( const SqlNode_t & tAttr, const SqlNode_t & tK, const SqlNode_t & tValues )
+bool SqlParser_c::SetKNN ( const SqlNode_t & tAttr, const SqlNode_t & tK, const SqlNode_t & tValues, const SqlNode_t * pEf )
 {
 	ToString ( m_pQuery->m_sKNNAttr, tAttr );
 	m_pQuery->m_iKNNK = tK.GetValueInt();
+	if ( pEf )
+		m_pQuery->m_iKnnEf = pEf->GetValueInt();
 	auto pValues = tValues.m_pValues;
 	if ( pValues )
 	{

+ 1 - 0
src/sphinx.h

@@ -512,6 +512,7 @@ struct CSphQuery
 
 	CSphString		m_sKNNAttr;					///< which attr to use for KNN search (enables KNN if not empty)
 	int				m_iKNNK = 0;				///< KNN K
+	int				m_iKnnEf = 0;				///< KNN ef
 	CSphVector<float> m_dKNNVec;				///< KNN anchor vector
 
 	bool			m_bSortKbuffer = false;		///< whether to use PQ or K-buffer sorting algorithm

+ 1 - 0
src/sphinxjsonquery.cpp

@@ -1026,6 +1026,7 @@ static bool ParseKNNQuery ( const JsonObj_c & tJson, CSphQuery & tQuery, CSphStr
 
 	if ( !tJson.FetchStrItem ( tQuery.m_sKNNAttr, "field", sError ) )	return false;
 	if ( !tJson.FetchIntItem ( tQuery.m_iKNNK, "k", sError ) )			return false;
+	if ( !tJson.FetchIntItem ( tQuery.m_iKnnEf, "ef", sError, true ) )		return false;
 
 	JsonObj_c tQueryVec = tJson.GetArrayItem ( "query_vector", sError );
 	if ( !tQueryVec )

+ 6 - 1
src/sphinxql.y

@@ -697,7 +697,12 @@ on_clause:
 knn_item:
 	TOK_KNN '(' ident ',' const_int ',' '(' const_list ')' ')'
 		{
-			if ( !pParser->SetKNN ( $3, $5, $8 ) )
+			if ( !pParser->SetKNN ( $3, $5, $8, nullptr ) )
+				YYERROR;
+		}
+	| TOK_KNN '(' ident ',' const_int ',' '(' const_list ')' ',' const_int ')'
+		{
+			if ( !pParser->SetKNN ( $3, $5, $8, &$11 ) )
 				YYERROR;
 		}
 	;