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.
18 package org.apache.lucene.spatial.tier;
20 import org.apache.lucene.search.ConstantScoreQuery;
21 import org.apache.lucene.search.ChainedFilter;
22 import org.apache.lucene.search.Filter;
23 import org.apache.lucene.search.Query;
24 import org.apache.lucene.search.QueryWrapperFilter;
25 import org.apache.lucene.spatial.geohash.GeoHashDistanceFilter;
28 * <p><font color="red"><b>NOTE:</b> This API is still in
29 * flux and might change in incompatible ways in the next
32 public class DistanceQueryBuilder {
34 private static final long serialVersionUID = 1L;
36 private final double lat;
37 private final double lng;
38 private final double miles;
39 private final Filter filter;
40 final DistanceFilter distanceFilter;
43 * Create a distance query using
44 * a boundary box wrapper around a more precise
47 * @param lat The latitude to search around
48 * @param lng the Longitude to search around
49 * @param miles The radius to search within
50 * @param latField The name of the field containing the latitude
51 * @param lngField The name of the field containing the longitude
52 * @param tierFieldPrefix The prefix of the tier
53 * @param needPrecise if true, then distance is calculated in addition to tier info
54 * @param minTierIndexed The minimum tier level indexed
55 * @param maxTierIndexed The maximum tier level indexed
57 public DistanceQueryBuilder (double lat, double lng, double miles,
58 String latField, String lngField, String tierFieldPrefix, boolean needPrecise, int minTierIndexed, int maxTierIndexed) {
64 CartesianPolyFilterBuilder cpf = new CartesianPolyFilterBuilder(tierFieldPrefix, minTierIndexed, maxTierIndexed);
65 Filter cartesianFilter = cpf.getBoundingArea(lat, lng, miles);
67 /* create precise distance filter */
69 filter = distanceFilter = new LatLongDistanceFilter(cartesianFilter, lat, lng, miles, latField, lngField);
71 filter = cartesianFilter;
72 distanceFilter = null;
77 * Create a distance query using
78 * a boundary box wrapper around a more precise
85 public DistanceQueryBuilder (double lat, double lng, double miles,
86 String geoHashFieldPrefix, String tierFieldPrefix, boolean needPrecise, int minTierIndexed, int maxTierIndexed){
92 CartesianPolyFilterBuilder cpf = new CartesianPolyFilterBuilder(tierFieldPrefix, minTierIndexed, maxTierIndexed);
93 Filter cartesianFilter = cpf.getBoundingArea(lat, lng, miles);
95 /* create precise distance filter */
97 filter = distanceFilter = new GeoHashDistanceFilter(cartesianFilter, lat, lng, miles, geoHashFieldPrefix);
99 filter = cartesianFilter;
100 distanceFilter = null;
106 * Create a distance query using
107 * a boundary box wrapper around a more precise
110 public Filter getFilter() {
111 if (distanceFilter != null) {
112 distanceFilter.reset();
117 public Filter getFilter(Query query) {
118 // Chain the Query (as filter) with our distance filter
119 if (distanceFilter != null) {
120 distanceFilter.reset();
122 QueryWrapperFilter qf = new QueryWrapperFilter(query);
123 return new ChainedFilter(new Filter[] {qf, filter},
127 public DistanceFilter getDistanceFilter() {
128 return distanceFilter;
131 public Query getQuery(Query query){
132 return new ConstantScoreQuery(getFilter(query));
135 public double getLat() {
139 public double getLng() {
143 public double getMiles() {
148 public String toString() {
149 return "DistanceQuery lat: " + lat + " lng: " + lng + " miles: "+ miles;