Introduce a struct to compute facets values

This commit is contained in:
Kerollmops 2020-12-28 19:08:53 +01:00
parent 30dae0205e
commit 3b64735058
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
7 changed files with 156 additions and 20 deletions

View file

@ -15,18 +15,18 @@ $('#query, #facet').on('input', function () {
type: "POST",
url: "query",
contentType: 'application/json',
data: JSON.stringify({ 'query': query, 'facetCondition': facet }),
data: JSON.stringify({ 'query': query, 'facetCondition': facet, "facetDistribution": true }),
contentType: 'application/json',
success: function (data, textStatus, request) {
results.innerHTML = '';
let timeSpent = request.getResponseHeader('Time-Ms');
let numberOfDocuments = data.length;
let numberOfDocuments = data.documents.length;
count.innerHTML = `${numberOfDocuments}`;
time.innerHTML = `${timeSpent}ms`;
time.classList.remove('fade-in-out');
for (element of data) {
for (element of data.documents) {
const elem = document.createElement('li');
elem.classList.add("document");

View file

@ -626,6 +626,14 @@ async fn main() -> anyhow::Result<()> {
struct QueryBody {
query: Option<String>,
facet_condition: Option<String>,
facet_distribution: Option<bool>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct Answer {
documents: Vec<Map<String, Value>>,
facets: HashMap<String, Vec<Value>>,
}
let disable_highlighting = opt.disable_highlighting;
@ -649,7 +657,13 @@ async fn main() -> anyhow::Result<()> {
}
}
let SearchResult { found_words, documents_ids } = search.execute().unwrap();
let SearchResult { found_words, candidates, documents_ids } = search.execute().unwrap();
let facets = if query.facet_distribution == Some(true) {
Some(index.facets(&rtxn).candidates(candidates).execute().unwrap())
} else {
None
};
let mut documents = Vec::new();
let fields_ids_map = index.fields_ids_map(&rtxn).unwrap();
@ -674,10 +688,15 @@ async fn main() -> anyhow::Result<()> {
documents.push(object);
}
let answer = Answer {
documents,
facets: facets.unwrap_or_default(),
};
Response::builder()
.header("Content-Type", "application/json")
.header("Time-Ms", before_search.elapsed().as_millis().to_string())
.body(serde_json::to_string(&documents).unwrap())
.body(serde_json::to_string(&answer).unwrap())
});
let index_cloned = index.clone();