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

Tweak locking for filtering.

Kishore Nallan2317c6adb050
diff --git a/include/index.h b/include/index.h
index 7d7e3d05..97c29dec 100644
--- a/include/index.h
+++ b/include/index.h
@@ -418,10 +418,14 @@ public:
art_leaf* get_token_leaf(const std::string & field_name, const unsigned char* token, uint32_t token_len);
- // the following methods are not synchronized because their parent calls are synchronized
-
uint32_t do_filtering(uint32_t** filter_ids_out, const std::vector<filter> & filters) const;
+ void refresh_schemas(const std::vector<field>& new_fields);
+
+ bool field_contains_string(const std::string& field_name, const std::string& value);
+
+ // the following methods are not synchronized because their parent calls are synchronized or they are const/static
+
static Option<uint32_t> validate_index_in_memory(nlohmann::json &document, uint32_t seq_id,
const std::string & default_sorting_field,
const std::unordered_map<std::string, field> & search_schema,
@@ -430,7 +434,5 @@ public:
const std::string& fallback_field_type,
const DIRTY_VALUES& dirty_values);
- void refresh_schemas(const std::vector<field>& new_fields);
-
};
diff --git a/src/index.cpp b/src/index.cpp
index 55355ca1..871d0390 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1044,6 +1044,7 @@ void Index::search_candidates(const uint8_t & field_id, bool field_is_array,
uint32_t Index::do_filtering(uint32_t** filter_ids_out, const std::vector<filter> & filters) const {
//auto begin = std::chrono::high_resolution_clock::now();
+ std::shared_lock lock(mutex);
uint32_t* filter_ids = nullptr;
uint32_t filter_ids_length = 0;
@@ -1487,8 +1488,6 @@ void Index::search(const std::vector<query_tokens_t>& field_query_tokens,
const bool exhaustive_search,
const size_t concurrency) const {
- std::shared_lock lock(mutex);
-
//auto begin = std::chrono::high_resolution_clock::now();
// process the filters
@@ -1496,6 +1495,8 @@ void Index::search(const std::vector<query_tokens_t>& field_query_tokens,
uint32_t* filter_ids = nullptr;
uint32_t filter_ids_length = do_filtering(&filter_ids, filters);
+ std::shared_lock lock(mutex);
+
// we will be removing all curated IDs from organic result ids before running topster
std::set<uint32_t> curated_ids;
std::vector<uint32_t> included_ids;
@@ -3100,3 +3101,16 @@ double Index::transform_for_180th_meridian(Geofence &poly) {
void Index::transform_for_180th_meridian(GeoCoord &point, double offset) {
point.lon = point.lon < 0.0 ? point.lon + offset : point.lon;
}
+
+bool Index::field_contains_string(const std::string& field_name, const std::string& value) {
+ std::shared_lock lock(mutex);
+
+ auto field_it = search_index.find(field_name);
+ if(field_it != search_index.end()) {
+ art_tree* t = field_it->second;
+ art_leaf* leaf = (art_leaf *) art_search(t, (const unsigned char *)value.c_str(), value.size()+1);
+ return (leaf != nullptr);
+ }
+
+ return false;
+}