Skip to content

Commit

Permalink
search lol
Browse files Browse the repository at this point in the history
  • Loading branch information
RealHinome authored Aug 24, 2024
1 parent 2e3b678 commit cbef6c6
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions graphql/api/src/schema/news.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::models::{image::Image, news::News, source::Media};
use crate::schema::Date;
use crate::Context;
use juniper::{graphql_object, FieldResult};
use juniper::{graphql_object, graphql_value, FieldError, FieldResult};

/// Implement GraphQL on News structure.
#[graphql_object(context = Context, description = "A media article.")]
Expand Down Expand Up @@ -49,18 +49,59 @@ pub struct NewsQuery;
/// Implement the GraphQL object for the news query.
#[graphql_object(context = Context)]
impl NewsQuery {
/// Get the most relevant news of the day.
/// Get the *three* most relevant news of the day.
async fn get_top_news(
ctx: &Context,
#[graphql(description = "ISO 3166-1 alpha-2 country code.")]
country: String,
#[graphql(description = "Maximum number of articles sent.")] limit: i32,
) -> FieldResult<Vec<News>> {
let rank = ctx.ranker.get_rank(limit.try_into()?).await?;
let mut most_revelant_news = Vec::new();

for word in rank {
let news = ctx
.meilisearch
.write()
.await
.index
.as_ref()
.ok_or_else(|| FieldError::new("Index not found", graphql_value!({ "internal_error": "Meilisearch index is not selected" })))?
.search()
.with_query(&word)
.with_limit(1)
.with_filter(&format!("source.country={:?}", country))
.with_attributes_to_search_on(&["title"])
.execute::<News>()
.await?;

most_revelant_news.push(news.hits[0].result.clone())
}

Ok(most_revelant_news)
}

/// Get news of the day.
async fn get_news(
ctx: &Context,
#[graphql(description = "ISO 3166-1 alpha-2 country code.")]
_country: String,
#[graphql(description = "Maximum number of articles sent.")]
_limit: i32,
country: String,
#[graphql(description = "Maximum number of articles sent.")] limit: i32,
) -> FieldResult<Vec<News>> {
let _rank = ctx.ranker.get_rank().await?;
let news = ctx
.meilisearch
.write()
.await
.index
.as_ref()
.ok_or_else(|| FieldError::new("Index not found", graphql_value!({ "internal_error": "Meilisearch index is not selected" })))?
.search()
.with_query("*")
.with_limit(limit.try_into()?)
.with_filter(&format!("source.country={:?}", country))
.execute::<News>()
.await?;

Ok(vec![News {
..Default::default()
}])
Ok(news.hits.iter().map(|r| r.result.clone()).collect())
}
}

0 comments on commit cbef6c6

Please sign in to comment.