Skip to content

Commit 685bcc8

Browse files
authored
geo s2 analyzer (#502)
1 parent 6bef3ec commit 685bcc8

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

src/main/java/com/arangodb/entity/arangosearch/AnalyzerType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public enum AnalyzerType {
3535
aql,
3636
geojson,
3737
geopoint,
38+
geo_s2,
3839
segmentation,
3940
collation,
4041
classification,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
22+
package com.arangodb.entity.arangosearch.analyzer;
23+
24+
import com.arangodb.entity.arangosearch.AnalyzerType;
25+
26+
import java.util.Objects;
27+
28+
/**
29+
* An Analyzer capable of breaking up a GeoJSON object or coordinate array in [longitude, latitude] order into a set of
30+
* indexable tokens for further usage with ArangoSearch Geo functions.
31+
* <p>
32+
* The Analyzer is similar to {@link GeoJSONAnalyzer}, but it internally uses a format for storing the geo-spatial data
33+
* that is more efficient. You can choose between different formats to make a tradeoff between the size on disk, the
34+
* precision, and query performance.
35+
*
36+
* @author Michele Rastelli
37+
* @see <a href= "https://www.arangodb.com/docs/stable/analyzers.html#geo_s2">API Documentation</a>
38+
* @since ArangoDB 3.10.5
39+
*/
40+
public final class GeoS2Analyzer extends SearchAnalyzer {
41+
private GeoS2AnalyzerProperties properties;
42+
43+
public GeoS2Analyzer() {
44+
setType(AnalyzerType.geo_s2);
45+
}
46+
47+
public GeoS2AnalyzerProperties getProperties() {
48+
return properties;
49+
}
50+
51+
public void setProperties(GeoS2AnalyzerProperties properties) {
52+
this.properties = properties;
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) return true;
58+
if (o == null || getClass() != o.getClass()) return false;
59+
if (!super.equals(o)) return false;
60+
GeoS2Analyzer that = (GeoS2Analyzer) o;
61+
return Objects.equals(properties, that.properties);
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(super.hashCode(), properties);
67+
}
68+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity.arangosearch.analyzer;
22+
23+
24+
import java.util.Objects;
25+
26+
/**
27+
* @author Michele Rastelli
28+
*/
29+
public final class GeoS2AnalyzerProperties {
30+
31+
private GeoS2AnalyzerType type;
32+
private GeoAnalyzerOptions options;
33+
private GeoS2Format format;
34+
35+
public GeoS2AnalyzerType getType() {
36+
return type;
37+
}
38+
39+
public void setType(GeoS2AnalyzerType type) {
40+
this.type = type;
41+
}
42+
43+
/**
44+
* @return Options for fine-tuning geo queries {@link GeoS2AnalyzerProperties}. These options should generally
45+
* remain unchanged.
46+
*/
47+
public GeoAnalyzerOptions getOptions() {
48+
return options;
49+
}
50+
51+
public void setOptions(GeoAnalyzerOptions options) {
52+
this.options = options;
53+
}
54+
55+
/**
56+
* @return The internal binary representation to use for storing the geo-spatial data in an index.
57+
*/
58+
public GeoS2Format getFormat() {
59+
return format;
60+
}
61+
62+
public void setFormat(GeoS2Format format) {
63+
this.format = format;
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) return true;
69+
if (o == null || getClass() != o.getClass()) return false;
70+
GeoS2AnalyzerProperties that = (GeoS2AnalyzerProperties) o;
71+
return type == that.type && Objects.equals(options, that.options) && format == that.format;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(type, options, format);
77+
}
78+
79+
public enum GeoS2AnalyzerType {
80+
81+
/**
82+
* (default): index all GeoJSON geometry types (Point, Polygon etc.)
83+
*/
84+
shape,
85+
86+
/**
87+
* compute and only index the centroid of the input geometry
88+
*/
89+
centroid,
90+
91+
/**
92+
* only index GeoJSON objects of type Point, ignore all other geometry types
93+
*/
94+
point
95+
}
96+
97+
public enum GeoS2Format {
98+
/**
99+
* Store each latitude and longitude value as an 8-byte floating-point value (16 bytes per coordinate pair).
100+
* This format preserves numeric values exactly and is more compact than the VelocyPack format used by
101+
* {@link GeoJSONAnalyzer}. (default)
102+
*/
103+
latLngDouble,
104+
105+
/**
106+
* Store each latitude and longitude value as an 4-byte integer value (8 bytes per coordinate pair). This is the
107+
* most compact format but the precision is limited to approximately 1 to 10 centimeters.
108+
*/
109+
latLngInt,
110+
111+
/**
112+
* Store each longitude-latitude pair in the native format of Google S2 which is used for geo-spatial
113+
* calculations (24 bytes per coordinate pair). This is not a particular compact format but it reduces the
114+
* number of computations necessary when you execute geo-spatial queries. This format preserves numeric values
115+
* exactly.
116+
*/
117+
s2Point
118+
}
119+
}

src/main/java/com/arangodb/internal/velocypack/VPackDeserializers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class VPackDeserializers {
8888
return context.deserialize(vpack, GeoJSONAnalyzer.class);
8989
case geopoint:
9090
return context.deserialize(vpack, GeoPointAnalyzer.class);
91+
case geo_s2:
92+
return context.deserialize(vpack, GeoS2Analyzer.class);
9193
case segmentation:
9294
return context.deserialize(vpack, SegmentationAnalyzer.class);
9395
case collation:

src/test/java/com/arangodb/ArangoSearchTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,36 @@ void geoJsonAnalyzer(ArangoDatabase db) {
11041104
}
11051105

11061106

1107+
@ParameterizedTest(name = "{index}")
1108+
@MethodSource("dbs")
1109+
void geoS2Analyzer(ArangoDatabase db) {
1110+
assumeTrue(isEnterprise());
1111+
assumeTrue(isAtLeastVersion(3, 10, 5));
1112+
1113+
GeoAnalyzerOptions options = new GeoAnalyzerOptions();
1114+
options.setMaxLevel(10);
1115+
options.setMaxCells(11);
1116+
options.setMinLevel(8);
1117+
1118+
GeoS2AnalyzerProperties properties = new GeoS2AnalyzerProperties();
1119+
properties.setOptions(options);
1120+
properties.setType(GeoS2AnalyzerProperties.GeoS2AnalyzerType.point);
1121+
properties.setFormat(GeoS2AnalyzerProperties.GeoS2Format.s2Point);
1122+
1123+
Set<AnalyzerFeature> features = new HashSet<>();
1124+
features.add(AnalyzerFeature.frequency);
1125+
features.add(AnalyzerFeature.norm);
1126+
features.add(AnalyzerFeature.position);
1127+
1128+
GeoS2Analyzer geoS2Analyzer = new GeoS2Analyzer();
1129+
geoS2Analyzer.setName("test-" + UUID.randomUUID());
1130+
geoS2Analyzer.setProperties(properties);
1131+
geoS2Analyzer.setFeatures(features);
1132+
1133+
createGetAndDeleteTypedAnalyzer(db, geoS2Analyzer);
1134+
}
1135+
1136+
11071137
@ParameterizedTest(name = "{index}")
11081138
@MethodSource("dbs")
11091139
void geoPointAnalyzer(ArangoDatabase db) {

0 commit comments

Comments
 (0)