You've already forked AstralRinth
forked from didirus/AstralRinth
Allow for API user to change the amount of mods responded with in search (#61)
* Add more info to search route: * Run formatter * Allow for API user to change the amount of mods responded with in search * Refactor SearchResults * Fix searchresults usage
This commit is contained in:
@@ -130,4 +130,5 @@ pub struct SearchRequest {
|
|||||||
pub version: Option<String>,
|
pub version: Option<String>,
|
||||||
pub offset: Option<String>,
|
pub offset: Option<String>,
|
||||||
pub index: Option<String>,
|
pub index: Option<String>,
|
||||||
|
pub limit: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use meilisearch_sdk::search::Query;
|
|||||||
use serde::ser::SerializeStruct;
|
use serde::ser::SerializeStruct;
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::cmp::min;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub mod indexing;
|
pub mod indexing;
|
||||||
@@ -86,10 +87,7 @@ pub struct SearchResults {
|
|||||||
pub hits: Vec<ResultSearchMod>,
|
pub hits: Vec<ResultSearchMod>,
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
pub limit: usize,
|
pub limit: usize,
|
||||||
pub nb_hits: usize,
|
pub total_hits: usize,
|
||||||
pub exhaustive_nb_hits: bool,
|
|
||||||
pub processing_time_ms: usize,
|
|
||||||
pub query: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
@@ -144,13 +142,16 @@ pub async fn search_for_mod(info: &SearchRequest) -> Result<SearchResults, Searc
|
|||||||
|
|
||||||
let offset = info.offset.as_deref().unwrap_or("0").parse()?;
|
let offset = info.offset.as_deref().unwrap_or("0").parse()?;
|
||||||
let index = info.index.as_deref().unwrap_or("relevance");
|
let index = info.index.as_deref().unwrap_or("relevance");
|
||||||
|
let limit = info.limit.unwrap_or(10);
|
||||||
let search_query: &str = info
|
let search_query: &str = info
|
||||||
.query
|
.query
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.filter(|s| !s.is_empty())
|
.filter(|s| !s.is_empty())
|
||||||
.unwrap_or("{}{}{}");
|
.unwrap_or("{}{}{}");
|
||||||
|
|
||||||
let mut query = Query::new(search_query).with_limit(10).with_offset(offset);
|
let mut query = Query::new(search_query)
|
||||||
|
.with_limit(min(100, limit))
|
||||||
|
.with_offset(offset);
|
||||||
|
|
||||||
if !filters.is_empty() {
|
if !filters.is_empty() {
|
||||||
query = query.with_filters(&filters);
|
query = query.with_filters(&filters);
|
||||||
@@ -170,9 +171,6 @@ pub async fn search_for_mod(info: &SearchRequest) -> Result<SearchResults, Searc
|
|||||||
hits: results.hits,
|
hits: results.hits,
|
||||||
offset: results.offset,
|
offset: results.offset,
|
||||||
limit: results.limit,
|
limit: results.limit,
|
||||||
nb_hits: results.nb_hits,
|
total_hits: results.nb_hits,
|
||||||
exhaustive_nb_hits: results.exhaustive_nb_hits,
|
|
||||||
processing_time_ms: results.processing_time_ms,
|
|
||||||
query: results.query,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user