make GeoPoint parsable in lat/lon json format

This commit is contained in:
Britta Weber 2013-08-16 13:19:59 +02:00
parent f125ac122c
commit db100aa2de
2 changed files with 62 additions and 11 deletions

View File

@ -24,6 +24,7 @@ import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.BytesStream; import org.elasticsearch.common.io.BytesStream;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
@ -1117,6 +1118,11 @@ public final class XContentBuilder implements BytesStream {
generator.writeNumber(((Short) value).shortValue()); generator.writeNumber(((Short) value).shortValue());
} else if (type == Boolean.class) { } else if (type == Boolean.class) {
generator.writeBoolean(((Boolean) value).booleanValue()); generator.writeBoolean(((Boolean) value).booleanValue());
} else if (type == GeoPoint.class) {
generator.writeStartObject();
generator.writeNumberField("lat", ((GeoPoint) value).lat());
generator.writeNumberField("lon", ((GeoPoint) value).lon());
generator.writeEndObject();
} else if (value instanceof Map) { } else if (value instanceof Map) {
writeMap((Map) value); writeMap((Map) value);
} else if (value instanceof Iterable) { } else if (value instanceof Iterable) {

View File

@ -25,6 +25,7 @@ import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction; import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.MatchAllFilterBuilder; import org.elasticsearch.index.query.MatchAllFilterBuilder;
import org.elasticsearch.index.query.functionscore.DecayFunctionBuilder; import org.elasticsearch.index.query.functionscore.DecayFunctionBuilder;
@ -45,8 +46,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery; import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.lessThan;
public class DecayFunctionScoreTests extends AbstractSharedClusterTest { public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
@ -149,7 +149,6 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
assertThat(sh.getAt(1).getId(), equalTo("2")); assertThat(sh.getAt(1).getId(), equalTo("2"));
} }
@Test @Test
public void testBoostModeSettingWorks() throws Exception { public void testBoostModeSettingWorks() throws Exception {
@ -169,8 +168,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
.setId("2") .setId("2")
.setIndex("test") .setIndex("test")
.setSource( .setSource(
jsonBuilder().startObject().field("test", "value value").startObject("loc").field("lat", 11).field("lon", 20).endObject() jsonBuilder().startObject().field("test", "value value").startObject("loc").field("lat", 11).field("lon", 20)
.endObject())); .endObject().endObject()));
IndexRequestBuilder[] builders = indexBuilders.toArray(new IndexRequestBuilder[indexBuilders.size()]); IndexRequestBuilder[] builders = indexBuilders.toArray(new IndexRequestBuilder[indexBuilders.size()]);
indexRandom("test", false, builders); indexRandom("test", false, builders);
@ -184,7 +183,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
ActionFuture<SearchResponse> response = client().search( ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source( searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName())))); searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName()))));
SearchResponse sr = response.actionGet(); SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits(); SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2))); assertThat(sh.getTotalHits(), equalTo((long) (2)));
@ -194,7 +194,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
// Test Exp // Test Exp
response = client().search( response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source( searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.PLAIN.getName())))); searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.PLAIN.getName()))));
sr = response.actionGet(); sr = response.actionGet();
sh = sr.getHits(); sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2))); assertThat(sh.getTotalHits(), equalTo((long) (2)));
@ -203,6 +204,50 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
} }
@Test
public void testParseGeoPoint() throws Exception {
createIndexMapped("test", "type1", "test", "string", "loc", "geo_point");
ensureYellow();
List<IndexRequestBuilder> indexBuilders = new ArrayList<IndexRequestBuilder>();
indexBuilders.add(new IndexRequestBuilder(client())
.setType("type1")
.setId("1")
.setIndex("test")
.setSource(
jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 20).field("lon", 11).endObject()
.endObject()));
IndexRequestBuilder[] builders = indexBuilders.toArray(new IndexRequestBuilder[indexBuilders.size()]);
indexRandom("test", false, builders);
refresh();
GeoPoint point = new GeoPoint(20, 11);
DecayFunctionBuilder fb = new GaussDecayFunctionBuilder("loc", point, "1000km");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName()))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat((double) sh.getAt(0).score(), closeTo(0.30685282, 1.e-5));
float[] coords = {11,20};
fb = new GaussDecayFunctionBuilder("loc", coords, "1000km");
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat((double) sh.getAt(0).score(), closeTo(0.30685282, 1.e-5));
}
@Test(expected = SearchPhaseExecutionException.class) @Test(expected = SearchPhaseExecutionException.class)
public void testExceptionThrownIfScaleLE0() throws Exception { public void testExceptionThrownIfScaleLE0() throws Exception {