Remove reverse fields (#579)

This commit is contained in:
Naïm Favier 2022-12-06 23:58:51 +01:00 committed by GitHub
parent 3a12920489
commit 4bea45b150
Failed to generate hash of commit
7 changed files with 27 additions and 303 deletions

View file

@ -1 +1 @@
32 33

View file

@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
use super::{ use super::{
import::{self, DocString, DocValue, ModulePath, NixOption}, import::{self, DocString, DocValue, ModulePath, NixOption},
pandoc::PandocExt, pandoc::PandocExt,
utility::{AttributeQuery, Flatten, OneOrMany, Reverse}, utility::{Flatten, OneOrMany},
}; };
type Flake = super::Flake; type Flake = super::Flake;
@ -57,13 +57,8 @@ pub enum Derivation {
#[allow(non_snake_case)] #[allow(non_snake_case)]
Package { Package {
package_attr_name: String, package_attr_name: String,
package_attr_name_reverse: Reverse<String>,
package_attr_name_query: AttributeQuery,
package_attr_name_query_reverse: Reverse<AttributeQuery>,
package_attr_set: String, package_attr_set: String,
package_attr_set_reverse: Reverse<String>,
package_pname: String, package_pname: String,
package_pname_reverse: Reverse<String>,
package_pversion: String, package_pversion: String,
package_platforms: Vec<String>, package_platforms: Vec<String>,
package_outputs: Vec<String>, package_outputs: Vec<String>,
@ -73,9 +68,7 @@ pub enum Derivation {
package_maintainers: Vec<Maintainer>, package_maintainers: Vec<Maintainer>,
package_maintainers_set: Vec<String>, package_maintainers_set: Vec<String>,
package_description: Option<String>, package_description: Option<String>,
package_description_reverse: Option<Reverse<String>>,
package_longDescription: Option<String>, package_longDescription: Option<String>,
package_longDescription_reverse: Option<Reverse<String>>,
package_hydra: (), package_hydra: (),
package_system: String, package_system: String,
package_homepage: Vec<String>, package_homepage: Vec<String>,
@ -94,9 +87,6 @@ pub enum Derivation {
Option { Option {
option_source: Option<String>, option_source: Option<String>,
option_name: String, option_name: String,
option_name_reverse: Reverse<String>,
option_name_query: AttributeQuery,
option_name_query_reverse: Reverse<AttributeQuery>,
option_description: Option<DocString>, option_description: Option<DocString>,
@ -135,8 +125,6 @@ impl TryFrom<(import::FlakeEntry, super::Flake)> for Derivation {
}) })
.into(); .into();
let package_attr_set_reverse = Reverse(package_attr_set.clone());
let package_license: Vec<License> = license let package_license: Vec<License> = license
.map(OneOrMany::into_list) .map(OneOrMany::into_list)
.unwrap_or_default() .unwrap_or_default()
@ -152,14 +140,9 @@ impl TryFrom<(import::FlakeEntry, super::Flake)> for Derivation {
let maintainer: Maintainer = f.into(); let maintainer: Maintainer = f.into();
Derivation::Package { Derivation::Package {
package_attr_name_query: AttributeQuery::new(&attribute_name),
package_attr_name_query_reverse: Reverse(AttributeQuery::new(&attribute_name)),
package_attr_name: attribute_name.clone(), package_attr_name: attribute_name.clone(),
package_attr_name_reverse: Reverse(attribute_name),
package_attr_set, package_attr_set,
package_attr_set_reverse,
package_pname: name.clone(), package_pname: name.clone(),
package_pname_reverse: Reverse(name),
package_pversion: version, package_pversion: version,
package_platforms: platforms, package_platforms: platforms,
package_outputs: outputs, package_outputs: outputs,
@ -169,9 +152,7 @@ impl TryFrom<(import::FlakeEntry, super::Flake)> for Derivation {
package_description: description.clone(), package_description: description.clone(),
package_maintainers: vec![maintainer.clone()], package_maintainers: vec![maintainer.clone()],
package_maintainers_set: maintainer.name.map_or(vec![], |n| vec![n]), package_maintainers_set: maintainer.name.map_or(vec![], |n| vec![n]),
package_description_reverse: description.map(Reverse),
package_longDescription: None, package_longDescription: None,
package_longDescription_reverse: None,
package_hydra: (), package_hydra: (),
package_system: String::new(), package_system: String::new(),
package_homepage: Vec::new(), package_homepage: Vec::new(),
@ -208,8 +189,6 @@ impl TryFrom<import::NixpkgsEntry> for Derivation {
}) })
.into(); .into();
let package_attr_set_reverse = Reverse(package_attr_set.clone());
let package_license: Vec<License> = package let package_license: Vec<License> = package
.meta .meta
.license .license
@ -269,13 +248,8 @@ impl TryFrom<import::NixpkgsEntry> for Derivation {
Derivation::Package { Derivation::Package {
package_attr_name: attribute.clone(), package_attr_name: attribute.clone(),
package_attr_name_reverse: Reverse(attribute.clone()),
package_attr_name_query: AttributeQuery::new(&attribute),
package_attr_name_query_reverse: Reverse(AttributeQuery::new(&attribute)),
package_attr_set, package_attr_set,
package_attr_set_reverse,
package_pname: package.pname.clone(), package_pname: package.pname.clone(),
package_pname_reverse: Reverse(package.pname),
package_pversion: package.version, package_pversion: package.version,
package_platforms: platforms, package_platforms: platforms,
package_outputs: package.outputs.into_keys().collect(), package_outputs: package.outputs.into_keys().collect(),
@ -285,9 +259,7 @@ impl TryFrom<import::NixpkgsEntry> for Derivation {
package_maintainers, package_maintainers,
package_maintainers_set, package_maintainers_set,
package_description: package.meta.description.clone(), package_description: package.meta.description.clone(),
package_description_reverse: package.meta.description.map(Reverse),
package_longDescription: long_description.clone(), package_longDescription: long_description.clone(),
package_longDescription_reverse: long_description.map(Reverse),
package_hydra: (), package_hydra: (),
package_system: package.system, package_system: package.system,
package_homepage: package package_homepage: package
@ -319,14 +291,11 @@ impl TryFrom<import::NixOption> for Derivation {
Ok(Derivation::Option { Ok(Derivation::Option {
option_source: declarations.get(0).map(Clone::clone), option_source: declarations.get(0).map(Clone::clone),
option_name: name.clone(), option_name: name.clone(),
option_name_reverse: Reverse(name.clone()),
option_description: description, option_description: description,
option_default: default, option_default: default,
option_example: example, option_example: example,
option_flake: flake, option_flake: flake,
option_type, option_type,
option_name_query: AttributeQuery::new(&name),
option_name_query_reverse: Reverse(AttributeQuery::new(&name)),
}) })
} }
} }

View file

@ -1,58 +1,4 @@
use fancy_regex::Regex; use serde::{Deserialize, Serialize, Serializer};
use lazy_static::lazy_static;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Debug, Clone, PartialEq)]
pub struct Reverse<T: Reversable + Serialize>(pub T);
pub trait Reversable {
fn reverse(&self) -> Self;
}
impl Reversable for String {
fn reverse(&self) -> Self {
self.chars().rev().collect::<String>()
}
}
impl<T: Reversable + Clone> Reversable for Vec<T> {
fn reverse(&self) -> Self {
self.iter().cloned().map(|item| item.reverse()).collect()
}
}
impl<T> Reversable for Reverse<T>
where
T: Reversable + Serialize,
{
fn reverse(&self) -> Self {
Reverse(self.0.reverse())
}
}
impl<T> Serialize for Reverse<T>
where
T: Reversable + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.reverse().serialize(serializer)
}
}
impl<'de, T> Deserialize<'de> for Reverse<T>
where
T: Reversable + Serialize + Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Reverse<T>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Reverse(T::deserialize(deserializer)?.reverse()))
}
}
/// A utility type that can represent the presence of either a single associated /// A utility type that can represent the presence of either a single associated
/// value or a list of those. Adding absence can be achieved by wrapping the type /// value or a list of those. Adding absence can be achieved by wrapping the type
@ -103,135 +49,3 @@ where
{ {
s.collect_seq(vec![item].iter()) s.collect_seq(vec![item].iter())
} }
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AttributeQuery(Vec<String>);
lazy_static! {
static ref QUERY: Regex =
Regex::new(".+?(?:(?<=[a-z])(?=[1-9A-Z])|(?<=[1-9A-Z])(?=[A-Z][a-z])|[._-]|$)").unwrap();
}
impl AttributeQuery {
pub fn new(attribute_name: &str) -> Self {
const SUFFIX: &[char] = &['-', '.', '_'];
let matches = QUERY
.find_iter(attribute_name)
.map(|found| found.unwrap().as_str())
.collect::<Vec<_>>();
let tokens = (0..matches.len())
.flat_map(|index| {
let (_, tokens) = matches.iter().skip(index).fold(
(String::new(), Vec::new()),
|(prev_parts, mut tokens), part| {
let token: String = prev_parts + part;
tokens.push(token.trim_end_matches(SUFFIX).to_owned());
(token, tokens)
},
);
tokens
})
.collect::<Vec<_>>();
AttributeQuery(tokens)
}
pub fn query(&self) -> &[String] {
&self.0
}
}
impl Reversable for AttributeQuery {
fn reverse(&self) -> Self {
AttributeQuery(self.query().to_owned().reverse())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn attr_query_test() {
assert_eq!(
{
let mut q = AttributeQuery::new("services.nginx.extraConfig")
.query()
.to_owned();
q.sort();
q
},
{
let mut ex = [
"services.nginx.extraConfig",
"services.nginx.extra",
"services.nginx",
"services",
"nginx.extraConfig",
"nginx.extra",
"nginx",
"extraConfig",
"extra",
"Config",
];
ex.sort_unstable();
ex
},
);
assert_eq!(
{
let mut q = AttributeQuery::new("python37Packages.test1_name-test2")
.query()
.to_owned();
q.sort();
q
},
{
let mut ex = [
"python37Packages.test1_name-test2",
"python37Packages.test1_name-test",
"python37Packages.test1_name",
"python37Packages.test1",
"python37Packages.test",
"python37Packages",
"python37",
"python",
"37Packages.test1_name-test2",
"37Packages.test1_name-test",
"37Packages.test1_name",
"37Packages.test1",
"37Packages.test",
"37Packages",
"37",
"Packages.test1_name-test2",
"Packages.test1_name-test",
"Packages.test1_name",
"Packages.test1",
"Packages.test",
"Packages",
"test1_name-test2",
"test1_name-test",
"test1_name",
"test1",
"test",
"1_name-test2",
"1_name-test",
"1_name",
"1",
"name-test2",
"name-test",
"name",
"test2",
"test",
"2",
];
ex.sort_unstable();
ex
}
);
}
}

View file

@ -67,34 +67,14 @@ lazy_static! {
"type": "keyword", "type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"package_attr_name_reverse": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_attr_name_query": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_attr_name_query_reverse": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_attr_set": { "package_attr_set": {
"type": "keyword", "type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"package_attr_set_reverse": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_pname": { "package_pname": {
"type": "keyword", "type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"package_pname_reverse": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_pversion": { "package_pversion": {
"type": "keyword" "type": "keyword"
}, },
@ -118,21 +98,11 @@ lazy_static! {
"analyzer": "english", "analyzer": "english",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"package_description_reverse": {
"type": "text",
"analyzer": "english",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_longDescription": { "package_longDescription": {
"type": "text", "type": "text",
"analyzer": "english", "analyzer": "english",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"package_longDescription_reverse": {
"type": "text",
"analyzer": "english",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"package_license": { "package_license": {
"type": "nested", "type": "nested",
"properties": { "properties": {
@ -157,18 +127,10 @@ lazy_static! {
"type": "keyword", "type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"option_name_reverse": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"option_name": { "option_name": {
"type": "keyword", "type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}}, "fields": {"edge": {"type": "text", "analyzer": "edge"}},
}, },
"option_name_reverse": {
"type": "keyword",
"fields": {"edge": {"type": "text", "analyzer": "edge"}},
},
"option_description": { "option_description": {
"type": "text", "type": "text",
"analyzer": "english", "analyzer": "english",

View file

@ -388,7 +388,6 @@ makeRequestBody query from size sort =
[] []
"option_name" "option_name"
[ ( "option_name", 6.0 ) [ ( "option_name", 6.0 )
, ( "option_name_query", 3.0 )
, ( "option_description", 1.0 ) , ( "option_description", 1.0 )
, ( "flake_name", 0.5 ) , ( "flake_name", 0.5 )
] ]

View file

@ -909,7 +909,6 @@ makeRequestBody query from size maybeBuckets sort =
"package_attr_name" "package_attr_name"
[ ( "package_attr_name", 9.0 ) [ ( "package_attr_name", 9.0 )
, ( "package_pname", 6.0 ) , ( "package_pname", 6.0 )
, ( "package_attr_name_query", 4.0 )
, ( "package_description", 1.3 ) , ( "package_description", 1.3 )
, ( "package_longDescription", 1.0 ) , ( "package_longDescription", 1.0 )
, ( "flake_name", 0.5 ) , ( "flake_name", 0.5 )

View file

@ -307,9 +307,14 @@ init args defaultNixOSChannel nixosChannels maybeModel =
, query = , query =
args.query args.query
|> Maybe.andThen Route.SearchQuery.searchQueryToString |> Maybe.andThen Route.SearchQuery.searchQueryToString
|> \ x -> case x of |> (\x ->
Just q -> Just q case x of
Nothing -> args.show Just q ->
Just q
Nothing ->
args.show
)
, result = getField .result RemoteData.NotAsked , result = getField .result RemoteData.NotAsked
, show = args.show , show = args.show
, from = , from =
@ -1225,38 +1230,16 @@ searchFields :
-> List (List ( String, Json.Encode.Value )) -> List (List ( String, Json.Encode.Value ))
searchFields query mainField fields = searchFields query mainField fields =
let let
queryVariations q =
case ( List.head q, List.tail q ) of
( Just h, Just t ) ->
let
tail : List (List String)
tail =
queryVariations t
in
List.append
(List.map (\x -> List.append [ h ] x) tail)
(List.map (\x -> List.append [ String.reverse h ] x) tail)
|> Set.fromList
|> Set.toList
( Just h, Nothing ) ->
[ [ h ], [ String.reverse h ] ]
( _, _ ) ->
[ [], [] ]
reverseFields =
List.map (\( field, score ) -> ( field ++ "_reverse", score * 0.8 )) fields
allFields = allFields =
List.append fields reverseFields fields
|> List.map (\( field, score ) -> [ field ++ "^" ++ String.fromFloat score, field ++ ".edge^" ++ String.fromFloat score ]) |> List.map (\( field, score ) -> [ field ++ "^" ++ String.fromFloat score, field ++ ".edge^" ++ String.fromFloat score ])
|> List.concat |> List.concat
queryWords =
String.words (String.toLower query)
in in
List.append List.append
(List.map [ [ ( "multi_match"
(\queryWords ->
[ ( "multi_match"
, Json.Encode.object , Json.Encode.object
[ ( "type", Json.Encode.string "cross_fields" ) [ ( "type", Json.Encode.string "cross_fields" )
, ( "query", Json.Encode.string <| String.join " " queryWords ) , ( "query", Json.Encode.string <| String.join " " queryWords )
@ -1268,9 +1251,7 @@ searchFields query mainField fields =
] ]
) )
] ]
) ]
(queryVariations (String.words (String.toLower query)))
)
(List.map (List.map
(\queryWord -> (\queryWord ->
[ ( "wildcard" [ ( "wildcard"
@ -1285,7 +1266,7 @@ searchFields query mainField fields =
) )
] ]
) )
(String.words query) queryWords
) )