mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-30 00:55:00 +08:00
Simplify the const default values
This commit is contained in:
parent
b83455f345
commit
10d3b367dc
@ -3,8 +3,8 @@ use log::debug;
|
|||||||
use meilisearch_auth::IndexSearchRules;
|
use meilisearch_auth::IndexSearchRules;
|
||||||
use meilisearch_error::ResponseError;
|
use meilisearch_error::ResponseError;
|
||||||
use meilisearch_lib::index::{
|
use meilisearch_lib::index::{
|
||||||
default_crop_length, default_crop_marker, default_highlight_post_tag,
|
SearchQuery, DEFAULT_CROP_LENGTH, DEFAULT_CROP_MARKER, DEFAULT_HIGHLIGHT_POST_TAG,
|
||||||
default_highlight_pre_tag, SearchQuery, DEFAULT_SEARCH_LIMIT,
|
DEFAULT_HIGHLIGHT_PRE_TAG, DEFAULT_SEARCH_LIMIT,
|
||||||
};
|
};
|
||||||
use meilisearch_lib::MeiliSearch;
|
use meilisearch_lib::MeiliSearch;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -30,7 +30,7 @@ pub struct SearchQueryGet {
|
|||||||
limit: Option<usize>,
|
limit: Option<usize>,
|
||||||
attributes_to_retrieve: Option<String>,
|
attributes_to_retrieve: Option<String>,
|
||||||
attributes_to_crop: Option<String>,
|
attributes_to_crop: Option<String>,
|
||||||
#[serde(default = "default_crop_length")]
|
#[serde(default = "DEFAULT_CROP_LENGTH")]
|
||||||
crop_length: usize,
|
crop_length: usize,
|
||||||
attributes_to_highlight: Option<String>,
|
attributes_to_highlight: Option<String>,
|
||||||
filter: Option<String>,
|
filter: Option<String>,
|
||||||
@ -38,11 +38,11 @@ pub struct SearchQueryGet {
|
|||||||
#[serde(default = "Default::default")]
|
#[serde(default = "Default::default")]
|
||||||
show_matches_position: bool,
|
show_matches_position: bool,
|
||||||
facets: Option<String>,
|
facets: Option<String>,
|
||||||
#[serde(default = "default_highlight_pre_tag")]
|
#[serde(default = "DEFAULT_HIGHLIGHT_PRE_TAG")]
|
||||||
highlight_pre_tag: String,
|
highlight_pre_tag: String,
|
||||||
#[serde(default = "default_highlight_post_tag")]
|
#[serde(default = "DEFAULT_HIGHLIGHT_POST_TAG")]
|
||||||
highlight_post_tag: String,
|
highlight_post_tag: String,
|
||||||
#[serde(default = "default_crop_marker")]
|
#[serde(default = "DEFAULT_CROP_MARKER")]
|
||||||
crop_marker: String,
|
crop_marker: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ impl From<SearchQueryGet> for SearchQuery {
|
|||||||
Self {
|
Self {
|
||||||
q: other.q,
|
q: other.q,
|
||||||
offset: other.offset,
|
offset: other.offset,
|
||||||
limit: other.limit.unwrap_or(DEFAULT_SEARCH_LIMIT),
|
limit: other.limit.unwrap_or_else(DEFAULT_SEARCH_LIMIT),
|
||||||
attributes_to_retrieve,
|
attributes_to_retrieve,
|
||||||
attributes_to_crop,
|
attributes_to_crop,
|
||||||
crop_length: other.crop_length,
|
crop_length: other.crop_length,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
pub use search::{
|
pub use search::{
|
||||||
default_crop_length, default_crop_marker, default_highlight_post_tag,
|
SearchQuery, SearchResult, DEFAULT_CROP_LENGTH, DEFAULT_CROP_MARKER,
|
||||||
default_highlight_pre_tag, SearchQuery, SearchResult, DEFAULT_CROP_LENGTH, DEFAULT_CROP_MARKER,
|
|
||||||
DEFAULT_HIGHLIGHT_POST_TAG, DEFAULT_HIGHLIGHT_PRE_TAG, DEFAULT_SEARCH_LIMIT,
|
DEFAULT_HIGHLIGHT_POST_TAG, DEFAULT_HIGHLIGHT_PRE_TAG, DEFAULT_SEARCH_LIMIT,
|
||||||
};
|
};
|
||||||
pub use updates::{apply_settings_to_builder, Checked, Facets, Settings, Unchecked};
|
pub use updates::{apply_settings_to_builder, Checked, Facets, Settings, Unchecked};
|
||||||
|
@ -20,30 +20,11 @@ use super::index::Index;
|
|||||||
pub type Document = serde_json::Map<String, Value>;
|
pub type Document = serde_json::Map<String, Value>;
|
||||||
type MatchesPosition = BTreeMap<String, Vec<MatchBounds>>;
|
type MatchesPosition = BTreeMap<String, Vec<MatchBounds>>;
|
||||||
|
|
||||||
pub const DEFAULT_SEARCH_LIMIT: usize = 20;
|
pub const DEFAULT_SEARCH_LIMIT: fn() -> usize = || 20;
|
||||||
const fn default_search_limit() -> usize {
|
pub const DEFAULT_CROP_LENGTH: fn() -> usize = || 10;
|
||||||
DEFAULT_SEARCH_LIMIT
|
pub const DEFAULT_CROP_MARKER: fn() -> String = || "…".to_string();
|
||||||
}
|
pub const DEFAULT_HIGHLIGHT_PRE_TAG: fn() -> String = || "<em>".to_string();
|
||||||
|
pub const DEFAULT_HIGHLIGHT_POST_TAG: fn() -> String = || "</em>".to_string();
|
||||||
pub const DEFAULT_CROP_LENGTH: usize = 10;
|
|
||||||
pub const fn default_crop_length() -> usize {
|
|
||||||
DEFAULT_CROP_LENGTH
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const DEFAULT_CROP_MARKER: &str = "…";
|
|
||||||
pub fn default_crop_marker() -> String {
|
|
||||||
DEFAULT_CROP_MARKER.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const DEFAULT_HIGHLIGHT_PRE_TAG: &str = "<em>";
|
|
||||||
pub fn default_highlight_pre_tag() -> String {
|
|
||||||
DEFAULT_HIGHLIGHT_PRE_TAG.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const DEFAULT_HIGHLIGHT_POST_TAG: &str = "</em>";
|
|
||||||
pub fn default_highlight_post_tag() -> String {
|
|
||||||
DEFAULT_HIGHLIGHT_POST_TAG.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The maximimum number of results that the engine
|
/// The maximimum number of results that the engine
|
||||||
/// will be able to return in one search call.
|
/// will be able to return in one search call.
|
||||||
@ -54,11 +35,11 @@ pub const HARD_RESULT_LIMIT: usize = 1000;
|
|||||||
pub struct SearchQuery {
|
pub struct SearchQuery {
|
||||||
pub q: Option<String>,
|
pub q: Option<String>,
|
||||||
pub offset: Option<usize>,
|
pub offset: Option<usize>,
|
||||||
#[serde(default = "default_search_limit")]
|
#[serde(default = "DEFAULT_SEARCH_LIMIT")]
|
||||||
pub limit: usize,
|
pub limit: usize,
|
||||||
pub attributes_to_retrieve: Option<BTreeSet<String>>,
|
pub attributes_to_retrieve: Option<BTreeSet<String>>,
|
||||||
pub attributes_to_crop: Option<Vec<String>>,
|
pub attributes_to_crop: Option<Vec<String>>,
|
||||||
#[serde(default = "default_crop_length")]
|
#[serde(default = "DEFAULT_CROP_LENGTH")]
|
||||||
pub crop_length: usize,
|
pub crop_length: usize,
|
||||||
pub attributes_to_highlight: Option<HashSet<String>>,
|
pub attributes_to_highlight: Option<HashSet<String>>,
|
||||||
// Default to false
|
// Default to false
|
||||||
@ -67,11 +48,11 @@ pub struct SearchQuery {
|
|||||||
pub filter: Option<Value>,
|
pub filter: Option<Value>,
|
||||||
pub sort: Option<Vec<String>>,
|
pub sort: Option<Vec<String>>,
|
||||||
pub facets: Option<Vec<String>>,
|
pub facets: Option<Vec<String>>,
|
||||||
#[serde(default = "default_highlight_pre_tag")]
|
#[serde(default = "DEFAULT_HIGHLIGHT_PRE_TAG")]
|
||||||
pub highlight_pre_tag: String,
|
pub highlight_pre_tag: String,
|
||||||
#[serde(default = "default_highlight_post_tag")]
|
#[serde(default = "DEFAULT_HIGHLIGHT_POST_TAG")]
|
||||||
pub highlight_post_tag: String,
|
pub highlight_post_tag: String,
|
||||||
#[serde(default = "default_crop_marker")]
|
#[serde(default = "DEFAULT_CROP_MARKER")]
|
||||||
pub crop_marker: String,
|
pub crop_marker: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -652,7 +652,7 @@ mod test {
|
|||||||
use crate::index::error::Result as IndexResult;
|
use crate::index::error::Result as IndexResult;
|
||||||
use crate::index::Index;
|
use crate::index::Index;
|
||||||
use crate::index::{
|
use crate::index::{
|
||||||
default_crop_marker, default_highlight_post_tag, default_highlight_pre_tag,
|
DEFAULT_CROP_MARKER, DEFAULT_HIGHLIGHT_POST_TAG, DEFAULT_HIGHLIGHT_PRE_TAG,
|
||||||
};
|
};
|
||||||
use crate::index_resolver::index_store::MockIndexStore;
|
use crate::index_resolver::index_store::MockIndexStore;
|
||||||
use crate::index_resolver::meta_store::MockIndexMetaStore;
|
use crate::index_resolver::meta_store::MockIndexMetaStore;
|
||||||
@ -692,9 +692,9 @@ mod test {
|
|||||||
filter: None,
|
filter: None,
|
||||||
sort: None,
|
sort: None,
|
||||||
facets: None,
|
facets: None,
|
||||||
highlight_pre_tag: default_highlight_pre_tag(),
|
highlight_pre_tag: DEFAULT_HIGHLIGHT_PRE_TAG(),
|
||||||
highlight_post_tag: default_highlight_post_tag(),
|
highlight_post_tag: DEFAULT_HIGHLIGHT_POST_TAG(),
|
||||||
crop_marker: default_crop_marker(),
|
crop_marker: DEFAULT_CROP_MARKER(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = SearchResult {
|
let result = SearchResult {
|
||||||
|
Loading…
Reference in New Issue
Block a user