1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::core::analysis::analyzer::Analyzer;
use crate::core::analysis::reader::ReaderEnum;
use crate::core::document::field::{Field, FieldBase, FieldDataEnum};
use crate::core::document::field_type::FieldType;
use crate::core::document::invertable_field::InvertableType;
use crate::core::document::xy_doc_values_point_in_geometry_query::XYDocValuesPointInGeometryQuery;
use crate::core::document::xy_point_sort_field::XYPointSortField;
use crate::core::geo::xy_circle::XYCircle;
use crate::core::geo::xy_encoding_utils::XYEncodingUtils;
use crate::core::geo::xy_geometry::{XYGeometry, XYGeometryEnum};
use crate::core::geo::xy_polygon::XYPolygon;
use crate::core::geo::xy_rectangle::XYRectangle;
use crate::core::index::BinaryValueEnum;
use crate::core::index::BytesRef;
use crate::core::index::doc_values_type::DocValuesType;
use crate::core::index::field_info::FieldInfo;
use crate::core::index::indexable_field::{
IndexableField, IndexingTokenStream, ReusedIndexingTokenStream,
};
use crate::core::index::indexable_field_type::IndexableFieldType;
use crate::core::search::query::Query;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::number::Number;
use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::sync::LazyLock;
/// Type for a XYDocValuesField
///
/// Each value stores an `i64` where the upper 32 bits are the encoded x value, and the
/// lower 32 bits are the encoded y value.
///
/// See [`XYEncodingUtils::decode`].
pub(crate) static TYPE: LazyLock<FieldType> = LazyLock::new(|| {
let mut ft = FieldType::new();
expect_invariant!(
ft.set_doc_values_type(DocValuesType::SortedNumeric),
"the built-in XY doc-values field type is mutable and unfrozen"
);
ft.freeze();
ft
});
/// An per-document location field.
///
/// Sorting by distance is efficient. Multiple values for the same field in one document is
/// allowed.
///
/// This field defines static factory methods for common operations:
///
/// - `new_slow_box_query` for matching points within a bounding box.
/// - `new_slow_distance_query` for matching points within a specified
/// distance.
/// - `new_slow_polygon_query` for matching points within an arbitrary
/// polygon.
/// - `new_slow_geometry_query` for matching points within an
/// arbitrary geometry.
/// - `new_distance_sort` for ordering documents by distance from a
/// specified location.
///
/// If you also need query operations, you should add a separate [`XYPointField`](crate::core::document::xy_point_field::XYPointField) instance. If
/// you also need to store the value, you should add a separate [`StoredField`](crate::core::document::stored_field::StoredField) instance.
///
/// See [`XYPointField`](crate::core::document::xy_point_field::XYPointField).
pub struct XYDocValuesField {
parent_field: Field,
}
impl XYDocValuesField {
/// Creates a new XYDocValuesField with the specified x and y
///
/// # Parameters
///
/// - `name`: field name
/// - `x`: x value.
/// - `y`: y values.
///
/// # Errors
///
/// Returns an error if `x` or `y` is infinite or NaN.
pub fn new<T>(name: T, x: f32, y: f32) -> Result<Self>
where
T: Into<Cow<'static, str>>,
{
let mut field = Self {
parent_field: Field::new(name.into(), 0i64, TYPE.clone()),
};
field.set_location_value(x, y)?;
Ok(field)
}
/// Change the values of this field
///
/// # Parameters
///
/// - `x`: x value.
/// - `y`: y value.
///
/// # Errors
///
/// Returns an error if x or y are infinite or NaN.
pub fn set_location_value(&mut self, x: f32, y: f32) -> Result<()> {
let x_encoded = XYEncodingUtils::encode(x)?;
let y_encoded = XYEncodingUtils::encode(y)?;
let value = ((x_encoded as i64) << 32) | (y_encoded as u32 as i64);
self.parent_field.fields_data = value.into();
Ok(())
}
/// Checks field information and returns an error if it is definitely not an [`XYDocValuesField`](crate::core::document::xy_doc_values_field::XYDocValuesField).
pub(crate) fn check_compatible(field_info: &FieldInfo) -> Result<()> {
if *field_info.get_doc_values_type() != DocValuesType::None
&& field_info.get_doc_values_type() != TYPE.doc_values_type()
{
return Err(LuceneError::illegal_argument(format!(
"field=\"{}\" was indexed with docValuesType={:?} but this type has docValuesType={:?}, is the field really a XYDocValuesField?",
field_info.name,
field_info.get_doc_values_type(),
TYPE.doc_values_type()
)));
}
Ok(())
}
/// Creates a SortField for sorting by distance from a location.
///
/// This sort orders documents by ascending distance from the location. The value returned in
/// [`FieldDoc`](crate::core::search::field_doc::FieldDoc) for the hits contains a Double instance with the distance in meters.
///
/// If a document is missing the field, then by default it is treated as having
/// [`f64::INFINITY`] distance (missing values sort last).
///
/// If a document contains multiple values for the field, the *closest* distance to the
/// location is used.
///
/// # Parameters
///
/// - `field`: field name.
/// - `x`: x at the center.
/// - `y`: y at the center.
///
/// # Returns
///
/// SortField ordering documents by distance
///
/// # Errors
///
/// Returns an error if the location has invalid coordinates.
pub fn new_distance_sort<T>(field: T, x: f32, y: f32) -> Result<XYPointSortField>
where
T: Into<String>,
{
XYPointSortField::new(field, x, y)
}
/// Create a query for matching a bounding box using doc values. This query is usually slow as it
/// does not use an index structure and needs to verify documents one-by-one in order to know
/// whether they match. It is best used wrapped in an [`IndexOrDocValuesQuery`](crate::core::search::index_or_doc_values_query::IndexOrDocValuesQuery) alongside a
/// [`XYPointField::new_box_query`](crate::core::document::xy_point_field::XYPointField::new_box_query).
pub fn new_slow_box_query<T>(
field: T,
min_x: f32,
max_x: f32,
min_y: f32,
max_y: f32,
) -> Result<Query>
where
T: Into<String>,
{
let rectangle = XYRectangle::new(min_x, max_x, min_y, max_y)?;
Ok(XYDocValuesPointInGeometryQuery::new(field.into(), vec![rectangle.into()])?.into())
}
/// Create a query for matching points within the specified distance of the supplied location. This
/// query is usually slow as it does not use an index structure and needs to verify documents
/// one-by-one in order to know whether they match. It is best used wrapped in an
/// [`IndexOrDocValuesQuery`](crate::core::search::index_or_doc_values_query::IndexOrDocValuesQuery) alongside a [`XYPointField::new_distance_query`](crate::core::document::xy_point_field::XYPointField::new_distance_query).
///
/// # Parameters
///
/// - `field`: field name.
/// - `x`: x at the center.
/// - `y`: y at the center: must be within standard +/-180 coordinate bounds.
/// - `radius`: maximum distance from the center in cartesian distance: must be non-negative and
/// finite.
///
/// # Returns
///
/// Query matching points within this distance
///
/// # Errors
///
/// Returns an error if the location or radius is invalid.
pub fn new_slow_distance_query<T>(field: T, x: f32, y: f32, radius: f32) -> Result<Query>
where
T: Into<String>,
{
let circle = XYCircle::new(x, y, radius)?;
Ok(XYDocValuesPointInGeometryQuery::new(field.into(), vec![circle.into()])?.into())
}
/// Create a query for matching points within the supplied polygons. This query is usually slow as
/// it does not use an index structure and needs to verify documents one-by-one in order to know
/// whether they match. It is best used wrapped in an [`IndexOrDocValuesQuery`](crate::core::search::index_or_doc_values_query::IndexOrDocValuesQuery) alongside a
/// [`XYPointField::new_polygon_query`](crate::core::document::xy_point_field::XYPointField::new_polygon_query).
///
/// # Parameters
///
/// - `field`: field name.
/// - `polygons`: array of polygons. must not be empty.
///
/// # Returns
///
/// Query matching points within the given polygons.
///
/// # Errors
///
/// Returns an error if `polygons` is empty.
pub fn new_slow_polygon_query<T, I>(field: T, polygons: I) -> Result<Query>
where
T: Into<String>,
I: IntoIterator<Item = XYPolygon>,
{
Self::new_slow_geometry_query(field, polygons)
}
/// Create a query for matching points within the supplied geometries. XYLine geometries are not
/// supported. This query is usually slow as it does not use an index structure and needs to verify
/// documents one-by-one in order to know whether they match. It is best used wrapped in an
/// [`IndexOrDocValuesQuery`](crate::core::search::index_or_doc_values_query::IndexOrDocValuesQuery) alongside a [`XYPointField::new_geometry_query`](crate::core::document::xy_point_field::XYPointField::new_geometry_query).
///
/// # Parameters
///
/// - `field`: field name.
/// - `geometries`: array of XY geometries. must not be empty.
///
/// # Returns
///
/// Query matching points within the given geometries.
///
/// # Errors
///
/// Returns an error if `geometries` is empty or contains an unsupported [`XYLine`](crate::core::geo::xy_line::XYLine) geometry.
pub fn new_slow_geometry_query<S, T, I>(field: S, geometries: I) -> Result<Query>
where
S: Into<String>,
T: XYGeometry + Into<XYGeometryEnum>,
I: IntoIterator<Item = T>,
{
let geometries: Vec<XYGeometryEnum> = geometries.into_iter().map(Into::into).collect();
Ok(XYDocValuesPointInGeometryQuery::new(field.into(), geometries)?.into())
}
}
impl Display for XYDocValuesField {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let current_value = match self.parent_field.fields_data {
FieldDataEnum::Number(Number::I64(v)) => v as u64,
_ => {
return Err(std::fmt::Error);
},
};
let x = XYEncodingUtils::decode((current_value >> 32) as i32);
let y = XYEncodingUtils::decode(current_value as u32 as i32);
write!(
f,
"XYDocValuesField <{}:{},{}>",
self.parent_field.name, x, y
)
}
}
impl IndexableField for XYDocValuesField {
fn name(&self) -> &str {
self.parent_field.name()
}
type FieldType<'a>
= &'a FieldType
where
Self: 'a;
fn field_type(&self) -> Self::FieldType<'_> {
self.parent_field.field_type()
}
fn token_stream<'a, A>(
&'a mut self,
analyzer: &'a A,
reuse_token_stream: &'a mut Option<ReusedIndexingTokenStream>,
) -> Result<IndexingTokenStream<'a>>
where
A: Analyzer,
{
self.parent_field.token_stream(analyzer, reuse_token_stream)
}
fn binary_value(&self) -> Result<Option<BinaryValueEnum<'_>>> {
self.parent_field.binary_value()
}
fn take_binary_value(&mut self) -> Result<Option<BytesRef<Vec<u8>>>> {
self.parent_field.take_binary_value()
}
fn string_value(&self) -> Result<Option<Cow<'_, str>>> {
self.parent_field.string_value()
}
fn take_string_value(&mut self) -> Result<Option<String>> {
self.parent_field.take_string_value()
}
fn take_reader_value(&mut self) -> Result<Option<ReaderEnum>> {
self.parent_field.take_reader_value()
}
fn numeric_value(&self) -> Result<Option<Number>> {
self.parent_field.numeric_value()
}
type StoredValue<'a>
= &'a FieldDataEnum
where
Self: 'a;
fn stored_value(&self) -> Result<Option<Self::StoredValue<'_>>> {
self.parent_field.stored_value()
}
fn invertable_type(&self) -> &InvertableType {
self.parent_field.invertable_type()
}
}
impl FieldBase for XYDocValuesField {}