use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceQueryBuilder method fromXContent.
public static GeoDistanceQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
XContentParser parser = parseContext.parser();
XContentParser.Token token;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
GeoPoint point = new GeoPoint(Double.NaN, Double.NaN);
String fieldName = null;
Object vDistance = null;
DistanceUnit unit = GeoDistanceQueryBuilder.DEFAULT_DISTANCE_UNIT;
GeoDistance geoDistance = GeoDistanceQueryBuilder.DEFAULT_GEO_DISTANCE;
GeoValidationMethod validationMethod = null;
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (parseContext.isDeprecatedSetting(currentFieldName)) {
// skip
} else if (token == XContentParser.Token.START_ARRAY) {
fieldName = currentFieldName;
GeoUtils.parseGeoPoint(parser, point);
} else if (token == XContentParser.Token.START_OBJECT) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
// the json in the format of -> field : { lat : 30, lon : 12 }
String currentName = parser.currentName();
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentName = parser.currentName();
} else if (token.isValue()) {
if (currentName.equals("lat")) {
point.resetLat(parser.doubleValue());
} else if (currentName.equals("lon")) {
point.resetLon(parser.doubleValue());
} else if (currentName.equals("geohash")) {
point.resetFromGeoHash(parser.text());
} else {
throw new ParsingException(parser.getTokenLocation(), "[geo_distance] query does not support [" + currentFieldName + "]");
}
}
}
} else if (token.isValue()) {
if (DISTANCE_FIELD.match(currentFieldName)) {
if (token == XContentParser.Token.VALUE_STRING) {
// a String
vDistance = parser.text();
} else {
// a Number
vDistance = parser.numberValue();
}
} else if (UNIT_FIELD.match(currentFieldName)) {
unit = DistanceUnit.fromString(parser.text());
} else if (DISTANCE_TYPE_FIELD.match(currentFieldName)) {
geoDistance = GeoDistance.fromString(parser.text());
} else if (currentFieldName.endsWith(".lat")) {
point.resetLat(parser.doubleValue());
fieldName = currentFieldName.substring(0, currentFieldName.length() - ".lat".length());
} else if (currentFieldName.endsWith(".lon")) {
point.resetLon(parser.doubleValue());
fieldName = currentFieldName.substring(0, currentFieldName.length() - ".lon".length());
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
queryName = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
boost = parser.floatValue();
} else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
ignoreUnmapped = parser.booleanValue();
} else if (VALIDATION_METHOD_FIELD.match(currentFieldName)) {
validationMethod = GeoValidationMethod.fromString(parser.text());
} else {
if (fieldName == null) {
point.resetFromString(parser.text());
fieldName = currentFieldName;
} else {
throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. unexpected field [{}]", NAME, currentFieldName);
}
}
}
}
if (vDistance == null) {
throw new ParsingException(parser.getTokenLocation(), "geo_distance requires 'distance' to be specified");
}
GeoDistanceQueryBuilder qb = new GeoDistanceQueryBuilder(fieldName);
if (vDistance instanceof Number) {
qb.distance(((Number) vDistance).doubleValue(), unit);
} else {
qb.distance((String) vDistance, unit);
}
qb.point(point);
if (validationMethod != null) {
qb.setValidationMethod(validationMethod);
}
qb.geoDistance(geoDistance);
qb.boost(boost);
qb.queryName(queryName);
qb.ignoreUnmapped(ignoreUnmapped);
return qb;
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoPolygonQueryBuilder method doWriteTo.
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
out.writeVInt(shell.size());
for (GeoPoint point : shell) {
out.writeGeoPoint(point);
}
validationMethod.writeTo(out);
out.writeBoolean(ignoreUnmapped);
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoContextMapping method parseContext.
@Override
public Set<CharSequence> parseContext(Document document) {
final Set<CharSequence> geohashes = new HashSet<>();
if (fieldName != null) {
IndexableField[] fields = document.getFields(fieldName);
GeoPoint spare = new GeoPoint();
if (fields.length == 0) {
IndexableField[] lonFields = document.getFields(fieldName + ".lon");
IndexableField[] latFields = document.getFields(fieldName + ".lat");
if (lonFields.length > 0 && latFields.length > 0) {
for (int i = 0; i < lonFields.length; i++) {
IndexableField lonField = lonFields[i];
IndexableField latField = latFields[i];
assert lonField.fieldType().docValuesType() == latField.fieldType().docValuesType();
// we write doc values fields differently: one field for all values, so we need to only care about indexed fields
if (lonField.fieldType().docValuesType() == DocValuesType.NONE) {
spare.reset(latField.numericValue().doubleValue(), lonField.numericValue().doubleValue());
geohashes.add(stringEncode(spare.getLon(), spare.getLat(), precision));
}
}
}
} else {
for (IndexableField field : fields) {
if (field instanceof StringField) {
spare.resetFromString(field.stringValue());
} else {
// todo return this to .stringValue() once LatLonPoint implements it
spare.resetFromIndexableField(field);
}
geohashes.add(spare.geohash());
}
}
}
Set<CharSequence> locations = new HashSet<>();
for (CharSequence geohash : geohashes) {
int precision = Math.min(this.precision, geohash.length());
CharSequence truncatedGeohash = geohash.subSequence(0, precision);
locations.add(truncatedGeohash);
}
return locations;
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class QueryDSLDocumentationTests method testGeoPolygon.
public void testGeoPolygon() {
List<GeoPoint> points = new ArrayList<GeoPoint>();
points.add(new GeoPoint(40, -70));
points.add(new GeoPoint(30, -80));
points.add(new GeoPoint(20, -90));
geoPolygonQuery("pin.location", points);
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceSortBuilderTests method randomGeoDistanceSortBuilder.
public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() {
String fieldName = randomAsciiOfLengthBetween(1, 10);
GeoDistanceSortBuilder result = null;
int id = randomIntBetween(0, 2);
switch(id) {
case 0:
int count = randomIntBetween(1, 10);
String[] geohashes = new String[count];
for (int i = 0; i < count; i++) {
geohashes[i] = RandomGeoGenerator.randomPoint(random()).geohash();
}
result = new GeoDistanceSortBuilder(fieldName, geohashes);
break;
case 1:
GeoPoint pt = RandomGeoGenerator.randomPoint(random());
result = new GeoDistanceSortBuilder(fieldName, pt.getLat(), pt.getLon());
break;
case 2:
result = new GeoDistanceSortBuilder(fieldName, points(new GeoPoint[0]));
break;
default:
throw new IllegalStateException("one of three geo initialisation strategies must be used");
}
if (randomBoolean()) {
result.geoDistance(geoDistance(result.geoDistance()));
}
if (randomBoolean()) {
result.unit(randomValueOtherThan(result.unit(), () -> randomFrom(DistanceUnit.values())));
}
if (randomBoolean()) {
result.order(randomFrom(SortOrder.values()));
}
if (randomBoolean()) {
result.sortMode(randomValueOtherThan(SortMode.SUM, () -> randomFrom(SortMode.values())));
}
if (randomBoolean()) {
result.setNestedFilter(new MatchAllQueryBuilder());
}
if (randomBoolean()) {
result.setNestedPath(randomValueOtherThan(result.getNestedPath(), () -> randomAsciiOfLengthBetween(1, 10)));
}
if (randomBoolean()) {
result.validation(randomValueOtherThan(result.validation(), () -> randomFrom(GeoValidationMethod.values())));
}
return result;
}
Aggregations