Re-implement lucene a bit

This commit is contained in:
Wojciech Kozlowski 2024-08-30 10:37:45 +02:00
parent f4f9b0bf3c
commit 5eef5f0f22
4 changed files with 200 additions and 100 deletions

View File

@ -5,10 +5,7 @@ use std::{num::ParseIntError, str::FromStr};
use musichoard::{
collection::{album::AlbumDate, musicbrainz::Mbid},
external::musicbrainz::{
api::{
search::{Expression, Query, SearchReleaseGroup},
MusicBrainzClient,
},
api::{search::SearchReleaseGroupRequest, MusicBrainzClient},
http::MusicBrainzHttp,
},
};
@ -93,18 +90,21 @@ fn main() {
let title: String;
let rgid: Mbid;
let query: Query<SearchReleaseGroup> = match opt.command {
let query = match opt.command {
OptCommand::Title(opt_title) => {
arid = opt_title.arid.into();
date = opt_title.date.map(Into::into).unwrap_or_default();
title = opt_title.title;
Query::expression(Expression::arid(&arid))
.and(Expression::release_group(&title))
.and(Expression::first_release_date(&date))
SearchReleaseGroupRequest::new()
.arid(&arid)
.and()
.release_group(&title)
.and()
.first_release_date(&date)
}
OptCommand::Rgid(opt_rgid) => {
rgid = opt_rgid.rgid.into();
Query::expression(Expression::rgid(&rgid))
SearchReleaseGroupRequest::new().rgid(&rgid)
}
};

View File

@ -3,7 +3,7 @@ mod query;
use std::fmt;
pub use query::{Expression, Query};
use query::{impl_term, EmptyQuery, EmptyQueryJoin, QueryJoin};
use serde::Deserialize;
use url::form_urlencoded;
@ -12,8 +12,8 @@ use crate::{
core::collection::album::{AlbumPrimaryType, AlbumSecondaryType},
external::musicbrainz::{
api::{
ApiDisplay, Error, MusicBrainzClient, SerdeAlbumDate, SerdeAlbumPrimaryType,
SerdeAlbumSecondaryType, SerdeMbid, MB_BASE_URL,
search::query::Query, ApiDisplay, Error, MusicBrainzClient, SerdeAlbumDate,
SerdeAlbumPrimaryType, SerdeAlbumSecondaryType, SerdeMbid, MB_BASE_URL,
},
IMusicBrainzHttp,
},
@ -41,28 +41,6 @@ pub enum SearchReleaseGroup<'a> {
Rgid(&'a Mbid),
}
impl<'a> Expression<SearchReleaseGroup<'a>> {
pub fn no_field(string: &'a str) -> Self {
Expression::Term(SearchReleaseGroup::NoField(string))
}
pub fn arid(arid: &'a Mbid) -> Self {
Expression::Term(SearchReleaseGroup::Arid(arid))
}
pub fn first_release_date(first_release_date: &'a AlbumDate) -> Self {
Expression::Term(SearchReleaseGroup::FirstReleaseDate(first_release_date))
}
pub fn release_group(release_group: &'a str) -> Self {
Expression::Term(SearchReleaseGroup::ReleaseGroup(release_group))
}
pub fn rgid(rgid: &'a Mbid) -> Self {
Expression::Term(SearchReleaseGroup::Rgid(rgid))
}
}
impl<'a> fmt::Display for SearchReleaseGroup<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@ -81,6 +59,17 @@ impl<'a> fmt::Display for SearchReleaseGroup<'a> {
pub type SearchReleaseGroupRequest<'a> = Query<SearchReleaseGroup<'a>>;
impl_term!(no_field, SearchReleaseGroup<'a>, NoField, &'a str);
impl_term!(arid, SearchReleaseGroup<'a>, Arid, &'a Mbid);
impl_term!(
first_release_date,
SearchReleaseGroup<'a>,
FirstReleaseDate,
&'a AlbumDate
);
impl_term!(release_group, SearchReleaseGroup<'a>, ReleaseGroup, &'a str);
impl_term!(rgid, SearchReleaseGroup<'a>, Rgid, &'a Mbid);
#[derive(Debug, PartialEq, Eq)]
pub struct SearchReleaseGroupResponse {
pub release_groups: Vec<SearchReleaseGroupResponseReleaseGroup>,
@ -212,16 +201,19 @@ mod tests {
let title: AlbumId = AlbumId::new("an album");
let date = (1986, 4).into();
let query = Query::expression(Expression::arid(&arid))
.and(Expression::release_group(&title.title))
.and(Expression::first_release_date(&date));
let query = SearchReleaseGroupRequest::new()
.arid(&arid)
.and()
.release_group(&title.title)
.and()
.first_release_date(&date);
let matches = client.search_release_group(query).unwrap();
assert_eq!(matches, response);
let rgid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap();
let query = Query::expression(Expression::rgid(&rgid));
let query = SearchReleaseGroupRequest::new().rgid(&rgid);
let matches = client.search_release_group(query).unwrap();
assert_eq!(matches, response);
@ -253,9 +245,12 @@ mod tests {
let title: AlbumId = AlbumId::new("an album");
let date = AlbumDate::default();
let query = Query::expression(Expression::arid(&arid))
.and(Expression::release_group(&title.title))
.and(Expression::first_release_date(&date));
let query = SearchReleaseGroupRequest::new()
.arid(&arid)
.and()
.release_group(&title.title)
.and()
.first_release_date(&date);
let _ = client.search_release_group(query).unwrap();
}

View File

@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, marker::PhantomData};
pub enum Logical {
Unary(Unary),
@ -49,6 +49,18 @@ pub enum Expression<Entity> {
Expr(Query<Entity>),
}
impl<Entity> From<Entity> for Expression<Entity> {
fn from(value: Entity) -> Self {
Expression::Term(value)
}
}
impl<Entity> From<Query<Entity>> for Expression<Entity> {
fn from(value: Query<Entity>) -> Self {
Expression::Expr(value)
}
}
impl<Entity: fmt::Display> fmt::Display for Expression<Entity> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@ -58,6 +70,55 @@ impl<Entity: fmt::Display> fmt::Display for Expression<Entity> {
}
}
pub struct EmptyQuery<Entity> {
_marker: PhantomData<Entity>,
}
impl<Entity> Default for EmptyQuery<Entity> {
fn default() -> Self {
EmptyQuery {
_marker: PhantomData,
}
}
}
impl<Entity> EmptyQuery<Entity> {
pub fn expression<Expr: Into<Expression<Entity>>>(self, expr: Expr) -> Query<Entity> {
Query {
left: (None, Box::new(expr.into())),
right: vec![],
}
}
pub fn require(self) -> EmptyQueryJoin<Entity> {
EmptyQueryJoin {
unary: Unary::Require,
_marker: PhantomData,
}
}
pub fn prohibit(self) -> EmptyQueryJoin<Entity> {
EmptyQueryJoin {
unary: Unary::Prohibit,
_marker: PhantomData,
}
}
}
pub struct EmptyQueryJoin<Entity> {
unary: Unary,
_marker: PhantomData<Entity>,
}
impl<Entity> EmptyQueryJoin<Entity> {
pub fn expression<Expr: Into<Expression<Entity>>>(self, expr: Expr) -> Query<Entity> {
Query {
left: (Some(self.unary), Box::new(expr.into())),
right: vec![],
}
}
}
pub struct Query<Entity> {
left: (Option<Unary>, Box<Expression<Entity>>),
right: Vec<(Logical, Box<Expression<Entity>>)>,
@ -79,94 +140,135 @@ impl<Entity: fmt::Display> fmt::Display for Query<Entity> {
}
}
impl<'a, Entity> Query<Entity> {
pub fn expression(expr: Expression<Entity>) -> Self {
Query {
left: (None, Box::new(expr)),
right: vec![],
impl<Entity> Query<Entity> {
pub fn new() -> EmptyQuery<Entity> {
EmptyQuery::default()
}
pub fn require(self) -> QueryJoin<Entity> {
QueryJoin {
logical: Logical::Unary(Unary::Require),
query: self,
}
}
pub fn require(expr: Expression<Entity>) -> Self {
Query {
left: (Some(Unary::Require), Box::new(expr)),
right: vec![],
pub fn prohibit(self) -> QueryJoin<Entity> {
QueryJoin {
logical: Logical::Unary(Unary::Prohibit),
query: self,
}
}
pub fn and_require(mut self, expr: Expression<Entity>) -> Self {
self.right
.push((Logical::Unary(Unary::Require), Box::new(expr)));
self
}
pub fn prohibit(expr: Expression<Entity>) -> Self {
Query {
left: (Some(Unary::Prohibit), Box::new(expr)),
right: vec![],
pub fn and(self) -> QueryJoin<Entity> {
QueryJoin {
logical: Logical::Binary(Boolean::And),
query: self,
}
}
pub fn and_prohibit(mut self, expr: Expression<Entity>) -> Self {
self.right
.push((Logical::Unary(Unary::Prohibit), Box::new(expr)));
self
pub fn or(self) -> QueryJoin<Entity> {
QueryJoin {
logical: Logical::Binary(Boolean::Or),
query: self,
}
}
pub fn and(mut self, expr: Expression<Entity>) -> Self {
self.right
.push((Logical::Binary(Boolean::And), Box::new(expr)));
self
}
pub fn or(mut self, expr: Expression<Entity>) -> Self {
self.right
.push((Logical::Binary(Boolean::Or), Box::new(expr)));
self
}
pub fn not(mut self, expr: Expression<Entity>) -> Self {
self.right
.push((Logical::Binary(Boolean::Not), Box::new(expr)));
self
pub fn not(self) -> QueryJoin<Entity> {
QueryJoin {
logical: Logical::Binary(Boolean::Not),
query: self,
}
}
}
pub struct QueryJoin<Entity> {
logical: Logical,
query: Query<Entity>,
}
impl<Entity> QueryJoin<Entity> {
pub fn expression<Expr: Into<Expression<Entity>>>(mut self, expr: Expr) -> Query<Entity> {
self.query.right.push((self.logical, Box::new(expr.into())));
self.query
}
}
macro_rules! impl_term {
($name:ident, $enum:ty, $variant:ident, $type:ty) => {
impl<'a> EmptyQuery<$enum> {
pub fn $name(self, $name: $type) -> Query<$enum> {
self.expression(<$enum>::$variant($name))
}
}
impl<'a> EmptyQueryJoin<$enum> {
pub fn $name(self, $name: $type) -> Query<$enum> {
self.expression(<$enum>::$variant($name))
}
}
impl<'a> QueryJoin<$enum> {
pub fn $name(self, $name: $type) -> Query<$enum> {
self.expression(<$enum>::$variant($name))
}
}
};
}
pub(crate) use impl_term;
#[cfg(test)]
mod tests {
use super::*;
use crate::external::musicbrainz::api::search::SearchReleaseGroupRequest;
#[test]
fn lucene_logical() {
let query = Query::expression(Expression::no_field("jakarta apache"))
.or(Expression::no_field("jakarta"));
let query = SearchReleaseGroupRequest::new()
.no_field("jakarta apache")
.or()
.no_field("jakarta");
assert_eq!(format!("{query}"), "\"jakarta apache\" OR \"jakarta\"");
let query = Query::expression(Expression::no_field("jakarta apache"))
.and(Expression::no_field("jakarta"));
let query = SearchReleaseGroupRequest::new()
.no_field("jakarta apache")
.and()
.no_field("jakarta");
assert_eq!(format!("{query}"), "\"jakarta apache\" AND \"jakarta\"");
let query =
Query::require(Expression::no_field("jakarta")).or(Expression::no_field("lucene"));
let query = SearchReleaseGroupRequest::new()
.require()
.no_field("jakarta")
.or()
.no_field("lucene");
assert_eq!(format!("{query}"), "+\"jakarta\" OR \"lucene\"");
let query = Query::expression(Expression::no_field("jakarta apache"))
.not(Expression::no_field("Apache Lucene"));
let query = SearchReleaseGroupRequest::new()
.no_field("jakarta apache")
.not()
.no_field("Apache Lucene");
assert_eq!(
format!("{query}"),
"\"jakarta apache\" NOT \"Apache Lucene\""
);
let query = Query::expression(Expression::no_field("jakarta apache"))
.and_prohibit(Expression::no_field("Apache Lucene"));
let query = SearchReleaseGroupRequest::new()
.no_field("jakarta apache")
.prohibit()
.no_field("Apache Lucene");
assert_eq!(format!("{query}"), "\"jakarta apache\" -\"Apache Lucene\"");
}
#[test]
fn lucene_grouping() {
let query = Query::expression(Expression::Expr(
Query::expression(Expression::no_field("jakarta")).or(Expression::no_field("apache")),
))
.and(Expression::no_field("website"));
let query = SearchReleaseGroupRequest::new()
.expression(
SearchReleaseGroupRequest::new()
.no_field("jakarta")
.or()
.no_field("apache"),
)
.and()
.no_field("website");
assert_eq!(
format!("{query}"),
"(\"jakarta\" OR \"apache\") AND \"website\""

View File

@ -7,7 +7,7 @@ use musichoard::{
},
external::musicbrainz::{
api::{
search::{Expression, Query, SearchReleaseGroupResponseReleaseGroup},
search::{SearchReleaseGroupRequest, SearchReleaseGroupResponseReleaseGroup},
MusicBrainzClient,
},
IMusicBrainzHttp,
@ -37,9 +37,12 @@ impl<Http: IMusicBrainzHttp> IMusicBrainz for MusicBrainz<Http> {
// with just the year should be enough anyway.
let date = AlbumDate::new(album.date.year, None, None);
let query = Query::expression(Expression::arid(arid))
.and(Expression::first_release_date(&date))
.and(Expression::release_group(&album.id.title));
let query = SearchReleaseGroupRequest::new()
.arid(arid)
.and()
.first_release_date(&date)
.and()
.release_group(&album.id.title);
let mb_response = self.client.search_release_group(query)?;