Skip to content

Commit

Permalink
feat: Add GeoJSON Analyzer (#87)
Browse files Browse the repository at this point in the history
* Initial support for GeoJson Analyzer (3.8.*)

* Add test

* Fix instructions

Co-authored-by: Evan Chan <evan@urbanlogiq.com>
  • Loading branch information
velvia and Evan Chan committed Sep 1, 2022
1 parent a1635d9 commit b85d3d9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ println!("{:?}", resp);

Contributions and feed back are welcome following Github workflow.

Setup instructions:
1. Install a local ArangoDB, version 3.8 or above, port 8529. Use `docker compose`.
2. Run `tests/init_db.sh`

### License

`arangors` is provided under the MIT license. See [LICENSE](./LICENSE).
Expand Down
29 changes: 29 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ pub enum NgramStreamType {
Utf8,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum GeoJsonType {
Shape,
Centroid,
Point,
}

#[derive(Debug, Serialize, Deserialize, TypedBuilder, PartialEq)]
#[builder(doc)]
pub struct DelimiterAnalyzerProperties {
Expand Down Expand Up @@ -114,6 +122,17 @@ pub struct TextAnalyzerProperties {
pub stemming: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, TypedBuilder, PartialEq)]
#[builder(doc)]
pub struct GeoJsonAnalyzerProperties {
/// Whether to index all GeoJSON geometry types, just the centroid, or just points
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub r#type: Option<GeoJsonType>,

// Skip the options as they "generally should remain unchanged"
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum AnalyzerInfo {
Expand Down Expand Up @@ -173,6 +192,16 @@ pub enum AnalyzerInfo {
#[serde(skip_serializing_if = "Option::is_none")]
properties: Option<TextAnalyzerProperties>,
},

Geojson {
name: String,

#[serde(skip_serializing_if = "Option::is_none")]
features: Option<Vec<AnalyzerFeature>>,

#[serde(skip_serializing_if = "Option::is_none")]
properties: Option<GeoJsonAnalyzerProperties>,
},
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
42 changes: 41 additions & 1 deletion tests/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use uclient::ClientExt;

use arangors::analyzer::{
AnalyzerCase, AnalyzerFeature, AnalyzerInfo, NgramAnalyzerProperties, NgramStreamType,
NormAnalyzerProperties,
NormAnalyzerProperties, GeoJsonAnalyzerProperties, GeoJsonType,
};
use arangors::{
collection::{
Expand Down Expand Up @@ -65,6 +65,24 @@ async fn create_ngram_analyzer<C: ClientExt>(
database.create_analyzer(info).await
}

#[maybe_async]
async fn create_geo_analyzer<C: ClientExt>(
database: &Database<C>,
analyzer_name: String,
) -> Result<AnalyzerInfo, ClientError> {
let info = AnalyzerInfo::Geojson {
name: analyzer_name,
features: Some(vec![AnalyzerFeature::Frequency, AnalyzerFeature::Norm]),
properties: Some(
GeoJsonAnalyzerProperties::builder()
.r#type(GeoJsonType::Centroid)
.build(),
),
};

database.create_analyzer(info).await
}

#[maybe_async::test(
any(feature = "reqwest_blocking"),
async(any(feature = "reqwest_async"), tokio::test),
Expand Down Expand Up @@ -109,6 +127,28 @@ async fn test_create_and_drop_ngram_analyzer() {
assert_eq!(result.is_err(), false);
}

#[maybe_async::test(
any(feature = "reqwest_blocking"),
async(any(feature = "reqwest_async"), tokio::test),
async(any(feature = "surf_async"), async_std::test)
)]
async fn test_create_and_drop_geo_analyzer() {
test_setup();
let analyzer_name = "test_analyzer_geo_create".to_string();
let conn = connection().await;
let database = conn.db("test_db").await.unwrap();

let analyzer = create_geo_analyzer(&database, analyzer_name.clone()).await;

trace!("{:?}", analyzer);

assert_eq!(analyzer.is_err(), false);

let result = database.drop_analyzer(&analyzer_name).await;

assert_eq!(result.is_err(), false);
}

#[maybe_async::test(
any(feature = "reqwest_blocking"),
async(any(feature = "reqwest_async"), tokio::test),
Expand Down

0 comments on commit b85d3d9

Please sign in to comment.