You've already forked AstralRinth
forked from didirus/AstralRinth
Add more info to search route (#60)
* Add more info to search route: * Run formatter
This commit is contained in:
3
build.rs
3
build.rs
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -57,4 +56,4 @@ pub fn copy<U: AsRef<Path>, V: AsRef<Path>>(from: U, to: V) -> Result<(), std::i
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ mod postgres_database;
|
|||||||
|
|
||||||
pub use models::Mod;
|
pub use models::Mod;
|
||||||
pub use models::Version;
|
pub use models::Version;
|
||||||
pub use postgres_database::connect;
|
|
||||||
pub use postgres_database::check_for_migrations;
|
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::postgres::{PgPool, PgPoolOptions};
|
||||||
use sqlx::migrate::{Migrator, Migrate, MigrateDatabase};
|
use sqlx::{Connection, PgConnection, Postgres};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use sqlx::{PgConnection, Connection, Postgres};
|
|
||||||
|
|
||||||
const MIGRATION_FOLDER: &'static str = "migrations";
|
const MIGRATION_FOLDER: &'static str = "migrations";
|
||||||
|
|
||||||
@@ -29,10 +29,9 @@ pub async fn check_for_migrations() -> Result<(), sqlx::Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> {
|
pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> {
|
||||||
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
|
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?;
|
conn.ensure_migrations_table().await?;
|
||||||
|
|
||||||
@@ -51,4 +50,4 @@ pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
check_env_vars();
|
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
|
// Database Connector
|
||||||
let pool = database::connect()
|
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)
|
// TODO: update index settings on startup (or delete old indices on startup)
|
||||||
Ok(index) => Ok(index),
|
Ok(index) => Ok(index),
|
||||||
Err(meilisearch_sdk::errors::Error::MeiliSearchError {
|
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
|
// Only create index and set settings if the index doesn't already exist
|
||||||
let index = client.create_index(name, Some("mod_id")).await?;
|
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::client::Client;
|
||||||
use meilisearch_sdk::document::Document;
|
use meilisearch_sdk::document::Document;
|
||||||
use meilisearch_sdk::search::Query;
|
use meilisearch_sdk::search::Query;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::ser::SerializeStruct;
|
||||||
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -80,6 +81,17 @@ pub struct UploadSearchMod {
|
|||||||
pub empty: Cow<'static, str>,
|
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)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct ResultSearchMod {
|
pub struct ResultSearchMod {
|
||||||
pub mod_id: String,
|
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 address = &*dotenv::var("MEILISEARCH_ADDR")?;
|
||||||
let client = Client::new(address, "");
|
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);
|
query = query.with_facet_filters(facets);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(client
|
let results = client
|
||||||
.get_index(format!("{}_mods", index).as_ref())
|
.get_index(format!("{}_mods", index).as_ref())
|
||||||
.await?
|
.await?
|
||||||
.search::<ResultSearchMod>(&query)
|
.search::<ResultSearchMod>(&query)
|
||||||
.await?
|
.await?;
|
||||||
.hits)
|
|
||||||
|
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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user