ElasticsearchIntegrationTest 상속하여 @SuiteScopeTest - setupSuiteScopeCluster() 사용
Elasticsearch 2015. 5. 6. 13:53
ElasticsearchIntegrationTest를 상속하여 integration test 를 진행할 때, @Before, @BeforeClass, @After, @AfterClass를 사용할 수 없다. (final), 그러나 @SuiteScopeTest를 사용하면 초기 생성에 대한 메소드를 사용할 수 있다.
setupSuiteScopeCluster() 메소드를 구현하면 before()메소드를 사용하는 것처럼 사용할 수 있다.
아래 예제 코드는 setupSuiteScopeCluster()에서 elasticsearch에 index/type를 생성하고, 데이터 mapping과 데이터를 추가하고, @Test 코드에서 동작할 수 있도록 의미한다.
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;
import java.io.IOException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.GeoDistanceFilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
import org.elasticsearch.test.ElasticsearchIntegrationTest.SuiteScopeTest;
import org.junit.Ignore;
import org.junit.Test;
@ClusterScope(scope = Scope.TEST, numDataNodes = 1)
public class ElasticsearchSetupTest extends ElasticsearchIntegrationTest {
@Override
public void setupSuiteScopeCluster() throws Exception {
// 생성
prepareCreate("location_poi")
.setSettings("index.number_of_shards", 1)
.setSettings("index.number_of_replicas", 0)
.addMapping("location", "user_lat_lon", "type=geo_point", "locId", "type=string,index=not_analyzed" ).execute().actionGet();
// 데이터 입력
GeoPoint[] geoPoint = {new GeoPoint(37.5133128626991, 127.10022875908236), new GeoPoint(37, 128) };
client().prepareIndex("location_poi", "location", "1").setSource(jsonBuilder().startObject()
.field("locId", "da_21160809")
.array("user_lat_lon", geoPoint[0].lon(), geoPoint[0].lat())
.endObject()
).setRefresh(true).execute().actionGet();
client().prepareIndex("location_poi", "location", "2").setSource(jsonBuilder().startObject()
.field("locId", "da_123456")
.array("user_lat_lon", geoPoint[1].lon(), geoPoint[1].lat())
.endObject()
).setRefresh(true).execute().actionGet();
refresh();
ensureSearchable("location_poi");
}
@Test
public void mapAndGeopointTest() throws IOException {
GeoDistanceFilterBuilder geoDistnaceFilterBuilder =
FilterBuilders.geoDistanceFilter("user_lat_lon")
.distance(3, DistanceUnit.KILOMETERS).lat(37.5133128626990).lon(127.10022875908237);
QueryBuilder queryBuilder = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), geoDistnaceFilterBuilder);
SearchRequestBuilder searchRequest = client().prepareSearch("location_poi").setTypes("location").setQuery(queryBuilder);
SearchResponse searchResponse = searchRequest.execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
}
'Elasticsearch' 카테고리의 다른 글
[elasticsearch] 거리 측정 API (0) | 2015.05.12 |
---|---|
elasticsearch의 geo distance filter 의 결과는 caching하지 않는다.(디폴트) (0) | 2015.05.06 |
elasticsearch intergration test 시 ClusterScope 주의사항 (0) | 2015.05.06 |
elasticsearch integration test (local deamon) 할 수 있도록 하기 (0) | 2015.05.06 |
위도 / 경도 (latitude, longitude) 찾기 (0) | 2015.04.09 |