-
Notifications
You must be signed in to change notification settings - Fork 12
/
ES004GeoDistanceFilterTest.java
66 lines (57 loc) · 2.79 KB
/
ES004GeoDistanceFilterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.elasticsearchfr.tests;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearchfr.tests.helper.TestNodeHelper;
import org.junit.Assert;
import org.junit.Test;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
* Testing GeoDistance Filter in Java
* See discussion on <a href="https://groups.google.com/d/topic/elasticsearch/un-ekdk1qD0/discussion">Elasticsearch Mailing List</a>
* @author David Pilato (aka dadoonet)
*/
public class ES004GeoDistanceFilterTest extends TestNodeHelper {
protected final ESLogger logger = ESLoggerFactory.getLogger(this.getClass().getName());
/**
* @throws Exception
*/
@Test
public void putMappingWithNotAnalyzed() throws Exception {
node.client().admin().indices().prepareCreate("es004index").execute().actionGet();
XContentBuilder xbMapping = buildMapping();
logger.info("Mapping is : {}", xbMapping.string());
PutMappingResponse response = node.client().admin().indices()
.preparePutMapping("es004index")
.setType("type1")
.setSource(xbMapping)
.execute().actionGet();
if (!response.isAcknowledged()) {
throw new Exception("Could not define mapping.");
}
node.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();
node.client().prepareIndex("es004index", "type1").setSource("{\"location\" : { \"lat\" : 5, \"lon\" : 5 }}").execute().actionGet();
node.client().admin().indices().prepareRefresh().execute().actionGet();
SearchRequestBuilder srb = node.client().prepareSearch("es004index").setTypes("type1")
.setQuery(QueryBuilders.matchAllQuery())
.setFilter(FilterBuilders.geoDistanceFilter("location").distance("0.5km").point(5, 5));
SearchResponse sr = srb.execute().actionGet();
Assert.assertNotNull(sr);
Assert.assertEquals(1L, sr.getHits().totalHits());
}
private static XContentBuilder buildMapping() throws Exception {
return jsonBuilder().prettyPrint()
.startObject()
.startObject("type1")
.startObject("properties")
.startObject("location").field("type", "geo_point").endObject()
.endObject()
.endObject()
.endObject();
}
}