mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-09-06 04:36:32 +00:00
feat(analytics): add personalization tracking to segment analytics
- Add total_personalized field to SearchAggregator to track personalization usage - Track when search requests include personalization parameters - Include personalization data in analytics JSON output - Maintain clean personalization service interface
This commit is contained in:
@ -23,15 +23,14 @@ impl CohereService {
|
|||||||
query: Option<&str>,
|
query: Option<&str>,
|
||||||
) -> Result<SearchResult, ResponseError> {
|
) -> Result<SearchResult, ResponseError> {
|
||||||
// Extract user context from personalization
|
// Extract user context from personalization
|
||||||
let Some(personalize) = personalize else { return Ok(search_result) };
|
let Some(user_context) = personalize.and_then(|p| p.user_context.as_deref()) else {
|
||||||
let user_context = personalize.user_context.as_deref();
|
return Ok(search_result);
|
||||||
|
};
|
||||||
|
|
||||||
// Build the prompt by merging query and user context
|
// Build the prompt by merging query and user context
|
||||||
let prompt = match (query, user_context) {
|
let prompt = match query {
|
||||||
(Some(q), Some(uc)) => format!("User Context: {}\nQuery: {}", uc, q),
|
Some(q) => format!("User Context: {user_context}\nQuery: {q}"),
|
||||||
(Some(q), None) => q.to_string(),
|
None => format!("User Context: {user_context}"),
|
||||||
(None, Some(uc)) => format!("User Context: {}", uc),
|
|
||||||
(None, None) => return Ok(search_result),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extract documents for reranking
|
// Extract documents for reranking
|
||||||
@ -65,7 +64,6 @@ impl CohereService {
|
|||||||
// Create a mapping from original index to new rank
|
// Create a mapping from original index to new rank
|
||||||
let reranked_indices: Vec<usize> =
|
let reranked_indices: Vec<usize> =
|
||||||
rerank_response.iter().map(|result| result.index as usize).collect();
|
rerank_response.iter().map(|result| result.index as usize).collect();
|
||||||
debug!("Reranked indices: {:?}", reranked_indices);
|
|
||||||
|
|
||||||
// Reorder the hits based on Cohere's reranking
|
// Reorder the hits based on Cohere's reranking
|
||||||
let mut reranked_hits = Vec::new();
|
let mut reranked_hits = Vec::new();
|
||||||
|
@ -92,6 +92,9 @@ pub struct SearchAggregator<Method: AggregateMethod> {
|
|||||||
show_ranking_score_details: bool,
|
show_ranking_score_details: bool,
|
||||||
ranking_score_threshold: bool,
|
ranking_score_threshold: bool,
|
||||||
|
|
||||||
|
// personalization
|
||||||
|
total_personalized: usize,
|
||||||
|
|
||||||
marker: std::marker::PhantomData<Method>,
|
marker: std::marker::PhantomData<Method>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +128,7 @@ impl<Method: AggregateMethod> SearchAggregator<Method> {
|
|||||||
hybrid,
|
hybrid,
|
||||||
ranking_score_threshold,
|
ranking_score_threshold,
|
||||||
locales,
|
locales,
|
||||||
personalize: _,
|
personalize,
|
||||||
} = query;
|
} = query;
|
||||||
|
|
||||||
let mut ret = Self::default();
|
let mut ret = Self::default();
|
||||||
@ -195,6 +198,11 @@ impl<Method: AggregateMethod> SearchAggregator<Method> {
|
|||||||
ret.locales = locales.iter().copied().collect();
|
ret.locales = locales.iter().copied().collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// personalization
|
||||||
|
if personalize.is_some() {
|
||||||
|
ret.total_personalized = 1;
|
||||||
|
}
|
||||||
|
|
||||||
ret.highlight_pre_tag = *highlight_pre_tag != DEFAULT_HIGHLIGHT_PRE_TAG();
|
ret.highlight_pre_tag = *highlight_pre_tag != DEFAULT_HIGHLIGHT_PRE_TAG();
|
||||||
ret.highlight_post_tag = *highlight_post_tag != DEFAULT_HIGHLIGHT_POST_TAG();
|
ret.highlight_post_tag = *highlight_post_tag != DEFAULT_HIGHLIGHT_POST_TAG();
|
||||||
ret.crop_marker = *crop_marker != DEFAULT_CROP_MARKER();
|
ret.crop_marker = *crop_marker != DEFAULT_CROP_MARKER();
|
||||||
@ -282,6 +290,7 @@ impl<Method: AggregateMethod> Aggregate for SearchAggregator<Method> {
|
|||||||
total_used_negative_operator,
|
total_used_negative_operator,
|
||||||
ranking_score_threshold,
|
ranking_score_threshold,
|
||||||
mut locales,
|
mut locales,
|
||||||
|
total_personalized,
|
||||||
marker: _,
|
marker: _,
|
||||||
} = *new;
|
} = *new;
|
||||||
|
|
||||||
@ -365,6 +374,9 @@ impl<Method: AggregateMethod> Aggregate for SearchAggregator<Method> {
|
|||||||
// locales
|
// locales
|
||||||
self.locales.append(&mut locales);
|
self.locales.append(&mut locales);
|
||||||
|
|
||||||
|
// personalization
|
||||||
|
self.total_personalized = self.total_personalized.saturating_add(total_personalized);
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -408,6 +420,7 @@ impl<Method: AggregateMethod> Aggregate for SearchAggregator<Method> {
|
|||||||
total_used_negative_operator,
|
total_used_negative_operator,
|
||||||
ranking_score_threshold,
|
ranking_score_threshold,
|
||||||
locales,
|
locales,
|
||||||
|
total_personalized,
|
||||||
marker: _,
|
marker: _,
|
||||||
} = *self;
|
} = *self;
|
||||||
|
|
||||||
@ -479,6 +492,9 @@ impl<Method: AggregateMethod> Aggregate for SearchAggregator<Method> {
|
|||||||
"show_ranking_score_details": show_ranking_score_details,
|
"show_ranking_score_details": show_ranking_score_details,
|
||||||
"ranking_score_threshold": ranking_score_threshold,
|
"ranking_score_threshold": ranking_score_threshold,
|
||||||
},
|
},
|
||||||
|
"personalization": {
|
||||||
|
"total_personalized": total_personalized,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user