mirror of
https://github.com/typesense/typesense.git
synced 2025-05-22 23:06:30 +08:00
Merge pull request #1453 from krunal1313/event_anaylytics
Store analytic rule in suggestion config
This commit is contained in:
commit
9e705ffa47
@ -119,10 +119,11 @@ private:
|
||||
std::string suggestion_collection;
|
||||
std::vector<std::string> query_collections;
|
||||
size_t limit;
|
||||
std::string rule_type;
|
||||
|
||||
void to_json(nlohmann::json& obj) const {
|
||||
obj["name"] = name;
|
||||
obj["type"] = POPULAR_QUERIES_TYPE;
|
||||
obj["type"] = rule_type;
|
||||
obj["params"] = nlohmann::json::object();
|
||||
obj["params"]["limit"] = limit;
|
||||
obj["params"]["source"]["collections"] = query_collections;
|
||||
|
@ -99,6 +99,7 @@ Option<bool> AnalyticsManager::create_queries_index(nlohmann::json &payload, boo
|
||||
suggestion_config.name = suggestion_config_name;
|
||||
suggestion_config.suggestion_collection = suggestion_collection;
|
||||
suggestion_config.limit = limit;
|
||||
suggestion_config.rule_type = payload["type"];
|
||||
|
||||
if(payload["type"] == POPULAR_QUERIES_TYPE) {
|
||||
if (!upsert && popular_queries.count(suggestion_collection) != 0) {
|
||||
|
@ -445,6 +445,78 @@ TEST_F(AnalyticsManagerTest, NoresultsQueries) {
|
||||
ASSERT_EQ(0, noresults_queries.size());
|
||||
}
|
||||
|
||||
TEST_F(AnalyticsManagerTest, SuggestionConfigRule) {
|
||||
nlohmann::json titles_schema = R"({
|
||||
"name": "titles",
|
||||
"fields": [
|
||||
{"name": "title", "type": "string"}
|
||||
]
|
||||
})"_json;
|
||||
|
||||
Collection* titles_coll = collectionManager.create_collection(titles_schema).get();
|
||||
|
||||
|
||||
// create a collection to store suggestions
|
||||
nlohmann::json suggestions_schema = R"({
|
||||
"name": "top_queries",
|
||||
"fields": [
|
||||
{"name": "q", "type": "string" },
|
||||
{"name": "count", "type": "int32" }
|
||||
]
|
||||
})"_json;
|
||||
|
||||
Collection* suggestions_coll = collectionManager.create_collection(suggestions_schema).get();
|
||||
|
||||
//add popular quries rule
|
||||
nlohmann::json analytics_rule = R"({
|
||||
"name": "top_search_queries",
|
||||
"type": "popular_queries",
|
||||
"params": {
|
||||
"limit": 100,
|
||||
"source": {
|
||||
"collections": ["titles"]
|
||||
},
|
||||
"destination": {
|
||||
"collection": "top_queries"
|
||||
}
|
||||
}
|
||||
})"_json;
|
||||
|
||||
auto create_op = analyticsManager.create_rule(analytics_rule, false, true);
|
||||
ASSERT_TRUE(create_op.ok());
|
||||
|
||||
//add nohits rule
|
||||
analytics_rule = R"({
|
||||
"name": "search_queries",
|
||||
"type": "nohits_queries",
|
||||
"params": {
|
||||
"limit": 100,
|
||||
"source": {
|
||||
"collections": ["titles"]
|
||||
},
|
||||
"destination": {
|
||||
"collection": "top_queries"
|
||||
}
|
||||
}
|
||||
})"_json;
|
||||
|
||||
create_op = analyticsManager.create_rule(analytics_rule, false, true);
|
||||
ASSERT_TRUE(create_op.ok());
|
||||
|
||||
auto rules = analyticsManager.list_rules().get()["rules"];
|
||||
ASSERT_EQ(2, rules.size());
|
||||
ASSERT_EQ("search_queries", rules[0]["name"]);
|
||||
ASSERT_EQ("nohits_queries", rules[0]["type"]);
|
||||
ASSERT_EQ("top_search_queries", rules[1]["name"]);
|
||||
ASSERT_EQ("popular_queries", rules[1]["type"]);
|
||||
|
||||
//try deleting rules
|
||||
ASSERT_TRUE(analyticsManager.remove_rule("search_queries").ok());
|
||||
ASSERT_TRUE(analyticsManager.remove_rule("top_search_queries").ok());
|
||||
rules = analyticsManager.list_rules().get()["rules"];
|
||||
ASSERT_EQ(0, rules.size());
|
||||
}
|
||||
|
||||
TEST_F(AnalyticsManagerTest, QueryHitsCount) {
|
||||
//flush all events from analytics store
|
||||
analyticsManager.resetAnalyticsStore();
|
||||
|
Loading…
x
Reference in New Issue
Block a user