DATAES-2 - all code reformatted to follow Spring Data code rules
This commit is contained in:
@@ -22,22 +22,22 @@ package org.springframework.data.elasticsearch;
|
||||
*/
|
||||
public class Car {
|
||||
|
||||
private String name;
|
||||
private String model;
|
||||
private String name;
|
||||
private String model;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,19 +20,19 @@ package org.springframework.data.elasticsearch;
|
||||
*/
|
||||
public class CarBuilder {
|
||||
|
||||
private Car car = new Car();
|
||||
private Car car = new Car();
|
||||
|
||||
public CarBuilder name(String name) {
|
||||
car.setName(name);
|
||||
return this;
|
||||
}
|
||||
public CarBuilder name(String name) {
|
||||
car.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CarBuilder model(String model) {
|
||||
car.setModel(model);
|
||||
return this;
|
||||
}
|
||||
public CarBuilder model(String model) {
|
||||
car.setModel(model);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Car build(){
|
||||
return car;
|
||||
}
|
||||
public Car build() {
|
||||
return car;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,5 +65,4 @@ public class DoubleIDEntity {
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,45 +15,45 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
|
||||
public class GirlFriend {
|
||||
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
private String type;
|
||||
|
||||
@Field(type = FieldType.Nested)
|
||||
private List<Car> cars;
|
||||
@Field(type = FieldType.Nested)
|
||||
private List<Car> cars;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<Car> getCars() {
|
||||
return cars;
|
||||
}
|
||||
public List<Car> getCars() {
|
||||
return cars;
|
||||
}
|
||||
|
||||
public void setCars(List<Car> cars) {
|
||||
this.cars = cars;
|
||||
}
|
||||
public void setCars(List<Car> cars) {
|
||||
this.cars = cars;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,5 +65,4 @@ public class IntegerIDEntity {
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
|
||||
/**
|
||||
* MinimalEntity
|
||||
*
|
||||
@@ -24,6 +25,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
*/
|
||||
@Document(indexName = "index", type = "type")
|
||||
public class MinimalEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.*;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.junit.Before;
|
||||
@@ -31,20 +40,6 @@ import org.springframework.data.elasticsearch.repositories.SampleElasticSearchBo
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -54,130 +49,128 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:/repository-test-nested-object.xml")
|
||||
public class NestedObjectTests {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private SampleElasticSearchBookRepository bookRepository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(Book.class);
|
||||
elasticsearchTemplate.createIndex(Book.class);
|
||||
elasticsearchTemplate.refresh(Book.class, true);
|
||||
elasticsearchTemplate.deleteIndex(Person.class);
|
||||
elasticsearchTemplate.createIndex(Person.class);
|
||||
elasticsearchTemplate.putMapping(Person.class);
|
||||
elasticsearchTemplate.refresh(Person.class, true);
|
||||
elasticsearchTemplate.deleteIndex(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.createIndex(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.refresh(PersonMultipleLevelNested.class,true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldIndexInitialLevelNestedObject(){
|
||||
|
||||
List<Car> cars = new ArrayList<Car>();
|
||||
|
||||
Car saturn = new Car();
|
||||
saturn.setName("Saturn");
|
||||
saturn.setModel("SL");
|
||||
|
||||
Car subaru = new Car();
|
||||
subaru.setName("Subaru");
|
||||
subaru.setModel("Imprezza");
|
||||
|
||||
Car ford = new Car();
|
||||
ford.setName("Ford");
|
||||
ford.setModel("Focus");
|
||||
|
||||
cars.add(saturn);
|
||||
cars.add(subaru);
|
||||
cars.add(ford);
|
||||
|
||||
Person foo = new Person();
|
||||
foo.setName("Foo");
|
||||
foo.setId("1");
|
||||
foo.setCar(cars);
|
||||
|
||||
|
||||
Car car = new Car();
|
||||
car.setName("Saturn");
|
||||
car.setModel("Imprezza");
|
||||
|
||||
Person bar = new Person();
|
||||
bar.setId("2");
|
||||
bar.setName("Bar");
|
||||
bar.setCar(Arrays.asList(car));
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
IndexQuery indexQuery1 = new IndexQuery();
|
||||
indexQuery1.setId(foo.getId());
|
||||
indexQuery1.setObject(foo);
|
||||
|
||||
IndexQuery indexQuery2 = new IndexQuery();
|
||||
indexQuery2.setId(bar.getId());
|
||||
indexQuery2.setObject(bar);
|
||||
|
||||
indexQueries.add(indexQuery1);
|
||||
indexQueries.add(indexQuery2);
|
||||
|
||||
elasticsearchTemplate.putMapping(Person.class);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(Person.class, true);
|
||||
|
||||
QueryBuilder builder = nestedQuery("car", boolQuery().must(termQuery("car.name", "saturn")).must(termQuery("car.model", "imprezza")));
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
||||
List<Person> persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);
|
||||
|
||||
assertThat(persons.size() , is(1));
|
||||
|
||||
}
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(Book.class);
|
||||
elasticsearchTemplate.createIndex(Book.class);
|
||||
elasticsearchTemplate.refresh(Book.class, true);
|
||||
elasticsearchTemplate.deleteIndex(Person.class);
|
||||
elasticsearchTemplate.createIndex(Person.class);
|
||||
elasticsearchTemplate.putMapping(Person.class);
|
||||
elasticsearchTemplate.refresh(Person.class, true);
|
||||
elasticsearchTemplate.deleteIndex(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.createIndex(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldIndexMultipleLevelNestedObject(){
|
||||
//given
|
||||
List<IndexQuery> indexQueries = createPerson();
|
||||
public void shouldIndexInitialLevelNestedObject() {
|
||||
|
||||
//when
|
||||
elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true);
|
||||
List<Car> cars = new ArrayList<Car>();
|
||||
|
||||
//then
|
||||
GetQuery getQuery = new GetQuery();
|
||||
getQuery.setId("1");
|
||||
PersonMultipleLevelNested personIndexed = elasticsearchTemplate.queryForObject(getQuery, PersonMultipleLevelNested.class);
|
||||
assertThat(personIndexed, is(notNullValue()));
|
||||
}
|
||||
Car saturn = new Car();
|
||||
saturn.setName("Saturn");
|
||||
saturn.setModel("SL");
|
||||
|
||||
@Test
|
||||
public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject(){
|
||||
//given
|
||||
List<IndexQuery> indexQueries = createPerson();
|
||||
Car subaru = new Car();
|
||||
subaru.setName("Subaru");
|
||||
subaru.setModel("Imprezza");
|
||||
|
||||
//when
|
||||
elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true);
|
||||
Car ford = new Car();
|
||||
ford.setName("Ford");
|
||||
ford.setModel("Focus");
|
||||
|
||||
//then
|
||||
BoolQueryBuilder builder = boolQuery();
|
||||
builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type","temp")))
|
||||
.must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase())));
|
||||
cars.add(saturn);
|
||||
cars.add(subaru);
|
||||
cars.add(ford);
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(builder)
|
||||
.build();
|
||||
Person foo = new Person();
|
||||
foo.setName("Foo");
|
||||
foo.setId("1");
|
||||
foo.setCar(cars);
|
||||
|
||||
Page<PersonMultipleLevelNested> personIndexed = elasticsearchTemplate.queryForPage(searchQuery, PersonMultipleLevelNested.class);
|
||||
assertThat(personIndexed, is(notNullValue()));
|
||||
assertThat(personIndexed.getTotalElements(), is(1L));
|
||||
assertThat(personIndexed.getContent().get(0).getId(), is("1"));
|
||||
}
|
||||
Car car = new Car();
|
||||
car.setName("Saturn");
|
||||
car.setModel("Imprezza");
|
||||
|
||||
Person bar = new Person();
|
||||
bar.setId("2");
|
||||
bar.setName("Bar");
|
||||
bar.setCar(Arrays.asList(car));
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
IndexQuery indexQuery1 = new IndexQuery();
|
||||
indexQuery1.setId(foo.getId());
|
||||
indexQuery1.setObject(foo);
|
||||
|
||||
IndexQuery indexQuery2 = new IndexQuery();
|
||||
indexQuery2.setId(bar.getId());
|
||||
indexQuery2.setObject(bar);
|
||||
|
||||
indexQueries.add(indexQuery1);
|
||||
indexQueries.add(indexQuery2);
|
||||
|
||||
elasticsearchTemplate.putMapping(Person.class);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(Person.class, true);
|
||||
|
||||
QueryBuilder builder = nestedQuery("car", boolQuery().must(termQuery("car.name", "saturn")).must(termQuery("car.model", "imprezza")));
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
||||
List<Person> persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);
|
||||
|
||||
assertThat(persons.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldIndexMultipleLevelNestedObject() {
|
||||
//given
|
||||
List<IndexQuery> indexQueries = createPerson();
|
||||
|
||||
//when
|
||||
elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true);
|
||||
|
||||
//then
|
||||
GetQuery getQuery = new GetQuery();
|
||||
getQuery.setId("1");
|
||||
PersonMultipleLevelNested personIndexed = elasticsearchTemplate.queryForObject(getQuery, PersonMultipleLevelNested.class);
|
||||
assertThat(personIndexed, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject() {
|
||||
//given
|
||||
List<IndexQuery> indexQueries = createPerson();
|
||||
|
||||
//when
|
||||
elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true);
|
||||
|
||||
//then
|
||||
BoolQueryBuilder builder = boolQuery();
|
||||
builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type", "temp")))
|
||||
.must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase())));
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(builder)
|
||||
.build();
|
||||
|
||||
Page<PersonMultipleLevelNested> personIndexed = elasticsearchTemplate.queryForPage(searchQuery, PersonMultipleLevelNested.class);
|
||||
assertThat(personIndexed, is(notNullValue()));
|
||||
assertThat(personIndexed.getTotalElements(), is(1L));
|
||||
assertThat(personIndexed.getContent().get(0).getId(), is("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldIndexInnerObject() {
|
||||
@@ -196,62 +189,60 @@ public class NestedObjectTests {
|
||||
assertThat(bookRepository.findOne(id), is(notNullValue()));
|
||||
}
|
||||
|
||||
private List<IndexQuery> createPerson(){
|
||||
private List<IndexQuery> createPerson() {
|
||||
|
||||
PersonMultipleLevelNested person1 = new PersonMultipleLevelNested();
|
||||
PersonMultipleLevelNested person1 = new PersonMultipleLevelNested();
|
||||
|
||||
person1.setId("1");
|
||||
person1.setName("name");
|
||||
person1.setId("1");
|
||||
person1.setName("name");
|
||||
|
||||
Car saturn = new Car();
|
||||
saturn.setName("Saturn");
|
||||
saturn.setModel("SL");
|
||||
Car saturn = new Car();
|
||||
saturn.setName("Saturn");
|
||||
saturn.setModel("SL");
|
||||
|
||||
Car subaru = new Car();
|
||||
subaru.setName("Subaru");
|
||||
subaru.setModel("Imprezza");
|
||||
Car subaru = new Car();
|
||||
subaru.setName("Subaru");
|
||||
subaru.setModel("Imprezza");
|
||||
|
||||
Car car = new Car();
|
||||
car.setName("Saturn");
|
||||
car.setModel("Imprezza");
|
||||
Car car = new Car();
|
||||
car.setName("Saturn");
|
||||
car.setModel("Imprezza");
|
||||
|
||||
Car ford = new Car();
|
||||
ford.setName("Ford");
|
||||
ford.setModel("Focus");
|
||||
Car ford = new Car();
|
||||
ford.setName("Ford");
|
||||
ford.setModel("Focus");
|
||||
|
||||
GirlFriend permanent = new GirlFriend();
|
||||
permanent.setName("permanent");
|
||||
permanent.setType("permanent");
|
||||
permanent.setCars(Arrays.asList(saturn, subaru));
|
||||
GirlFriend permanent = new GirlFriend();
|
||||
permanent.setName("permanent");
|
||||
permanent.setType("permanent");
|
||||
permanent.setCars(Arrays.asList(saturn, subaru));
|
||||
|
||||
GirlFriend temp = new GirlFriend();
|
||||
temp.setName("temp");
|
||||
temp.setType("temp");
|
||||
temp.setCars(Arrays.asList(car,ford));
|
||||
GirlFriend temp = new GirlFriend();
|
||||
temp.setName("temp");
|
||||
temp.setType("temp");
|
||||
temp.setCars(Arrays.asList(car, ford));
|
||||
|
||||
person1.setGirlFriends(Arrays.asList(permanent, temp));
|
||||
person1.setGirlFriends(Arrays.asList(permanent, temp));
|
||||
|
||||
IndexQuery indexQuery1 = new IndexQuery();
|
||||
indexQuery1.setId(person1.getId());
|
||||
indexQuery1.setObject(person1);
|
||||
IndexQuery indexQuery1 = new IndexQuery();
|
||||
indexQuery1.setId(person1.getId());
|
||||
indexQuery1.setObject(person1);
|
||||
|
||||
PersonMultipleLevelNested person2 = new PersonMultipleLevelNested();
|
||||
|
||||
PersonMultipleLevelNested person2 = new PersonMultipleLevelNested();
|
||||
person2.setId("2");
|
||||
person2.setName("name");
|
||||
|
||||
person2.setId("2");
|
||||
person2.setName("name");
|
||||
person2.setGirlFriends(Arrays.asList(permanent));
|
||||
|
||||
person2.setGirlFriends(Arrays.asList(permanent));
|
||||
IndexQuery indexQuery2 = new IndexQuery();
|
||||
indexQuery2.setId(person2.getId());
|
||||
indexQuery2.setObject(person2);
|
||||
|
||||
IndexQuery indexQuery2 = new IndexQuery();
|
||||
indexQuery2.setId(person2.getId());
|
||||
indexQuery2.setObject(person2);
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(indexQuery1);
|
||||
indexQueries.add(indexQuery2);
|
||||
|
||||
return indexQueries;
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(indexQuery1);
|
||||
indexQueries.add(indexQuery2);
|
||||
|
||||
return indexQueries;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.*;
|
||||
|
||||
/**
|
||||
* ParentEntity
|
||||
*
|
||||
@@ -26,6 +27,7 @@ import org.springframework.data.elasticsearch.annotations.*;
|
||||
*/
|
||||
@Document(indexName = ParentEntity.INDEX, type = ParentEntity.PARENT_TYPE, indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class ParentEntity {
|
||||
|
||||
public static final String INDEX = "parent-child";
|
||||
public static final String PARENT_TYPE = "parent-entity";
|
||||
public static final String CHILD_TYPE = "child-entity";
|
||||
@@ -58,6 +60,7 @@ public class ParentEntity {
|
||||
|
||||
@Document(indexName = INDEX, type = CHILD_TYPE, indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public static class ChildEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Field(type = FieldType.String, store = true)
|
||||
|
||||
@@ -16,51 +16,51 @@
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
|
||||
@Document( indexName = "person" , type = "user", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
@Document(indexName = "person", type = "user", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
@Field( type = FieldType.Nested)
|
||||
private List<Car> car;
|
||||
@Field(type = FieldType.Nested)
|
||||
private List<Car> car;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Car> getCar() {
|
||||
return car;
|
||||
}
|
||||
public List<Car> getCar() {
|
||||
return car;
|
||||
}
|
||||
|
||||
public void setCar(List<Car> car) {
|
||||
this.car = car;
|
||||
}
|
||||
public void setCar(List<Car> car) {
|
||||
this.car = car;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,51 +16,51 @@
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
|
||||
@Document( indexName = "person-multiple-level-nested" , type = "user", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
@Document(indexName = "person-multiple-level-nested", type = "user", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class PersonMultipleLevelNested {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
@Field(type = FieldType.Nested)
|
||||
private List<GirlFriend> girlFriends;
|
||||
@Field(type = FieldType.Nested)
|
||||
private List<GirlFriend> girlFriends;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<GirlFriend> getGirlFriends() {
|
||||
return girlFriends;
|
||||
}
|
||||
public List<GirlFriend> getGirlFriends() {
|
||||
return girlFriends;
|
||||
}
|
||||
|
||||
public void setGirlFriends(List<GirlFriend> girlFriends) {
|
||||
this.girlFriends = girlFriends;
|
||||
}
|
||||
public void setGirlFriends(List<GirlFriend> girlFriends) {
|
||||
this.girlFriends = girlFriends;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
|
||||
/**
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
@@ -174,5 +174,4 @@ public class Product {
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,73 +1,74 @@
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.Date;
|
||||
|
||||
/**
|
||||
* @author Jakub Vavrik
|
||||
*/
|
||||
@Document(indexName = "test-datemapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class SampleDateMappingEntity {
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
|
||||
private String message;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field(type = Date, format = DateFormat.custom, pattern = "dd.MM.yyyy hh:mm")
|
||||
private Date customFormatDate;
|
||||
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
|
||||
private String message;
|
||||
|
||||
@Field(type = Date)
|
||||
private Date defaultFormatDate;
|
||||
@Field(type = Date, format = DateFormat.custom, pattern = "dd.MM.yyyy hh:mm")
|
||||
private Date customFormatDate;
|
||||
|
||||
@Field(type = Date, format = DateFormat.basic_date)
|
||||
private Date basicFormatDate;
|
||||
@Field(type = Date)
|
||||
private Date defaultFormatDate;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
@Field(type = Date, format = DateFormat.basic_date)
|
||||
private Date basicFormatDate;
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public Date getCustomFormatDate() {
|
||||
return customFormatDate;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void setCustomFormatDate(Date customFormatDate) {
|
||||
this.customFormatDate = customFormatDate;
|
||||
}
|
||||
public Date getCustomFormatDate() {
|
||||
return customFormatDate;
|
||||
}
|
||||
|
||||
public Date getDefaultFormatDate() {
|
||||
return defaultFormatDate;
|
||||
}
|
||||
public void setCustomFormatDate(Date customFormatDate) {
|
||||
this.customFormatDate = customFormatDate;
|
||||
}
|
||||
|
||||
public void setDefaultFormatDate(Date defaultFormatDate) {
|
||||
this.defaultFormatDate = defaultFormatDate;
|
||||
}
|
||||
public Date getDefaultFormatDate() {
|
||||
return defaultFormatDate;
|
||||
}
|
||||
|
||||
public Date getBasicFormatDate() {
|
||||
return basicFormatDate;
|
||||
}
|
||||
public void setDefaultFormatDate(Date defaultFormatDate) {
|
||||
this.defaultFormatDate = defaultFormatDate;
|
||||
}
|
||||
|
||||
public void setBasicFormatDate(Date basicFormatDate) {
|
||||
this.basicFormatDate = basicFormatDate;
|
||||
}
|
||||
public Date getBasicFormatDate() {
|
||||
return basicFormatDate;
|
||||
}
|
||||
|
||||
public void setBasicFormatDate(Date basicFormatDate) {
|
||||
this.basicFormatDate = basicFormatDate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class SampleEntity {
|
||||
private String message;
|
||||
private int rate;
|
||||
private boolean available;
|
||||
private String highlightedMessage;
|
||||
private String highlightedMessage;
|
||||
@Version
|
||||
private Long version;
|
||||
|
||||
@@ -78,12 +78,12 @@ public class SampleEntity {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
public String getHighlightedMessage() {
|
||||
return highlightedMessage;
|
||||
}
|
||||
public String getHighlightedMessage() {
|
||||
return highlightedMessage;
|
||||
}
|
||||
|
||||
public void setHighlightedMessage(String highlightedMessage) {
|
||||
this.highlightedMessage = highlightedMessage;
|
||||
public void setHighlightedMessage(String highlightedMessage) {
|
||||
this.highlightedMessage = highlightedMessage;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
@@ -113,16 +113,16 @@ public class SampleEntity {
|
||||
.toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SampleEntity{" +
|
||||
"id='" + id + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", message='" + message + '\'' +
|
||||
", rate=" + rate +
|
||||
", available=" + available +
|
||||
", highlightedMessage='" + highlightedMessage + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SampleEntity{" +
|
||||
"id='" + id + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", message='" + message + '\'' +
|
||||
", rate=" + rate +
|
||||
", available=" + available +
|
||||
", highlightedMessage='" + highlightedMessage + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,51 +24,51 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
*/
|
||||
public class SampleEntityBuilder {
|
||||
|
||||
private SampleEntity result;
|
||||
private SampleEntity result;
|
||||
|
||||
public SampleEntityBuilder(String id) {
|
||||
result = new SampleEntity();
|
||||
result.setId(id);
|
||||
}
|
||||
public SampleEntityBuilder(String id) {
|
||||
result = new SampleEntity();
|
||||
result.setId(id);
|
||||
}
|
||||
|
||||
public SampleEntityBuilder type(String type) {
|
||||
result.setType(type);
|
||||
return this;
|
||||
}
|
||||
public SampleEntityBuilder type(String type) {
|
||||
result.setType(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleEntityBuilder message(String message) {
|
||||
result.setMessage(message);
|
||||
return this;
|
||||
}
|
||||
public SampleEntityBuilder message(String message) {
|
||||
result.setMessage(message);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleEntityBuilder rate(int rate) {
|
||||
result.setRate(rate);
|
||||
return this;
|
||||
}
|
||||
public SampleEntityBuilder rate(int rate) {
|
||||
result.setRate(rate);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleEntityBuilder available(boolean available) {
|
||||
result.setAvailable(available);
|
||||
return this;
|
||||
}
|
||||
public SampleEntityBuilder available(boolean available) {
|
||||
result.setAvailable(available);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleEntityBuilder highlightedMessage(String highlightedMessage) {
|
||||
result.setHighlightedMessage(highlightedMessage);
|
||||
return this;
|
||||
}
|
||||
public SampleEntityBuilder highlightedMessage(String highlightedMessage) {
|
||||
result.setHighlightedMessage(highlightedMessage);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleEntityBuilder version(Long version) {
|
||||
result.setVersion(version);
|
||||
return this;
|
||||
}
|
||||
public SampleEntityBuilder version(Long version) {
|
||||
result.setVersion(version);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SampleEntity build() {
|
||||
return result;
|
||||
}
|
||||
public SampleEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,14 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldIndex;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
@@ -31,41 +29,41 @@ import static org.springframework.data.elasticsearch.annotations.FieldType.*;
|
||||
@Document(indexName = "test-mapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class SampleMappingEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
|
||||
private String message;
|
||||
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
|
||||
private String message;
|
||||
|
||||
private NestedEntity nested;
|
||||
private NestedEntity nested;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
static class NestedEntity {
|
||||
@Field(type = String)
|
||||
private String someField;
|
||||
static class NestedEntity {
|
||||
|
||||
public String getSomeField() {
|
||||
return someField;
|
||||
}
|
||||
@Field(type = String)
|
||||
private String someField;
|
||||
|
||||
public void setSomeField(String someField) {
|
||||
this.someField = someField;
|
||||
}
|
||||
}
|
||||
public String getSomeField() {
|
||||
return someField;
|
||||
}
|
||||
|
||||
public void setSomeField(String someField) {
|
||||
this.someField = someField;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,62 +15,66 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Transient;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
|
||||
/**
|
||||
* @author Jakub Vavrik
|
||||
*/
|
||||
@Document(indexName = "test-recursive-mapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class SampleTransientEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
|
||||
private String message;
|
||||
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
|
||||
private String message;
|
||||
|
||||
@Transient
|
||||
private NestedEntity nested;
|
||||
@Transient
|
||||
private NestedEntity nested;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
static class NestedEntity {
|
||||
@Field
|
||||
private static NestedEntity someField = new NestedEntity();
|
||||
@Field
|
||||
private Boolean something;
|
||||
static class NestedEntity {
|
||||
|
||||
public NestedEntity getSomeField() {
|
||||
return someField;
|
||||
}
|
||||
@Field
|
||||
private static NestedEntity someField = new NestedEntity();
|
||||
@Field
|
||||
private Boolean something;
|
||||
|
||||
public void setSomeField(NestedEntity someField) {
|
||||
this.someField = someField;
|
||||
}
|
||||
public NestedEntity getSomeField() {
|
||||
return someField;
|
||||
}
|
||||
|
||||
public Boolean getSomething() { return something; }
|
||||
public void setSomeField(NestedEntity someField) {
|
||||
this.someField = someField;
|
||||
}
|
||||
|
||||
public void setSomething(Boolean something) { this.something = something; }
|
||||
}
|
||||
public Boolean getSomething() {
|
||||
return something;
|
||||
}
|
||||
|
||||
public void setSomething(Boolean something) {
|
||||
this.something = something;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
* @author Stuart Stevenson
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
@Document(indexName = "circular-objects", type = "circular-object" , indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
@Document(indexName = "circular-objects", type = "circular-object", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class SimpleRecursiveEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Field(type = FieldType.Object, ignoreFields = {"circularObject"})
|
||||
private SimpleRecursiveEntity circularObject;
|
||||
@Id
|
||||
private String id;
|
||||
@Field(type = FieldType.Object, ignoreFields = {"circularObject"})
|
||||
private SimpleRecursiveEntity circularObject;
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
@@ -29,43 +29,43 @@ import java.math.BigDecimal;
|
||||
@Document(indexName = "stock", type = "price", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class StockPrice {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String symbol;
|
||||
private String symbol;
|
||||
|
||||
@Field(type = FieldType.Double)
|
||||
private BigDecimal price;
|
||||
@Field(type = FieldType.Double)
|
||||
private BigDecimal price;
|
||||
|
||||
private StockPrice(){
|
||||
//don't delete
|
||||
}
|
||||
private StockPrice() {
|
||||
//don't delete
|
||||
}
|
||||
|
||||
public StockPrice(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public StockPrice(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public void setSymbol(String symbol) {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
public void setSymbol(String symbol) {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,45 +15,45 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public class StockPriceBuilder {
|
||||
|
||||
private StockPrice result;
|
||||
private StockPrice result;
|
||||
|
||||
public StockPriceBuilder(String id) {
|
||||
result = new StockPrice(id);
|
||||
}
|
||||
public StockPriceBuilder(String id) {
|
||||
result = new StockPrice(id);
|
||||
}
|
||||
|
||||
public StockPriceBuilder symbol(String symbol) {
|
||||
result.setSymbol(symbol);
|
||||
return this;
|
||||
}
|
||||
public StockPriceBuilder symbol(String symbol) {
|
||||
result.setSymbol(symbol);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StockPriceBuilder price(BigDecimal price) {
|
||||
result.setPrice(price);
|
||||
return this;
|
||||
}
|
||||
public StockPriceBuilder price(BigDecimal price) {
|
||||
result.setPrice(price);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StockPriceBuilder price(double price) {
|
||||
result.setPrice(new BigDecimal(price));
|
||||
return this;
|
||||
}
|
||||
public StockPriceBuilder price(double price) {
|
||||
result.setPrice(new BigDecimal(price));
|
||||
return this;
|
||||
}
|
||||
|
||||
public StockPrice build() {
|
||||
return result;
|
||||
}
|
||||
public StockPrice build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,21 +15,20 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.config;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.elasticsearch.repositories.SampleElasticsearchRepository;
|
||||
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
|
||||
import org.springframework.data.elasticsearch.repositories.SampleElasticsearchRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -60,5 +59,4 @@ public class ElasticsearchNamespaceHandlerTests {
|
||||
assertThat(context.getBean(SampleElasticsearchRepository.class),
|
||||
is(instanceOf(SampleElasticsearchRepository.class)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.config;
|
||||
|
||||
import static org.elasticsearch.node.NodeBuilder.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,11 +31,6 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -57,5 +56,4 @@ public class EnableElasticsearchRepositoriesTests {
|
||||
public void bootstrapsRepository() {
|
||||
assertThat(repository, is(notNullValue()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,19 +23,19 @@ import java.io.IOException;
|
||||
*/
|
||||
public class CustomEntityMapper implements EntityMapper {
|
||||
|
||||
public CustomEntityMapper(){
|
||||
//custom configuration/implementation (e.g. FasterXML/jackson)
|
||||
}
|
||||
public CustomEntityMapper() {
|
||||
//custom configuration/implementation (e.g. FasterXML/jackson)
|
||||
}
|
||||
|
||||
@Override
|
||||
public String mapToString(Object object) throws IOException {
|
||||
//mapping Object to String
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public String mapToString(Object object) throws IOException {
|
||||
//mapping Object to String
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
|
||||
//mapping String to Object
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
|
||||
//mapping String to Object
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,27 +23,27 @@ import org.springframework.data.domain.Pageable;
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public class CustomResultMapper implements ResultsMapper{
|
||||
public class CustomResultMapper implements ResultsMapper {
|
||||
|
||||
|
||||
private EntityMapper entityMapper;
|
||||
private EntityMapper entityMapper;
|
||||
|
||||
public CustomResultMapper(EntityMapper entityMapper) {
|
||||
this.entityMapper = entityMapper;
|
||||
}
|
||||
public CustomResultMapper(EntityMapper entityMapper) {
|
||||
this.entityMapper = entityMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityMapper getEntityMapper() {
|
||||
return entityMapper;
|
||||
}
|
||||
@Override
|
||||
public EntityMapper getEntityMapper() {
|
||||
return entityMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T mapResult(GetResponse response, Class<T> clazz) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
@Override
|
||||
public <T> T mapResult(GetResponse response, Class<T> clazz) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
@Override
|
||||
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,53 +15,52 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.elasticsearch.Car;
|
||||
import org.springframework.data.elasticsearch.CarBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public class DefaultEntityMapperTests {
|
||||
|
||||
public static final String JSON_STRING = "{\"name\":\"Grat\",\"model\":\"Ford\"}";
|
||||
public static final String CAR_MODEL = "Ford";
|
||||
public static final String CAR_NAME = "Grat";
|
||||
DefaultEntityMapper entityMapper;
|
||||
public static final String JSON_STRING = "{\"name\":\"Grat\",\"model\":\"Ford\"}";
|
||||
public static final String CAR_MODEL = "Ford";
|
||||
public static final String CAR_NAME = "Grat";
|
||||
DefaultEntityMapper entityMapper;
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
entityMapper = new DefaultEntityMapper();
|
||||
}
|
||||
@Before
|
||||
public void init() {
|
||||
entityMapper = new DefaultEntityMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapObjectToJsonString() throws IOException {
|
||||
//Given
|
||||
@Test
|
||||
public void shouldMapObjectToJsonString() throws IOException {
|
||||
//Given
|
||||
|
||||
//When
|
||||
String jsonResult = entityMapper.mapToString(new CarBuilder().model(CAR_MODEL).name(CAR_NAME).build());
|
||||
//When
|
||||
String jsonResult = entityMapper.mapToString(new CarBuilder().model(CAR_MODEL).name(CAR_NAME).build());
|
||||
|
||||
//Then
|
||||
assertThat(jsonResult, is(JSON_STRING));
|
||||
}
|
||||
//Then
|
||||
assertThat(jsonResult, is(JSON_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapJsonStringToObject() throws IOException {
|
||||
//Given
|
||||
@Test
|
||||
public void shouldMapJsonStringToObject() throws IOException {
|
||||
//Given
|
||||
|
||||
//When
|
||||
Car result = entityMapper.mapToObject(JSON_STRING,Car.class);
|
||||
|
||||
//Then
|
||||
assertThat(result.getName(),is(CAR_NAME));
|
||||
assertThat(result.getModel(),is(CAR_MODEL));
|
||||
}
|
||||
//When
|
||||
Car result = entityMapper.mapToObject(JSON_STRING, Car.class);
|
||||
|
||||
//Then
|
||||
assertThat(result.getName(), is(CAR_NAME));
|
||||
assertThat(result.getModel(), is(CAR_MODEL));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections.iterators.ArrayIterator;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
@@ -28,110 +36,99 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.data.elasticsearch.Car;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public class DefaultResultMapperTests {
|
||||
|
||||
private DefaultResultMapper resultMapper;
|
||||
private DefaultResultMapper resultMapper;
|
||||
|
||||
@Mock
|
||||
private SearchResponse response;
|
||||
@Mock
|
||||
private SearchResponse response;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
resultMapper = new DefaultResultMapper();
|
||||
}
|
||||
@Before
|
||||
public void init() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
resultMapper = new DefaultResultMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapSearchRequestToPage() {
|
||||
//Given
|
||||
SearchHit[] hits = {createCarHit("Ford", "Grat"), createCarHit("BMW", "Arrow")};
|
||||
SearchHits searchHits = mock(SearchHits.class);
|
||||
when(searchHits.totalHits()).thenReturn(2L);
|
||||
when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
|
||||
when(response.getHits()).thenReturn(searchHits);
|
||||
@Test
|
||||
public void shouldMapSearchRequestToPage() {
|
||||
//Given
|
||||
SearchHit[] hits = {createCarHit("Ford", "Grat"), createCarHit("BMW", "Arrow")};
|
||||
SearchHits searchHits = mock(SearchHits.class);
|
||||
when(searchHits.totalHits()).thenReturn(2L);
|
||||
when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
|
||||
when(response.getHits()).thenReturn(searchHits);
|
||||
|
||||
//When
|
||||
FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
|
||||
//When
|
||||
FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
|
||||
|
||||
//Then
|
||||
assertThat(page.hasContent(), is(true));
|
||||
assertThat(page.getTotalElements(), is(2L));
|
||||
assertThat(page.getContent().get(0).getName(), is("Ford"));
|
||||
}
|
||||
//Then
|
||||
assertThat(page.hasContent(), is(true));
|
||||
assertThat(page.getTotalElements(), is(2L));
|
||||
assertThat(page.getContent().get(0).getName(), is("Ford"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapPartialSearchRequestToObject() {
|
||||
//Given
|
||||
SearchHit[] hits = {createCarPartialHit("Ford", "Grat"), createCarPartialHit("BMW", "Arrow")};
|
||||
SearchHits searchHits = mock(SearchHits.class);
|
||||
when(searchHits.totalHits()).thenReturn(2L);
|
||||
when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
|
||||
when(response.getHits()).thenReturn(searchHits);
|
||||
@Test
|
||||
public void shouldMapPartialSearchRequestToObject() {
|
||||
//Given
|
||||
SearchHit[] hits = {createCarPartialHit("Ford", "Grat"), createCarPartialHit("BMW", "Arrow")};
|
||||
SearchHits searchHits = mock(SearchHits.class);
|
||||
when(searchHits.totalHits()).thenReturn(2L);
|
||||
when(searchHits.iterator()).thenReturn(new ArrayIterator(hits));
|
||||
when(response.getHits()).thenReturn(searchHits);
|
||||
|
||||
//When
|
||||
FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
|
||||
//When
|
||||
FacetedPage<Car> page = resultMapper.mapResults(response, Car.class, null);
|
||||
|
||||
//Then
|
||||
assertThat(page.hasContent(), is(true));
|
||||
assertThat(page.getTotalElements(), is(2L));
|
||||
assertThat(page.getContent().get(0).getName(), is("Ford"));
|
||||
}
|
||||
//Then
|
||||
assertThat(page.hasContent(), is(true));
|
||||
assertThat(page.getTotalElements(), is(2L));
|
||||
assertThat(page.getContent().get(0).getName(), is("Ford"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapGetRequestToObject() {
|
||||
//Given
|
||||
GetResponse response = mock(GetResponse.class);
|
||||
when(response.getSourceAsString()).thenReturn(createJsonCar("Ford", "Grat"));
|
||||
@Test
|
||||
public void shouldMapGetRequestToObject() {
|
||||
//Given
|
||||
GetResponse response = mock(GetResponse.class);
|
||||
when(response.getSourceAsString()).thenReturn(createJsonCar("Ford", "Grat"));
|
||||
|
||||
//When
|
||||
Car result = resultMapper.mapResult(response, Car.class);
|
||||
//When
|
||||
Car result = resultMapper.mapResult(response, Car.class);
|
||||
|
||||
//Then
|
||||
assertThat(result, notNullValue());
|
||||
assertThat(result.getModel(), is("Grat"));
|
||||
assertThat(result.getName(), is("Ford"));
|
||||
}
|
||||
//Then
|
||||
assertThat(result, notNullValue());
|
||||
assertThat(result.getModel(), is("Grat"));
|
||||
assertThat(result.getName(), is("Ford"));
|
||||
}
|
||||
|
||||
private SearchHit createCarHit(String name, String model) {
|
||||
SearchHit hit = mock(SearchHit.class);
|
||||
when(hit.sourceAsString()).thenReturn(createJsonCar(name, model));
|
||||
return hit;
|
||||
}
|
||||
private SearchHit createCarHit(String name, String model) {
|
||||
SearchHit hit = mock(SearchHit.class);
|
||||
when(hit.sourceAsString()).thenReturn(createJsonCar(name, model));
|
||||
return hit;
|
||||
}
|
||||
|
||||
private SearchHit createCarPartialHit(String name, String model) {
|
||||
SearchHit hit = mock(SearchHit.class);
|
||||
when(hit.sourceAsString()).thenReturn(null);
|
||||
when(hit.getFields()).thenReturn(createCarFields(name, model));
|
||||
return hit;
|
||||
}
|
||||
private SearchHit createCarPartialHit(String name, String model) {
|
||||
SearchHit hit = mock(SearchHit.class);
|
||||
when(hit.sourceAsString()).thenReturn(null);
|
||||
when(hit.getFields()).thenReturn(createCarFields(name, model));
|
||||
return hit;
|
||||
}
|
||||
|
||||
private String createJsonCar(String name, String model) {
|
||||
final String q = "\"";
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("{").append(q).append("name").append(q).append(":").append(q).append(name).append(q).append(",");
|
||||
sb.append(q).append("model").append(q).append(":").append(q).append(model).append(q).append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Map<String, SearchHitField> createCarFields(String name, String model) {
|
||||
Map<String, SearchHitField> result = new HashMap<String, SearchHitField>();
|
||||
result.put("name", new InternalSearchHitField("name", Arrays.<Object>asList(name)));
|
||||
result.put("model", new InternalSearchHitField("model", Arrays.<Object>asList(model)));
|
||||
return result;
|
||||
}
|
||||
private String createJsonCar(String name, String model) {
|
||||
final String q = "\"";
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("{").append(q).append("name").append(q).append(":").append(q).append(name).append(q).append(",");
|
||||
sb.append(q).append("model").append(q).append(":").append(q).append(model).append(q).append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Map<String, SearchHitField> createCarFields(String name, String model) {
|
||||
Map<String, SearchHitField> result = new HashMap<String, SearchHitField>();
|
||||
result.put("name", new InternalSearchHitField("name", Arrays.<Object>asList(name)));
|
||||
result.put("model", new InternalSearchHitField("model", Arrays.<Object>asList(model)));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
@@ -31,22 +31,21 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-custom-mapper.xml")
|
||||
public class ElasticsearchTemplateCustomMapperTests {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Autowired
|
||||
private EntityMapper entityMapper;
|
||||
@Autowired
|
||||
private EntityMapper entityMapper;
|
||||
|
||||
@Autowired
|
||||
private ResultsMapper resultsMapper;
|
||||
|
||||
@Test
|
||||
public void shouldUseCustomMapper() {
|
||||
//given
|
||||
//when
|
||||
//them
|
||||
assertThat(elasticsearchTemplate.getResultsMapper(), is(resultsMapper));
|
||||
assertThat(elasticsearchTemplate.getResultsMapper().getEntityMapper(), is(entityMapper));
|
||||
}
|
||||
@Autowired
|
||||
private ResultsMapper resultsMapper;
|
||||
|
||||
@Test
|
||||
public void shouldUseCustomMapper() {
|
||||
//given
|
||||
//when
|
||||
//them
|
||||
assertThat(elasticsearchTemplate.getResultsMapper(), is(resultsMapper));
|
||||
assertThat(elasticsearchTemplate.getResultsMapper().getEntityMapper(), is(entityMapper));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,16 +15,17 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.hasChildQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.topChildrenQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.junit.*;
|
||||
import org.junit.After;
|
||||
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.ParentEntity;
|
||||
@@ -81,27 +82,27 @@ public class ElasticsearchTemplateParentChildTests {
|
||||
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSearchTopChildrenForGivenParent(){
|
||||
// index two parents
|
||||
ParentEntity parent1 = index("parent1", "First Parent");
|
||||
ParentEntity parent2 = index("parent2", "Second Parent");
|
||||
@Test
|
||||
public void shouldSearchTopChildrenForGivenParent() {
|
||||
// index two parents
|
||||
ParentEntity parent1 = index("parent1", "First Parent");
|
||||
ParentEntity parent2 = index("parent2", "Second Parent");
|
||||
|
||||
// index a child for each parent
|
||||
String child1name = "First";
|
||||
index("child1", parent1.getId(), child1name);
|
||||
index("child2", parent2.getId(), "Second");
|
||||
// index a child for each parent
|
||||
String child1name = "First";
|
||||
index("child1", parent1.getId(), child1name);
|
||||
index("child2", parent2.getId(), "Second");
|
||||
|
||||
elasticsearchTemplate.refresh(ParentEntity.class, true);
|
||||
elasticsearchTemplate.refresh(ChildEntity.class, true);
|
||||
elasticsearchTemplate.refresh(ParentEntity.class, true);
|
||||
elasticsearchTemplate.refresh(ChildEntity.class, true);
|
||||
|
||||
// find all parents that have the first child using topChildren Query
|
||||
QueryBuilder query = topChildrenQuery(ParentEntity.CHILD_TYPE, QueryBuilders.fieldQuery("name", child1name));
|
||||
List<ParentEntity> parents = elasticsearchTemplate.queryForList(new NativeSearchQuery(query), ParentEntity.class);
|
||||
// find all parents that have the first child using topChildren Query
|
||||
QueryBuilder query = topChildrenQuery(ParentEntity.CHILD_TYPE, QueryBuilders.fieldQuery("name", child1name));
|
||||
List<ParentEntity> parents = elasticsearchTemplate.queryForList(new NativeSearchQuery(query), ParentEntity.class);
|
||||
|
||||
// we're expecting only the first parent as result
|
||||
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
||||
}
|
||||
// we're expecting only the first parent as result
|
||||
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
||||
}
|
||||
|
||||
private ParentEntity index(String parentId, String name) {
|
||||
ParentEntity parent = new ParentEntity(parentId, name);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,14 @@
|
||||
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -26,14 +34,6 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Stuart Stevenson
|
||||
* @author Jakub Vavrik
|
||||
@@ -43,64 +43,64 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class MappingBuilderTests {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Test
|
||||
public void shouldNotFailOnCircularReference() {
|
||||
elasticsearchTemplate.createIndex(SimpleRecursiveEntity.class);
|
||||
elasticsearchTemplate.putMapping(SimpleRecursiveEntity.class);
|
||||
}
|
||||
@Test
|
||||
public void shouldNotFailOnCircularReference() {
|
||||
elasticsearchTemplate.createIndex(SimpleRecursiveEntity.class);
|
||||
elasticsearchTemplate.putMapping(SimpleRecursiveEntity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInfiniteLoopAvoidance() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
|
||||
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
|
||||
"\"index_analyzer\":\"standard\"}}}}";
|
||||
@Test
|
||||
public void testInfiniteLoopAvoidance() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
|
||||
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
|
||||
"\"index_analyzer\":\"standard\"}}}}";
|
||||
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null);
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
}
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null);
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseValueFromAnnotationType() throws IOException {
|
||||
//Given
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}";
|
||||
@Test
|
||||
public void shouldUseValueFromAnnotationType() throws IOException {
|
||||
//Given
|
||||
final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}";
|
||||
|
||||
//When
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id", null);
|
||||
//When
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id", null);
|
||||
|
||||
//Then
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
}
|
||||
//Then
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAddStockPriceDocumentToIndex() throws IOException {
|
||||
//Given
|
||||
@Test
|
||||
public void shouldAddStockPriceDocumentToIndex() throws IOException {
|
||||
//Given
|
||||
|
||||
//When
|
||||
elasticsearchTemplate.deleteIndex(StockPrice.class);
|
||||
elasticsearchTemplate.createIndex(StockPrice.class);
|
||||
elasticsearchTemplate.putMapping(StockPrice.class);
|
||||
String symbol = "AU";
|
||||
double price = 2.34;
|
||||
String id = "abc";
|
||||
elasticsearchTemplate.index(new StockPriceBuilder(id).symbol(symbol).price(price).buildIndex());
|
||||
elasticsearchTemplate.refresh(StockPrice.class, true);
|
||||
//When
|
||||
elasticsearchTemplate.deleteIndex(StockPrice.class);
|
||||
elasticsearchTemplate.createIndex(StockPrice.class);
|
||||
elasticsearchTemplate.putMapping(StockPrice.class);
|
||||
String symbol = "AU";
|
||||
double price = 2.34;
|
||||
String id = "abc";
|
||||
elasticsearchTemplate.index(new StockPriceBuilder(id).symbol(symbol).price(price).buildIndex());
|
||||
elasticsearchTemplate.refresh(StockPrice.class, true);
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
|
||||
List<StockPrice> result = elasticsearchTemplate.queryForList(searchQuery, StockPrice.class);
|
||||
//Then
|
||||
assertThat(result.size(), is(1));
|
||||
StockPrice entry = result.get(0);
|
||||
assertThat(entry.getSymbol(), is(symbol));
|
||||
assertThat(entry.getPrice(), is(new BigDecimal(price)));
|
||||
}
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
|
||||
List<StockPrice> result = elasticsearchTemplate.queryForList(searchQuery, StockPrice.class);
|
||||
//Then
|
||||
assertThat(result.size(), is(1));
|
||||
StockPrice entry = result.get(0);
|
||||
assertThat(entry.getSymbol(), is(symbol));
|
||||
assertThat(entry.getPrice(), is(new BigDecimal(price)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateMappingForSpecifiedParentType() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"_parent\":{\"type\":\"parentType\"},\"properties\":{}}}";
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(MinimalEntity.class, "mapping", "id", "parentType");
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
}
|
||||
@Test
|
||||
public void shouldCreateMappingForSpecifiedParentType() throws IOException {
|
||||
final String expected = "{\"mapping\":{\"_parent\":{\"type\":\"parentType\"},\"properties\":{}}}";
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(MinimalEntity.class, "mapping", "id", "parentType");
|
||||
assertThat(xContentBuilder.string(), is(expected));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,28 +15,29 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.elasticsearch.SampleDateMappingEntity;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Jakub Vavrik
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public class SimpleElasticsearchDateMappingTest {
|
||||
private static final String EXPECTED_MAPPING = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true," +
|
||||
"\"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\",\"index_analyzer\"" +
|
||||
":\"standard\"},\"customFormatDate\":{\"store\":false,\"type\":\"date\",\"format\":\"dd.MM.yyyy hh:mm\"}," +
|
||||
"\"defaultFormatDate\":{\"store\":false,\"type\":\"date\"},\"basicFormatDate\":{\"store\":false,\"" +
|
||||
"type\":\"date\",\"format\":\"basic_date\"}}}}";
|
||||
|
||||
@Test
|
||||
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id", null);
|
||||
Assert.assertEquals(EXPECTED_MAPPING, xContentBuilder.string());
|
||||
}
|
||||
private static final String EXPECTED_MAPPING = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true," +
|
||||
"\"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\",\"index_analyzer\"" +
|
||||
":\"standard\"},\"customFormatDate\":{\"store\":false,\"type\":\"date\",\"format\":\"dd.MM.yyyy hh:mm\"}," +
|
||||
"\"defaultFormatDate\":{\"store\":false,\"type\":\"date\"},\"basicFormatDate\":{\"store\":false,\"" +
|
||||
"type\":\"date\",\"format\":\"basic_date\"}}}}";
|
||||
|
||||
@Test
|
||||
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
|
||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id", null);
|
||||
Assert.assertEquals(EXPECTED_MAPPING, xContentBuilder.string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,20 +15,21 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.convert;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public class DateTimeConvertersTests {
|
||||
|
||||
@Test
|
||||
public void testJodaDateTimeConverterWithNullValue() {
|
||||
Assert.assertNull(DateTimeConverters.JodaDateTimeConverter.INSTANCE.convert(null));
|
||||
|
||||
@@ -15,14 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.convert;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -54,5 +54,4 @@ public class MappingElasticsearchConverterTests {
|
||||
// then
|
||||
assertThat(conversionService, is(notNullValue()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package org.springframework.data.elasticsearch.core.facet;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.*;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.Integer;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.analyzed;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.Integer;
|
||||
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.MultiField;
|
||||
import org.springframework.data.elasticsearch.annotations.NestedField;
|
||||
|
||||
/**
|
||||
* Simple type to test facets
|
||||
@@ -17,70 +19,70 @@ import static org.springframework.data.elasticsearch.annotations.FieldType.Strin
|
||||
@Document(indexName = "articles", type = "article", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
|
||||
public class ArticleEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String title;
|
||||
private String title;
|
||||
|
||||
@MultiField(
|
||||
mainField = @Field(type = String, index = analyzed),
|
||||
otherFields = {
|
||||
@NestedField(dotSuffix = "untouched", type = String, store = true, index = not_analyzed),
|
||||
@NestedField(dotSuffix = "sort", type = String, store = true, indexAnalyzer = "keyword")
|
||||
}
|
||||
)
|
||||
private List<String> authors = new ArrayList<String>();
|
||||
@MultiField(
|
||||
mainField = @Field(type = String, index = analyzed),
|
||||
otherFields = {
|
||||
@NestedField(dotSuffix = "untouched", type = String, store = true, index = not_analyzed),
|
||||
@NestedField(dotSuffix = "sort", type = String, store = true, indexAnalyzer = "keyword")
|
||||
}
|
||||
)
|
||||
private List<String> authors = new ArrayList<String>();
|
||||
|
||||
@Field(type = Integer, store = true)
|
||||
private List<Integer> publishedYears = new ArrayList<Integer>();
|
||||
@Field(type = Integer, store = true)
|
||||
private List<Integer> publishedYears = new ArrayList<Integer>();
|
||||
|
||||
private int score;
|
||||
private int score;
|
||||
|
||||
private ArticleEntity(){
|
||||
private ArticleEntity() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public ArticleEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public ArticleEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<String> getAuthors() {
|
||||
return authors;
|
||||
}
|
||||
public List<String> getAuthors() {
|
||||
return authors;
|
||||
}
|
||||
|
||||
public void setAuthors(List<String> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
public void setAuthors(List<String> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public List<Integer> getPublishedYears() {
|
||||
return publishedYears;
|
||||
}
|
||||
public List<Integer> getPublishedYears() {
|
||||
return publishedYears;
|
||||
}
|
||||
|
||||
public void setPublishedYears(List<Integer> publishedYears) {
|
||||
this.publishedYears = publishedYears;
|
||||
}
|
||||
public void setPublishedYears(List<Integer> publishedYears) {
|
||||
this.publishedYears = publishedYears;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,41 +7,40 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
*/
|
||||
public class ArticleEntityBuilder {
|
||||
|
||||
private ArticleEntity result;
|
||||
private ArticleEntity result;
|
||||
|
||||
public ArticleEntityBuilder(String id) {
|
||||
result = new ArticleEntity(id);
|
||||
}
|
||||
public ArticleEntityBuilder(String id) {
|
||||
result = new ArticleEntity(id);
|
||||
}
|
||||
|
||||
public ArticleEntityBuilder title(String title) {
|
||||
result.setTitle(title);
|
||||
return this;
|
||||
}
|
||||
public ArticleEntityBuilder title(String title) {
|
||||
result.setTitle(title);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ArticleEntityBuilder addAuthor(String author) {
|
||||
result.getAuthors().add(author);
|
||||
return this;
|
||||
}
|
||||
public ArticleEntityBuilder addAuthor(String author) {
|
||||
result.getAuthors().add(author);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ArticleEntityBuilder addPublishedYear(Integer year) {
|
||||
result.getPublishedYears().add(year);
|
||||
return this;
|
||||
}
|
||||
public ArticleEntityBuilder addPublishedYear(Integer year) {
|
||||
result.getPublishedYears().add(year);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ArticleEntityBuilder score(int score) {
|
||||
result.setScore(score);
|
||||
return this;
|
||||
}
|
||||
public ArticleEntityBuilder score(int score) {
|
||||
result.setScore(score);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ArticleEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public ArticleEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.facet;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.elasticsearch.index.query.FilterBuilders;
|
||||
import org.elasticsearch.search.facet.FacetBuilders;
|
||||
import org.junit.Before;
|
||||
@@ -31,10 +35,6 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -45,487 +45,477 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class ElasticsearchTemplateFacetTests {
|
||||
|
||||
public static final String RIZWAN_IDREES = "Rizwan Idrees";
|
||||
public static final String MOHSIN_HUSEN = "Mohsin Husen";
|
||||
public static final String JONATHAN_YAN = "Jonathan Yan";
|
||||
public static final String ARTUR_KONCZAK = "Artur Konczak";
|
||||
public static final int YEAR_2002 = 2002;
|
||||
public static final int YEAR_2001 = 2001;
|
||||
public static final int YEAR_2000 = 2000;
|
||||
public static final String PUBLISHED_YEARS = "publishedYears";
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(ArticleEntity.class);
|
||||
elasticsearchTemplate.createIndex(ArticleEntity.class);
|
||||
elasticsearchTemplate.putMapping(ArticleEntity.class);
|
||||
elasticsearchTemplate.refresh(ArticleEntity.class, true);
|
||||
|
||||
IndexQuery article1 = new ArticleEntityBuilder("1").title("article four").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addAuthor(JONATHAN_YAN).score(10).buildIndex();
|
||||
IndexQuery article2 = new ArticleEntityBuilder("2").title("article three").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addPublishedYear(YEAR_2000).score(20).buildIndex();
|
||||
IndexQuery article3 = new ArticleEntityBuilder("3").title("article two").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(30).buildIndex();
|
||||
IndexQuery article4 = new ArticleEntityBuilder("4").title("article one").addAuthor(RIZWAN_IDREES).addPublishedYear(YEAR_2002).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(40).buildIndex();
|
||||
|
||||
elasticsearchTemplate.index(article1);
|
||||
elasticsearchTemplate.index(article2);
|
||||
elasticsearchTemplate.index(article3);
|
||||
elasticsearchTemplate.index(article4);
|
||||
elasticsearchTemplate.refresh(ArticleEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenQueryWithDefaultOrder() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withFacet(new TermFacetRequestBuilder(facetName).fields("authors.untouched").build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenFilteredQuery() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExcludeTermsFromFacetedAuthorsForGivenQuery() {
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").excludeTerms(RIZWAN_IDREES, ARTUR_KONCZAK).build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
|
||||
assertThat(facet.getTerms().size(), is(1));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenQueryOrderedByTerm() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("authors.untouched").ascTerm().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenQueryOrderedByCountAsc() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("authors.untouched").ascCount().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedYearsForGivenQuery() {
|
||||
|
||||
// given
|
||||
String facetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("publishedYears").descCount().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
assertThat(facet.getTerms().size(), is(equalTo(3)));
|
||||
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnSingleFacetOverYearsAndAuthorsForGivenQuery() {
|
||||
|
||||
// given
|
||||
String facetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("publishedYears", "authors.untouched").ascTerm().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
assertThat(facet.getTerms().size(), is(equalTo(7)));
|
||||
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(4);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(5);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(6);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedYearsAndFacetedAuthorsForGivenQuery() {
|
||||
|
||||
// given
|
||||
String numberFacetName = "fAuthors";
|
||||
String stringFacetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(numberFacetName).fields("publishedYears").ascTerm().build())
|
||||
.withFacet(new TermFacetRequestBuilder(stringFacetName).fields("authors.untouched").ascTerm().build())
|
||||
.build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult numberFacet = (TermResult) result.getFacet(numberFacetName);
|
||||
assertThat(numberFacet.getTerms().size(), is(equalTo(3)));
|
||||
|
||||
Term numberTerm = numberFacet.getTerms().get(0);
|
||||
assertThat(numberTerm.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(numberTerm.getCount(), is(3));
|
||||
|
||||
numberTerm = numberFacet.getTerms().get(1);
|
||||
assertThat(numberTerm.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(numberTerm.getCount(), is(2));
|
||||
|
||||
numberTerm = numberFacet.getTerms().get(2);
|
||||
assertThat(numberTerm.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(numberTerm.getCount(), is(1));
|
||||
|
||||
|
||||
TermResult stringFacet = (TermResult) result.getFacet(stringFacetName);
|
||||
Term stringTerm = stringFacet.getTerms().get(0);
|
||||
assertThat(stringTerm.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(stringTerm.getCount(), is(3));
|
||||
|
||||
stringTerm = stringFacet.getTerms().get(1);
|
||||
assertThat(stringTerm.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(stringTerm.getCount(), is(1));
|
||||
|
||||
stringTerm = stringFacet.getTerms().get(2);
|
||||
assertThat(stringTerm.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(stringTerm.getCount(), is(2));
|
||||
|
||||
stringTerm = stringFacet.getTerms().get(3);
|
||||
assertThat(stringTerm.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(stringTerm.getCount(), is(4));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedYearsForNativeFacet() {
|
||||
|
||||
// given
|
||||
String facetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new NativeFacetRequest(FacetBuilders.termsFacet(facetName).field("publishedYears"))).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
assertThat(facet.getTerms().size(), is(equalTo(3)));
|
||||
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFilterResultByRegexForGivenQuery() {
|
||||
// given
|
||||
String facetName = "regex_authors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").regex("Art.*").build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
|
||||
assertThat(facet.getTerms().size(), is(1));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAllTermsForGivenQuery() {
|
||||
// given
|
||||
String facetName = "all_authors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").allTerms().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
|
||||
assertThat(facet.getTerms().size(), is(4));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnRangeFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "rangeYears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(
|
||||
new RangeFacetRequestBuilder(facetName).field(PUBLISHED_YEARS)
|
||||
.to(YEAR_2000).range(YEAR_2000, YEAR_2002).from(YEAR_2002).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
RangeResult facet = (RangeResult) result.getFacet(facetName);
|
||||
assertThat(facet.getRanges().size(), is(equalTo(3)));
|
||||
|
||||
|
||||
Range range = facet.getRanges().get(0);
|
||||
assertThat(range.getFrom(), nullValue());
|
||||
assertThat(range.getTo(), is((double) YEAR_2000));
|
||||
assertThat(range.getCount(), is(0L));
|
||||
assertThat(range.getTotal(), is(0.0));
|
||||
|
||||
range = facet.getRanges().get(1);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2000));
|
||||
assertThat(range.getTo(), is((double) YEAR_2002));
|
||||
assertThat(range.getCount(), is(3L));
|
||||
assertThat(range.getTotal(), is(6000.0));
|
||||
|
||||
range = facet.getRanges().get(2);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2002));
|
||||
assertThat(range.getTo(), nullValue());
|
||||
assertThat(range.getCount(), is(1L));
|
||||
assertThat(range.getTotal(), is(2002.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnKeyValueRangeFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "rangeScoreOverYears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(
|
||||
new RangeFacetRequestBuilder(facetName).fields(PUBLISHED_YEARS, "score")
|
||||
.to(YEAR_2000).range(YEAR_2000, YEAR_2002).from(YEAR_2002).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
RangeResult facet = (RangeResult) result.getFacet(facetName);
|
||||
assertThat(facet.getRanges().size(), is(equalTo(3)));
|
||||
|
||||
|
||||
Range range = facet.getRanges().get(0);
|
||||
assertThat(range.getFrom(), nullValue());
|
||||
assertThat(range.getTo(), is((double) YEAR_2000));
|
||||
assertThat(range.getCount(), is(0L));
|
||||
assertThat(range.getTotal(), is(0.0));
|
||||
|
||||
range = facet.getRanges().get(1);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2000));
|
||||
assertThat(range.getTo(), is((double) YEAR_2002));
|
||||
assertThat(range.getCount(), is(3L));
|
||||
assertThat(range.getTotal(), is(90.0));
|
||||
|
||||
range = facet.getRanges().get(2);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2002));
|
||||
assertThat(range.getTo(), nullValue());
|
||||
assertThat(range.getCount(), is(1L));
|
||||
assertThat(range.getTotal(), is(40.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatisticalFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "statPublishedYear";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new StatisticalFacetRequestBuilder(facetName).field(PUBLISHED_YEARS).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
StatisticalResult facet = (StatisticalResult) result.getFacet(facetName);
|
||||
assertThat(facet.getCount(), is(equalTo(6L)));
|
||||
assertThat(facet.getMax(), is(equalTo(2002.0)));
|
||||
assertThat(facet.getMin(), is(equalTo(2000.0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnHistogramFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "numberPublicationPerYear";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new HistogramFacetRequestBuilder(facetName).field(PUBLISHED_YEARS).interval(1).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
HistogramResult facet = (HistogramResult) result.getFacet(facetName);
|
||||
assertThat(facet.getIntervalUnit().size(), is(equalTo(3)));
|
||||
|
||||
IntervalUnit unit = facet.getIntervalUnit().get(0);
|
||||
assertThat(unit.getKey(), is(Long.valueOf(YEAR_2000)));
|
||||
assertThat(unit.getCount(), is(3L));
|
||||
|
||||
unit = facet.getIntervalUnit().get(1);
|
||||
assertThat(unit.getKey(), is(Long.valueOf(YEAR_2001)));
|
||||
assertThat(unit.getCount(), is(2L));
|
||||
|
||||
unit = facet.getIntervalUnit().get(2);
|
||||
assertThat(unit.getKey(), is(Long.valueOf(YEAR_2002)));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
}
|
||||
public static final String RIZWAN_IDREES = "Rizwan Idrees";
|
||||
public static final String MOHSIN_HUSEN = "Mohsin Husen";
|
||||
public static final String JONATHAN_YAN = "Jonathan Yan";
|
||||
public static final String ARTUR_KONCZAK = "Artur Konczak";
|
||||
public static final int YEAR_2002 = 2002;
|
||||
public static final int YEAR_2001 = 2001;
|
||||
public static final int YEAR_2000 = 2000;
|
||||
public static final String PUBLISHED_YEARS = "publishedYears";
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(ArticleEntity.class);
|
||||
elasticsearchTemplate.createIndex(ArticleEntity.class);
|
||||
elasticsearchTemplate.putMapping(ArticleEntity.class);
|
||||
elasticsearchTemplate.refresh(ArticleEntity.class, true);
|
||||
|
||||
IndexQuery article1 = new ArticleEntityBuilder("1").title("article four").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addAuthor(JONATHAN_YAN).score(10).buildIndex();
|
||||
IndexQuery article2 = new ArticleEntityBuilder("2").title("article three").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addAuthor(MOHSIN_HUSEN).addPublishedYear(YEAR_2000).score(20).buildIndex();
|
||||
IndexQuery article3 = new ArticleEntityBuilder("3").title("article two").addAuthor(RIZWAN_IDREES).addAuthor(ARTUR_KONCZAK).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(30).buildIndex();
|
||||
IndexQuery article4 = new ArticleEntityBuilder("4").title("article one").addAuthor(RIZWAN_IDREES).addPublishedYear(YEAR_2002).addPublishedYear(YEAR_2001).addPublishedYear(YEAR_2000).score(40).buildIndex();
|
||||
|
||||
elasticsearchTemplate.index(article1);
|
||||
elasticsearchTemplate.index(article2);
|
||||
elasticsearchTemplate.index(article3);
|
||||
elasticsearchTemplate.index(article4);
|
||||
elasticsearchTemplate.refresh(ArticleEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenQueryWithDefaultOrder() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withFacet(new TermFacetRequestBuilder(facetName).fields("authors.untouched").build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenFilteredQuery() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExcludeTermsFromFacetedAuthorsForGivenQuery() {
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").excludeTerms(RIZWAN_IDREES, ARTUR_KONCZAK).build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
|
||||
assertThat(facet.getTerms().size(), is(1));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenQueryOrderedByTerm() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("authors.untouched").ascTerm().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedAuthorsForGivenQueryOrderedByCountAsc() {
|
||||
|
||||
// given
|
||||
String facetName = "fauthors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("authors.untouched").ascCount().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedYearsForGivenQuery() {
|
||||
|
||||
// given
|
||||
String facetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("publishedYears").descCount().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
assertThat(facet.getTerms().size(), is(equalTo(3)));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnSingleFacetOverYearsAndAuthorsForGivenQuery() {
|
||||
|
||||
// given
|
||||
String facetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).fields("publishedYears", "authors.untouched").ascTerm().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
assertThat(facet.getTerms().size(), is(equalTo(7)));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(3);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(4);
|
||||
assertThat(term.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(term.getCount(), is(1));
|
||||
|
||||
term = facet.getTerms().get(5);
|
||||
assertThat(term.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(6);
|
||||
assertThat(term.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(term.getCount(), is(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedYearsAndFacetedAuthorsForGivenQuery() {
|
||||
|
||||
// given
|
||||
String numberFacetName = "fAuthors";
|
||||
String stringFacetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new TermFacetRequestBuilder(numberFacetName).fields("publishedYears").ascTerm().build())
|
||||
.withFacet(new TermFacetRequestBuilder(stringFacetName).fields("authors.untouched").ascTerm().build())
|
||||
.build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult numberFacet = (TermResult) result.getFacet(numberFacetName);
|
||||
assertThat(numberFacet.getTerms().size(), is(equalTo(3)));
|
||||
|
||||
Term numberTerm = numberFacet.getTerms().get(0);
|
||||
assertThat(numberTerm.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(numberTerm.getCount(), is(3));
|
||||
|
||||
numberTerm = numberFacet.getTerms().get(1);
|
||||
assertThat(numberTerm.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(numberTerm.getCount(), is(2));
|
||||
|
||||
numberTerm = numberFacet.getTerms().get(2);
|
||||
assertThat(numberTerm.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(numberTerm.getCount(), is(1));
|
||||
|
||||
TermResult stringFacet = (TermResult) result.getFacet(stringFacetName);
|
||||
Term stringTerm = stringFacet.getTerms().get(0);
|
||||
assertThat(stringTerm.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(stringTerm.getCount(), is(3));
|
||||
|
||||
stringTerm = stringFacet.getTerms().get(1);
|
||||
assertThat(stringTerm.getTerm(), is(JONATHAN_YAN));
|
||||
assertThat(stringTerm.getCount(), is(1));
|
||||
|
||||
stringTerm = stringFacet.getTerms().get(2);
|
||||
assertThat(stringTerm.getTerm(), is(MOHSIN_HUSEN));
|
||||
assertThat(stringTerm.getCount(), is(2));
|
||||
|
||||
stringTerm = stringFacet.getTerms().get(3);
|
||||
assertThat(stringTerm.getTerm(), is(RIZWAN_IDREES));
|
||||
assertThat(stringTerm.getCount(), is(4));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnFacetedYearsForNativeFacet() {
|
||||
|
||||
// given
|
||||
String facetName = "fyears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new NativeFacetRequest(FacetBuilders.termsFacet(facetName).field("publishedYears"))).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
assertThat(facet.getTerms().size(), is(equalTo(3)));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2000)));
|
||||
assertThat(term.getCount(), is(3));
|
||||
|
||||
term = facet.getTerms().get(1);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2001)));
|
||||
assertThat(term.getCount(), is(2));
|
||||
|
||||
term = facet.getTerms().get(2);
|
||||
assertThat(term.getTerm(), is(Integer.toString(YEAR_2002)));
|
||||
assertThat(term.getCount(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFilterResultByRegexForGivenQuery() {
|
||||
// given
|
||||
String facetName = "regex_authors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").regex("Art.*").build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
|
||||
assertThat(facet.getTerms().size(), is(1));
|
||||
|
||||
Term term = facet.getTerms().get(0);
|
||||
assertThat(term.getTerm(), is(ARTUR_KONCZAK));
|
||||
assertThat(term.getCount(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAllTermsForGivenQuery() {
|
||||
// given
|
||||
String facetName = "all_authors";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("title", "four")))
|
||||
.withFacet(new TermFacetRequestBuilder(facetName).applyQueryFilter().fields("authors.untouched").allTerms().build()).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(3)));
|
||||
|
||||
TermResult facet = (TermResult) result.getFacet(facetName);
|
||||
|
||||
assertThat(facet.getTerms().size(), is(4));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnRangeFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "rangeYears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(
|
||||
new RangeFacetRequestBuilder(facetName).field(PUBLISHED_YEARS)
|
||||
.to(YEAR_2000).range(YEAR_2000, YEAR_2002).from(YEAR_2002).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
RangeResult facet = (RangeResult) result.getFacet(facetName);
|
||||
assertThat(facet.getRanges().size(), is(equalTo(3)));
|
||||
|
||||
Range range = facet.getRanges().get(0);
|
||||
assertThat(range.getFrom(), nullValue());
|
||||
assertThat(range.getTo(), is((double) YEAR_2000));
|
||||
assertThat(range.getCount(), is(0L));
|
||||
assertThat(range.getTotal(), is(0.0));
|
||||
|
||||
range = facet.getRanges().get(1);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2000));
|
||||
assertThat(range.getTo(), is((double) YEAR_2002));
|
||||
assertThat(range.getCount(), is(3L));
|
||||
assertThat(range.getTotal(), is(6000.0));
|
||||
|
||||
range = facet.getRanges().get(2);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2002));
|
||||
assertThat(range.getTo(), nullValue());
|
||||
assertThat(range.getCount(), is(1L));
|
||||
assertThat(range.getTotal(), is(2002.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnKeyValueRangeFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "rangeScoreOverYears";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(
|
||||
new RangeFacetRequestBuilder(facetName).fields(PUBLISHED_YEARS, "score")
|
||||
.to(YEAR_2000).range(YEAR_2000, YEAR_2002).from(YEAR_2002).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
RangeResult facet = (RangeResult) result.getFacet(facetName);
|
||||
assertThat(facet.getRanges().size(), is(equalTo(3)));
|
||||
|
||||
Range range = facet.getRanges().get(0);
|
||||
assertThat(range.getFrom(), nullValue());
|
||||
assertThat(range.getTo(), is((double) YEAR_2000));
|
||||
assertThat(range.getCount(), is(0L));
|
||||
assertThat(range.getTotal(), is(0.0));
|
||||
|
||||
range = facet.getRanges().get(1);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2000));
|
||||
assertThat(range.getTo(), is((double) YEAR_2002));
|
||||
assertThat(range.getCount(), is(3L));
|
||||
assertThat(range.getTotal(), is(90.0));
|
||||
|
||||
range = facet.getRanges().get(2);
|
||||
assertThat(range.getFrom(), is((double) YEAR_2002));
|
||||
assertThat(range.getTo(), nullValue());
|
||||
assertThat(range.getCount(), is(1L));
|
||||
assertThat(range.getTotal(), is(40.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnStatisticalFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "statPublishedYear";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new StatisticalFacetRequestBuilder(facetName).field(PUBLISHED_YEARS).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
StatisticalResult facet = (StatisticalResult) result.getFacet(facetName);
|
||||
assertThat(facet.getCount(), is(equalTo(6L)));
|
||||
assertThat(facet.getMax(), is(equalTo(2002.0)));
|
||||
assertThat(facet.getMin(), is(equalTo(2000.0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnHistogramFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "numberPublicationPerYear";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new HistogramFacetRequestBuilder(facetName).field(PUBLISHED_YEARS).interval(1).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<ArticleEntity> result = elasticsearchTemplate.queryForPage(searchQuery, ArticleEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
HistogramResult facet = (HistogramResult) result.getFacet(facetName);
|
||||
assertThat(facet.getIntervalUnit().size(), is(equalTo(3)));
|
||||
|
||||
IntervalUnit unit = facet.getIntervalUnit().get(0);
|
||||
assertThat(unit.getKey(), is(Long.valueOf(YEAR_2000)));
|
||||
assertThat(unit.getCount(), is(3L));
|
||||
|
||||
unit = facet.getIntervalUnit().get(1);
|
||||
assertThat(unit.getKey(), is(Long.valueOf(YEAR_2001)));
|
||||
assertThat(unit.getCount(), is(2L));
|
||||
|
||||
unit = facet.getIntervalUnit().get(2);
|
||||
assertThat(unit.getKey(), is(Long.valueOf(YEAR_2002)));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.facet;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -22,21 +30,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.FacetedPage;
|
||||
import org.springframework.data.elasticsearch.core.facet.request.HistogramFacetRequestBuilder;
|
||||
import org.springframework.data.elasticsearch.core.facet.result.*;
|
||||
import org.springframework.data.elasticsearch.core.facet.result.HistogramResult;
|
||||
import org.springframework.data.elasticsearch.core.facet.result.IntervalUnit;
|
||||
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;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -47,93 +48,92 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class ElasticsearchTemplateHistogramFacetTests {
|
||||
|
||||
public static final long SEQUECE_CODE_INSERT = 1;
|
||||
public static final long SEQUECE_CODE_UPDATE = 2;
|
||||
public static final long SEQUECE_CODE_DELETE = 3;
|
||||
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
public static final String DATE_18 = "2013-10-18 18:01";
|
||||
public static final String DATE_17 = "2013-10-18 17:01";
|
||||
public static final String DATE_16 = "2013-10-18 16:01";
|
||||
public static final long SEQUECE_CODE_INSERT = 1;
|
||||
public static final long SEQUECE_CODE_UPDATE = 2;
|
||||
public static final long SEQUECE_CODE_DELETE = 3;
|
||||
public static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
public static final String DATE_18 = "2013-10-18 18:01";
|
||||
public static final String DATE_17 = "2013-10-18 17:01";
|
||||
public static final String DATE_16 = "2013-10-18 16:01";
|
||||
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() throws ParseException {
|
||||
elasticsearchTemplate.deleteIndex(LogEntity.class);
|
||||
elasticsearchTemplate.createIndex(LogEntity.class);
|
||||
elasticsearchTemplate.putMapping(LogEntity.class);
|
||||
elasticsearchTemplate.refresh(LogEntity.class, true);
|
||||
@Before
|
||||
public void before() throws ParseException {
|
||||
elasticsearchTemplate.deleteIndex(LogEntity.class);
|
||||
elasticsearchTemplate.createIndex(LogEntity.class);
|
||||
elasticsearchTemplate.putMapping(LogEntity.class);
|
||||
elasticsearchTemplate.refresh(LogEntity.class, true);
|
||||
|
||||
IndexQuery entry1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse(DATE_18)).code(SEQUECE_CODE_UPDATE).buildIndex();
|
||||
IndexQuery entry2 = new LogEntityBuilder("2").action("insert").date(dateFormatter.parse(DATE_17)).code(SEQUECE_CODE_INSERT).buildIndex();
|
||||
IndexQuery entry3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse(DATE_17)).code(SEQUECE_CODE_UPDATE).buildIndex();
|
||||
IndexQuery entry4 = new LogEntityBuilder("4").action("delete").date(dateFormatter.parse(DATE_16)).code(SEQUECE_CODE_DELETE).buildIndex();
|
||||
IndexQuery entry1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse(DATE_18)).code(SEQUECE_CODE_UPDATE).buildIndex();
|
||||
IndexQuery entry2 = new LogEntityBuilder("2").action("insert").date(dateFormatter.parse(DATE_17)).code(SEQUECE_CODE_INSERT).buildIndex();
|
||||
IndexQuery entry3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse(DATE_17)).code(SEQUECE_CODE_UPDATE).buildIndex();
|
||||
IndexQuery entry4 = new LogEntityBuilder("4").action("delete").date(dateFormatter.parse(DATE_16)).code(SEQUECE_CODE_DELETE).buildIndex();
|
||||
|
||||
elasticsearchTemplate.index(entry1);
|
||||
elasticsearchTemplate.index(entry2);
|
||||
elasticsearchTemplate.index(entry3);
|
||||
elasticsearchTemplate.index(entry4);
|
||||
|
||||
elasticsearchTemplate.refresh(LogEntity.class, true);
|
||||
}
|
||||
|
||||
|
||||
elasticsearchTemplate.index(entry1);
|
||||
elasticsearchTemplate.index(entry2);
|
||||
elasticsearchTemplate.index(entry3);
|
||||
elasticsearchTemplate.index(entry4);
|
||||
@Test
|
||||
public void shouldReturnSimpleHistogramFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "sequenceCodeFacet";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new HistogramFacetRequestBuilder(facetName).field("sequenceCode").interval(1).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<LogEntity> result = elasticsearchTemplate.queryForPage(searchQuery, LogEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
elasticsearchTemplate.refresh(LogEntity.class, true);
|
||||
}
|
||||
HistogramResult facet = (HistogramResult) result.getFacet(facetName);
|
||||
assertThat(facet.getIntervalUnit().size(), is(equalTo(3)));
|
||||
|
||||
IntervalUnit unit = facet.getIntervalUnit().get(0);
|
||||
assertThat(unit.getKey(), is(SEQUECE_CODE_INSERT));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
|
||||
@Test
|
||||
public void shouldReturnSimpleHistogramFacetForGivenQuery() {
|
||||
// given
|
||||
String facetName = "sequenceCodeFacet";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new HistogramFacetRequestBuilder(facetName).field("sequenceCode").interval(1).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<LogEntity> result = elasticsearchTemplate.queryForPage(searchQuery, LogEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
unit = facet.getIntervalUnit().get(1);
|
||||
assertThat(unit.getKey(), is(SEQUECE_CODE_UPDATE));
|
||||
assertThat(unit.getCount(), is(2L));
|
||||
|
||||
HistogramResult facet = (HistogramResult) result.getFacet(facetName);
|
||||
assertThat(facet.getIntervalUnit().size(), is(equalTo(3)));
|
||||
unit = facet.getIntervalUnit().get(2);
|
||||
assertThat(unit.getKey(), is(SEQUECE_CODE_DELETE));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
}
|
||||
|
||||
IntervalUnit unit = facet.getIntervalUnit().get(0);
|
||||
assertThat(unit.getKey(), is(SEQUECE_CODE_INSERT));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
@Test
|
||||
public void shouldReturnDateHistogramFacetForGivenQuery() throws ParseException {
|
||||
// given
|
||||
String facetName = "sequenceCodeFacet";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new HistogramFacetRequestBuilder(facetName).field("date").interval(1).timeUnit(TimeUnit.HOURS).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<LogEntity> result = elasticsearchTemplate.queryForPage(searchQuery, LogEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
|
||||
unit = facet.getIntervalUnit().get(1);
|
||||
assertThat(unit.getKey(), is(SEQUECE_CODE_UPDATE));
|
||||
assertThat(unit.getCount(), is(2L));
|
||||
HistogramResult facet = (HistogramResult) result.getFacet(facetName);
|
||||
assertThat(facet.getIntervalUnit().size(), is(equalTo(3)));
|
||||
|
||||
unit = facet.getIntervalUnit().get(2);
|
||||
assertThat(unit.getKey(), is(SEQUECE_CODE_DELETE));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
}
|
||||
IntervalUnit unit = facet.getIntervalUnit().get(0);
|
||||
assertThat(unit.getKey(), is(dateFormatter.parse("2013-10-18 16:00").getTime()));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
|
||||
@Test
|
||||
public void shouldReturnDateHistogramFacetForGivenQuery() throws ParseException {
|
||||
// given
|
||||
String facetName = "sequenceCodeFacet";
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||
.withFacet(new HistogramFacetRequestBuilder(facetName).field("date").interval(1).timeUnit(TimeUnit.HOURS).build()
|
||||
).build();
|
||||
// when
|
||||
FacetedPage<LogEntity> result = elasticsearchTemplate.queryForPage(searchQuery, LogEntity.class);
|
||||
// then
|
||||
assertThat(result.getNumberOfElements(), is(equalTo(4)));
|
||||
unit = facet.getIntervalUnit().get(1);
|
||||
assertThat(unit.getKey(), is(dateFormatter.parse("2013-10-18 17:00").getTime()));
|
||||
assertThat(unit.getCount(), is(2L));
|
||||
|
||||
HistogramResult facet = (HistogramResult) result.getFacet(facetName);
|
||||
assertThat(facet.getIntervalUnit().size(), is(equalTo(3)));
|
||||
|
||||
IntervalUnit unit = facet.getIntervalUnit().get(0);
|
||||
assertThat(unit.getKey(),is(dateFormatter.parse("2013-10-18 16:00").getTime()));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
|
||||
unit = facet.getIntervalUnit().get(1);
|
||||
assertThat(unit.getKey(),is(dateFormatter.parse("2013-10-18 17:00").getTime()));
|
||||
assertThat(unit.getCount(), is(2L));
|
||||
|
||||
unit = facet.getIntervalUnit().get(2);
|
||||
assertThat(unit.getKey(),is(dateFormatter.parse("2013-10-18 18:00").getTime()));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
}
|
||||
unit = facet.getIntervalUnit().get(2);
|
||||
assertThat(unit.getKey(), is(dateFormatter.parse("2013-10-18 18:00").getTime()));
|
||||
assertThat(unit.getCount(), is(1L));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package org.springframework.data.elasticsearch.core.facet;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* User: Artur Konczak
|
||||
* Date: 18/10/13
|
||||
@@ -17,58 +17,58 @@ import java.util.Date;
|
||||
@Document(indexName = "logs", type = "log", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
|
||||
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");
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String action;
|
||||
private String action;
|
||||
|
||||
private long sequenceCode;
|
||||
private long sequenceCode;
|
||||
|
||||
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
|
||||
private Date date;
|
||||
@Field(type = FieldType.Date, format = DateFormat.basic_date_time)
|
||||
private Date date;
|
||||
|
||||
private LogEntity() {
|
||||
}
|
||||
private LogEntity() {
|
||||
}
|
||||
|
||||
public LogEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public LogEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return new StringBuffer().append("{id:").append(id).append(",action:").append(action).append(",code:").append(sequenceCode).append(",date:").append(format.format(date)).append("}").toString();
|
||||
}
|
||||
public String toString() {
|
||||
return new StringBuffer().append("{id:").append(id).append(",action:").append(action).append(",code:").append(sequenceCode).append(",date:").append(format.format(date)).append("}").toString();
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public long getSequenceCode() {
|
||||
return sequenceCode;
|
||||
}
|
||||
public long getSequenceCode() {
|
||||
return sequenceCode;
|
||||
}
|
||||
|
||||
public void setSequenceCode(long sequenceCode) {
|
||||
this.sequenceCode = sequenceCode;
|
||||
}
|
||||
public void setSequenceCode(long sequenceCode) {
|
||||
this.sequenceCode = sequenceCode;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.springframework.data.elasticsearch.core.facet;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
|
||||
/**
|
||||
* User: Artur Konczak
|
||||
* Date: 18/10/13
|
||||
@@ -11,36 +11,35 @@ import java.util.Date;
|
||||
*/
|
||||
public class LogEntityBuilder {
|
||||
|
||||
private LogEntity result;
|
||||
private LogEntity result;
|
||||
|
||||
public LogEntityBuilder(String id) {
|
||||
result = new LogEntity(id);
|
||||
}
|
||||
public LogEntityBuilder(String id) {
|
||||
result = new LogEntity(id);
|
||||
}
|
||||
|
||||
public LogEntityBuilder action(String action) {
|
||||
result.setAction(action);
|
||||
return this;
|
||||
}
|
||||
public LogEntityBuilder action(String action) {
|
||||
result.setAction(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LogEntityBuilder code(long sequenceCode) {
|
||||
result.setSequenceCode(sequenceCode);
|
||||
return this;
|
||||
}
|
||||
public LogEntityBuilder code(long sequenceCode) {
|
||||
result.setSequenceCode(sequenceCode);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LogEntityBuilder date(Date date) {
|
||||
result.setDate(date);
|
||||
return this;
|
||||
}
|
||||
public LogEntityBuilder date(Date date) {
|
||||
result.setDate(date);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LogEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public LogEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,53 +25,53 @@ import org.springframework.data.elasticsearch.annotations.GeoPointField;
|
||||
@Document(indexName = "test-geo-index", type = "geo-annotation-point-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class AuthorMarkerAnnotatedEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
@GeoPointField
|
||||
private String location;
|
||||
@GeoPointField
|
||||
private String location;
|
||||
|
||||
@GeoPointField
|
||||
private double[] additionalLocation;
|
||||
@GeoPointField
|
||||
private double[] additionalLocation;
|
||||
|
||||
private AuthorMarkerAnnotatedEntity() {
|
||||
private AuthorMarkerAnnotatedEntity() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public AuthorMarkerAnnotatedEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public AuthorMarkerAnnotatedEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public double[] getAdditionalLocation() {
|
||||
return additionalLocation;
|
||||
}
|
||||
public double[] getAdditionalLocation() {
|
||||
return additionalLocation;
|
||||
}
|
||||
|
||||
public void setAdditionalLocation(double... additionalLocation) {
|
||||
this.additionalLocation = additionalLocation;
|
||||
}
|
||||
public void setAdditionalLocation(double... additionalLocation) {
|
||||
this.additionalLocation = additionalLocation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,35 +19,35 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
|
||||
public class AuthorMarkerAnnotatedEntityBuilder {
|
||||
|
||||
private AuthorMarkerAnnotatedEntity result;
|
||||
private AuthorMarkerAnnotatedEntity result;
|
||||
|
||||
public AuthorMarkerAnnotatedEntityBuilder(String id) {
|
||||
result = new AuthorMarkerAnnotatedEntity(id);
|
||||
}
|
||||
public AuthorMarkerAnnotatedEntityBuilder(String id) {
|
||||
result = new AuthorMarkerAnnotatedEntity(id);
|
||||
}
|
||||
|
||||
public AuthorMarkerAnnotatedEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
public AuthorMarkerAnnotatedEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthorMarkerAnnotatedEntityBuilder location(String location) {
|
||||
result.setLocation(location);
|
||||
return this;
|
||||
}
|
||||
public AuthorMarkerAnnotatedEntityBuilder location(String location) {
|
||||
result.setLocation(location);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthorMarkerAnnotatedEntityBuilder additionalLocation(double... location) {
|
||||
result.setAdditionalLocation(location);
|
||||
return this;
|
||||
}
|
||||
public AuthorMarkerAnnotatedEntityBuilder additionalLocation(double... location) {
|
||||
result.setAdditionalLocation(location);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthorMarkerAnnotatedEntity build() {
|
||||
return result;
|
||||
}
|
||||
public AuthorMarkerAnnotatedEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,40 +25,40 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
||||
@Document(indexName = "test-geo-index", type = "geo-class-point-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class AuthorMarkerEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
private GeoPoint location;
|
||||
private GeoPoint location;
|
||||
|
||||
private AuthorMarkerEntity(){
|
||||
}
|
||||
private AuthorMarkerEntity() {
|
||||
}
|
||||
|
||||
public AuthorMarkerEntity(String id){
|
||||
this.id = id;
|
||||
}
|
||||
public AuthorMarkerEntity(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public GeoPoint getLocation() {
|
||||
return location;
|
||||
}
|
||||
public GeoPoint getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(GeoPoint location) {
|
||||
this.location = location;
|
||||
}
|
||||
public void setLocation(GeoPoint location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,30 +24,30 @@ import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||
|
||||
public class AuthorMarkerEntityBuilder {
|
||||
|
||||
private AuthorMarkerEntity result;
|
||||
private AuthorMarkerEntity result;
|
||||
|
||||
public AuthorMarkerEntityBuilder(String id) {
|
||||
result = new AuthorMarkerEntity(id);
|
||||
}
|
||||
public AuthorMarkerEntityBuilder(String id) {
|
||||
result = new AuthorMarkerEntity(id);
|
||||
}
|
||||
|
||||
public AuthorMarkerEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
public AuthorMarkerEntityBuilder name(String name) {
|
||||
result.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthorMarkerEntityBuilder location(double latitude, double longitude) {
|
||||
result.setLocation(new GeoPoint(latitude, longitude));
|
||||
return this;
|
||||
}
|
||||
public AuthorMarkerEntityBuilder location(double latitude, double longitude) {
|
||||
result.setLocation(new GeoPoint(latitude, longitude));
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthorMarkerEntity build() {
|
||||
return result;
|
||||
}
|
||||
public AuthorMarkerEntity build() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
public IndexQuery buildIndex() {
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(result.getId());
|
||||
indexQuery.setObject(result);
|
||||
return indexQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.geo;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.index.query.FilterBuilders;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -28,13 +35,6 @@ import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilde
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -45,190 +45,189 @@ import static org.junit.Assert.assertEquals;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class ElasticsearchTemplateGeoTests {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
@Before
|
||||
public void before() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void loadClassBaseEntities() {
|
||||
elasticsearchTemplate.deleteIndex(AuthorMarkerEntity.class);
|
||||
elasticsearchTemplate.createIndex(AuthorMarkerEntity.class);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AuthorMarkerEntity.class);
|
||||
private void loadClassBaseEntities() {
|
||||
elasticsearchTemplate.deleteIndex(AuthorMarkerEntity.class);
|
||||
elasticsearchTemplate.createIndex(AuthorMarkerEntity.class);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AuthorMarkerEntity.class);
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new AuthorMarkerEntityBuilder("1").name("Franck Marchand").location(45.7806d, 3.0875d).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerEntityBuilder("2").name("Mohsin Husen").location(51.5171d, 0.1062d).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerEntityBuilder("3").name("Rizwan Idrees").location(51.5171d, 0.1062d).buildIndex());
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
|
||||
}
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
indexQueries.add(new AuthorMarkerEntityBuilder("1").name("Franck Marchand").location(45.7806d, 3.0875d).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerEntityBuilder("2").name("Mohsin Husen").location(51.5171d, 0.1062d).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerEntityBuilder("3").name("Rizwan Idrees").location(51.5171d, 0.1062d).buildIndex());
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
|
||||
}
|
||||
|
||||
private void loadAnnotationBaseEntities() {
|
||||
elasticsearchTemplate.deleteIndex(AuthorMarkerAnnotatedEntity.class);
|
||||
elasticsearchTemplate.createIndex(AuthorMarkerAnnotatedEntity.class);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerAnnotatedEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AuthorMarkerAnnotatedEntity.class);
|
||||
private void loadAnnotationBaseEntities() {
|
||||
elasticsearchTemplate.deleteIndex(AuthorMarkerAnnotatedEntity.class);
|
||||
elasticsearchTemplate.createIndex(AuthorMarkerAnnotatedEntity.class);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerAnnotatedEntity.class, true);
|
||||
elasticsearchTemplate.putMapping(AuthorMarkerAnnotatedEntity.class);
|
||||
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
double[] latLonArray = {0.100000, 51.000000};
|
||||
String lonLatString = "51.000000, 0.100000";
|
||||
String geohash = "u1044k2bd6u";
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("2").name("Mohsin Husen").location(geohash.substring(0,5)).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("1").name("Artur Konczak").location(lonLatString).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("3").name("Rizwan Idrees").location(geohash).additionalLocation(latLonArray).buildIndex());
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
double[] latLonArray = {0.100000, 51.000000};
|
||||
String lonLatString = "51.000000, 0.100000";
|
||||
String geohash = "u1044k2bd6u";
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("2").name("Mohsin Husen").location(geohash.substring(0, 5)).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("1").name("Artur Konczak").location(lonLatString).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("3").name("Rizwan Idrees").location(geohash).additionalLocation(latLonArray).buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
|
||||
}
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPutMappingForGivenEntityWithGeoLocation() throws Exception {
|
||||
//given
|
||||
Class entity = AuthorMarkerEntity.class;
|
||||
elasticsearchTemplate.createIndex(entity);
|
||||
//when
|
||||
assertThat(elasticsearchTemplate.putMapping(entity), is(true));
|
||||
}
|
||||
@Test
|
||||
public void shouldPutMappingForGivenEntityWithGeoLocation() throws Exception {
|
||||
//given
|
||||
Class entity = AuthorMarkerEntity.class;
|
||||
elasticsearchTemplate.createIndex(entity);
|
||||
//when
|
||||
assertThat(elasticsearchTemplate.putMapping(entity), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("location").within(new GeoPoint(45.7806d, 3.0875d), "20km"));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerEntity.class);
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("location").within(new GeoPoint(45.7806d, 3.0875d), "20km"));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(1));
|
||||
assertEquals("Franck Marchand", geoAuthorsForGeoCriteria.get(0).getName());
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(1));
|
||||
assertEquals("Franck Marchand", geoAuthorsForGeoCriteria.get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindSelectedAuthorMarkerInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery2 = new CriteriaQuery(
|
||||
new Criteria("name").is("Mohsin Husen").and("location").within(new GeoPoint(51.5171d, 0.1062d), "20km"));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria2 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery2, AuthorMarkerEntity.class);
|
||||
@Test
|
||||
public void shouldFindSelectedAuthorMarkerInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery2 = new CriteriaQuery(
|
||||
new Criteria("name").is("Mohsin Husen").and("location").within(new GeoPoint(51.5171d, 0.1062d), "20km"));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria2 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery2, AuthorMarkerEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria2.size(), is(1));
|
||||
assertEquals("Mohsin Husen", geoAuthorsForGeoCriteria2.get(0).getName());
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria2.size(), is(1));
|
||||
assertEquals("Mohsin Husen", geoAuthorsForGeoCriteria2.get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindStringAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("location").within(new GeoPoint(51.000000, 0.100000), "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
@Test
|
||||
public void shouldFindStringAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("location").within(new GeoPoint(51.000000, 0.100000), "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindDoubleAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("additionalLocation").within(new GeoPoint(51.001000, 0.10100), "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
@Test
|
||||
public void shouldFindDoubleAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("additionalLocation").within(new GeoPoint(51.001000, 0.10100), "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("additionalLocation").within("51.001000, 0.10100", "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
@Test
|
||||
public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("additionalLocation").within("51.001000, 0.10100", "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQueryUsingGeohashLocation() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("additionalLocation").within("u1044", "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
@Test
|
||||
public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQueryUsingGeohashLocation() {
|
||||
//given
|
||||
loadAnnotationBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
|
||||
new Criteria("additionalLocation").within("u1044", "1km"));
|
||||
//when
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAllMarkersForNativeSearchQuery() {
|
||||
//Given
|
||||
loadAnnotationBaseEntities();
|
||||
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withFilter(FilterBuilders.geoBoundingBoxFilter("additionalLocation").topLeft("52, -1").bottomRight("50,1"));
|
||||
//When
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(queryBuilder.build(), AuthorMarkerAnnotatedEntity.class);
|
||||
//Then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
@Test
|
||||
public void shouldFindAllMarkersForNativeSearchQuery() {
|
||||
//Given
|
||||
loadAnnotationBaseEntities();
|
||||
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().withFilter(FilterBuilders.geoBoundingBoxFilter("additionalLocation").topLeft("52, -1").bottomRight("50,1"));
|
||||
//When
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(queryBuilder.build(), AuthorMarkerAnnotatedEntity.class);
|
||||
//Then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoBox() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
|
||||
new Criteria("location").boundedBy(
|
||||
new GeoBox(new GeoPoint(53.5171d, 0),
|
||||
new GeoPoint(49.5171d, 0.2062d))));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoBox() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
|
||||
new Criteria("location").boundedBy(
|
||||
new GeoBox(new GeoPoint(53.5171d, 0),
|
||||
new GeoPoint(49.5171d, 0.2062d))));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria3.size(), is(2));
|
||||
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name", equalTo("Rizwan Idrees"))));
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria3.size(), is(2));
|
||||
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name", equalTo("Rizwan Idrees"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingString() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
|
||||
new Criteria("location").boundedBy("53.5171d, 0", "49.5171d, 0.2062d"));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingString() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
|
||||
new Criteria("location").boundedBy("53.5171d, 0", "49.5171d, 0.2062d"));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria3.size(), is(2));
|
||||
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name", equalTo("Rizwan Idrees"))));
|
||||
}
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria3.size(), is(2));
|
||||
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name", equalTo("Rizwan Idrees"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoPoints() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
|
||||
new Criteria("location").boundedBy(
|
||||
new GeoPoint(53.5171d, 0),
|
||||
new GeoPoint(49.5171d, 0.2062d)));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria3.size(), is(2));
|
||||
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name", equalTo("Rizwan Idrees"))));
|
||||
}
|
||||
@Test
|
||||
public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoPoints() {
|
||||
//given
|
||||
loadClassBaseEntities();
|
||||
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
|
||||
new Criteria("location").boundedBy(
|
||||
new GeoPoint(53.5171d, 0),
|
||||
new GeoPoint(49.5171d, 0.2062d)));
|
||||
//when
|
||||
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria3.size(), is(2));
|
||||
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name", equalTo("Rizwan Idrees"))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.mapping;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
@@ -22,9 +25,6 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -37,8 +37,8 @@ public class SimpleElasticsearchPersistentEntityTests {
|
||||
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithWrongVersionType.class);
|
||||
SimpleElasticsearchPersistentProperty persistentProperty = new SimpleElasticsearchPersistentProperty(
|
||||
EntityWithWrongVersionType.class.getDeclaredField("version"), new PropertyDescriptor("version",
|
||||
EntityWithWrongVersionType.class), new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>(
|
||||
typeInformation), new SimpleTypeHolder());
|
||||
EntityWithWrongVersionType.class), new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>(
|
||||
typeInformation), new SimpleTypeHolder());
|
||||
|
||||
// when
|
||||
new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty);
|
||||
@@ -51,13 +51,13 @@ public class SimpleElasticsearchPersistentEntityTests {
|
||||
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithMultipleVersionField.class);
|
||||
SimpleElasticsearchPersistentProperty persistentProperty1 = new SimpleElasticsearchPersistentProperty(
|
||||
EntityWithMultipleVersionField.class.getDeclaredField("version1"), new PropertyDescriptor("version1",
|
||||
EntityWithMultipleVersionField.class),
|
||||
EntityWithMultipleVersionField.class),
|
||||
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
|
||||
new SimpleTypeHolder());
|
||||
|
||||
SimpleElasticsearchPersistentProperty persistentProperty2 = new SimpleElasticsearchPersistentProperty(
|
||||
EntityWithMultipleVersionField.class.getDeclaredField("version2"), new PropertyDescriptor("version2",
|
||||
EntityWithMultipleVersionField.class),
|
||||
EntityWithMultipleVersionField.class),
|
||||
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
|
||||
new SimpleTypeHolder());
|
||||
|
||||
@@ -69,6 +69,7 @@ public class SimpleElasticsearchPersistentEntityTests {
|
||||
}
|
||||
|
||||
private class EntityWithWrongVersionType {
|
||||
|
||||
@Version
|
||||
private String version;
|
||||
|
||||
@@ -104,5 +105,4 @@ public class SimpleElasticsearchPersistentEntityTests {
|
||||
this.version2 = version2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,10 +15,18 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.query;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.elasticsearch.SampleEntity;
|
||||
import org.springframework.data.elasticsearch.SampleEntityBuilder;
|
||||
@@ -26,14 +34,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -42,12 +42,12 @@ import static org.junit.Assert.*;
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class CriteriaQueryTests {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class CriteriaQueryTests {
|
||||
assertThat(sampleEntity1, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Ignore("DATAES-30")
|
||||
@Ignore("DATAES-30")
|
||||
@Test
|
||||
public void shouldPerformOrOperation() {
|
||||
// given
|
||||
@@ -140,7 +140,7 @@ public class CriteriaQueryTests {
|
||||
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
|
||||
}
|
||||
|
||||
@Ignore("DATAES-30")
|
||||
@Ignore("DATAES-30")
|
||||
@Test
|
||||
public void shouldPerformOrOperationWithinCriteria() {
|
||||
// given
|
||||
@@ -702,24 +702,24 @@ public class CriteriaQueryTests {
|
||||
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnDocumentAboveMinimalScoreGivenCriteria() {
|
||||
// given
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
@Test
|
||||
public void shouldReturnDocumentAboveMinimalScoreGivenCriteria() {
|
||||
// given
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
|
||||
indexQueries.add(new SampleEntityBuilder("1").message("ab").buildIndex());
|
||||
indexQueries.add(new SampleEntityBuilder("2").message("bc").buildIndex());
|
||||
indexQueries.add(new SampleEntityBuilder("3").message("ac").buildIndex());
|
||||
indexQueries.add(new SampleEntityBuilder("1").message("ab").buildIndex());
|
||||
indexQueries.add(new SampleEntityBuilder("2").message("bc").buildIndex());
|
||||
indexQueries.add(new SampleEntityBuilder("3").message("ac").buildIndex());
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
// when
|
||||
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("message").contains("a").or(new Criteria("message").contains("b")));
|
||||
criteriaQuery.setMinScore(0.5F);
|
||||
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(criteriaQuery, SampleEntity.class);
|
||||
// then
|
||||
assertThat(page.getTotalElements(),is(1L));
|
||||
assertThat(page.getContent().get(0).getMessage(), is("ab"));
|
||||
}
|
||||
// when
|
||||
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("message").contains("a").or(new Criteria("message").contains("b")));
|
||||
criteriaQuery.setMinScore(0.5F);
|
||||
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(criteriaQuery, SampleEntity.class);
|
||||
// then
|
||||
assertThat(page.getTotalElements(), is(1L));
|
||||
assertThat(page.getContent().get(0).getMessage(), is("ab"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repositories;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -27,15 +34,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.random;
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -44,7 +42,7 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:custom-method-repository-test.xml")
|
||||
public class CustomMethodRepositoryTests {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private SampleCustomMethodRepository repository;
|
||||
|
||||
@Autowired
|
||||
@@ -52,7 +50,7 @@ public class CustomMethodRepositoryTests {
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
@@ -487,5 +485,4 @@ public class CustomMethodRepositoryTests {
|
||||
assertThat(sampleEntities.isEmpty(), is(false));
|
||||
assertThat(sampleEntities.size(), is(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,4 +26,5 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
|
||||
*/
|
||||
@Lazy
|
||||
public interface NonDocumentEntityRepository extends ElasticsearchRepository<NonDocumentEntity, String> {
|
||||
|
||||
}
|
||||
|
||||
@@ -15,14 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.SampleEntity;
|
||||
import org.springframework.data.elasticsearch.annotations.Query;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -64,5 +64,4 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
|
||||
Page<SampleEntity> findByAvailableFalse(Pageable pageable);
|
||||
|
||||
Page<SampleEntity> findByMessageOrderByTypeAsc(String message, Pageable pageable);
|
||||
|
||||
}
|
||||
|
||||
@@ -23,4 +23,5 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public interface SampleElasticSearchBookRepository extends ElasticsearchRepository<Book, String> {
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import javax.inject.Inject;
|
||||
*/
|
||||
class CdiRepositoryClient {
|
||||
|
||||
private CdiProductRepository repository;
|
||||
private CdiProductRepository repository;
|
||||
|
||||
public CdiProductRepository getRepository() {
|
||||
return repository;
|
||||
@@ -33,5 +33,4 @@ class CdiRepositoryClient {
|
||||
public void setRepository(CdiProductRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,13 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repositories.cdi;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.webbeans.cditest.CdiTestContainer;
|
||||
import org.apache.webbeans.cditest.CdiTestContainerLoader;
|
||||
import org.junit.*;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.elasticsearch.Product;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
@@ -77,5 +80,4 @@ public class CdiRepositoryTests {
|
||||
retrieved = repository.findOne(bean.getId());
|
||||
assertNull(retrieved);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repositories.cdi;
|
||||
|
||||
import static org.elasticsearch.node.NodeBuilder.*;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.elasticsearch.client.node.NodeClient;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
@@ -23,25 +31,17 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
|
||||
|
||||
/**
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
@ApplicationScoped
|
||||
class ElasticsearchTemplateProducer {
|
||||
|
||||
@Produces
|
||||
@Produces
|
||||
public ElasticsearchOperations createElasticsearchTemplate() throws IOException, ParserConfigurationException, SAXException {
|
||||
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled","false" );
|
||||
NodeClient client = (NodeClient) nodeBuilder().settings(settings).clusterName("testClusterForCDI").local(true).node()
|
||||
.client();
|
||||
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled", "false");
|
||||
NodeClient client = (NodeClient) nodeBuilder().settings(settings).clusterName("testClusterForCDI").local(true).node()
|
||||
.client();
|
||||
return new ElasticsearchTemplate(client);
|
||||
}
|
||||
|
||||
@@ -55,10 +55,10 @@ class ElasticsearchTemplateProducer {
|
||||
ElasticsearchOperations template;
|
||||
try {
|
||||
template = createElasticsearchTemplate();
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(QueryBuilders.matchAllQuery());
|
||||
deleteQuery.setIndex("test-product-index");
|
||||
deleteQuery.setType("test-product-type");
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(QueryBuilders.matchAllQuery());
|
||||
deleteQuery.setIndex("test-product-index");
|
||||
deleteQuery.setType("test-product-type");
|
||||
template.delete(deleteQuery);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -67,7 +67,5 @@ class ElasticsearchTemplateProducer {
|
||||
} catch (SAXException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.complex;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -24,11 +27,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
@@ -36,28 +34,26 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:complex-custom-method-repository-manual-wiring-test.xml")
|
||||
public class ComplexCustomMethodRepositoryManualWiringTests {
|
||||
|
||||
@Resource
|
||||
private ComplexElasticsearchRepositoryManualWiring complexRepository;
|
||||
@Autowired
|
||||
private ComplexElasticsearchRepositoryManualWiring complexRepository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteComplexCustomMethod() {
|
||||
//Given
|
||||
|
||||
//When
|
||||
String result = complexRepository.doSomethingSpecial();
|
||||
//Then
|
||||
assertThat(result, is("3+3=6"));
|
||||
|
||||
}
|
||||
@Test
|
||||
public void shouldExecuteComplexCustomMethod() {
|
||||
//Given
|
||||
|
||||
//When
|
||||
String result = complexRepository.doSomethingSpecial();
|
||||
//Then
|
||||
assertThat(result, is("3+3=6"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.complex;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -24,11 +27,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
* @author Mohsin Husen
|
||||
@@ -37,28 +35,26 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:complex-custom-method-repository-test.xml")
|
||||
public class ComplexCustomMethodRepositoryTests {
|
||||
|
||||
@Resource
|
||||
private ComplexElasticsearchRepository complexRepository;
|
||||
@Autowired
|
||||
private ComplexElasticsearchRepository complexRepository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteComplexCustomMethod() {
|
||||
//Given
|
||||
|
||||
//When
|
||||
String result = complexRepository.doSomethingSpecial();
|
||||
//Then
|
||||
assertThat(result, is("2+2=4"));
|
||||
|
||||
}
|
||||
@Test
|
||||
public void shouldExecuteComplexCustomMethod() {
|
||||
//Given
|
||||
|
||||
//When
|
||||
String result = complexRepository.doSomethingSpecial();
|
||||
//Then
|
||||
assertThat(result, is("2+2=4"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,5 @@ package org.springframework.data.elasticsearch.repository.complex;
|
||||
*/
|
||||
public interface ComplexElasticsearchRepositoryCustom {
|
||||
|
||||
public String doSomethingSpecial();
|
||||
|
||||
public String doSomethingSpecial();
|
||||
}
|
||||
|
||||
@@ -8,14 +8,12 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
*/
|
||||
public class ComplexElasticsearchRepositoryImpl implements ComplexElasticsearchRepositoryCustom {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate template;
|
||||
|
||||
@Override
|
||||
public String doSomethingSpecial() {
|
||||
assert(template.getElasticsearchConverter()!=null);
|
||||
return "2+2=4";
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate template;
|
||||
|
||||
@Override
|
||||
public String doSomethingSpecial() {
|
||||
assert (template.getElasticsearchConverter() != null);
|
||||
return "2+2=4";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,15 +23,15 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
*/
|
||||
public class ComplexElasticsearchRepositoryManualWiringImpl implements ComplexElasticsearchRepositoryCustom {
|
||||
|
||||
private ElasticsearchTemplate template;
|
||||
private ElasticsearchTemplate template;
|
||||
|
||||
@Override
|
||||
public String doSomethingSpecial() {
|
||||
assert(template.getElasticsearchConverter()!=null);
|
||||
return "3+3=6";
|
||||
}
|
||||
@Override
|
||||
public String doSomethingSpecial() {
|
||||
assert (template.getElasticsearchConverter() != null);
|
||||
return "3+3=6";
|
||||
}
|
||||
|
||||
public void setTemplate(ElasticsearchTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
public void setTemplate(ElasticsearchTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang.math.RandomUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -25,11 +30,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.repositories.DoubleIDRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
@@ -40,18 +40,18 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
||||
public class DoubleIDRepositoryTests {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private DoubleIDRepository repository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(DoubleIDEntity.class);
|
||||
elasticsearchTemplate.createIndex(DoubleIDEntity.class);
|
||||
elasticsearchTemplate.refresh(DoubleIDEntity.class, true);
|
||||
elasticsearchTemplate.createIndex(DoubleIDEntity.class);
|
||||
elasticsearchTemplate.refresh(DoubleIDEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -31,8 +33,6 @@ import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -60,5 +60,4 @@ public class ElasticsearchRepositoryFactoryTests {
|
||||
// when
|
||||
factory.getRepositoryBaseClass(metadata);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang.math.RandomUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -25,11 +30,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.repositories.IntegerIDRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
@@ -40,19 +40,19 @@ import static org.junit.Assert.assertThat;
|
||||
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
||||
public class IntegerIDRepositoryTests {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private IntegerIDRepository repository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(IntegerIDEntity.class);
|
||||
elasticsearchTemplate.createIndex(IntegerIDEntity.class);
|
||||
elasticsearchTemplate.refresh(IntegerIDEntity.class, true);
|
||||
}
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(IntegerIDEntity.class);
|
||||
elasticsearchTemplate.createIndex(IntegerIDEntity.class);
|
||||
elasticsearchTemplate.refresh(IntegerIDEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDoBulkIndexDocument() {
|
||||
|
||||
@@ -15,6 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.*;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@@ -31,16 +40,6 @@ import org.springframework.data.elasticsearch.repositories.SampleElasticsearchRe
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
@@ -50,19 +49,19 @@ import static org.junit.Assert.*;
|
||||
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
||||
public class SimpleElasticsearchRepositoryTests {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private SampleElasticsearchRepository repository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDoBulkIndexDocument() {
|
||||
@@ -105,16 +104,16 @@ public class SimpleElasticsearchRepositoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveDocumentWithoutId() {
|
||||
// given
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setMessage("some message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
// when
|
||||
repository.save(sampleEntity);
|
||||
// then
|
||||
assertThat(sampleEntity.getId(), is(notNullValue()));
|
||||
}
|
||||
public void shouldSaveDocumentWithoutId() {
|
||||
// given
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setMessage("some message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
// when
|
||||
repository.save(sampleEntity);
|
||||
// then
|
||||
assertThat(sampleEntity.getId(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindDocumentById() {
|
||||
@@ -410,7 +409,7 @@ public class SimpleElasticsearchRepositoryTests {
|
||||
repository.save(sampleEntities);
|
||||
|
||||
// when
|
||||
Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0), new String[]{"message"} ,new PageRequest(0, 5));
|
||||
Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0), new String[]{"message"}, new PageRequest(0, 5));
|
||||
|
||||
// then
|
||||
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
|
||||
|
||||
Reference in New Issue
Block a user