mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-23 23:11:11 +00:00
Add option to search exclusively by post title (#5015)
* Add option to search exclusively by post title * Address format issues * Remove duplicated 'removed' filter * Replace url_search with search_term * Build generic PostQuery before search match * Create default queries. Move title_only to Search struct. Rename Url to PostURL * Revert PostUrl to Url
This commit is contained in:
parent
d57d9f6fe1
commit
21e24dc96d
|
@ -78,6 +78,7 @@ pub struct Search {
|
||||||
pub listing_type: Option<ListingType>,
|
pub listing_type: Option<ListingType>,
|
||||||
pub page: Option<i64>,
|
pub page: Option<i64>,
|
||||||
pub limit: Option<i64>,
|
pub limit: Option<i64>,
|
||||||
|
pub post_title_only: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
|
|
@ -56,133 +56,92 @@ pub async fn search(
|
||||||
};
|
};
|
||||||
let creator_id = data.creator_id;
|
let creator_id = data.creator_id;
|
||||||
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
||||||
|
let post_title_only = data.post_title_only;
|
||||||
|
|
||||||
match search_type {
|
let posts_query = PostQuery {
|
||||||
SearchType::Posts => {
|
|
||||||
posts = PostQuery {
|
|
||||||
sort: (sort),
|
sort: (sort),
|
||||||
listing_type: (listing_type),
|
listing_type: (listing_type),
|
||||||
community_id: (community_id),
|
community_id: (community_id),
|
||||||
creator_id: (creator_id),
|
creator_id: (creator_id),
|
||||||
local_user,
|
local_user,
|
||||||
search_term: (Some(q)),
|
search_term: (Some(q.clone())),
|
||||||
page: (page),
|
page: (page),
|
||||||
limit: (limit),
|
limit: (limit),
|
||||||
|
title_only: (post_title_only),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
};
|
||||||
.list(&local_site.site, &mut context.pool())
|
|
||||||
.await?;
|
let comment_query = CommentQuery {
|
||||||
}
|
|
||||||
SearchType::Comments => {
|
|
||||||
comments = CommentQuery {
|
|
||||||
sort: (sort.map(post_to_comment_sort_type)),
|
sort: (sort.map(post_to_comment_sort_type)),
|
||||||
listing_type: (listing_type),
|
listing_type: (listing_type),
|
||||||
search_term: (Some(q)),
|
search_term: (Some(q.clone())),
|
||||||
community_id: (community_id),
|
community_id: (community_id),
|
||||||
creator_id: (creator_id),
|
creator_id: (creator_id),
|
||||||
local_user,
|
local_user,
|
||||||
page: (page),
|
page: (page),
|
||||||
limit: (limit),
|
limit: (limit),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
};
|
||||||
.list(&mut context.pool())
|
|
||||||
.await?;
|
let community_query = CommunityQuery {
|
||||||
}
|
|
||||||
SearchType::Communities => {
|
|
||||||
communities = CommunityQuery {
|
|
||||||
sort: (sort),
|
sort: (sort),
|
||||||
listing_type: (listing_type),
|
listing_type: (listing_type),
|
||||||
search_term: (Some(q)),
|
search_term: (Some(q.clone())),
|
||||||
local_user,
|
local_user,
|
||||||
is_mod_or_admin: (is_admin),
|
is_mod_or_admin: (is_admin),
|
||||||
page: (page),
|
page: (page),
|
||||||
limit: (limit),
|
limit: (limit),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let person_query = PersonQuery {
|
||||||
|
sort,
|
||||||
|
search_term: (Some(q.clone())),
|
||||||
|
listing_type: (listing_type),
|
||||||
|
page: (page),
|
||||||
|
limit: (limit),
|
||||||
|
};
|
||||||
|
|
||||||
|
match search_type {
|
||||||
|
SearchType::Posts => {
|
||||||
|
posts = posts_query
|
||||||
|
.list(&local_site.site, &mut context.pool())
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
SearchType::Comments => {
|
||||||
|
comments = comment_query.list(&mut context.pool()).await?;
|
||||||
|
}
|
||||||
|
SearchType::Communities => {
|
||||||
|
communities = community_query
|
||||||
.list(&local_site.site, &mut context.pool())
|
.list(&local_site.site, &mut context.pool())
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
SearchType::Users => {
|
SearchType::Users => {
|
||||||
users = PersonQuery {
|
users = person_query.list(&mut context.pool()).await?;
|
||||||
sort,
|
|
||||||
search_term: (Some(q)),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
}
|
|
||||||
.list(&mut context.pool())
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
SearchType::All => {
|
SearchType::All => {
|
||||||
// If the community or creator is included, dont search communities or users
|
// If the community or creator is included, dont search communities or users
|
||||||
let community_or_creator_included =
|
let community_or_creator_included =
|
||||||
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
|
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
|
||||||
|
|
||||||
let q = data.q.clone();
|
posts = posts_query
|
||||||
|
|
||||||
posts = PostQuery {
|
|
||||||
sort: (sort),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
community_id: (community_id),
|
|
||||||
creator_id: (creator_id),
|
|
||||||
local_user,
|
|
||||||
search_term: (Some(q)),
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(&local_site.site, &mut context.pool())
|
.list(&local_site.site, &mut context.pool())
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let q = data.q.clone();
|
comments = comment_query.list(&mut context.pool()).await?;
|
||||||
|
|
||||||
comments = CommentQuery {
|
|
||||||
sort: (sort.map(post_to_comment_sort_type)),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
search_term: (Some(q)),
|
|
||||||
community_id: (community_id),
|
|
||||||
creator_id: (creator_id),
|
|
||||||
local_user,
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(&mut context.pool())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let q = data.q.clone();
|
|
||||||
|
|
||||||
communities = if community_or_creator_included {
|
communities = if community_or_creator_included {
|
||||||
vec![]
|
vec![]
|
||||||
} else {
|
} else {
|
||||||
CommunityQuery {
|
community_query
|
||||||
sort: (sort),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
search_term: (Some(q)),
|
|
||||||
local_user,
|
|
||||||
is_mod_or_admin: (is_admin),
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(&local_site.site, &mut context.pool())
|
.list(&local_site.site, &mut context.pool())
|
||||||
.await?
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
let q = data.q.clone();
|
|
||||||
|
|
||||||
users = if community_or_creator_included {
|
users = if community_or_creator_included {
|
||||||
vec![]
|
vec![]
|
||||||
} else {
|
} else {
|
||||||
PersonQuery {
|
person_query.list(&mut context.pool()).await?
|
||||||
sort,
|
|
||||||
search_term: (Some(q)),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
}
|
|
||||||
.list(&mut context.pool())
|
|
||||||
.await?
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
SearchType::Url => {
|
SearchType::Url => {
|
||||||
|
|
|
@ -387,12 +387,15 @@ fn queries<'a>() -> Queries<
|
||||||
|
|
||||||
if let Some(search_term) = &options.search_term {
|
if let Some(search_term) = &options.search_term {
|
||||||
let searcher = fuzzy_search(search_term);
|
let searcher = fuzzy_search(search_term);
|
||||||
query = query
|
query = if options.title_only.unwrap_or_default() {
|
||||||
.filter(
|
query.filter(post::name.ilike(searcher))
|
||||||
|
} else {
|
||||||
|
query.filter(
|
||||||
post::name
|
post::name
|
||||||
.ilike(searcher.clone())
|
.ilike(searcher.clone())
|
||||||
.or(post::body.ilike(searcher)),
|
.or(post::body.ilike(searcher)),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
.filter(not(post::removed.or(post::deleted)));
|
.filter(not(post::removed.or(post::deleted)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -617,6 +620,7 @@ pub struct PostQuery<'a> {
|
||||||
pub saved_only: Option<bool>,
|
pub saved_only: Option<bool>,
|
||||||
pub liked_only: Option<bool>,
|
pub liked_only: Option<bool>,
|
||||||
pub disliked_only: Option<bool>,
|
pub disliked_only: Option<bool>,
|
||||||
|
pub title_only: Option<bool>,
|
||||||
pub page: Option<i64>,
|
pub page: Option<i64>,
|
||||||
pub limit: Option<i64>,
|
pub limit: Option<i64>,
|
||||||
pub page_after: Option<PaginationCursorData>,
|
pub page_after: Option<PaginationCursorData>,
|
||||||
|
|
Loading…
Reference in a new issue