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_distance_feature_query::LatLonPointDistanceFeatureQuery;
use crate::core::document::lat_lon_point_distance_query::LatLonPointDistanceQuery;
use crate::core::document::lat_lon_point_query::lat_lon_point_query;
use crate::core::document::nearest_neighbor;
use crate::core::document::shape_field::QueryRelation;
use crate::core::geo::geo_encoding_utils::GeoEncodingUtils;
use crate::core::geo::geo_utils::GeoUtils;
use crate::core::geo::lat_lon_geometry::LatLonGeometry;
use crate::core::geo::lat_lon_geometry::LatLonGeometryEnum;
use crate::core::geo::polygon::Polygon;
use crate::core::index::BinaryValueEnum;
use crate::core::index::BytesRef;
use crate::core::index::field_info::FieldInfo;
use crate::core::index::index_reader_context::IndexReaderContext;
use crate::core::index::indexable_field::{
IndexableField, IndexingTokenStream, ReusedIndexingTokenStream,
};
use crate::core::index::indexable_field_type::IndexableFieldType;
use crate::core::index::leaf_reader::LeafReader;
use crate::core::index::point_values::PointValues;
use crate::core::search::boolean_clause::Occur;
use crate::core::search::boolean_query::Builder;
use crate::core::search::boost_query::BoostQuery;
use crate::core::search::constant_score_query::ConstantScoreQuery;
use crate::core::search::field_doc::FieldDoc;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::match_no_docs_query::MatchNoDocsQuery;
use crate::core::search::point_range_query::{PointRangeBase, PointRangeQuery};
use crate::core::search::query::Query;
use crate::core::search::similarities_impl::similarities::Similarity;
use crate::core::search::top_field_docs::TopFieldDocs;
use crate::core::search::total_hits::Relation::EqualTo;
use crate::core::search::total_hits::TotalHits;
use crate::core::util::SloppyMath;
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::{Display, Formatter, Write as _};
use std::sync::LazyLock;
pub(crate) static TYPE_: LazyLock<FieldType> = LazyLock::new(|| {
let mut ft = FieldType::new();
expect_invariant!(
ft.set_dimensions(2, BitUtil::INT_BYTES),
"the built-in latitude-longitude point dimensions are valid"
);
ft.freeze();
ft
});
pub struct LatLonPoint {
parent_field: Field,
}
impl LatLonPoint {
pub const BYTES: usize = BitUtil::INT_BYTES;
pub fn new(name: &str, latitude: f64, longitude: f64) -> Result<Self> {
let mut point = Self {
parent_field: Field::new(name.to_owned(), Dummy(()), TYPE_.clone()),
};
point.set_location_value(latitude, longitude)?;
Ok(point)
}
pub fn set_location_value(&mut self, latitude: f64, longitude: f64) -> Result<()> {
if matches!(self.parent_field.fields_data, FieldDataEnum::Dummy(_)) {
self.parent_field.fields_data = FieldDataEnum::Binary(BytesRef::from_bytes(vec![0u8; 8]));
}
let bytes = match &mut self.parent_field.fields_data {
FieldDataEnum::Binary(v) => v.bytes.as_mut_slice(),
_ => {
return Err(LuceneError::illegal_state("should not be here"));
},
};
let latitude_encoded = GeoEncodingUtils::encode_latitude(latitude)?;
let longitude_encoded = GeoEncodingUtils::encode_longitude(longitude)?;
NumericUtils::int_to_sortable_bytes(latitude_encoded, bytes, 0);
NumericUtils::int_to_sortable_bytes(longitude_encoded, bytes, BitUtil::INT_BYTES);
Ok(())
}
fn encode(latitude: f64, longitude: f64) -> Result<Vec<u8>> {
let mut bytes = vec![0u8; 2 * BitUtil::INT_BYTES];
NumericUtils::int_to_sortable_bytes(
GeoEncodingUtils::encode_latitude(latitude)?,
&mut bytes,
0,
);
NumericUtils::int_to_sortable_bytes(
GeoEncodingUtils::encode_longitude(longitude)?,
&mut bytes,
BitUtil::INT_BYTES,
);
Ok(bytes)
}
fn encode_ceil(latitude: f64, longitude: f64) -> Result<Vec<u8>> {
let mut bytes = vec![0u8; 2 * BitUtil::INT_BYTES];
NumericUtils::int_to_sortable_bytes(
GeoEncodingUtils::encode_latitude_ceil(latitude)?,
&mut bytes,
0,
);
NumericUtils::int_to_sortable_bytes(
GeoEncodingUtils::encode_longitude_ceil(longitude)?,
&mut bytes,
BitUtil::INT_BYTES,
);
Ok(bytes)
}
pub(crate) fn check_compatible(field_info: &FieldInfo) -> Result<()> {
if field_info.get_point_dimension_count() != 0
&& field_info.get_point_dimension_count() != TYPE_.point_dimension_count()
{
return Err(LuceneError::illegal_argument(format!(
"field=\"{}\" was indexed with numDims={} but this point type has numDims={}, is the field really a LatLonPoint?",
field_info.name,
field_info.get_point_dimension_count(),
TYPE_.point_dimension_count()
)));
}
if field_info.get_point_num_bytes() != 0
&& field_info.get_point_num_bytes() != TYPE_.point_num_bytes()
{
return Err(LuceneError::illegal_argument(format!(
"field=\"{}\" was indexed with bytesPerDim={} but this point type has bytesPerDim={}, is the field really a LatLonPoint?",
field_info.name,
field_info.get_point_num_bytes(),
TYPE_.point_num_bytes()
)));
}
Ok(())
}
pub fn new_box_query(
field: &str,
min_latitude: f64,
max_latitude: f64,
min_longitude: f64,
max_longitude: f64,
) -> Result<Query> {
let mut min_longitude = min_longitude;
if min_latitude == 90.0 {
return Ok(
MatchNoDocsQuery::with_reason("LatLonPoint.newBoxQuery with minLatitude=90.0").into(),
);
}
if min_longitude == 180.0 {
if max_longitude == 180.0 {
return Ok(
MatchNoDocsQuery::with_reason(
"LatLonPoint.newBoxQuery with minLongitude=maxLongitude=180.0",
)
.into(),
);
} else if max_longitude < min_longitude {
min_longitude = -180.0;
}
}
let lower = LatLonPoint::encode_ceil(min_latitude, min_longitude)?;
let upper = LatLonPoint::encode(max_latitude, max_longitude)?;
if max_longitude < min_longitude {
let mut q = Builder::new();
let mut left_open = lower.clone();
NumericUtils::int_to_sortable_bytes(i32::MIN, &mut left_open, BitUtil::INT_BYTES);
let left = Self::new_box_internal(field, left_open, upper.clone())?;
q.add(left, Occur::Should)?;
let mut right_open = upper;
NumericUtils::int_to_sortable_bytes(i32::MAX, &mut right_open, BitUtil::INT_BYTES);
let right = Self::new_box_internal(field, lower, right_open)?;
q.add(right, Occur::Should)?;
Ok(ConstantScoreQuery::new(q.build()).into())
} else {
Ok(Self::new_box_internal(field, lower, upper)?.into())
}
}
fn new_box_internal(field: &str, min: Vec<u8>, max: Vec<u8>) -> Result<PointRangeQuery> {
PointRangeQuery::new(field.to_string(), min, max, 2, LatLonPointRangeQuery)
}
pub fn new_distance_query(
field: &str,
latitude: f64,
longitude: f64,
radius_meters: f64,
) -> Result<LatLonPointDistanceQuery> {
LatLonPointDistanceQuery::new(field.to_string(), latitude, longitude, radius_meters)
}
pub fn new_polygon_query<I>(field: &str, polygons: I) -> Result<Query>
where
I: IntoIterator<Item = Polygon>,
{
Self::new_geometry_query(field, QueryRelation::Intersects, polygons)
}
pub fn new_geometry_query<T, I>(
field: &str,
query_relation: QueryRelation,
lat_lon_geometries: I,
) -> Result<Query>
where
T: LatLonGeometry + Into<LatLonGeometryEnum>,
I: IntoIterator<Item = T>,
{
let lat_lon_geometries: Vec<LatLonGeometryEnum> =
lat_lon_geometries.into_iter().map(Into::into).collect();
if query_relation == QueryRelation::Intersects && lat_lon_geometries.len() == 1 {
match &lat_lon_geometries[0] {
LatLonGeometryEnum::Rectangle(rect) => {
return Self::new_box_query(
field,
rect.min_lat,
rect.max_lat,
rect.min_lon,
rect.max_lon,
);
},
LatLonGeometryEnum::Circle(circle) => {
return Ok(
Self::new_distance_query(
field,
circle.get_lat(),
circle.get_lon(),
circle.get_radius(),
)?
.into(),
);
},
_ => {},
}
}
if query_relation == QueryRelation::Contains {
return Self::make_contains_geometry_query(field, lat_lon_geometries);
}
Ok(lat_lon_point_query(field.to_string(), query_relation, lat_lon_geometries)?.into())
}
fn make_contains_geometry_query<T>(field: &str, lat_lon_geometries: Vec<T>) -> Result<Query>
where
T: LatLonGeometry + Into<LatLonGeometryEnum>,
{
let mut builder = Builder::new();
for geometry in lat_lon_geometries {
let geometry = geometry.into();
if !matches!(geometry, LatLonGeometryEnum::Point(_)) {
return Ok(
MatchNoDocsQuery::with_reason(
"Contains LatLonPoint.newGeometryQuery with non-point geometries",
)
.into(),
);
}
builder.add(
lat_lon_point_query(field.to_string(), QueryRelation::Contains, vec![geometry])?,
Occur::Must,
)?;
}
Ok(ConstantScoreQuery::new(builder.build()).into())
}
pub fn new_distance_feature_query(
field: &str,
weight: f32,
origin_lat: f64,
origin_lon: f64,
pivot_distance_meters: f64,
) -> Result<Query> {
let mut query: Query = LatLonPointDistanceFeatureQuery::new(
field.to_string(),
origin_lat,
origin_lon,
pivot_distance_meters,
)?
.into();
if weight != 1f32 {
query = BoostQuery::new(query, weight)?.into();
}
Ok(query)
}
pub fn nearest<IRC, SIM>(
searcher: &IndexSearcher<IRC, SIM>,
field: &str,
latitude: f64,
longitude: f64,
n: i32,
) -> Result<TopFieldDocs>
where
SIM: Similarity,
IRC: IndexReaderContext,
IndexSearcher<IRC, SIM>: Sync,
{
GeoUtils::check_latitude(latitude)?;
GeoUtils::check_longitude(longitude)?;
if n < 1 {
return Err(LuceneError::illegal_argument(format!(
"n must be at least 1; got {}",
n
)));
}
let mut readers = Vec::new();
let mut doc_bases = Vec::new();
let mut live_docs = Vec::new();
let mut total_hits = 0;
for leaf in searcher.get_leaf_contexts()? {
let reader = leaf.reader();
let points = reader.get_point_values(field)?;
if let Some(points) = points {
total_hits += points.get_doc_count()?;
readers.push(points);
doc_bases.push(leaf.doc_base as i32);
live_docs.push(reader.get_live_docs()?);
}
}
let hits = nearest_neighbor::nearest(
latitude, longitude, &readers, &live_docs, &doc_bases, n as usize,
)?;
let mut score_docs = Vec::with_capacity(hits.len());
for hit in hits {
let hit_distance = SloppyMath::haversin_meters_from_sort_key(hit.distance_sort_key);
score_docs.push(FieldDoc::with_fields(hit.doc_id, 0.0, vec![hit_distance.into()]).into());
}
Ok(TopFieldDocs::new(
TotalHits::new(total_hits as usize, EqualTo),
score_docs,
vec![],
))
}
}
impl Display for LatLonPoint {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut result = String::new();
result.push_str(
std::any::type_name::<Self>()
.rsplit("::")
.next()
.unwrap_or("LatLonPoint"),
);
result.push_str(" <");
result.push_str(&self.parent_field.name);
result.push(':');
let bytes = match self.parent_field.fields_data {
FieldDataEnum::Binary(ref v) => v.bytes.as_slice(),
_ => {
return Err(std::fmt::Error);
},
};
write!(
result,
"{}",
GeoEncodingUtils::decode_latitude_from_bytes(bytes, 0)
)?;
result.push(',');
write!(
result,
"{}",
GeoEncodingUtils::decode_longitude_from_bytes(bytes, BitUtil::INT_BYTES)
)?;
result.push('>');
write!(f, "{result}")
}
}
impl IndexableField for LatLonPoint {
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 LatLonPoint {}
#[derive(Debug, Clone)]
pub struct LatLonPointRangeQuery;
impl PointRangeBase for LatLonPointRangeQuery {
fn to_string(&self, dimension: usize, value: &[u8]) -> Result<String> {
match dimension {
0 => Ok(GeoEncodingUtils::decode_latitude_from_bytes(value, 0).to_string()),
1 => Ok(GeoEncodingUtils::decode_longitude_from_bytes(value, 0).to_string()),
_ => Err(LuceneError::illegal_state(format!(
"invalid dimension {} for LatLonPoint, must be 0 or 1",
dimension
))),
}
}
}