Skip to content

Commit 0307803

Browse files
committed
DATAES-66 - Add Ip support to FieldType
added test cases for FieldType.Ip
1 parent cbe5496 commit 0307803

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core;
17+
18+
import static org.elasticsearch.index.query.QueryBuilders.*;
19+
import static org.hamcrest.Matchers.*;
20+
import static org.hamcrest.core.Is.is;
21+
import static org.junit.Assert.*;
22+
23+
import java.text.ParseException;
24+
import java.text.SimpleDateFormat;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
28+
import org.elasticsearch.action.search.SearchPhaseExecutionException;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.data.elasticsearch.core.facet.LogEntity;
34+
import org.springframework.data.elasticsearch.core.facet.LogEntityBuilder;
35+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
36+
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
37+
import org.springframework.data.elasticsearch.core.query.SearchQuery;
38+
import org.springframework.test.context.ContextConfiguration;
39+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
40+
41+
/**
42+
* DynamicMappingAndSettingTests
43+
*
44+
* @author Artur Konczak
45+
* @author Mohsin Husen
46+
*/
47+
@RunWith(SpringJUnit4ClassRunner.class)
48+
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
49+
public class DynamicMappingAndSettingTests {
50+
51+
@Autowired
52+
private ElasticsearchTemplate template;
53+
54+
@Before
55+
public void before() throws ParseException {
56+
template.deleteIndex(LogEntity.class);
57+
template.createIndex(LogEntity.class);
58+
template.putMapping(LogEntity.class);
59+
60+
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
61+
IndexQuery indexQuery1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse("2013-10-18 18:01")).code(2)
62+
.ip("10.10.10.1").buildIndex();
63+
64+
IndexQuery indexQuery2 = new LogEntityBuilder("2").action("update").date(dateFormatter.parse("2013-10-19 18:02")).code(2)
65+
.ip("10.10.10.2").buildIndex();
66+
67+
IndexQuery indexQuery3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse("2013-10-19 18:03")).code(2)
68+
.ip("10.10.10.3").buildIndex();
69+
70+
IndexQuery indexQuery4 = new LogEntityBuilder("4").action("update").date(dateFormatter.parse("2013-10-19 18:04")).code(2)
71+
.ip("10.10.10.4").buildIndex();
72+
73+
template.bulkIndex(Arrays.asList(indexQuery1, indexQuery2, indexQuery3, indexQuery4));
74+
template.refresh(LogEntity.class, true);
75+
}
76+
77+
/*
78+
DATAES-66
79+
*/
80+
@Test
81+
public void shouldIndexGivenLogEntityWithIPFieldType() throws ParseException {
82+
//when
83+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
84+
.withQuery(termQuery("ip", "10.10.10.1")).build();
85+
86+
List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class);
87+
//then
88+
assertThat(entities, is(notNullValue()));
89+
assertThat(entities.size(), is(1));
90+
}
91+
92+
/*
93+
DATAES-66
94+
*/
95+
@Test(expected = SearchPhaseExecutionException.class)
96+
public void shouldThrowExceptionWhenInvalidIPGivenForSearchQuery() {
97+
//when
98+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
99+
.withQuery(termQuery("ip", "10.10.10")).build();
100+
101+
List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class);
102+
//then
103+
assertThat(entities, is(notNullValue()));
104+
assertThat(entities.size(), is(1));
105+
}
106+
107+
/*
108+
DATAES-66
109+
*/
110+
@Test
111+
public void shouldReturnLogsForGivenIPRanges() {
112+
//when
113+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
114+
.withQuery(rangeQuery("ip").from("10.10.10.1").to("10.10.10.3")).build();
115+
116+
List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class);
117+
//then
118+
assertThat(entities, is(notNullValue()));
119+
assertThat(entities.size(), is(3));
120+
}
121+
}

src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntity.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.facet;
1717

18+
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
19+
1820
import java.text.SimpleDateFormat;
1921
import java.util.Date;
2022

2123
import org.springframework.data.annotation.Id;
2224
import org.springframework.data.elasticsearch.annotations.DateFormat;
2325
import org.springframework.data.elasticsearch.annotations.Document;
2426
import org.springframework.data.elasticsearch.annotations.Field;
25-
import org.springframework.data.elasticsearch.annotations.FieldType;
2627

2728
/**
2829
* Simple type to test facets
@@ -31,7 +32,7 @@
3132
* @author Mohsin Husen
3233
*/
3334

34-
@Document(indexName = "logs", type = "log", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
35+
@Document(indexName = "test-log-index", type = "test-log-type", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
3536
public class LogEntity {
3637

3738
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
@@ -43,7 +44,10 @@ public class LogEntity {
4344

4445
private long sequenceCode;
4546

46-
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
47+
@Field(type = Ip)
48+
private String ip;
49+
50+
@Field(type = Date, format = DateFormat.basic_date_time)
4751
private Date date;
4852

4953
private LogEntity() {
@@ -81,6 +85,14 @@ public void setSequenceCode(long sequenceCode) {
8185
this.sequenceCode = sequenceCode;
8286
}
8387

88+
public String getIp() {
89+
return ip;
90+
}
91+
92+
public void setIp(String ip) {
93+
this.ip = ip;
94+
}
95+
8496
public Date getDate() {
8597
return date;
8698
}

src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntityBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public LogEntityBuilder date(Date date) {
4949
return this;
5050
}
5151

52+
public LogEntityBuilder ip(String ip) {
53+
result.setIp(ip);
54+
return this;
55+
}
56+
5257
public LogEntity build() {
5358
return result;
5459
}

0 commit comments

Comments
 (0)