3030#include " knowhere/comp/index_param.h"
3131#include " knowhere/expected.h"
3232#include " knowhere/log.h"
33+ #include " knowhere/prometheus_client.h"
3334#include " knowhere/sparse_utils.h"
3435#include " knowhere/utils.h"
3536
@@ -94,6 +95,18 @@ template <typename DType, typename QType, InvertedIndexAlgo algo, bool mmapped =
9495class InvertedIndex : public BaseInvertedIndex <DType> {
9596 public:
9697 explicit InvertedIndex (SparseMetricType metric_type) : metric_type_(metric_type) {
98+ #if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
99+ // for now, use timestamp as index_id
100+ index_id_ = std::to_string (
101+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now ().time_since_epoch ())
102+ .count ());
103+ index_size_gauge_ =
104+ &sparse_inverted_index_size_family.Add ({{" index_id" , index_id_}, {" index_type" , " inverted" }});
105+ index_dataset_nnz_len_histogram_ =
106+ &sparse_dataset_nnz_len_family.Add ({{" index_id" , index_id_}, {" index_type" , " inverted" }}, defaultBuckets);
107+ index_posting_list_len_histogram_ = &sparse_inverted_index_posting_list_len_family.Add (
108+ {{" index_id" , index_id_}, {" index_type" , " inverted" }}, defaultBuckets);
109+ #endif
97110 }
98111
99112 ~InvertedIndex () override {
@@ -112,6 +125,17 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
112125 map_fd_ = -1 ;
113126 }
114127 }
128+ #if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
129+ if (index_size_gauge_ != nullptr ) {
130+ sparse_inverted_index_size_family.Remove (index_size_gauge_);
131+ }
132+ if (index_dataset_nnz_len_histogram_ != nullptr ) {
133+ sparse_dataset_nnz_len_family.Remove (index_dataset_nnz_len_histogram_);
134+ }
135+ if (index_posting_list_len_histogram_ != nullptr ) {
136+ sparse_inverted_index_posting_list_len_family.Remove (index_posting_list_len_histogram_);
137+ }
138+ #endif
115139 }
116140
117141 template <typename U>
@@ -248,6 +272,10 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
248272 }
249273
250274 for (int64_t i = 0 ; i < rows; ++i) {
275+ if (i % (rows / 10 ) == 0 ) {
276+ LOG_KNOWHERE_INFO_ << " Loading progress: " << (i * 100 / rows) << " %" ;
277+ }
278+
251279 size_t count;
252280 readBinaryPOD (reader, count);
253281 SparseRow<DType> raw_row;
@@ -261,10 +289,21 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
261289 }
262290 }
263291 add_row_to_index (raw_row, i);
292+ #if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
293+ index_dataset_nnz_len_histogram_->Observe (count);
294+ #endif
295+ }
296+
297+ #if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
298+ for (size_t i = 0 ; i < dim_map_.size (); ++i) {
299+ index_posting_list_len_histogram_->Observe (inverted_index_ids_[i].size ());
264300 }
301+ index_size_gauge_->Set ((double )size () / 1024.0 / 1024.0 );
302+ #endif
265303
266304 n_rows_internal_ = rows;
267305
306+ LOG_KNOWHERE_INFO_ << " Loading progress: 100%" ;
268307 return Status::success;
269308 }
270309
@@ -921,7 +960,18 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
921960 }
922961 inverted_index_ids_[dim_it->second ].emplace_back (vec_id);
923962 inverted_index_vals_[dim_it->second ].emplace_back (get_quant_val (val));
924- if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE ) {
963+ }
964+ // update max_score_in_dim_
965+ if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE ) {
966+ for (size_t j = 0 ; j < row.size (); ++j) {
967+ auto [dim, val] = row[j];
968+ if (val == 0 ) {
969+ continue ;
970+ }
971+ auto dim_it = dim_map_.find (dim);
972+ if (dim_it == dim_map_.cend ()) {
973+ throw std::runtime_error (" unexpected vector dimension in InvertedIndex" );
974+ }
925975 auto score = static_cast <float >(val);
926976 if (metric_type_ == SparseMetricType::METRIC_BM25 ) {
927977 score = bm25_params_->max_score_computer (val, row_sum);
@@ -984,6 +1034,12 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
9841034
9851035 std::unique_ptr<BM25Params> bm25_params_;
9861036
1037+ #if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
1038+ std::string index_id_{};
1039+ prometheus::Gauge* index_size_gauge_{nullptr };
1040+ prometheus::Histogram* index_dataset_nnz_len_histogram_{nullptr };
1041+ prometheus::Histogram* index_posting_list_len_histogram_{nullptr };
1042+ #endif
9871043}; // class InvertedIndex
9881044
9891045} // namespace knowhere::sparse
0 commit comments