2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.lucene.spatial.tier;
19 import java.io.IOException;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.analysis.WhitespaceAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.index.IndexReader;
28 import org.apache.lucene.search.QueryWrapperFilter;
29 import org.apache.lucene.search.MatchAllDocsQuery;
30 import org.apache.lucene.util.LuceneTestCase;
31 import org.apache.lucene.util.NumericUtils;
32 import org.apache.lucene.store.Directory;
34 public class TestDistance extends LuceneTestCase {
36 private Directory directory;
38 private double lat = 38.969398;
39 private double lng= -77.386398;
40 private String latField = "lat";
41 private String lngField = "lng";
42 private IndexWriter writer;
45 public void setUp() throws Exception {
47 directory = newDirectory();
48 writer = new IndexWriter(directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
54 public void tearDown() throws Exception {
60 private void addPoint(IndexWriter writer, String name, double lat, double lng) throws IOException{
62 Document doc = new Document();
64 doc.add(newField("name", name,Field.Store.YES, Field.Index.ANALYZED));
66 // convert the lat / long to lucene fields
67 doc.add(new Field(latField, NumericUtils.doubleToPrefixCoded(lat),Field.Store.YES, Field.Index.NOT_ANALYZED));
68 doc.add(new Field(lngField, NumericUtils.doubleToPrefixCoded(lng),Field.Store.YES, Field.Index.NOT_ANALYZED));
70 // add a default meta field to make searching all documents easy
71 doc.add(newField("metafile", "doc",Field.Store.YES, Field.Index.ANALYZED));
72 writer.addDocument(doc);
77 private void addData(IndexWriter writer) throws IOException {
78 addPoint(writer,"McCormick & Schmick's Seafood Restaurant",38.9579000,-77.3572000);
79 addPoint(writer,"Jimmy's Old Town Tavern",38.9690000,-77.3862000);
80 addPoint(writer,"Ned Devine's",38.9510000,-77.4107000);
81 addPoint(writer,"Old Brogue Irish Pub",38.9955000,-77.2884000);
82 addPoint(writer,"Alf Laylah Wa Laylah",38.8956000,-77.4258000);
83 addPoint(writer,"Sully's Restaurant & Supper",38.9003000,-77.4467000);
84 addPoint(writer,"TGIFriday",38.8725000,-77.3829000);
85 addPoint(writer,"Potomac Swing Dance Club",38.9027000,-77.2639000);
86 addPoint(writer,"White Tiger Restaurant",38.9027000,-77.2638000);
87 addPoint(writer,"Jammin' Java",38.9039000,-77.2622000);
88 addPoint(writer,"Potomac Swing Dance Club",38.9027000,-77.2639000);
89 addPoint(writer,"WiseAcres Comedy Club",38.9248000,-77.2344000);
90 addPoint(writer,"Glen Echo Spanish Ballroom",38.9691000,-77.1400000);
91 addPoint(writer,"Whitlow's on Wilson",38.8889000,-77.0926000);
92 addPoint(writer,"Iota Club and Cafe",38.8890000,-77.0923000);
93 addPoint(writer,"Hilton Washington Embassy Row",38.9103000,-77.0451000);
94 addPoint(writer,"HorseFeathers, Bar & Grill", 39.01220000000001, -77.3942);
98 public void testLatLongFilterOnDeletedDocs() throws Exception {
99 writer.deleteDocuments(new Term("name", "Potomac"));
100 IndexReader r = IndexReader.open(writer, true);
101 LatLongDistanceFilter f = new LatLongDistanceFilter(new QueryWrapperFilter(new MatchAllDocsQuery()),
102 lat, lng, 1.0, latField, lngField);
104 IndexReader[] readers = r.getSequentialSubReaders();
105 for(int i=0;i<readers.length;i++) {
106 f.getDocIdSet(readers[i]);
111 /* these tests do not test anything, as no assertions:
112 public void testMiles() {
113 double LLM = DistanceUtils.getInstance().getLLMDistance(lat, lng,39.012200001, -77.3942);
114 System.out.println(LLM);
115 System.out.println("-->"+DistanceUtils.getInstance().getDistanceMi(lat, lng, 39.0122, -77.3942));
118 public void testMiles2(){
119 System.out.println("Test Miles 2");
120 double LLM = DistanceUtils.getInstance().getLLMDistance(44.30073, -78.32131,43.687267, -79.39842);
121 System.out.println(LLM);
122 System.out.println("-->"+DistanceUtils.getInstance().getDistanceMi(44.30073, -78.32131, 43.687267, -79.39842));
127 // public void testDistanceQueryCacheable() throws IOException {
129 // // create two of the same distance queries
130 // double miles = 6.0;
131 // DistanceQuery dq1 = new DistanceQuery(lat, lng, miles, latField, lngField, true);
132 // DistanceQuery dq2 = new DistanceQuery(lat, lng, miles, latField, lngField, true);
134 // /* ensure that they hash to the same code, which will cause a cache hit in solr */
135 // System.out.println("hash differences?");
136 // assertEquals(dq1.getQuery().hashCode(), dq2.getQuery().hashCode());
138 // /* ensure that changing the radius makes a different hash code, creating a cache miss in solr */
139 // DistanceQuery widerQuery = new DistanceQuery(lat, lng, miles + 5.0, latField, lngField, false);
140 // assertTrue(dq1.getQuery().hashCode() != widerQuery.getQuery().hashCode());