From b99f45874fdd79bab825caab4527df0ab429f62c Mon Sep 17 00:00:00 2001 From: Geometrically <18202329+Geometrically@users.noreply.github.com> Date: Sun, 6 Sep 2020 08:19:53 -0700 Subject: [PATCH] Add more info to search route (#60) * Add more info to search route: * Run formatter --- build.rs | 3 +-- src/database/mod.rs | 2 +- src/database/postgres_database.rs | 11 +++++------ src/main.rs | 4 +++- src/search/indexing/mod.rs | 4 ++-- src/search/mod.rs | 31 ++++++++++++++++++++++++++----- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/build.rs b/build.rs index 432b3cd75..bf021783f 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,3 @@ - use std::fs; use std::path::{Path, PathBuf}; fn main() { @@ -57,4 +56,4 @@ pub fn copy, V: AsRef>(from: U, to: V) -> Result<(), std::i } Ok(()) -} \ No newline at end of file +} diff --git a/src/database/mod.rs b/src/database/mod.rs index da5154f89..73d2d1938 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -3,5 +3,5 @@ mod postgres_database; pub use models::Mod; pub use models::Version; -pub use postgres_database::connect; pub use postgres_database::check_for_migrations; +pub use postgres_database::connect; diff --git a/src/database/postgres_database.rs b/src/database/postgres_database.rs index ea916e6a0..38b67eaf2 100644 --- a/src/database/postgres_database.rs +++ b/src/database/postgres_database.rs @@ -1,8 +1,8 @@ -use log::{info, debug}; +use log::{debug, info}; +use sqlx::migrate::{Migrate, MigrateDatabase, Migrator}; use sqlx::postgres::{PgPool, PgPoolOptions}; -use sqlx::migrate::{Migrator, Migrate, MigrateDatabase}; +use sqlx::{Connection, PgConnection, Postgres}; use std::path::Path; -use sqlx::{PgConnection, Connection, Postgres}; const MIGRATION_FOLDER: &'static str = "migrations"; @@ -29,10 +29,9 @@ pub async fn check_for_migrations() -> Result<(), sqlx::Error> { Ok(()) } - pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> { let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?; - let mut conn : PgConnection = PgConnection::connect(uri).await?; + let mut conn: PgConnection = PgConnection::connect(uri).await?; conn.ensure_migrations_table().await?; @@ -51,4 +50,4 @@ pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> { } Ok(()) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index ff1be43c6..976476d91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,9 @@ async fn main() -> std::io::Result<()> { check_env_vars(); - database::check_for_migrations().await.expect("An error occurred while running migrations."); + database::check_for_migrations() + .await + .expect("An error occurred while running migrations."); // Database Connector let pool = database::connect() diff --git a/src/search/indexing/mod.rs b/src/search/indexing/mod.rs index d6a496488..8d365b6e0 100644 --- a/src/search/indexing/mod.rs +++ b/src/search/indexing/mod.rs @@ -152,8 +152,8 @@ async fn create_index<'a>( // TODO: update index settings on startup (or delete old indices on startup) Ok(index) => Ok(index), Err(meilisearch_sdk::errors::Error::MeiliSearchError { - error_code: meilisearch_sdk::errors::ErrorCode::IndexNotFound, - .. + error_code: meilisearch_sdk::errors::ErrorCode::IndexNotFound, + .. }) => { // Only create index and set settings if the index doesn't already exist let index = client.create_index(name, Some("mod_id")).await?; diff --git a/src/search/mod.rs b/src/search/mod.rs index 997969215..28edd3de3 100644 --- a/src/search/mod.rs +++ b/src/search/mod.rs @@ -5,7 +5,8 @@ use actix_web::web::HttpResponse; use meilisearch_sdk::client::Client; use meilisearch_sdk::document::Document; use meilisearch_sdk::search::Query; -use serde::{Deserialize, Serialize}; +use serde::ser::SerializeStruct; +use serde::{Deserialize, Serialize, Serializer}; use std::borrow::Cow; use thiserror::Error; @@ -80,6 +81,17 @@ pub struct UploadSearchMod { pub empty: Cow<'static, str>, } +#[derive(Serialize, Deserialize, Debug)] +pub struct SearchResults { + pub hits: Vec, + pub offset: usize, + pub limit: usize, + pub nb_hits: usize, + pub exhaustive_nb_hits: bool, + pub processing_time_ms: usize, + pub query: String, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ResultSearchMod { pub mod_id: String, @@ -119,7 +131,7 @@ impl Document for ResultSearchMod { } } -pub async fn search_for_mod(info: &SearchRequest) -> Result, SearchError> { +pub async fn search_for_mod(info: &SearchRequest) -> Result { let address = &*dotenv::var("MEILISEARCH_ADDR")?; let client = Client::new(address, ""); @@ -148,10 +160,19 @@ pub async fn search_for_mod(info: &SearchRequest) -> Result query = query.with_facet_filters(facets); } - Ok(client + let results = client .get_index(format!("{}_mods", index).as_ref()) .await? .search::(&query) - .await? - .hits) + .await?; + + Ok(SearchResults { + hits: results.hits, + offset: results.offset, + limit: results.limit, + nb_hits: results.nb_hits, + exhaustive_nb_hits: results.exhaustive_nb_hits, + processing_time_ms: results.processing_time_ms, + query: results.query, + }) }