Add more info to search route (#60)

* Add more info to search route:

* Run formatter
This commit is contained in:
Geometrically 2020-09-06 08:19:53 -07:00 committed by GitHub
parent 0dfa378e38
commit b99f45874f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 17 deletions

View File

@ -1,4 +1,3 @@
use std::fs;
use std::path::{Path, PathBuf};
fn main() {
@ -57,4 +56,4 @@ pub fn copy<U: AsRef<Path>, V: AsRef<Path>>(from: U, to: V) -> Result<(), std::i
}
Ok(())
}
}

View File

@ -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;

View File

@ -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(())
}
}

View File

@ -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()

View File

@ -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?;

View File

@ -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<ResultSearchMod>,
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<Vec<ResultSearchMod>, SearchError> {
pub async fn search_for_mod(info: &SearchRequest) -> Result<SearchResults, SearchError> {
let address = &*dotenv::var("MEILISEARCH_ADDR")?;
let client = Client::new(address, "");
@ -148,10 +160,19 @@ pub async fn search_for_mod(info: &SearchRequest) -> Result<Vec<ResultSearchMod>
query = query.with_facet_filters(facets);
}
Ok(client
let results = client
.get_index(format!("{}_mods", index).as_ref())
.await?
.search::<ResultSearchMod>(&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,
})
}