mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 12:02:10 +00:00
DATAES-66 - Add Ip support to FieldType
added test cases for FieldType.Ip
This commit is contained in:
parent
cbe549694e
commit
0307803316
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2014 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.hamcrest.core.Is.is;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.elasticsearch.core.facet.LogEntity;
|
||||||
|
import org.springframework.data.elasticsearch.core.facet.LogEntityBuilder;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DynamicMappingAndSettingTests
|
||||||
|
*
|
||||||
|
* @author Artur Konczak
|
||||||
|
* @author Mohsin Husen
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||||
|
public class DynamicMappingAndSettingTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ElasticsearchTemplate template;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() throws ParseException {
|
||||||
|
template.deleteIndex(LogEntity.class);
|
||||||
|
template.createIndex(LogEntity.class);
|
||||||
|
template.putMapping(LogEntity.class);
|
||||||
|
|
||||||
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||||
|
IndexQuery indexQuery1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse("2013-10-18 18:01")).code(2)
|
||||||
|
.ip("10.10.10.1").buildIndex();
|
||||||
|
|
||||||
|
IndexQuery indexQuery2 = new LogEntityBuilder("2").action("update").date(dateFormatter.parse("2013-10-19 18:02")).code(2)
|
||||||
|
.ip("10.10.10.2").buildIndex();
|
||||||
|
|
||||||
|
IndexQuery indexQuery3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse("2013-10-19 18:03")).code(2)
|
||||||
|
.ip("10.10.10.3").buildIndex();
|
||||||
|
|
||||||
|
IndexQuery indexQuery4 = new LogEntityBuilder("4").action("update").date(dateFormatter.parse("2013-10-19 18:04")).code(2)
|
||||||
|
.ip("10.10.10.4").buildIndex();
|
||||||
|
|
||||||
|
template.bulkIndex(Arrays.asList(indexQuery1, indexQuery2, indexQuery3, indexQuery4));
|
||||||
|
template.refresh(LogEntity.class, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
DATAES-66
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldIndexGivenLogEntityWithIPFieldType() throws ParseException {
|
||||||
|
//when
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(termQuery("ip", "10.10.10.1")).build();
|
||||||
|
|
||||||
|
List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class);
|
||||||
|
//then
|
||||||
|
assertThat(entities, is(notNullValue()));
|
||||||
|
assertThat(entities.size(), is(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
DATAES-66
|
||||||
|
*/
|
||||||
|
@Test(expected = SearchPhaseExecutionException.class)
|
||||||
|
public void shouldThrowExceptionWhenInvalidIPGivenForSearchQuery() {
|
||||||
|
//when
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(termQuery("ip", "10.10.10")).build();
|
||||||
|
|
||||||
|
List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class);
|
||||||
|
//then
|
||||||
|
assertThat(entities, is(notNullValue()));
|
||||||
|
assertThat(entities.size(), is(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
DATAES-66
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnLogsForGivenIPRanges() {
|
||||||
|
//when
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(rangeQuery("ip").from("10.10.10.1").to("10.10.10.3")).build();
|
||||||
|
|
||||||
|
List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class);
|
||||||
|
//then
|
||||||
|
assertThat(entities, is(notNullValue()));
|
||||||
|
assertThat(entities.size(), is(3));
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core.facet;
|
package org.springframework.data.elasticsearch.core.facet;
|
||||||
|
|
||||||
|
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@ -22,7 +24,6 @@ import org.springframework.data.annotation.Id;
|
|||||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
import org.springframework.data.elasticsearch.annotations.Field;
|
import org.springframework.data.elasticsearch.annotations.Field;
|
||||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple type to test facets
|
* Simple type to test facets
|
||||||
@ -31,7 +32,7 @@ import org.springframework.data.elasticsearch.annotations.FieldType;
|
|||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Document(indexName = "logs", type = "log", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
|
@Document(indexName = "test-log-index", type = "test-log-type", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
|
||||||
public class LogEntity {
|
public class LogEntity {
|
||||||
|
|
||||||
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||||
@ -43,7 +44,10 @@ public class LogEntity {
|
|||||||
|
|
||||||
private long sequenceCode;
|
private long sequenceCode;
|
||||||
|
|
||||||
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
|
@Field(type = Ip)
|
||||||
|
private String ip;
|
||||||
|
|
||||||
|
@Field(type = Date, format = DateFormat.basic_date_time)
|
||||||
private Date date;
|
private Date date;
|
||||||
|
|
||||||
private LogEntity() {
|
private LogEntity() {
|
||||||
@ -81,6 +85,14 @@ public class LogEntity {
|
|||||||
this.sequenceCode = sequenceCode;
|
this.sequenceCode = sequenceCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getIp() {
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIp(String ip) {
|
||||||
|
this.ip = ip;
|
||||||
|
}
|
||||||
|
|
||||||
public Date getDate() {
|
public Date getDate() {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,11 @@ public class LogEntityBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LogEntityBuilder ip(String ip) {
|
||||||
|
result.setIp(ip);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public LogEntity build() {
|
public LogEntity build() {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user