rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * 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::FieldDataEnum::Dummy;
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::lat_lon_point::LatLonPoint;
use crate::core::document::range_field_query::{QueryType, RangeFieldQuery, RangeFieldQueryBase};
use crate::core::geo::geo_encoding_utils::GeoEncodingUtils;
use crate::core::index::BinaryValueEnum;
use crate::core::index::BytesRef;
use crate::core::index::indexable_field::{
  IndexableField, IndexingTokenStream, ReusedIndexingTokenStream,
};
use crate::core::util::bit_util::BitUtil;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::number::Number;
use crate::core::util::numeric_utils::NumericUtils;
use std::borrow::Cow;
use std::fmt;
use std::fmt::Write as _;
/// An indexed 2-Dimension Bounding Box field for the Geospatial Lat/Lon Coordinate system.
///
/// This field indexes 2-dimension Latitude, Longitude based Geospatial Bounding Boxes. The
/// bounding boxes are defined as `minLat, minLon, maxLat, maxLon` where min/max lat,lon pairs
/// using double floating point precision.
///
/// Multiple values for the same field in one document is supported.
///
/// This field defines the following static factory methods for common search operations over
/// double ranges:
///
/// - [`new_intersects_query`](Self::new_intersects_query) matches bounding boxes that intersect
///   the defined search bounding box.
/// - [`new_within_query`](Self::new_within_query) matches bounding boxes that are within the
///   defined search bounding box.
/// - [`new_contains_query`](Self::new_contains_query) matches bounding boxes that contain the
///   defined search bounding box.
/// - [`new_crosses_query`](Self::new_crosses_query) matches bounding boxes that cross the defined
///   search bounding box.
///
/// The following Field limitations and restrictions apply:
///
/// - Dateline wrapping is not supported.
/// - Due to an encoding limitation Eastern and Western Hemisphere Bounding Boxes that share the
///   dateline are not supported.
pub struct LatLonBoundingBox {
  parent_field: Field,
}

impl LatLonBoundingBox {
  /// Uses same encoding as [`LatLonPoint`] so numBytes is the same.
  pub const BYTES: usize = LatLonPoint::BYTES;

  /// Create a new 2D GeoBoundingBoxField representing a 2 dimensional geospatial bounding box.
  ///
  /// # Arguments
  ///
  /// - `name` - Field name.
  /// - `min_lat` - Minimum latitude value (in degrees); valid in `[-90.0 : 90.0]`.
  /// - `min_lon` - Minimum longitude value (in degrees); valid in `[-180.0 : 180.0]`.
  /// - `max_lat` - Maximum latitude value (in degrees); valid in `[minLat : 90.0]`.
  /// - `max_lon` - Maximum longitude value (in degrees); valid in `[minLon : 180.0]`.
  pub fn new<T>(name: T, min_lat: f64, min_lon: f64, max_lat: f64, max_lon: f64) -> Result<Self>
  where
    T: Into<Cow<'static, str>>,
  {
    let mut parent_field = Field::new(name, Dummy(()), Self::get_type(2)?);
    Self::set_range_values_internal(&mut parent_field, min_lat, min_lon, max_lat, max_lon)?;
    Ok(Self { parent_field })
  }

  fn get_type(geo_dimensions: usize) -> Result<FieldType> {
    let mut ft = FieldType::new();
    ft.set_dimensions(geo_dimensions * 2, Self::BYTES)?;
    ft.freeze();
    Ok(ft)
  }

  /// Changes the values of the field.
  pub fn set_range_values(
    &mut self,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
  ) -> Result<()> {
    Self::set_range_values_internal(&mut self.parent_field, min_lat, min_lon, max_lat, max_lon)
  }

  fn set_range_values_internal(
    parent_field: &mut Field,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
  ) -> Result<()> {
    check_args(min_lat, min_lon, max_lat, max_lon)?;

    let bytes = match &mut parent_field.fields_data {
      FieldDataEnum::Binary(b) => &mut b.bytes,
      FieldDataEnum::Dummy(_) => {
        let new_bytes = vec![0u8; 4 * Self::BYTES];
        parent_field.fields_data = BytesRef::from_bytes(new_bytes).into();
        match &mut parent_field.fields_data {
          FieldDataEnum::Binary(b) => &mut b.bytes,
          _ => return Err(LuceneError::illegal_state("should not be here")),
        }
      },
      _ => Err(LuceneError::illegal_state(
        "Unsupported FieldDataEnum variant",
      ))?,
    };
    encode_point(min_lat, min_lon, bytes, 0)?;
    encode_point(max_lat, max_lon, bytes, 2 * Self::BYTES)
  }

  /// Create a new 2d query that finds all indexed 2d GeoBoundingBoxField values that intersect the
  /// defined 3d bounding ranges.
  ///
  /// # Arguments
  ///
  /// - `field` - Field name.
  /// - `min_lat` - Minimum latitude value (in degrees); valid in `[-90.0 : 90.0]`.
  /// - `min_lon` - Minimum longitude value (in degrees); valid in `[-180.0 : 180.0]`.
  /// - `max_lat` - Maximum latitude value (in degrees); valid in `[minLat : 90.0]`.
  /// - `max_lon` - Maximum longitude value (in degrees); valid in `[minLon : 180.0]`.
  ///
  /// # Returns
  ///
  /// Query for matching intersecting 2d bounding boxes.
  pub fn new_intersects_query<T>(
    field: T,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
  ) -> Result<RangeFieldQuery>
  where
    T: Into<String>,
  {
    Self::new_range_query(
      field,
      min_lat,
      min_lon,
      max_lat,
      max_lon,
      QueryType::Intersects,
    )
  }

  /// Create a new 2d query that finds all indexed 2d GeoBoundingBoxField values that are within the
  /// defined 2d bounding box.
  ///
  /// # Arguments
  ///
  /// - `field` - Field name.
  /// - `min_lat` - Minimum latitude value (in degrees); valid in `[-90.0 : 90.0]`.
  /// - `min_lon` - Minimum longitude value (in degrees); valid in `[-180.0 : 180.0]`.
  /// - `max_lat` - Maximum latitude value (in degrees); valid in `[minLat : 90.0]`.
  /// - `max_lon` - Maximum longitude value (in degrees); valid in `[minLon : 180.0]`.
  ///
  /// # Returns
  ///
  /// Query for matching 3d bounding boxes that are within the defined bounding box.
  pub fn new_within_query<T>(
    field: T,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
  ) -> Result<RangeFieldQuery>
  where
    T: Into<String>,
  {
    Self::new_range_query(field, min_lat, min_lon, max_lat, max_lon, QueryType::Within)
  }

  /// Create a new 2d query that finds all indexed 2d GeoBoundingBoxField values that contain the
  /// defined 2d bounding box.
  ///
  /// # Arguments
  ///
  /// - `field` - Field name.
  /// - `min_lat` - Minimum latitude value (in degrees); valid in `[-90.0 : 90.0]`.
  /// - `min_lon` - Minimum longitude value (in degrees); valid in `[-180.0 : 180.0]`.
  /// - `max_lat` - Maximum latitude value (in degrees); valid in `[minLat : 90.0]`.
  /// - `max_lon` - Maximum longitude value (in degrees); valid in `[minLon : 180.0]`.
  ///
  /// # Returns
  ///
  /// Query for matching 2d bounding boxes that contain the defined bounding box.
  pub fn new_contains_query<T>(
    field: T,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
  ) -> Result<RangeFieldQuery>
  where
    T: Into<String>,
  {
    Self::new_range_query(
      field,
      min_lat,
      min_lon,
      max_lat,
      max_lon,
      QueryType::Contains,
    )
  }

  /// Create a new 2d query that finds all indexed 2d GeoBoundingBoxField values that cross the
  /// defined 3d bounding box.
  ///
  /// # Arguments
  ///
  /// - `field` - Field name.
  /// - `min_lat` - Minimum latitude value (in degrees); valid in `[-90.0 : 90.0]`.
  /// - `min_lon` - Minimum longitude value (in degrees); valid in `[-180.0 : 180.0]`.
  /// - `max_lat` - Maximum latitude value (in degrees); valid in `[minLat : 90.0]`.
  /// - `max_lon` - Maximum longitude value (in degrees); valid in `[minLon : 180.0]`.
  ///
  /// # Returns
  ///
  /// Query for matching 2d bounding boxes that cross the defined bounding box.
  pub fn new_crosses_query<T>(
    field: T,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
  ) -> Result<RangeFieldQuery>
  where
    T: Into<String>,
  {
    Self::new_range_query(
      field,
      min_lat,
      min_lon,
      max_lat,
      max_lon,
      QueryType::Crosses,
    )
  }
  /// helper method to create a two-dimensional geospatial bounding box query
  fn new_range_query<T>(
    field: T,
    min_lat: f64,
    min_lon: f64,
    max_lat: f64,
    max_lon: f64,
    query_type: QueryType,
  ) -> Result<RangeFieldQuery>
  where
    T: Into<String>,
  {
    check_args(min_lat, min_lon, max_lat, max_lon)?;
    RangeFieldQuery::new(
      field.into(),
      encode(min_lat, min_lon, max_lat, max_lon)?,
      2,
      query_type,
      LatLonBoundingBoxFieldQuery,
    )
  }
}

impl FieldBase for LatLonBoundingBox {}

impl IndexableField for LatLonBoundingBox {
  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>> {
    todo!()
  }

  fn numeric_value(&self) -> Result<Option<Number>> {
    Err(LuceneError::illegal_argument(
      "cannot convert LatLonBoundingBox to a single 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 fmt::Display for LatLonBoundingBox {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "LatLonBoundingBox <{}:", self.parent_field.name())?;
    match &self.parent_field.fields_data {
      FieldDataEnum::Binary(bytes) => {
        let mut buffer = String::new();
        to_string(&bytes.bytes, 0, &mut buffer).map_err(|_| fmt::Error)?;
        let min_end = buffer.len();
        to_string(&bytes.bytes, 1, &mut buffer).map_err(|_| fmt::Error)?;
        write!(f, "[{},{}]", &buffer[..min_end], &buffer[min_end..])?;
      },
      _ => {
        write!(f, "Unsupported FieldDataEnum variant")?;
      },
    }
    write!(f, ">")
  }
}

fn check_args(min_lat: f64, min_lon: f64, max_lat: f64, max_lon: f64) -> Result<()> {
  if min_lon > max_lon {
    return Err(LuceneError::illegal_argument(format!(
      "cannot have minLon [{}] exceed maxLon [{}].",
      min_lon, max_lon
    )));
  }
  if min_lat > max_lat {
    return Err(LuceneError::illegal_argument(format!(
      "cannot have minLat [{}] exceed maxLat [{}].",
      min_lat, max_lat
    )));
  }
  Ok(())
}

pub(crate) fn encode(min_lat: f64, min_lon: f64, max_lat: f64, max_lon: f64) -> Result<Vec<u8>> {
  let mut bytes = vec![0u8; LatLonBoundingBox::BYTES * 4];
  encode_point(min_lat, min_lon, &mut bytes, 0)?;
  encode_point(max_lat, max_lon, &mut bytes, LatLonBoundingBox::BYTES * 2)?;
  Ok(bytes)
}

fn encode_point(lat: f64, lon: f64, result: &mut [u8], offset: usize) -> Result<()> {
  NumericUtils::int_to_sortable_bytes(GeoEncodingUtils::encode_latitude(lat)?, result, offset);
  NumericUtils::int_to_sortable_bytes(
    GeoEncodingUtils::encode_longitude(lon)?,
    result,
    offset + BitUtil::INT_BYTES,
  );
  Ok(())
}

fn to_string(ranges: &[u8], dimension: usize, buffer: &mut String) -> Result<()> {
  let (lat, lon) = match dimension {
    0 => (
      GeoEncodingUtils::decode_latitude_from_bytes(ranges, 0),
      GeoEncodingUtils::decode_longitude_from_bytes(ranges, 4),
    ),
    1 => (
      GeoEncodingUtils::decode_latitude_from_bytes(ranges, 8),
      GeoEncodingUtils::decode_longitude_from_bytes(ranges, 12),
    ),
    _ => {
      return Err(LuceneError::illegal_argument(format!(
        "invalid dimension [{}] in toString",
        dimension
      )));
    },
  };
  write!(buffer, "{:?},{:?}", lat, lon)?;
  Ok(())
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LatLonBoundingBoxFieldQuery;

impl RangeFieldQueryBase for LatLonBoundingBoxFieldQuery {
  fn to_string(&self, value: &[u8], dimension: usize) -> Result<String> {
    let mut buffer = String::new();
    to_string(value, dimension, &mut buffer)?;
    Ok(buffer)
  }
}