Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Add `filter_result_iterator_t::contains_atleast_one`.

Harpreet Sangare192fa767720
diff --git a/include/filter.h b/include/filter.h
index 9bb78deb..c8d4e3b0 100644
--- a/include/filter.h
+++ b/include/filter.h
@@ -2,6 +2,7 @@
#include <string>
#include <map>
+#include "posting_list.h"
#include "index.h"
class filter_result_iterator_t {
@@ -92,4 +93,6 @@ public:
/// Advances the iterator until the doc value reaches or just overshoots id. The iterator may become invalid during
/// this operation.
void skip_to(uint32_t id);
+
+ bool contains_atleast_one(const void* obj);
};
diff --git a/src/filter.cpp b/src/filter.cpp
index 44393d8d..304799b3 100644
--- a/src/filter.cpp
+++ b/src/filter.cpp
@@ -502,3 +502,45 @@ Option<bool> filter_result_iterator_t::init_status() {
return status;
}
+
+bool filter_result_iterator_t::contains_atleast_one(const void *obj) {
+ if(IS_COMPACT_POSTING(obj)) {
+ compact_posting_list_t* list = COMPACT_POSTING_PTR(obj);
+
+ size_t i = 0;
+ while(i < list->length && valid()) {
+ size_t num_existing_offsets = list->id_offsets[i];
+ size_t existing_id = list->id_offsets[i + num_existing_offsets + 1];
+
+ if (existing_id == seq_id) {
+ return true;
+ }
+
+ // advance smallest value
+ if (existing_id < seq_id) {
+ i += num_existing_offsets + 2;
+ } else {
+ skip_to(existing_id);
+ }
+ }
+ } else {
+ auto list = (posting_list_t*)(obj);
+ posting_list_t::iterator_t it = list->new_iterator();
+
+ while(it.valid() && valid()) {
+ uint32_t id = it.id();
+
+ if(id == seq_id) {
+ return true;
+ }
+
+ if(id < seq_id) {
+ it.skip_to(seq_id);
+ } else {
+ skip_to(id);
+ }
+ }
+ }
+
+ return false;
+}