List of usage examples for org.apache.solr.util NumberUtils SortableStr2double
public static double SortableStr2double(BytesRef val)
From source file:com.pjaol.search.geo.utils.DistanceFilter.java
License:Apache License
@Override public BitSet bits(IndexReader reader) throws IOException { /* Create a BitSet to store the result */ int maxdocs = reader.numDocs(); BitSet bits = new BitSet(maxdocs); setPrecision(maxdocs);//from w ww . j a v a 2 s . com /* create an intermediate cache to avoid recomputing distances for the same point TODO: Why is this a WeakHashMap? */ WeakHashMap<String, Double> cdistance = new WeakHashMap<String, Double>(maxdocs); String[] latIndex = FieldCache.DEFAULT.getStrings(reader, latField); String[] lngIndex = FieldCache.DEFAULT.getStrings(reader, lngField); /* store calculated distances for reuse by other components */ distances = new HashMap<Integer, Double>(maxdocs); for (int i = 0; i < maxdocs; i++) { String sx = latIndex[i]; String sy = lngIndex[i]; if (sx != null && sy != null) { double x = NumberUtils.SortableStr2double(sx); double y = NumberUtils.SortableStr2double(sy); // round off lat / longs if necessary x = DistanceHandler.getPrecision(x, precise); y = DistanceHandler.getPrecision(y, precise); String ck = new Double(x).toString() + "," + new Double(y).toString(); Double cachedDistance = cdistance.get(ck); double d; if (cachedDistance != null) { d = cachedDistance.doubleValue(); } else { d = DistanceUtils.getDistanceMi(lat, lng, x, y); cdistance.put(ck, d); } distances.put(i, d); if (distance < 0 || d < distance) { bits.set(i); } } } return bits; }
From source file:com.pjaol.search.geo.utils.DistanceFilter.java
License:Apache License
@Override public BitSet bits(IndexReader reader, BitSet bits) throws Exception { /* Create a BitSet to store the result */ int size = bits.cardinality(); BitSet result = new BitSet(size); setPrecision(size);/*from w w w. j a v a 2 s . c o m*/ /* create an intermediate cache to avoid recomputing distances for the same point */ HashMap<String, Double> cdistance = new HashMap<String, Double>(size); /* store calculated distances for reuse by other components */ distances = new HashMap<Integer, Double>(size); long start = System.currentTimeMillis(); String[] latIndex = FieldCache.DEFAULT.getStrings(reader, latField); String[] lngIndex = FieldCache.DEFAULT.getStrings(reader, lngField); /* loop over indexText set bits (hits from the boundary box filter) */ int i = bits.nextSetBit(0); while (i >= 0) { double x, y; // if we have a completed // filter chain, lat / lngs can be retrived from // memory rather than document base. String sx = latIndex[i]; String sy = lngIndex[i]; if (sx != null && sy != null) //No coordinate no distance -> no bit set { x = NumberUtils.SortableStr2double(sx); y = NumberUtils.SortableStr2double(sy); // round off lat / longs if necessary x = DistanceHandler.getPrecision(x, precise); y = DistanceHandler.getPrecision(y, precise); String ck = new Double(x).toString() + "," + new Double(y).toString(); Double cachedDistance = cdistance.get(ck); double d; if (cachedDistance != null) { d = cachedDistance.doubleValue(); } else { d = DistanceUtils.getDistanceMi(lat, lng, x, y); cdistance.put(ck, d); } distances.put(i, d); if (distance < 0 || d < distance) { result.set(i); } } i = bits.nextSetBit(i + 1); } long end = System.currentTimeMillis(); log.fine("Time taken : " + (end - start) + ", results : " + distances.size() + ", cached : " + cdistance.size() + ", incoming size: " + size); cdistance = null; return result; }
From source file:com.pjaol.search.test.UnitTests.TestCartesian.java
License:Apache License
public void testRange() throws IOException, InvalidGeoException { searcher = new IndexSearcher(directory); final double miles = 6.0; // create a distance query final DistanceQuery dq = new DistanceQuery(lat, lng, miles, latField, lngField, true); System.out.println(dq);//from w w w.j a v a 2s . c o m //create a term query to search against all documents Query tq = new TermQuery(new Term("metafile", "doc")); FieldScoreQuery fsQuery = new FieldScoreQuery("geo_distance", Type.FLOAT); CustomScoreQuery customScore = new CustomScoreQuery(tq, fsQuery) { public float customScore(int doc, float subQueryScore, float valSrcScore) { System.out.println(doc); if (dq.distanceFilter.getDistance(doc) == null) return 0; double distance = (double) dq.distanceFilter.getDistance(doc); // boost score shouldn't exceed 1 if (distance < 1.0d) distance = 1.0d; //boost by distance is invertly proportional to // to distance from center point to location float score = new Float((miles - distance) / miles).floatValue(); return score * subQueryScore; } }; // Create a distance sort // As the radius filter has performed the distance calculations // already, pass in the filter to reuse the results. // DistanceSortSource dsort = new DistanceSortSource(dq.distanceFilter); Sort sort = new Sort(new SortField("foo", dsort)); // Perform the search, using the term query, the serial chain filter, and the // distance sort Hits hits = searcher.search(customScore, dq.getFilter()); //,sort); int results = hits.length(); // Get a list of distances Map<Integer, Double> distances = dq.distanceFilter.getDistances(); // distances calculated from filter first pass must be less than total // docs, from the above test of 20 items, 12 will come from the boundary box // filter, but only 5 are actually in the radius of the results. // Note Boundary Box filtering, is not accurate enough for most systems. System.out.println("Distance Filter filtered: " + distances.size()); System.out.println("Results: " + results); System.out.println("============================="); System.out.println("Distances should be 14 " + distances.size()); System.out.println("Results should be 7 " + results); assertEquals(14, distances.size()); assertEquals(7, results); for (int i = 0; i < results; i++) { Document d = hits.doc(i); String name = d.get("name"); double rsLat = NumberUtils.SortableStr2double(d.get(latField)); double rsLng = NumberUtils.SortableStr2double(d.get(lngField)); Double geo_distance = distances.get(hits.id(i)); double distance = DistanceUtils.getDistanceMi(lat, lng, rsLat, rsLng); double llm = DistanceUtils.getLLMDistance(lat, lng, rsLat, rsLng); System.out.println("Name: " + name + ", Distance (res, ortho, harvesine):" + distance + " |" + geo_distance + "|" + llm + " | score " + hits.score(i)); assertTrue(Math.abs((distance - llm)) < 1); assertTrue((distance < miles)); } }
From source file:com.pjaol.search.test.UnitTests.TestDistance.java
License:Apache License
public void testRange() throws IOException, InvalidGeoException { searcher = new IndexSearcher(directory); double miles = 6.0; // create a distance query DistanceQuery dq = new DistanceQuery(lat, lng, miles, latField, lngField, false); System.out.println(dq.latFilter.toString() + " " + dq.lngFilter); //create a term query to search against all documents Query tq = new TermQuery(new Term("metafile", "doc")); // Create a distance sort // As the radius filter has performed the distance calculations // already, pass in the filter to reuse the results. // // w w w. j av a 2 s . c om DistanceSortSource dsort = new DistanceSortSource(dq.distanceFilter); Sort sort = new Sort(new SortField("foo", dsort)); // Perform the search, using the term query, the serial chain filter, and the // distance sort Hits hits = searcher.search(tq, dq.getFilter(), sort); int results = hits.length(); // Get a list of distances Map<Integer, Double> distances = dq.distanceFilter.getDistances(); // distances calculated from filter first pass must be less than total // docs, from the above test of 20 items, 12 will come from the boundary box // filter, but only 5 are actually in the radius of the results. // Note Boundary Box filtering, is not accurate enough for most systems. System.out.println("Distance Filter filtered: " + distances.size()); System.out.println("Results: " + results); System.out.println("============================="); assertEquals(7, distances.size()); assertEquals(7, results); for (int i = 0; i < results; i++) { Document d = hits.doc(i); String name = d.get("name"); double rsLat = NumberUtils.SortableStr2double(d.get(latField)); double rsLng = NumberUtils.SortableStr2double(d.get(lngField)); Double geo_distance = distances.get(hits.id(i)); double distance = DistanceUtils.getDistanceMi(lat, lng, rsLat, rsLng); double llm = DistanceUtils.getLLMDistance(lat, lng, rsLat, rsLng); System.out.println("Name: " + name + ", Distance (res, ortho, harvesine):" + distance + " |" + geo_distance + "|" + llm); assertTrue(Math.abs((distance - llm)) < 1); assertTrue((distance < miles)); } }