mirror of
https://github.com/typesense/typesense.git
synced 2025-05-21 06:02:26 +08:00
Fix nohits query aggregation.
This commit is contained in:
parent
5722a9dddd
commit
f1e1485d6b
@ -1837,17 +1837,17 @@ 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.count("found") != 0 && result["found"].get<size_t>() != 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<std::string>());
|
||||
|
||||
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<size_t>() == 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<size_t>() != 0) {
|
||||
const std::string& expanded_query = Tokenizer::normalize_ascii_no_spaces(
|
||||
result["request_params"]["first_q"].get<std::string>());
|
||||
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"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -672,6 +672,61 @@ TEST_F(CollectionManagerTest, QuerySuggestionsShouldBeTrimmed) {
|
||||
collectionManager.drop_collection("coll1");
|
||||
}
|
||||
|
||||
TEST_F(CollectionManagerTest, NoHitsQueryAggregation) {
|
||||
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* 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<std::string, std::string> 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::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());
|
||||
|
||||
// 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;
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <vector>
|
||||
#include <collection_manager.h>
|
||||
#include <core_api.h>
|
||||
#include <analytics_manager.h>
|
||||
#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<std::string, std::string> headers;
|
||||
|
Loading…
x
Reference in New Issue
Block a user