From f1e1485d6b347b9028e06ba746e7033f9cd5ce3e Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 20 Feb 2024 20:45:49 +0530 Subject: [PATCH] Fix nohits query aggregation. --- src/collection_manager.cpp | 20 ++++++------ test/collection_manager_test.cpp | 55 ++++++++++++++++++++++++++++++++ test/core_api_utils_test.cpp | 3 +- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp index 5e5a073c..7f1a8aa2 100644 --- a/src/collection_manager.cpp +++ b/src/collection_manager.cpp @@ -1837,17 +1837,17 @@ Option CollectionManager::do_search(std::map& re nlohmann::json result = result_op.get(); if(Config::get_instance().get_enable_search_analytics()) { - if(result.count("found") != 0 && result["found"].get() != 0) { + if(result.contains("found")) { std::string analytics_query = Tokenizer::normalize_ascii_no_spaces(raw_query); - const std::string& expanded_query = Tokenizer::normalize_ascii_no_spaces( - result["request_params"]["first_q"].get()); - - AnalyticsManager::get_instance().add_suggestion(orig_coll_name, analytics_query, expanded_query, - true, req_params["x-typesense-user-id"]); - } else if(result.contains("found") == 0 && result["found"].get() == 0) { - std::string analytics_query = Tokenizer::normalize_ascii_no_spaces(raw_query); - AnalyticsManager::get_instance().add_nohits_query(orig_coll_name, analytics_query, - true, req_params["x-typesense-user-id"]); + if(result["found"].get() != 0) { + const std::string& expanded_query = Tokenizer::normalize_ascii_no_spaces( + result["request_params"]["first_q"].get()); + AnalyticsManager::get_instance().add_suggestion(orig_coll_name, analytics_query, expanded_query, + true, req_params["x-typesense-user-id"]); + } else { + AnalyticsManager::get_instance().add_nohits_query(orig_coll_name, analytics_query, + true, req_params["x-typesense-user-id"]); + } } } diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp index 538c784d..86482919 100644 --- a/test/collection_manager_test.cpp +++ b/test/collection_manager_test.cpp @@ -672,6 +672,61 @@ TEST_F(CollectionManagerTest, QuerySuggestionsShouldBeTrimmed) { collectionManager.drop_collection("coll1"); } +TEST_F(CollectionManagerTest, NoHitsQueryAggregation) { + std::vector fields = {field("title", field_types::STRING, false, false, true, "", -1, 1), + field("year", field_types::INT32, false), + field("points", field_types::INT32, false),}; + + Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get(); + + nlohmann::json doc1; + doc1["id"] = "0"; + doc1["title"] = "Tom Sawyer"; + doc1["year"] = 1876; + doc1["points"] = 100; + + ASSERT_TRUE(coll1->add(doc1.dump()).ok()); + + Config::get_instance().set_enable_search_analytics(true); + + nlohmann::json analytics_rule = R"({ + "name": "nohits_search_queries", + "type": "nohits_queries", + "params": { + "limit": 100, + "source": { + "collections": ["coll1"] + }, + "destination": { + "collection": "nohits_queries" + } + } + })"_json; + + auto create_op = AnalyticsManager::get_instance().create_rule(analytics_rule, false, true); + ASSERT_TRUE(create_op.ok()); + + nlohmann::json embedded_params; + std::map req_params; + req_params["collection"] = "coll1"; + req_params["q"] = "foobarbaz"; + req_params["query_by"] = "title"; + + std::string json_res; + auto now_ts = std::chrono::duration_cast( + 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()); + + // check that no hits queries have been populated + auto nohits_queries = AnalyticsManager::get_instance().get_nohits_queries(); + ASSERT_EQ(1, nohits_queries["nohits_queries"]->get_user_prefix_queries()[""].size()); + ASSERT_EQ("foobarbaz", nohits_queries["nohits_queries"]->get_user_prefix_queries()[""][0].query); + + collectionManager.drop_collection("coll1"); +} + TEST_F(CollectionManagerTest, RestoreAutoSchemaDocsOnRestart) { Collection *coll1; diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp index e610ccfc..ba07c356 100644 --- a/test/core_api_utils_test.cpp +++ b/test/core_api_utils_test.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "core_api_utils.h" #include "raft_server.h" #include "conversation_model_manager.h" @@ -1051,8 +1052,6 @@ TEST_F(CoreAPIUtilsTest, ExportIncludeExcludeFieldsWithFilter) { collectionManager.drop_collection("coll1"); } - - TEST_F(CoreAPIUtilsTest, TestProxy) { std::string res; std::unordered_map headers;