Skip to content

Commit

Permalink
Fixed the GeoPointFieldMapper to parse geohashes correctly.
Browse files Browse the repository at this point in the history
Closes #3073
  • Loading branch information
chilling committed Jun 10, 2013
1 parent b9feaa9 commit f64f7c0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Expand Up @@ -440,7 +440,7 @@ private void parseGeohash(ParseContext context, String geohash) throws IOExcepti
}
}

context.externalValue(Double.toString(point.lat()) + ',' + Double.toString(point.lat()));
context.externalValue(Double.toString(point.lat()) + ',' + Double.toString(point.lon()));
geoStringMapper.parse(context);
if (enableGeoHash) {
context.externalValue(geohash);
Expand Down
Expand Up @@ -19,22 +19,29 @@

package org.elasticsearch.test.integration.search.geo;

import java.io.IOException;

import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.filteredQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
Expand Down Expand Up @@ -688,5 +695,50 @@ public void testDistanceSortingNestedFields() throws Exception {
assertThat(e.shardFailures()[0].status(), equalTo(RestStatus.BAD_REQUEST));
}
}

/**
* Issue 3073
*/
@Test
public void testGeoDistanceFilter() throws IOException {
double lat = 40.720611;
double lon = -73.998776;

XContentBuilder mapping = JsonXContent.contentBuilder()
.startObject()
.startObject("location")
.startObject("properties")
.startObject("pin")
.field("type", "geo_point")
.field("geohash", true)
.field("geohash_precision", 24)
.field("lat_lon", true)
.endObject()
.endObject()
.endObject()
.endObject();

XContentBuilder source = JsonXContent.contentBuilder()
.startObject()
.field("pin", GeoHashUtils.encode(lat, lon))
.endObject();

ensureYellow();

client().admin().indices().prepareCreate("locations").addMapping("location", mapping).execute().actionGet();
client().prepareIndex("locations", "location", "1").setCreate(true).setSource(source).execute().actionGet();
client().admin().indices().prepareRefresh("locations").execute().actionGet();
client().prepareGet("locations", "location", "1").execute().actionGet();

SearchResponse result = client().prepareSearch("locations")
.setQuery(QueryBuilders.matchAllQuery())
.setFilter(FilterBuilders.geoDistanceFilter("pin")
.geoDistance(GeoDistance.ARC)
.lat(lat).lon(lon)
.distance("1m"))
.execute().actionGet();

assertHitCount(result, 1);
}

}

0 comments on commit f64f7c0

Please sign in to comment.