You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2018/07/28 04:49:59 UTC
[36/50] [abbrv] lucene-solr:jira/solr-11990: LUCENE-7314: Graduate
LatLonPoint and query classes to core
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
deleted file mode 100644
index 1b2e3a6..0000000
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import java.io.IOException;
-import java.util.Arrays;
-
-import org.apache.lucene.geo.GeoEncodingUtils;
-import org.apache.lucene.geo.Polygon;
-import org.apache.lucene.geo.Polygon2D;
-import org.apache.lucene.geo.Rectangle;
-import org.apache.lucene.index.FieldInfo;
-import org.apache.lucene.index.LeafReader;
-import org.apache.lucene.index.LeafReaderContext;
-import org.apache.lucene.index.PointValues;
-import org.apache.lucene.index.PointValues.IntersectVisitor;
-import org.apache.lucene.index.PointValues.Relation;
-import org.apache.lucene.search.ConstantScoreScorer;
-import org.apache.lucene.search.ConstantScoreWeight;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreMode;
-import org.apache.lucene.search.Scorer;
-import org.apache.lucene.search.Weight;
-import org.apache.lucene.util.DocIdSetBuilder;
-import org.apache.lucene.util.NumericUtils;
-import org.apache.lucene.util.StringHelper;
-
-import static org.apache.lucene.geo.GeoEncodingUtils.decodeLatitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.decodeLongitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.encodeLatitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.encodeLongitude;
-
-/** Finds all previously indexed points that fall within the specified polygons.
- *
- * <p>The field must be indexed with using {@link org.apache.lucene.document.LatLonPoint} added per document.
- *
- * @lucene.experimental */
-
-final class LatLonPointInPolygonQuery extends Query {
- final String field;
- final Polygon[] polygons;
-
- LatLonPointInPolygonQuery(String field, Polygon[] polygons) {
- if (field == null) {
- throw new IllegalArgumentException("field must not be null");
- }
- if (polygons == null) {
- throw new IllegalArgumentException("polygons must not be null");
- }
- if (polygons.length == 0) {
- throw new IllegalArgumentException("polygons must not be empty");
- }
- for (int i = 0; i < polygons.length; i++) {
- if (polygons[i] == null) {
- throw new IllegalArgumentException("polygon[" + i + "] must not be null");
- }
- }
- this.field = field;
- this.polygons = polygons.clone();
- // TODO: we could also compute the maximal inner bounding box, to make relations faster to compute?
- }
-
- @Override
- public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
-
- // I don't use RandomAccessWeight here: it's no good to approximate with "match all docs"; this is an inverted structure and should be
- // used in the first pass:
-
- // bounding box over all polygons, this can speed up tree intersection/cheaply improve approximation for complex multi-polygons
- // these are pre-encoded with LatLonPoint's encoding
- final Rectangle box = Rectangle.fromPolygon(polygons);
- final byte minLat[] = new byte[Integer.BYTES];
- final byte maxLat[] = new byte[Integer.BYTES];
- final byte minLon[] = new byte[Integer.BYTES];
- final byte maxLon[] = new byte[Integer.BYTES];
- NumericUtils.intToSortableBytes(encodeLatitude(box.minLat), minLat, 0);
- NumericUtils.intToSortableBytes(encodeLatitude(box.maxLat), maxLat, 0);
- NumericUtils.intToSortableBytes(encodeLongitude(box.minLon), minLon, 0);
- NumericUtils.intToSortableBytes(encodeLongitude(box.maxLon), maxLon, 0);
-
- final Polygon2D tree = Polygon2D.create(polygons);
- final GeoEncodingUtils.PolygonPredicate polygonPredicate = GeoEncodingUtils.createPolygonPredicate(polygons, tree);
-
- return new ConstantScoreWeight(this, boost) {
-
- @Override
- public Scorer scorer(LeafReaderContext context) throws IOException {
- LeafReader reader = context.reader();
- PointValues values = reader.getPointValues(field);
- if (values == null) {
- // No docs in this segment had any points fields
- return null;
- }
- FieldInfo fieldInfo = reader.getFieldInfos().fieldInfo(field);
- if (fieldInfo == null) {
- // No docs in this segment indexed this field at all
- return null;
- }
- LatLonPoint.checkCompatible(fieldInfo);
-
- // matching docids
- DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, field);
-
- values.intersect(
- new IntersectVisitor() {
-
- DocIdSetBuilder.BulkAdder adder;
-
- @Override
- public void grow(int count) {
- adder = result.grow(count);
- }
-
- @Override
- public void visit(int docID) {
- adder.add(docID);
- }
-
- @Override
- public void visit(int docID, byte[] packedValue) {
- if (polygonPredicate.test(NumericUtils.sortableBytesToInt(packedValue, 0),
- NumericUtils.sortableBytesToInt(packedValue, Integer.BYTES))) {
- adder.add(docID);
- }
- }
-
- @Override
- public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
- if (StringHelper.compare(Integer.BYTES, minPackedValue, 0, maxLat, 0) > 0 ||
- StringHelper.compare(Integer.BYTES, maxPackedValue, 0, minLat, 0) < 0 ||
- StringHelper.compare(Integer.BYTES, minPackedValue, Integer.BYTES, maxLon, 0) > 0 ||
- StringHelper.compare(Integer.BYTES, maxPackedValue, Integer.BYTES, minLon, 0) < 0) {
- // outside of global bounding box range
- return Relation.CELL_OUTSIDE_QUERY;
- }
-
- double cellMinLat = decodeLatitude(minPackedValue, 0);
- double cellMinLon = decodeLongitude(minPackedValue, Integer.BYTES);
- double cellMaxLat = decodeLatitude(maxPackedValue, 0);
- double cellMaxLon = decodeLongitude(maxPackedValue, Integer.BYTES);
-
- return tree.relate(cellMinLat, cellMaxLat, cellMinLon, cellMaxLon);
- }
- });
-
- return new ConstantScoreScorer(this, score(), result.build().iterator());
- }
-
- @Override
- public boolean isCacheable(LeafReaderContext ctx) {
- return true;
- }
- };
- }
-
- /** Returns the query field */
- public String getField() {
- return field;
- }
-
- /** Returns a copy of the internal polygon array */
- public Polygon[] getPolygons() {
- return polygons.clone();
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = classHash();
- result = prime * result + field.hashCode();
- result = prime * result + Arrays.hashCode(polygons);
- return result;
- }
-
- @Override
- public boolean equals(Object other) {
- return sameClassAs(other) &&
- equalsTo(getClass().cast(other));
- }
-
- private boolean equalsTo(LatLonPointInPolygonQuery other) {
- return field.equals(other.field) &&
- Arrays.equals(polygons, other.polygons);
- }
-
- @Override
- public String toString(String field) {
- final StringBuilder sb = new StringBuilder();
- sb.append(getClass().getSimpleName());
- sb.append(':');
- if (this.field.equals(field) == false) {
- sb.append(" field=");
- sb.append(this.field);
- sb.append(':');
- }
- sb.append(Arrays.toString(polygons));
- return sb.toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
deleted file mode 100644
index 10e72cc..0000000
--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointSortField.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import org.apache.lucene.geo.GeoUtils;
-import org.apache.lucene.search.FieldComparator;
-import org.apache.lucene.search.SortField;
-
-/**
- * Sorts by distance from an origin location.
- */
-final class LatLonPointSortField extends SortField {
- final double latitude;
- final double longitude;
-
- LatLonPointSortField(String field, double latitude, double longitude) {
- super(field, SortField.Type.CUSTOM);
- if (field == null) {
- throw new IllegalArgumentException("field must not be null");
- }
- GeoUtils.checkLatitude(latitude);
- GeoUtils.checkLongitude(longitude);
- this.latitude = latitude;
- this.longitude = longitude;
- setMissingValue(Double.POSITIVE_INFINITY);
- }
-
- @Override
- public FieldComparator<?> getComparator(int numHits, int sortPos) {
- return new LatLonPointDistanceComparator(getField(), latitude, longitude, numHits);
- }
-
- @Override
- public Double getMissingValue() {
- return (Double) super.getMissingValue();
- }
-
- @Override
- public void setMissingValue(Object missingValue) {
- if (Double.valueOf(Double.POSITIVE_INFINITY).equals(missingValue) == false) {
- throw new IllegalArgumentException("Missing value can only be Double.POSITIVE_INFINITY (missing values last), but got " + missingValue);
- }
- this.missingValue = missingValue;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- long temp;
- temp = Double.doubleToLongBits(latitude);
- result = prime * result + (int) (temp ^ (temp >>> 32));
- temp = Double.doubleToLongBits(longitude);
- result = prime * result + (int) (temp ^ (temp >>> 32));
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) return true;
- if (!super.equals(obj)) return false;
- if (getClass() != obj.getClass()) return false;
- LatLonPointSortField other = (LatLonPointSortField) obj;
- if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude)) return false;
- if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) return false;
- return true;
- }
-
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("<distance:");
- builder.append('"');
- builder.append(getField());
- builder.append('"');
- builder.append(" latitude=");
- builder.append(latitude);
- builder.append(" longitude=");
- builder.append(longitude);
- if (Double.POSITIVE_INFINITY != getMissingValue()) {
- builder.append(" missingValue=" + getMissingValue());
- }
- builder.append('>');
- return builder.toString();
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/java/org/apache/lucene/document/NearestNeighbor.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/NearestNeighbor.java b/lucene/sandbox/src/java/org/apache/lucene/document/NearestNeighbor.java
deleted file mode 100644
index 587c63f..0000000
--- a/lucene/sandbox/src/java/org/apache/lucene/document/NearestNeighbor.java
+++ /dev/null
@@ -1,328 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Comparator;
-import java.util.List;
-import java.util.PriorityQueue;
-
-import org.apache.lucene.geo.Rectangle;
-import org.apache.lucene.index.PointValues.IntersectVisitor;
-import org.apache.lucene.index.PointValues.Relation;
-import org.apache.lucene.util.Bits;
-import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.SloppyMath;
-import org.apache.lucene.util.bkd.BKDReader.IndexTree;
-import org.apache.lucene.util.bkd.BKDReader.IntersectState;
-import org.apache.lucene.util.bkd.BKDReader;
-
-import static org.apache.lucene.geo.GeoEncodingUtils.decodeLatitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.decodeLongitude;
-
-/**
- * KNN search on top of 2D lat/lon indexed points.
- *
- * @lucene.experimental
- */
-class NearestNeighbor {
-
- static class Cell implements Comparable<Cell> {
- final int readerIndex;
- final byte[] minPacked;
- final byte[] maxPacked;
- final IndexTree index;
-
- /** The closest possible distance of all points in this cell */
- final double distanceMeters;
-
- public Cell(IndexTree index, int readerIndex, byte[] minPacked, byte[] maxPacked, double distanceMeters) {
- this.index = index;
- this.readerIndex = readerIndex;
- this.minPacked = minPacked.clone();
- this.maxPacked = maxPacked.clone();
- this.distanceMeters = distanceMeters;
- }
-
- public int compareTo(Cell other) {
- return Double.compare(distanceMeters, other.distanceMeters);
- }
-
- @Override
- public String toString() {
- double minLat = decodeLatitude(minPacked, 0);
- double minLon = decodeLongitude(minPacked, Integer.BYTES);
- double maxLat = decodeLatitude(maxPacked, 0);
- double maxLon = decodeLongitude(maxPacked, Integer.BYTES);
- return "Cell(readerIndex=" + readerIndex + " nodeID=" + index.getNodeID() + " isLeaf=" + index.isLeafNode() + " lat=" + minLat + " TO " + maxLat + ", lon=" + minLon + " TO " + maxLon + "; distanceMeters=" + distanceMeters + ")";
- }
- }
-
- private static class NearestVisitor implements IntersectVisitor {
-
- public int curDocBase;
- public Bits curLiveDocs;
- final int topN;
- final PriorityQueue<NearestHit> hitQueue;
- final double pointLat;
- final double pointLon;
- private int setBottomCounter;
-
- private double minLon = Double.NEGATIVE_INFINITY;
- private double maxLon = Double.POSITIVE_INFINITY;
- private double minLat = Double.NEGATIVE_INFINITY;
- private double maxLat = Double.POSITIVE_INFINITY;
-
- // second set of longitude ranges to check (for cross-dateline case)
- private double minLon2 = Double.POSITIVE_INFINITY;
-
- public NearestVisitor(PriorityQueue<NearestHit> hitQueue, int topN, double pointLat, double pointLon) {
- this.hitQueue = hitQueue;
- this.topN = topN;
- this.pointLat = pointLat;
- this.pointLon = pointLon;
- }
-
- @Override
- public void visit(int docID) {
- throw new AssertionError();
- }
-
- private void maybeUpdateBBox() {
- if (setBottomCounter < 1024 || (setBottomCounter & 0x3F) == 0x3F) {
- NearestHit hit = hitQueue.peek();
- Rectangle box = Rectangle.fromPointDistance(pointLat, pointLon, hit.distanceMeters);
- //System.out.println(" update bbox to " + box);
- minLat = box.minLat;
- maxLat = box.maxLat;
- if (box.crossesDateline()) {
- // box1
- minLon = Double.NEGATIVE_INFINITY;
- maxLon = box.maxLon;
- // box2
- minLon2 = box.minLon;
- } else {
- minLon = box.minLon;
- maxLon = box.maxLon;
- // disable box2
- minLon2 = Double.POSITIVE_INFINITY;
- }
- }
- setBottomCounter++;
- }
-
- @Override
- public void visit(int docID, byte[] packedValue) {
- //System.out.println("visit docID=" + docID + " liveDocs=" + curLiveDocs);
-
- if (curLiveDocs != null && curLiveDocs.get(docID) == false) {
- return;
- }
-
- // TODO: work in int space, use haversinSortKey
-
- double docLatitude = decodeLatitude(packedValue, 0);
- double docLongitude = decodeLongitude(packedValue, Integer.BYTES);
-
- // test bounding box
- if (docLatitude < minLat || docLatitude > maxLat) {
- return;
- }
- if ((docLongitude < minLon || docLongitude > maxLon) && (docLongitude < minLon2)) {
- return;
- }
-
- double distanceMeters = SloppyMath.haversinMeters(pointLat, pointLon, docLatitude, docLongitude);
-
- //System.out.println(" visit docID=" + docID + " distanceMeters=" + distanceMeters + " docLat=" + docLatitude + " docLon=" + docLongitude);
-
- int fullDocID = curDocBase + docID;
-
- if (hitQueue.size() == topN) {
- // queue already full
- NearestHit hit = hitQueue.peek();
- //System.out.println(" bottom distanceMeters=" + hit.distanceMeters);
- // we don't collect docs in order here, so we must also test the tie-break case ourselves:
- if (distanceMeters < hit.distanceMeters || (distanceMeters == hit.distanceMeters && fullDocID < hit.docID)) {
- hitQueue.poll();
- hit.docID = fullDocID;
- hit.distanceMeters = distanceMeters;
- hitQueue.offer(hit);
- //System.out.println(" ** keep2, now bottom=" + hit);
- maybeUpdateBBox();
- }
-
- } else {
- NearestHit hit = new NearestHit();
- hit.docID = fullDocID;
- hit.distanceMeters = distanceMeters;
- hitQueue.offer(hit);
- //System.out.println(" ** keep1, now bottom=" + hit);
- }
- }
-
- @Override
- public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
- throw new AssertionError();
- }
- }
-
- /** Holds one hit from {@link LatLonPoint#nearest} */
- static class NearestHit {
- public int docID;
- public double distanceMeters;
-
- @Override
- public String toString() {
- return "NearestHit(docID=" + docID + " distanceMeters=" + distanceMeters + ")";
- }
- }
-
- // TODO: can we somehow share more with, or simply directly use, the LatLonPointDistanceComparator? It's really doing the same thing as
- // our hitQueue...
-
- public static NearestHit[] nearest(double pointLat, double pointLon, List<BKDReader> readers, List<Bits> liveDocs, List<Integer> docBases, final int n) throws IOException {
-
- //System.out.println("NEAREST: readers=" + readers + " liveDocs=" + liveDocs + " pointLat=" + pointLat + " pointLon=" + pointLon);
- // Holds closest collected points seen so far:
- // TODO: if we used lucene's PQ we could just updateTop instead of poll/offer:
- final PriorityQueue<NearestHit> hitQueue = new PriorityQueue<>(n, new Comparator<NearestHit>() {
- @Override
- public int compare(NearestHit a, NearestHit b) {
- // sort by opposite distanceMeters natural order
- int cmp = Double.compare(a.distanceMeters, b.distanceMeters);
- if (cmp != 0) {
- return -cmp;
- }
-
- // tie-break by higher docID:
- return b.docID - a.docID;
- }
- });
-
- // Holds all cells, sorted by closest to the point:
- PriorityQueue<Cell> cellQueue = new PriorityQueue<>();
-
- NearestVisitor visitor = new NearestVisitor(hitQueue, n, pointLat, pointLon);
- List<BKDReader.IntersectState> states = new ArrayList<>();
-
- // Add root cell for each reader into the queue:
- int bytesPerDim = -1;
-
- for(int i=0;i<readers.size();i++) {
- BKDReader reader = readers.get(i);
- if (bytesPerDim == -1) {
- bytesPerDim = reader.getBytesPerDimension();
- } else if (bytesPerDim != reader.getBytesPerDimension()) {
- throw new IllegalStateException("bytesPerDim changed from " + bytesPerDim + " to " + reader.getBytesPerDimension() + " across readers");
- }
- byte[] minPackedValue = reader.getMinPackedValue();
- byte[] maxPackedValue = reader.getMaxPackedValue();
- IntersectState state = reader.getIntersectState(visitor);
- states.add(state);
-
- cellQueue.offer(new Cell(state.index, i, reader.getMinPackedValue(), reader.getMaxPackedValue(),
- approxBestDistance(minPackedValue, maxPackedValue, pointLat, pointLon)));
- }
-
- while (cellQueue.size() > 0) {
- Cell cell = cellQueue.poll();
- //System.out.println(" visit " + cell);
-
- // TODO: if we replace approxBestDistance with actualBestDistance, we can put an opto here to break once this "best" cell is fully outside of the hitQueue bottom's radius:
- BKDReader reader = readers.get(cell.readerIndex);
-
- if (cell.index.isLeafNode()) {
- //System.out.println(" leaf");
- // Leaf block: visit all points and possibly collect them:
- visitor.curDocBase = docBases.get(cell.readerIndex);
- visitor.curLiveDocs = liveDocs.get(cell.readerIndex);
- reader.visitLeafBlockValues(cell.index, states.get(cell.readerIndex));
- //System.out.println(" now " + hitQueue.size() + " hits");
- } else {
- //System.out.println(" non-leaf");
- // Non-leaf block: split into two cells and put them back into the queue:
-
- double cellMinLat = decodeLatitude(cell.minPacked, 0);
- double cellMinLon = decodeLongitude(cell.minPacked, Integer.BYTES);
- double cellMaxLat = decodeLatitude(cell.maxPacked, 0);
- double cellMaxLon = decodeLongitude(cell.maxPacked, Integer.BYTES);
-
- if (cellMaxLat < visitor.minLat || visitor.maxLat < cellMinLat || ((cellMaxLon < visitor.minLon || visitor.maxLon < cellMinLon) && cellMaxLon < visitor.minLon2)) {
- // this cell is outside our search bbox; don't bother exploring any more
- continue;
- }
-
- BytesRef splitValue = BytesRef.deepCopyOf(cell.index.getSplitDimValue());
- int splitDim = cell.index.getSplitDim();
-
- // we must clone the index so that we we can recurse left and right "concurrently":
- IndexTree newIndex = cell.index.clone();
- byte[] splitPackedValue = cell.maxPacked.clone();
- System.arraycopy(splitValue.bytes, splitValue.offset, splitPackedValue, splitDim*bytesPerDim, bytesPerDim);
-
- cell.index.pushLeft();
- cellQueue.offer(new Cell(cell.index, cell.readerIndex, cell.minPacked, splitPackedValue,
- approxBestDistance(cell.minPacked, splitPackedValue, pointLat, pointLon)));
-
- splitPackedValue = cell.minPacked.clone();
- System.arraycopy(splitValue.bytes, splitValue.offset, splitPackedValue, splitDim*bytesPerDim, bytesPerDim);
-
- newIndex.pushRight();
- cellQueue.offer(new Cell(newIndex, cell.readerIndex, splitPackedValue, cell.maxPacked,
- approxBestDistance(splitPackedValue, cell.maxPacked, pointLat, pointLon)));
- }
- }
-
- NearestHit[] hits = new NearestHit[hitQueue.size()];
- int downTo = hitQueue.size()-1;
- while (hitQueue.size() != 0) {
- hits[downTo] = hitQueue.poll();
- downTo--;
- }
-
- return hits;
- }
-
- // NOTE: incoming args never cross the dateline, since they are a BKD cell
- private static double approxBestDistance(byte[] minPackedValue, byte[] maxPackedValue, double pointLat, double pointLon) {
- double minLat = decodeLatitude(minPackedValue, 0);
- double minLon = decodeLongitude(minPackedValue, Integer.BYTES);
- double maxLat = decodeLatitude(maxPackedValue, 0);
- double maxLon = decodeLongitude(maxPackedValue, Integer.BYTES);
- return approxBestDistance(minLat, maxLat, minLon, maxLon, pointLat, pointLon);
- }
-
- // NOTE: incoming args never cross the dateline, since they are a BKD cell
- private static double approxBestDistance(double minLat, double maxLat, double minLon, double maxLon, double pointLat, double pointLon) {
-
- // TODO: can we make this the trueBestDistance? I.e., minimum distance between the point and ANY point on the box? we can speed things
- // up if so, but not enrolling any BKD cell whose true best distance is > bottom of the current hit queue
-
- if (pointLat >= minLat && pointLat <= maxLat && pointLon >= minLon && pointLon <= maxLon) {
- // point is inside the cell!
- return 0.0;
- }
-
- double d1 = SloppyMath.haversinMeters(pointLat, pointLon, minLat, minLon);
- double d2 = SloppyMath.haversinMeters(pointLat, pointLon, minLat, maxLon);
- double d3 = SloppyMath.haversinMeters(pointLat, pointLon, maxLat, maxLon);
- double d4 = SloppyMath.haversinMeters(pointLat, pointLon, maxLat, minLon);
- return Math.min(Math.min(d1, d2), Math.min(d3, d4));
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java b/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java
new file mode 100644
index 0000000..ee0f076
--- /dev/null
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/LatLonPointPrototypeQueries.java
@@ -0,0 +1,111 @@
+/*
+ * 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.
+ */
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.lucene.codecs.lucene60.Lucene60PointsFormat;
+import org.apache.lucene.document.LatLonDocValuesField;
+import org.apache.lucene.document.LatLonPoint;
+import org.apache.lucene.geo.GeoUtils;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.PointValues;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.bkd.BKDReader;
+
+/**
+ * Holder class for prototype sandboxed queries
+ *
+ * When the query graduates from sandbox, these static calls should be
+ * placed in {@link LatLonPoint}
+ *
+ * @lucene.experimental
+ */
+public class LatLonPointPrototypeQueries {
+
+ // no instance
+ private LatLonPointPrototypeQueries() {
+ }
+
+ /**
+ * Finds the {@code n} nearest indexed points to the provided point, according to Haversine distance.
+ * <p>
+ * This is functionally equivalent to running {@link MatchAllDocsQuery} with a {@link LatLonDocValuesField#newDistanceSort},
+ * but is far more efficient since it takes advantage of properties the indexed BKD tree. Currently this
+ * only works with {@link Lucene60PointsFormat} (used by the default codec). Multi-valued fields are
+ * currently not de-duplicated, so if a document had multiple instances of the specified field that
+ * make it into the top n, that document will appear more than once.
+ * <p>
+ * Documents are ordered by ascending distance from the location. The value returned in {@link FieldDoc} for
+ * the hits contains a Double instance with the distance in meters.
+ *
+ * @param searcher IndexSearcher to find nearest points from.
+ * @param field field name. must not be null.
+ * @param latitude latitude at the center: must be within standard +/-90 coordinate bounds.
+ * @param longitude longitude at the center: must be within standard +/-180 coordinate bounds.
+ * @param n the number of nearest neighbors to retrieve.
+ * @return TopFieldDocs containing documents ordered by distance, where the field value for each {@link FieldDoc} is the distance in meters
+ * @throws IllegalArgumentException if the underlying PointValues is not a {@code Lucene60PointsReader} (this is a current limitation), or
+ * if {@code field} or {@code searcher} is null, or if {@code latitude}, {@code longitude} or {@code n} are out-of-bounds
+ * @throws IOException if an IOException occurs while finding the points.
+ */
+ // TODO: what about multi-valued documents? what happens?
+ public static TopFieldDocs nearest(IndexSearcher searcher, String field, double latitude, double longitude, int n) throws IOException {
+ GeoUtils.checkLatitude(latitude);
+ GeoUtils.checkLongitude(longitude);
+ if (n < 1) {
+ throw new IllegalArgumentException("n must be at least 1; got " + n);
+ }
+ if (field == null) {
+ throw new IllegalArgumentException("field must not be null");
+ }
+ if (searcher == null) {
+ throw new IllegalArgumentException("searcher must not be null");
+ }
+ List<BKDReader> readers = new ArrayList<>();
+ List<Integer> docBases = new ArrayList<>();
+ List<Bits> liveDocs = new ArrayList<>();
+ int totalHits = 0;
+ for(LeafReaderContext leaf : searcher.getIndexReader().leaves()) {
+ PointValues points = leaf.reader().getPointValues(field);
+ if (points != null) {
+ if (points instanceof BKDReader == false) {
+ throw new IllegalArgumentException("can only run on Lucene60PointsReader points implementation, but got " + points);
+ }
+ totalHits += points.getDocCount();
+ BKDReader reader = (BKDReader) points;
+ if (reader != null) {
+ readers.add(reader);
+ docBases.add(leaf.docBase);
+ liveDocs.add(leaf.reader().getLiveDocs());
+ }
+ }
+ }
+
+ NearestNeighbor.NearestHit[] hits = NearestNeighbor.nearest(latitude, longitude, readers, liveDocs, docBases, n);
+
+ // Convert to TopFieldDocs:
+ ScoreDoc[] scoreDocs = new ScoreDoc[hits.length];
+ for(int i=0;i<hits.length;i++) {
+ NearestNeighbor.NearestHit hit = hits[i];
+ scoreDocs[i] = new FieldDoc(hit.docID, 0.0f, new Object[] {Double.valueOf(hit.distanceMeters)});
+ }
+ return new TopFieldDocs(totalHits, scoreDocs, null);
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/java/org/apache/lucene/search/NearestNeighbor.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/NearestNeighbor.java b/lucene/sandbox/src/java/org/apache/lucene/search/NearestNeighbor.java
new file mode 100644
index 0000000..8502c45
--- /dev/null
+++ b/lucene/sandbox/src/java/org/apache/lucene/search/NearestNeighbor.java
@@ -0,0 +1,329 @@
+/*
+ * 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.
+ */
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+
+import org.apache.lucene.geo.Rectangle;
+import org.apache.lucene.index.PointValues.IntersectVisitor;
+import org.apache.lucene.index.PointValues.Relation;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.SloppyMath;
+import org.apache.lucene.util.bkd.BKDReader.IndexTree;
+import org.apache.lucene.util.bkd.BKDReader.IntersectState;
+import org.apache.lucene.util.bkd.BKDReader;
+
+import static org.apache.lucene.geo.GeoEncodingUtils.decodeLatitude;
+import static org.apache.lucene.geo.GeoEncodingUtils.decodeLongitude;
+
+/**
+ * KNN search on top of 2D lat/lon indexed points.
+ *
+ * @lucene.experimental
+ */
+class NearestNeighbor {
+
+ static class Cell implements Comparable<Cell> {
+ final int readerIndex;
+ final byte[] minPacked;
+ final byte[] maxPacked;
+ final IndexTree index;
+
+ /** The closest possible distance of all points in this cell */
+ final double distanceMeters;
+
+ public Cell(IndexTree index, int readerIndex, byte[] minPacked, byte[] maxPacked, double distanceMeters) {
+ this.index = index;
+ this.readerIndex = readerIndex;
+ this.minPacked = minPacked.clone();
+ this.maxPacked = maxPacked.clone();
+ this.distanceMeters = distanceMeters;
+ }
+
+ public int compareTo(Cell other) {
+ return Double.compare(distanceMeters, other.distanceMeters);
+ }
+
+ @Override
+ public String toString() {
+ double minLat = decodeLatitude(minPacked, 0);
+ double minLon = decodeLongitude(minPacked, Integer.BYTES);
+ double maxLat = decodeLatitude(maxPacked, 0);
+ double maxLon = decodeLongitude(maxPacked, Integer.BYTES);
+ return "Cell(readerIndex=" + readerIndex + " nodeID=" + index.getNodeID() + " isLeaf=" + index.isLeafNode() + " lat=" + minLat + " TO " + maxLat + ", lon=" + minLon + " TO " + maxLon + "; distanceMeters=" + distanceMeters + ")";
+ }
+ }
+
+ private static class NearestVisitor implements IntersectVisitor {
+
+ public int curDocBase;
+ public Bits curLiveDocs;
+ final int topN;
+ final PriorityQueue<NearestHit> hitQueue;
+ final double pointLat;
+ final double pointLon;
+ private int setBottomCounter;
+
+ private double minLon = Double.NEGATIVE_INFINITY;
+ private double maxLon = Double.POSITIVE_INFINITY;
+ private double minLat = Double.NEGATIVE_INFINITY;
+ private double maxLat = Double.POSITIVE_INFINITY;
+
+ // second set of longitude ranges to check (for cross-dateline case)
+ private double minLon2 = Double.POSITIVE_INFINITY;
+
+ public NearestVisitor(PriorityQueue<NearestHit> hitQueue, int topN, double pointLat, double pointLon) {
+ this.hitQueue = hitQueue;
+ this.topN = topN;
+ this.pointLat = pointLat;
+ this.pointLon = pointLon;
+ }
+
+ @Override
+ public void visit(int docID) {
+ throw new AssertionError();
+ }
+
+ private void maybeUpdateBBox() {
+ if (setBottomCounter < 1024 || (setBottomCounter & 0x3F) == 0x3F) {
+ NearestHit hit = hitQueue.peek();
+ Rectangle box = Rectangle.fromPointDistance(pointLat, pointLon, hit.distanceMeters);
+ //System.out.println(" update bbox to " + box);
+ minLat = box.minLat;
+ maxLat = box.maxLat;
+ if (box.crossesDateline()) {
+ // box1
+ minLon = Double.NEGATIVE_INFINITY;
+ maxLon = box.maxLon;
+ // box2
+ minLon2 = box.minLon;
+ } else {
+ minLon = box.minLon;
+ maxLon = box.maxLon;
+ // disable box2
+ minLon2 = Double.POSITIVE_INFINITY;
+ }
+ }
+ setBottomCounter++;
+ }
+
+ @Override
+ public void visit(int docID, byte[] packedValue) {
+ //System.out.println("visit docID=" + docID + " liveDocs=" + curLiveDocs);
+
+ if (curLiveDocs != null && curLiveDocs.get(docID) == false) {
+ return;
+ }
+
+ // TODO: work in int space, use haversinSortKey
+
+ double docLatitude = decodeLatitude(packedValue, 0);
+ double docLongitude = decodeLongitude(packedValue, Integer.BYTES);
+
+ // test bounding box
+ if (docLatitude < minLat || docLatitude > maxLat) {
+ return;
+ }
+ if ((docLongitude < minLon || docLongitude > maxLon) && (docLongitude < minLon2)) {
+ return;
+ }
+
+ double distanceMeters = SloppyMath.haversinMeters(pointLat, pointLon, docLatitude, docLongitude);
+
+ //System.out.println(" visit docID=" + docID + " distanceMeters=" + distanceMeters + " docLat=" + docLatitude + " docLon=" + docLongitude);
+
+ int fullDocID = curDocBase + docID;
+
+ if (hitQueue.size() == topN) {
+ // queue already full
+ NearestHit hit = hitQueue.peek();
+ //System.out.println(" bottom distanceMeters=" + hit.distanceMeters);
+ // we don't collect docs in order here, so we must also test the tie-break case ourselves:
+ if (distanceMeters < hit.distanceMeters || (distanceMeters == hit.distanceMeters && fullDocID < hit.docID)) {
+ hitQueue.poll();
+ hit.docID = fullDocID;
+ hit.distanceMeters = distanceMeters;
+ hitQueue.offer(hit);
+ //System.out.println(" ** keep2, now bottom=" + hit);
+ maybeUpdateBBox();
+ }
+
+ } else {
+ NearestHit hit = new NearestHit();
+ hit.docID = fullDocID;
+ hit.distanceMeters = distanceMeters;
+ hitQueue.offer(hit);
+ //System.out.println(" ** keep1, now bottom=" + hit);
+ }
+ }
+
+ @Override
+ public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
+ throw new AssertionError();
+ }
+ }
+
+ /** Holds one hit from {@link LatLonPointPrototypeQueries#nearest} */
+ static class NearestHit {
+ public int docID;
+ public double distanceMeters;
+
+ @Override
+ public String toString() {
+ return "NearestHit(docID=" + docID + " distanceMeters=" + distanceMeters + ")";
+ }
+ }
+
+ // TODO: can we somehow share more with, or simply directly use, the LatLonPointDistanceComparator? It's really doing the same thing as
+ // our hitQueue...
+
+ public static NearestHit[] nearest(double pointLat, double pointLon, List<BKDReader> readers, List<Bits> liveDocs, List<Integer> docBases, final int n) throws IOException {
+
+ //System.out.println("NEAREST: readers=" + readers + " liveDocs=" + liveDocs + " pointLat=" + pointLat + " pointLon=" + pointLon);
+ // Holds closest collected points seen so far:
+ // TODO: if we used lucene's PQ we could just updateTop instead of poll/offer:
+ final PriorityQueue<NearestHit> hitQueue = new PriorityQueue<>(n, new Comparator<NearestHit>() {
+ @Override
+ public int compare(NearestHit a, NearestHit b) {
+ // sort by opposite distanceMeters natural order
+ int cmp = Double.compare(a.distanceMeters, b.distanceMeters);
+ if (cmp != 0) {
+ return -cmp;
+ }
+
+ // tie-break by higher docID:
+ return b.docID - a.docID;
+ }
+ });
+
+ // Holds all cells, sorted by closest to the point:
+ PriorityQueue<Cell> cellQueue = new PriorityQueue<>();
+
+ NearestVisitor visitor = new NearestVisitor(hitQueue, n, pointLat, pointLon);
+ List<BKDReader.IntersectState> states = new ArrayList<>();
+
+ // Add root cell for each reader into the queue:
+ int bytesPerDim = -1;
+
+ for(int i=0;i<readers.size();i++) {
+ BKDReader reader = readers.get(i);
+ if (bytesPerDim == -1) {
+ bytesPerDim = reader.getBytesPerDimension();
+ } else if (bytesPerDim != reader.getBytesPerDimension()) {
+ throw new IllegalStateException("bytesPerDim changed from " + bytesPerDim + " to " + reader.getBytesPerDimension() + " across readers");
+ }
+ byte[] minPackedValue = reader.getMinPackedValue();
+ byte[] maxPackedValue = reader.getMaxPackedValue();
+ IntersectState state = reader.getIntersectState(visitor);
+ states.add(state);
+
+ cellQueue.offer(new Cell(state.index, i, reader.getMinPackedValue(), reader.getMaxPackedValue(),
+ approxBestDistance(minPackedValue, maxPackedValue, pointLat, pointLon)));
+ }
+
+ while (cellQueue.size() > 0) {
+ Cell cell = cellQueue.poll();
+ //System.out.println(" visit " + cell);
+
+ // TODO: if we replace approxBestDistance with actualBestDistance, we can put an opto here to break once this "best" cell is fully outside of the hitQueue bottom's radius:
+ BKDReader reader = readers.get(cell.readerIndex);
+
+ if (cell.index.isLeafNode()) {
+ //System.out.println(" leaf");
+ // Leaf block: visit all points and possibly collect them:
+ visitor.curDocBase = docBases.get(cell.readerIndex);
+ visitor.curLiveDocs = liveDocs.get(cell.readerIndex);
+ reader.visitLeafBlockValues(cell.index, states.get(cell.readerIndex));
+ //System.out.println(" now " + hitQueue.size() + " hits");
+ } else {
+ //System.out.println(" non-leaf");
+ // Non-leaf block: split into two cells and put them back into the queue:
+
+ double cellMinLat = decodeLatitude(cell.minPacked, 0);
+ double cellMinLon = decodeLongitude(cell.minPacked, Integer.BYTES);
+ double cellMaxLat = decodeLatitude(cell.maxPacked, 0);
+ double cellMaxLon = decodeLongitude(cell.maxPacked, Integer.BYTES);
+
+ if (cellMaxLat < visitor.minLat || visitor.maxLat < cellMinLat || ((cellMaxLon < visitor.minLon || visitor.maxLon < cellMinLon) && cellMaxLon < visitor.minLon2)) {
+ // this cell is outside our search bbox; don't bother exploring any more
+ continue;
+ }
+
+ BytesRef splitValue = BytesRef.deepCopyOf(cell.index.getSplitDimValue());
+ int splitDim = cell.index.getSplitDim();
+
+ // we must clone the index so that we we can recurse left and right "concurrently":
+ IndexTree newIndex = cell.index.clone();
+ byte[] splitPackedValue = cell.maxPacked.clone();
+ System.arraycopy(splitValue.bytes, splitValue.offset, splitPackedValue, splitDim*bytesPerDim, bytesPerDim);
+
+ cell.index.pushLeft();
+ cellQueue.offer(new Cell(cell.index, cell.readerIndex, cell.minPacked, splitPackedValue,
+ approxBestDistance(cell.minPacked, splitPackedValue, pointLat, pointLon)));
+
+ splitPackedValue = cell.minPacked.clone();
+ System.arraycopy(splitValue.bytes, splitValue.offset, splitPackedValue, splitDim*bytesPerDim, bytesPerDim);
+
+ newIndex.pushRight();
+ cellQueue.offer(new Cell(newIndex, cell.readerIndex, splitPackedValue, cell.maxPacked,
+ approxBestDistance(splitPackedValue, cell.maxPacked, pointLat, pointLon)));
+ }
+ }
+
+ NearestHit[] hits = new NearestHit[hitQueue.size()];
+ int downTo = hitQueue.size()-1;
+ while (hitQueue.size() != 0) {
+ hits[downTo] = hitQueue.poll();
+ downTo--;
+ }
+
+ return hits;
+ }
+
+ // NOTE: incoming args never cross the dateline, since they are a BKD cell
+ private static double approxBestDistance(byte[] minPackedValue, byte[] maxPackedValue, double pointLat, double pointLon) {
+ double minLat = decodeLatitude(minPackedValue, 0);
+ double minLon = decodeLongitude(minPackedValue, Integer.BYTES);
+ double maxLat = decodeLatitude(maxPackedValue, 0);
+ double maxLon = decodeLongitude(maxPackedValue, Integer.BYTES);
+ return approxBestDistance(minLat, maxLat, minLon, maxLon, pointLat, pointLon);
+ }
+
+ // NOTE: incoming args never cross the dateline, since they are a BKD cell
+ private static double approxBestDistance(double minLat, double maxLat, double minLon, double maxLon, double pointLat, double pointLon) {
+
+ // TODO: can we make this the trueBestDistance? I.e., minimum distance between the point and ANY point on the box? we can speed things
+ // up if so, but not enrolling any BKD cell whose true best distance is > bottom of the current hit queue
+
+ if (pointLat >= minLat && pointLat <= maxLat && pointLon >= minLon && pointLon <= maxLon) {
+ // point is inside the cell!
+ return 0.0;
+ }
+
+ double d1 = SloppyMath.haversinMeters(pointLat, pointLon, minLat, minLon);
+ double d2 = SloppyMath.haversinMeters(pointLat, pointLon, minLat, maxLon);
+ double d3 = SloppyMath.haversinMeters(pointLat, pointLon, maxLat, maxLon);
+ double d4 = SloppyMath.haversinMeters(pointLat, pointLon, maxLat, minLon);
+ return Math.min(Math.min(d1, d2), Math.min(d3, d4));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/document/TestFloatPointNearestNeighbor.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestFloatPointNearestNeighbor.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestFloatPointNearestNeighbor.java
index 9e3c3c1..72e7900 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestFloatPointNearestNeighbor.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestFloatPointNearestNeighbor.java
@@ -28,6 +28,7 @@ import org.apache.lucene.index.SerialMergeScheduler;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.LatLonPointPrototypeQueries;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
@@ -59,7 +60,7 @@ public class TestFloatPointNearestNeighbor extends LuceneTestCase {
r = w.getReader();
// can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
s = newSearcher(r, false);
- hit = (FieldDoc)LatLonPoint.nearest(s, "point", 40.0, 50.0, 1).scoreDocs[0];
+ hit = (FieldDoc) LatLonPointPrototypeQueries.nearest(s, "point", 40.0, 50.0, 1).scoreDocs[0];
assertEquals("1", r.document(hit.doc).getField("id").stringValue());
r.close();
w.close();
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonDocValuesField.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonDocValuesField.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonDocValuesField.java
deleted file mode 100644
index df934d1..0000000
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonDocValuesField.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import org.apache.lucene.util.LuceneTestCase;
-
-/** Simple tests for LatLonDocValuesField */
-public class TestLatLonDocValuesField extends LuceneTestCase {
- public void testToString() throws Exception {
- // looks crazy due to lossiness
- assertEquals("LatLonDocValuesField <field:18.313693958334625,-65.22744401358068>",(new LatLonDocValuesField("field", 18.313694, -65.227444)).toString());
-
- // sort field
- assertEquals("<distance:\"field\" latitude=18.0 longitude=19.0>", LatLonDocValuesField.newDistanceSort("field", 18.0, 19.0).toString());
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java
deleted file mode 100644
index 700eb56..0000000
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPoint.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import org.apache.lucene.util.LuceneTestCase;
-
-/**
- * Simple tests for {@link LatLonPoint}
- * TODO: move this lone test and remove class?
- * */
-public class TestLatLonPoint extends LuceneTestCase {
-
- public void testToString() throws Exception {
- // looks crazy due to lossiness
- assertEquals("LatLonPoint <field:18.313693958334625,-65.22744401358068>",(new LatLonPoint("field", 18.313694, -65.227444)).toString());
-
- // looks crazy due to lossiness
- assertEquals("field:[18.000000016763806 TO 18.999999999068677],[-65.9999999217689 TO -65.00000006519258]", LatLonPoint.newBoxQuery("field", 18, 19, -66, -65).toString());
-
- // distance query does not quantize inputs
- assertEquals("field:18.0,19.0 +/- 25.0 meters", LatLonPoint.newDistanceQuery("field", 18, 19, 25).toString());
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPointDistanceSort.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPointDistanceSort.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPointDistanceSort.java
deleted file mode 100644
index 6d825d2..0000000
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestLatLonPointDistanceSort.java
+++ /dev/null
@@ -1,264 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import java.io.IOException;
-import java.util.Arrays;
-
-import org.apache.lucene.geo.GeoTestUtil;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriterConfig;
-import org.apache.lucene.index.RandomIndexWriter;
-import org.apache.lucene.index.SerialMergeScheduler;
-import org.apache.lucene.search.FieldDoc;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.MatchAllDocsQuery;
-import org.apache.lucene.search.Sort;
-import org.apache.lucene.search.SortField;
-import org.apache.lucene.search.TopDocs;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.SloppyMath;
-import org.apache.lucene.util.TestUtil;
-
-import static org.apache.lucene.geo.GeoEncodingUtils.decodeLatitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.decodeLongitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.encodeLatitude;
-import static org.apache.lucene.geo.GeoEncodingUtils.encodeLongitude;
-
-/** Simple tests for {@link LatLonDocValuesField#newDistanceSort} */
-public class TestLatLonPointDistanceSort extends LuceneTestCase {
-
- /** Add three points and sort by distance */
- public void testDistanceSort() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
-
- // add some docs
- Document doc = new Document();
- doc.add(new LatLonDocValuesField("location", 40.759011, -73.9844722));
- iw.addDocument(doc);
-
- doc = new Document();
- doc.add(new LatLonDocValuesField("location", 40.718266, -74.007819));
- iw.addDocument(doc);
-
- doc = new Document();
- doc.add(new LatLonDocValuesField("location", 40.7051157, -74.0088305));
- iw.addDocument(doc);
-
- IndexReader reader = iw.getReader();
- IndexSearcher searcher = newSearcher(reader);
- iw.close();
-
- Sort sort = new Sort(LatLonDocValuesField.newDistanceSort("location", 40.7143528, -74.0059731));
- TopDocs td = searcher.search(new MatchAllDocsQuery(), 3, sort);
-
- FieldDoc d = (FieldDoc) td.scoreDocs[0];
- assertEquals(462.1028401330431, (Double)d.fields[0], 0.0D);
-
- d = (FieldDoc) td.scoreDocs[1];
- assertEquals(1054.9842850974826, (Double)d.fields[0], 0.0D);
-
- d = (FieldDoc) td.scoreDocs[2];
- assertEquals(5285.881528419706, (Double)d.fields[0], 0.0D);
-
- reader.close();
- dir.close();
- }
-
- /** Add two points (one doc missing) and sort by distance */
- public void testMissingLast() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
-
- // missing
- Document doc = new Document();
- iw.addDocument(doc);
-
- doc = new Document();
- doc.add(new LatLonDocValuesField("location", 40.718266, -74.007819));
- iw.addDocument(doc);
-
- doc = new Document();
- doc.add(new LatLonDocValuesField("location", 40.7051157, -74.0088305));
- iw.addDocument(doc);
-
- IndexReader reader = iw.getReader();
- IndexSearcher searcher = newSearcher(reader);
- iw.close();
-
- Sort sort = new Sort(LatLonDocValuesField.newDistanceSort("location", 40.7143528, -74.0059731));
- TopDocs td = searcher.search(new MatchAllDocsQuery(), 3, sort);
-
- FieldDoc d = (FieldDoc) td.scoreDocs[0];
- assertEquals(462.1028401330431D, (Double)d.fields[0], 0.0D);
-
- d = (FieldDoc) td.scoreDocs[1];
- assertEquals(1054.9842850974826, (Double)d.fields[0], 0.0D);
-
- d = (FieldDoc) td.scoreDocs[2];
- assertEquals(Double.POSITIVE_INFINITY, (Double)d.fields[0], 0.0D);
-
- reader.close();
- dir.close();
- }
-
- /** Run a few iterations with just 10 docs, hopefully easy to debug */
- public void testRandom() throws Exception {
- for (int iters = 0; iters < 100; iters++) {
- doRandomTest(10, 100);
- }
- }
-
- /** Runs with thousands of docs */
- @Nightly
- public void testRandomHuge() throws Exception {
- for (int iters = 0; iters < 10; iters++) {
- doRandomTest(2000, 100);
- }
- }
-
- // result class used for testing. holds an id+distance.
- // we sort these with Arrays.sort and compare with lucene's results
- static class Result implements Comparable<Result> {
- int id;
- double distance;
-
- Result(int id, double distance) {
- this.id = id;
- this.distance = distance;
- }
-
- @Override
- public int compareTo(Result o) {
- int cmp = Double.compare(distance, o.distance);
- if (cmp == 0) {
- return Integer.compare(id, o.id);
- }
- return cmp;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- long temp;
- temp = Double.doubleToLongBits(distance);
- result = prime * result + (int) (temp ^ (temp >>> 32));
- result = prime * result + id;
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) return true;
- if (obj == null) return false;
- if (getClass() != obj.getClass()) return false;
- Result other = (Result) obj;
- if (Double.doubleToLongBits(distance) != Double.doubleToLongBits(other.distance)) return false;
- if (id != other.id) return false;
- return true;
- }
-
- @Override
- public String toString() {
- return "Result [id=" + id + ", distance=" + distance + "]";
- }
- }
-
- private void doRandomTest(int numDocs, int numQueries) throws IOException {
- Directory dir = newDirectory();
- IndexWriterConfig iwc = newIndexWriterConfig();
- // else seeds may not to reproduce:
- iwc.setMergeScheduler(new SerialMergeScheduler());
- RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
-
- for (int i = 0; i < numDocs; i++) {
- Document doc = new Document();
- doc.add(new StoredField("id", i));
- doc.add(new NumericDocValuesField("id", i));
- if (random().nextInt(10) > 7) {
- double latRaw = GeoTestUtil.nextLatitude();
- double lonRaw = GeoTestUtil.nextLongitude();
- // pre-normalize up front, so we can just use quantized value for testing and do simple exact comparisons
- double lat = decodeLatitude(encodeLatitude(latRaw));
- double lon = decodeLongitude(encodeLongitude(lonRaw));
-
- doc.add(new LatLonDocValuesField("field", lat, lon));
- doc.add(new StoredField("lat", lat));
- doc.add(new StoredField("lon", lon));
- } // otherwise "missing"
- writer.addDocument(doc);
- }
- IndexReader reader = writer.getReader();
- IndexSearcher searcher = newSearcher(reader);
-
- for (int i = 0; i < numQueries; i++) {
- double lat = GeoTestUtil.nextLatitude();
- double lon = GeoTestUtil.nextLongitude();
- double missingValue = Double.POSITIVE_INFINITY;
-
- Result expected[] = new Result[reader.maxDoc()];
-
- for (int doc = 0; doc < reader.maxDoc(); doc++) {
- Document targetDoc = reader.document(doc);
- final double distance;
- if (targetDoc.getField("lat") == null) {
- distance = missingValue; // missing
- } else {
- double docLatitude = targetDoc.getField("lat").numericValue().doubleValue();
- double docLongitude = targetDoc.getField("lon").numericValue().doubleValue();
- distance = SloppyMath.haversinMeters(lat, lon, docLatitude, docLongitude);
- }
- int id = targetDoc.getField("id").numericValue().intValue();
- expected[doc] = new Result(id, distance);
- }
-
- Arrays.sort(expected);
-
- // randomize the topN a bit
- int topN = TestUtil.nextInt(random(), 1, reader.maxDoc());
- // sort by distance, then ID
- SortField distanceSort = LatLonDocValuesField.newDistanceSort("field", lat, lon);
- distanceSort.setMissingValue(missingValue);
- Sort sort = new Sort(distanceSort,
- new SortField("id", SortField.Type.INT));
-
- TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), topN, sort);
- for (int resultNumber = 0; resultNumber < topN; resultNumber++) {
- FieldDoc fieldDoc = (FieldDoc) topDocs.scoreDocs[resultNumber];
- Result actual = new Result((Integer) fieldDoc.fields[1], (Double) fieldDoc.fields[0]);
- assertEquals(expected[resultNumber], actual);
- }
-
- // get page2 with searchAfter()
- if (topN < reader.maxDoc()) {
- int page2 = TestUtil.nextInt(random(), 1, reader.maxDoc() - topN);
- TopDocs topDocs2 = searcher.searchAfter(topDocs.scoreDocs[topN - 1], new MatchAllDocsQuery(), page2, sort);
- for (int resultNumber = 0; resultNumber < page2; resultNumber++) {
- FieldDoc fieldDoc = (FieldDoc) topDocs2.scoreDocs[resultNumber];
- Result actual = new Result((Integer) fieldDoc.fields[1], (Double) fieldDoc.fields[0]);
- assertEquals(expected[topN + resultNumber], actual);
- }
- }
- }
- reader.close();
- writer.close();
- dir.close();
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/document/TestNearest.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestNearest.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestNearest.java
deleted file mode 100644
index 2fb2077..0000000
--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestNearest.java
+++ /dev/null
@@ -1,253 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.document;
-
-import java.util.Arrays;
-import java.util.Comparator;
-
-import org.apache.lucene.codecs.Codec;
-import org.apache.lucene.document.NearestNeighbor.NearestHit;
-import org.apache.lucene.geo.GeoEncodingUtils;
-import org.apache.lucene.geo.GeoTestUtil;
-import org.apache.lucene.index.DirectoryReader;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.IndexWriterConfig;
-import org.apache.lucene.index.RandomIndexWriter;
-import org.apache.lucene.index.SerialMergeScheduler;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.search.FieldDoc;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.MatchAllDocsQuery;
-import org.apache.lucene.search.ScoreDoc;
-import org.apache.lucene.search.Sort;
-import org.apache.lucene.search.TopFieldDocs;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.SloppyMath;
-import org.apache.lucene.util.TestUtil;
-
-public class TestNearest extends LuceneTestCase {
-
- public void testNearestNeighborWithDeletedDocs() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir, getIndexWriterConfig());
- Document doc = new Document();
- doc.add(new LatLonPoint("point", 40.0, 50.0));
- doc.add(new StringField("id", "0", Field.Store.YES));
- w.addDocument(doc);
-
- doc = new Document();
- doc.add(new LatLonPoint("point", 45.0, 55.0));
- doc.add(new StringField("id", "1", Field.Store.YES));
- w.addDocument(doc);
-
- DirectoryReader r = w.getReader();
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- IndexSearcher s = newSearcher(r, false);
- FieldDoc hit = (FieldDoc) LatLonPoint.nearest(s, "point", 40.0, 50.0, 1).scoreDocs[0];
- assertEquals("0", r.document(hit.doc).getField("id").stringValue());
- r.close();
-
- w.deleteDocuments(new Term("id", "0"));
- r = w.getReader();
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- s = newSearcher(r, false);
- hit = (FieldDoc) LatLonPoint.nearest(s, "point", 40.0, 50.0, 1).scoreDocs[0];
- assertEquals("1", r.document(hit.doc).getField("id").stringValue());
- r.close();
- w.close();
- dir.close();
- }
-
- public void testNearestNeighborWithAllDeletedDocs() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir, getIndexWriterConfig());
- Document doc = new Document();
- doc.add(new LatLonPoint("point", 40.0, 50.0));
- doc.add(new StringField("id", "0", Field.Store.YES));
- w.addDocument(doc);
- doc = new Document();
- doc.add(new LatLonPoint("point", 45.0, 55.0));
- doc.add(new StringField("id", "1", Field.Store.YES));
- w.addDocument(doc);
-
- DirectoryReader r = w.getReader();
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- IndexSearcher s = newSearcher(r, false);
- FieldDoc hit = (FieldDoc) LatLonPoint.nearest(s, "point", 40.0, 50.0, 1).scoreDocs[0];
- assertEquals("0", r.document(hit.doc).getField("id").stringValue());
- r.close();
-
- w.deleteDocuments(new Term("id", "0"));
- w.deleteDocuments(new Term("id", "1"));
- r = w.getReader();
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- s = newSearcher(r, false);
- assertEquals(0, LatLonPoint.nearest(s, "point", 40.0, 50.0, 1).scoreDocs.length);
- r.close();
- w.close();
- dir.close();
- }
-
- public void testTieBreakByDocID() throws Exception {
- Directory dir = newDirectory();
- IndexWriter w = new IndexWriter(dir, getIndexWriterConfig());
- Document doc = new Document();
- doc.add(new LatLonPoint("point", 40.0, 50.0));
- doc.add(new StringField("id", "0", Field.Store.YES));
- w.addDocument(doc);
- doc = new Document();
- doc.add(new LatLonPoint("point", 40.0, 50.0));
- doc.add(new StringField("id", "1", Field.Store.YES));
- w.addDocument(doc);
-
- DirectoryReader r = DirectoryReader.open(w);
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- ScoreDoc[] hits = LatLonPoint.nearest(newSearcher(r, false), "point", 45.0, 50.0, 2).scoreDocs;
- assertEquals("0", r.document(hits[0].doc).getField("id").stringValue());
- assertEquals("1", r.document(hits[1].doc).getField("id").stringValue());
-
- r.close();
- w.close();
- dir.close();
- }
-
- public void testNearestNeighborWithNoDocs() throws Exception {
- Directory dir = newDirectory();
- RandomIndexWriter w = new RandomIndexWriter(random(), dir, getIndexWriterConfig());
- DirectoryReader r = w.getReader();
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- assertEquals(0, LatLonPoint.nearest(newSearcher(r, false), "point", 40.0, 50.0, 1).scoreDocs.length);
- r.close();
- w.close();
- dir.close();
- }
-
- private double quantizeLat(double latRaw) {
- return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(latRaw));
- }
-
- private double quantizeLon(double lonRaw) {
- return GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(lonRaw));
- }
-
- public void testNearestNeighborRandom() throws Exception {
-
- int numPoints = atLeast(5000);
- Directory dir;
- if (numPoints > 100000) {
- dir = newFSDirectory(createTempDir(getClass().getSimpleName()));
- } else {
- dir = newDirectory();
- }
- double[] lats = new double[numPoints];
- double[] lons = new double[numPoints];
-
- IndexWriterConfig iwc = getIndexWriterConfig();
- iwc.setMergePolicy(newLogMergePolicy());
- iwc.setMergeScheduler(new SerialMergeScheduler());
- RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
- for(int id=0;id<numPoints;id++) {
- lats[id] = quantizeLat(GeoTestUtil.nextLatitude());
- lons[id] = quantizeLon(GeoTestUtil.nextLongitude());
- Document doc = new Document();
- doc.add(new LatLonPoint("point", lats[id], lons[id]));
- doc.add(new LatLonDocValuesField("point", lats[id], lons[id]));
- doc.add(new StoredField("id", id));
- w.addDocument(doc);
- }
-
- if (random().nextBoolean()) {
- w.forceMerge(1);
- }
-
- DirectoryReader r = w.getReader();
- if (VERBOSE) {
- System.out.println("TEST: reader=" + r);
- }
- // can't wrap because we require Lucene60PointsFormat directly but e.g. ParallelReader wraps with its own points impl:
- IndexSearcher s = newSearcher(r, false);
- int iters = atLeast(100);
- for(int iter=0;iter<iters;iter++) {
- if (VERBOSE) {
- System.out.println("\nTEST: iter=" + iter);
- }
- double pointLat = GeoTestUtil.nextLatitude();
- double pointLon = GeoTestUtil.nextLongitude();
-
- // dumb brute force search to get the expected result:
- NearestHit[] expectedHits = new NearestHit[lats.length];
- for(int id=0;id<lats.length;id++) {
- NearestHit hit = new NearestHit();
- hit.distanceMeters = SloppyMath.haversinMeters(pointLat, pointLon, lats[id], lons[id]);
- hit.docID = id;
- expectedHits[id] = hit;
- }
-
- Arrays.sort(expectedHits, new Comparator<NearestHit>() {
- @Override
- public int compare(NearestHit a, NearestHit b) {
- int cmp = Double.compare(a.distanceMeters, b.distanceMeters);
- if (cmp != 0) {
- return cmp;
- }
- // tie break by smaller docID:
- return a.docID - b.docID;
- }
- });
-
- int topN = TestUtil.nextInt(random(), 1, lats.length);
-
- if (VERBOSE) {
- System.out.println("\nhits for pointLat=" + pointLat + " pointLon=" + pointLon);
- }
-
- // Also test with MatchAllDocsQuery, sorting by distance:
- TopFieldDocs fieldDocs = s.search(new MatchAllDocsQuery(), topN, new Sort(LatLonDocValuesField.newDistanceSort("point", pointLat, pointLon)));
-
- ScoreDoc[] hits = LatLonPoint.nearest(s, "point", pointLat, pointLon, topN).scoreDocs;
- for(int i=0;i<topN;i++) {
- NearestHit expected = expectedHits[i];
- FieldDoc expected2 = (FieldDoc) fieldDocs.scoreDocs[i];
- FieldDoc actual = (FieldDoc) hits[i];
- Document actualDoc = r.document(actual.doc);
-
- if (VERBOSE) {
- System.out.println("hit " + i);
- System.out.println(" expected id=" + expected.docID + " lat=" + lats[expected.docID] + " lon=" + lons[expected.docID] + " distance=" + expected.distanceMeters + " meters");
- System.out.println(" actual id=" + actualDoc.getField("id") + " distance=" + actual.fields[0] + " meters");
- }
-
- assertEquals(expected.docID, actual.doc);
- assertEquals(expected.distanceMeters, ((Double) actual.fields[0]).doubleValue(), 0.0);
-
- assertEquals(expected.docID, expected.docID);
- assertEquals(((Double) expected2.fields[0]).doubleValue(), expected.distanceMeters, 0.0);
- }
- }
-
- r.close();
- w.close();
- dir.close();
- }
-
- private IndexWriterConfig getIndexWriterConfig() {
- IndexWriterConfig iwc = newIndexWriterConfig();
- iwc.setCodec(Codec.forName("Lucene70"));
- return iwc;
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonDocValuesQueries.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonDocValuesQueries.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonDocValuesQueries.java
deleted file mode 100644
index bf1b8cb..0000000
--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonDocValuesQueries.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.search;
-
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.LatLonDocValuesField;
-import org.apache.lucene.geo.BaseGeoPointTestCase;
-import org.apache.lucene.geo.GeoEncodingUtils;
-import org.apache.lucene.geo.Polygon;
-
-public class TestLatLonDocValuesQueries extends BaseGeoPointTestCase {
-
- @Override
- protected boolean supportsPolygons() {
- return false;
- }
-
- @Override
- protected void addPointToDoc(String field, Document doc, double lat, double lon) {
- doc.add(new LatLonDocValuesField(field, lat, lon));
- }
-
- @Override
- protected Query newRectQuery(String field, double minLat, double maxLat, double minLon, double maxLon) {
- return LatLonDocValuesField.newSlowBoxQuery(field, minLat, maxLat, minLon, maxLon);
- }
-
- @Override
- protected Query newDistanceQuery(String field, double centerLat, double centerLon, double radiusMeters) {
- return LatLonDocValuesField.newSlowDistanceQuery(field, centerLat, centerLon, radiusMeters);
- }
-
- @Override
- protected Query newPolygonQuery(String field, Polygon... polygons) {
- fail();
- return null;
- }
-
- @Override
- protected double quantizeLat(double latRaw) {
- return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(latRaw));
- }
-
- @Override
- protected double quantizeLon(double lonRaw) {
- return GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(lonRaw));
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/6ab3ff83/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java
deleted file mode 100644
index f0e9612..0000000
--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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.
- */
-package org.apache.lucene.search;
-
-import java.io.IOException;
-
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.LatLonPoint;
-import org.apache.lucene.geo.BaseGeoPointTestCase;
-import org.apache.lucene.geo.GeoEncodingUtils;
-import org.apache.lucene.geo.Polygon;
-import org.apache.lucene.index.DirectoryReader;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.util.bkd.BKDWriter;
-
-public class TestLatLonPointQueries extends BaseGeoPointTestCase {
-
- @Override
- protected void addPointToDoc(String field, Document doc, double lat, double lon) {
- doc.add(new LatLonPoint(field, lat, lon));
- }
-
- @Override
- protected Query newRectQuery(String field, double minLat, double maxLat, double minLon, double maxLon) {
- return LatLonPoint.newBoxQuery(field, minLat, maxLat, minLon, maxLon);
- }
-
- @Override
- protected Query newDistanceQuery(String field, double centerLat, double centerLon, double radiusMeters) {
- return LatLonPoint.newDistanceQuery(field, centerLat, centerLon, radiusMeters);
- }
-
- @Override
- protected Query newPolygonQuery(String field, Polygon... polygons) {
- return LatLonPoint.newPolygonQuery(field, polygons);
- }
-
- @Override
- protected double quantizeLat(double latRaw) {
- return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(latRaw));
- }
-
- @Override
- protected double quantizeLon(double lonRaw) {
- return GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(lonRaw));
- }
-
- public void testDistanceQueryWithInvertedIntersection() throws IOException {
- final int numMatchingDocs = atLeast(10 * BKDWriter.DEFAULT_MAX_POINTS_IN_LEAF_NODE);
-
- try (Directory dir = newDirectory()) {
-
- try (IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
- for (int i = 0; i < numMatchingDocs; ++i) {
- Document doc = new Document();
- addPointToDoc("field", doc, 18.313694, -65.227444);
- w.addDocument(doc);
- }
-
- // Add a handful of docs that don't match
- for (int i = 0; i < 11; ++i) {
- Document doc = new Document();
- addPointToDoc("field", doc, 10, -65.227444);
- w.addDocument(doc);
- }
- w.forceMerge(1);
- }
-
- try (IndexReader r = DirectoryReader.open(dir)) {
- IndexSearcher searcher = newSearcher(r);
- assertEquals(numMatchingDocs, searcher.count(newDistanceQuery("field", 18, -65, 50_000)));
- }
- }
- }
-}