knnmisc.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Copyright (c) 2023-2026, Manticore Software LTD (https://manticoresearch.com)
  3. // All rights reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License. You should have
  7. // received a copy of the GPL license along with this program; if you
  8. // did not, you can find it at http://www.gnu.org/
  9. //
  10. #pragma once
  11. #include "sphinxstd.h"
  12. #include "sphinxdefs.h"
  13. #include "columnarmisc.h"
  14. #include "indexsettings.h"
  15. #include "secondaryindex.h"
  16. class MemoryWriter_c;
  17. class MemoryReader_c;
  18. class TableEmbeddings_c
  19. {
  20. public:
  21. bool Load ( const CSphString & sAttr, const knn::ModelSettings_t & tSettings, CSphString & sError );
  22. knn::TextToEmbeddings_i * GetModel ( const CSphString & sAttr ) const;
  23. private:
  24. SmallStringHash_T<std::unique_ptr<knn::TextToEmbeddings_i>> m_hModels;
  25. };
  26. class EmbeddingsSrc_c
  27. {
  28. public:
  29. EmbeddingsSrc_c ( int iAttrs );
  30. void Add ( int iAttr, CSphVector<char> & dSrc );
  31. void Remove ( const CSphFixedVector<RowID_t> & dRowMap );
  32. const VecTraits_T<char> Get ( RowID_t tRowID, int iAttr ) const;
  33. bool Has ( RowID_t tRowID, int iAttr ) const;
  34. void Save ( MemoryWriter_c & tWriter ) const;
  35. void Load ( MemoryReader_c & tReader );
  36. private:
  37. CSphVector<CSphVector<CSphVector<char>>> m_dStored;
  38. };
  39. bool IsKnnDist ( const CSphString & sExpr );
  40. const char * GetKnnDistAttrName();
  41. const char * GetKnnDistRescoreAttrName();
  42. void SetupKNNLimit ( CSphQuery & tQuery );
  43. ISphExpr * CreateExpr_KNNDist ( const CSphVector<float> & dAnchor, const CSphColumnInfo & tAttr );
  44. ISphExpr * CreateExpr_KNNDistRescore ( const CSphVector<float> & dAnchor, const CSphColumnInfo & tAttr );
  45. void NormalizeVec ( VecTraits_T<float> & dData );
  46. void AddKNNSettings ( StringBuilder_c & sRes, const CSphColumnInfo & tAttr );
  47. void ReadKNNJson ( bson::Bson_c tRoot, knn::IndexSettings_t & tIS, knn::ModelSettings_t & tMS, CSphString & sKNNFrom );
  48. CSphString FormatKNNConfigStr ( const CSphVector<NamedKNNSettings_t> & dAttrs );
  49. bool ParseKNNConfigStr ( const CSphString & sStr, CSphVector<NamedKNNSettings_t> & dParsed, CSphString & sError );
  50. void FormatKNNSettings ( JsonEscapedBuilder & tOut, const knn::IndexSettings_t & tIndexSettings, const knn::ModelSettings_t & tModelSettings, const CSphString & sKNNFrom );
  51. bool Str2HNSWSimilarity ( const CSphString & sSimilarity, knn::HNSWSimilarity_e & eSimilarity, CSphString * pError = nullptr );
  52. bool Str2Quantization ( const CSphString & sQuantization, knn::Quantization_e & eQuantization, CSphString * pError = nullptr );
  53. std::unique_ptr<knn::Builder_i> BuildCreateKNN ( const ISphSchema & tSchema, int64_t iNumElements, CSphVector<std::pair<PlainOrColumnar_t,int>> & dAttrs, const CSphString & sTmpFilename, CSphString & sError );
  54. void BuildTrainKNN ( RowID_t tRowIDSrc, RowID_t tRowIDDst, const CSphRowitem * pRow, const BYTE * pPool, CSphVector<ScopedTypedIterator_t> & dIterators, const VecTraits_T<PlainOrColumnar_t> & dAttrs, knn::Builder_i & tBuilder );
  55. bool BuildStoreKNN ( RowID_t tRowIDSrc, RowID_t tRowIDDst, const CSphRowitem * pRow, const BYTE * pPool, CSphVector<ScopedTypedIterator_t> & dIterators, const VecTraits_T<PlainOrColumnar_t> & dAttrs, knn::Builder_i & tBuilder );
  56. std::pair<RowidIterator_i *, bool> CreateKNNIterator ( knn::KNN_i * pKNN, const CSphQuery & tQuery, const ISphSchema & tIndexSchema, const ISphSchema & tSorterSchema, CSphString & sError );
  57. RowIteratorsWithEstimates_t CreateKNNIterators ( knn::KNN_i * pKNN, const CSphQuery & tQuery, const ISphSchema & tIndexSchema, const ISphSchema & tSorterSchema, bool & bError, CSphString & sError );
  58. ISphMatchSorter * CreateKNNRescoreSorter ( ISphMatchSorter * pSorter, const KnnSearchSettings_t & tSettings );