2010-07-28 21:06:18 +03:00
|
|
|
/*
|
2014-01-06 22:48:02 +01:00
|
|
|
* Licensed to Elasticsearch under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch 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
|
2010-07-28 21:06:18 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-06-03 04:32:27 +03:00
|
|
|
package org.elasticsearch.index.query;
|
2010-07-28 21:06:18 +03:00
|
|
|
|
2013-01-23 14:13:12 +01:00
|
|
|
import org.elasticsearch.common.geo.GeoDistance;
|
2010-07-28 21:06:18 +03:00
|
|
|
import org.elasticsearch.common.unit.DistanceUnit;
|
2010-09-12 23:20:13 +02:00
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
2010-07-28 21:06:18 +03:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2013-05-18 23:49:04 +02:00
|
|
|
import java.util.Locale;
|
2010-07-28 21:06:18 +03:00
|
|
|
|
2015-05-08 16:34:18 +02:00
|
|
|
public class GeoDistanceQueryBuilder extends QueryBuilder {
|
2010-07-28 21:06:18 +03:00
|
|
|
|
|
|
|
private final String name;
|
|
|
|
|
|
|
|
private String distance;
|
|
|
|
|
|
|
|
private double lat;
|
|
|
|
|
|
|
|
private double lon;
|
|
|
|
|
|
|
|
private String geohash;
|
|
|
|
|
|
|
|
private GeoDistance geoDistance;
|
|
|
|
|
2011-09-14 14:27:39 +03:00
|
|
|
private String optimizeBbox;
|
2011-08-19 07:07:02 +03:00
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
private String queryName;
|
2010-09-11 12:38:19 +03:00
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder(String name) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder point(double lat, double lon) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.lat = lat;
|
|
|
|
this.lon = lon;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder lat(double lat) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.lat = lat;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder lon(double lon) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.lon = lon;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder distance(String distance) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.distance = distance;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder distance(double distance, DistanceUnit unit) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.distance = unit.toString(distance);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder geohash(String geohash) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.geohash = geohash;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder geoDistance(GeoDistance geoDistance) {
|
2010-07-28 21:06:18 +03:00
|
|
|
this.geoDistance = geoDistance;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder optimizeBbox(String optimizeBbox) {
|
2011-08-19 07:07:02 +03:00
|
|
|
this.optimizeBbox = optimizeBbox;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-10-16 16:12:38 +02:00
|
|
|
/**
|
|
|
|
* Sets the filter name for the filter that can be used when searching for matched_filters per hit.
|
|
|
|
*/
|
2015-05-05 08:27:52 +02:00
|
|
|
public GeoDistanceQueryBuilder queryName(String queryName) {
|
|
|
|
this.queryName = queryName;
|
2010-09-11 12:38:19 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
2015-05-05 08:27:52 +02:00
|
|
|
builder.startObject(GeoDistanceQueryParser.NAME);
|
2010-07-28 21:06:18 +03:00
|
|
|
if (geohash != null) {
|
|
|
|
builder.field(name, geohash);
|
|
|
|
} else {
|
2011-02-01 01:39:01 +02:00
|
|
|
builder.startArray(name).value(lon).value(lat).endArray();
|
2010-07-28 21:06:18 +03:00
|
|
|
}
|
|
|
|
builder.field("distance", distance);
|
|
|
|
if (geoDistance != null) {
|
2013-05-18 23:49:04 +02:00
|
|
|
builder.field("distance_type", geoDistance.name().toLowerCase(Locale.ROOT));
|
2010-07-28 21:06:18 +03:00
|
|
|
}
|
2011-08-19 07:07:02 +03:00
|
|
|
if (optimizeBbox != null) {
|
2011-09-14 14:27:39 +03:00
|
|
|
builder.field("optimize_bbox", optimizeBbox);
|
2011-08-19 07:07:02 +03:00
|
|
|
}
|
2015-05-05 08:27:52 +02:00
|
|
|
if (queryName != null) {
|
|
|
|
builder.field("_name", queryName);
|
2010-09-11 12:38:19 +03:00
|
|
|
}
|
2010-07-28 21:06:18 +03:00
|
|
|
builder.endObject();
|
|
|
|
}
|
|
|
|
}
|