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 flag hide_from_analytics (#1737)

Krunal Gandhief674a80f9a1
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index ce205811..cab255f3 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1520,6 +1520,9 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
const char *SYNONYM_PREFIX = "synonym_prefix";
const char *SYNONYM_NUM_TYPOS = "synonym_num_typos";
+ //query time flag to enable analyitcs for that query
+ const char *ENABLE_ANALYTICS = "enable_analytics";
+
// enrich params with values from embedded params
for(auto& item: embedded_params.items()) {
if(item.key() == "expires_at") {
@@ -1663,7 +1666,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
std::string override_tags;
std::string voice_query;
-
+ bool enable_analytics = true;
std::unordered_map<std::string, size_t*> unsigned_int_values = {
{MIN_LEN_1TYPO, &min_len_1typo},
@@ -1725,6 +1728,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
{ENABLE_LAZY_FILTER, &enable_lazy_filter},
{ENABLE_TYPOS_FOR_ALPHA_NUMERICAL_TOKENS, &enable_typos_for_alpha_numerical_tokens},
{FILTER_CURATED_HITS, &filter_curated_hits_option},
+ {ENABLE_ANALYTICS, &enable_analytics},
};
std::unordered_map<std::string, std::vector<std::string>*> str_list_values = {
@@ -1959,7 +1963,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
nlohmann::json result = result_op.get();
if(Config::get_instance().get_enable_search_analytics()) {
- if(result.contains("found")) {
+ if(enable_analytics && result.contains("found")) {
std::string analytics_query = Tokenizer::normalize_ascii_no_spaces(raw_query);
if(result["found"].get<size_t>() != 0) {
const std::string& expanded_query = Tokenizer::normalize_ascii_no_spaces(
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 4d3265d9..86b13819 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -2068,4 +2068,67 @@ TEST_F(CollectionManagerTest, CollectionPagination) {
collection_op = collectionManager.get_collections(limit, offset);
ASSERT_FALSE(collection_op.ok());
ASSERT_EQ("Invalid offset param.", collection_op.error());
+}
+
+TEST_F(CollectionManagerTest, HideQueryFromAnalytics) {
+ std::vector<field> fields = {field("title", field_types::STRING, false, false, true, "", -1, 1),
+ field("year", field_types::INT32, false),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll3 = collectionManager.create_collection("coll3", 1, fields, "points").get();
+
+ nlohmann::json doc1;
+ doc1["id"] = "0";
+ doc1["title"] = "Tom Sawyer";
+ doc1["year"] = 1876;
+ doc1["points"] = 100;
+
+ ASSERT_TRUE(coll3->add(doc1.dump()).ok());
+
+ Config::get_instance().set_enable_search_analytics(true);
+
+ nlohmann::json analytics_rule = R"({
+ "name": "hide_search_queries",
+ "type": "popular_queries",
+ "params": {
+ "limit": 100,
+ "source": {
+ "collections": ["coll3"]
+ },
+ "destination": {
+ "collection": "top_queries2"
+ }
+ }
+ })"_json;
+
+ auto create_op = AnalyticsManager::get_instance().create_rule(analytics_rule, false, true);
+ ASSERT_TRUE(create_op.ok());
+
+ nlohmann::json embedded_params;
+ std::string json_res;
+
+ std::map<std::string, std::string> req_params;
+ req_params["collection"] = "coll3";
+ req_params["q"] = "tom";
+ req_params["query_by"] = "title";
+ req_params["enable_analytics"] = "false";
+
+ auto now_ts = std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count();
+
+ auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_TRUE(search_op.ok());
+
+ auto popular_queries = AnalyticsManager::get_instance().get_popular_queries();
+ ASSERT_EQ(0, popular_queries["top_queries2"]->get_user_prefix_queries().size());
+
+ req_params["enable_analytics"] = "true";
+
+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);
+ ASSERT_TRUE(search_op.ok());
+
+ popular_queries = AnalyticsManager::get_instance().get_popular_queries();
+ ASSERT_EQ(1, popular_queries["top_queries2"]->get_user_prefix_queries().size());
+
+ collectionManager.drop_collection("coll3");
}
\ No newline at end of file