Add more info to search route (#60)
* Add more info to search route: * Run formatter
This commit is contained in:
parent
0dfa378e38
commit
b99f45874f
3
build.rs
3
build.rs
@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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?;
|
||||
|
||||
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user