From ff7cfa39652c193f555be0f67d8748fc0a757e7e Mon Sep 17 00:00:00 2001 From: Mohsin Husen Date: Wed, 30 Oct 2013 14:40:10 +0000 Subject: [PATCH] DATAES-23 : Add support for Nested Type (Root level initially) --- .../elasticsearch/annotations/FieldType.java | 20 ++++- .../data/elasticsearch/Car.java | 43 ++++++++++ .../data/elasticsearch/NestedObjectTests.java | 85 ++++++++++++++++++- .../data/elasticsearch/Person.java | 66 ++++++++++++++ 4 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/Car.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/Person.java diff --git a/src/main/java/org/springframework/data/elasticsearch/annotations/FieldType.java b/src/main/java/org/springframework/data/elasticsearch/annotations/FieldType.java index ef982b601..a8a6860d8 100644 --- a/src/main/java/org/springframework/data/elasticsearch/annotations/FieldType.java +++ b/src/main/java/org/springframework/data/elasticsearch/annotations/FieldType.java @@ -1,7 +1,25 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.data.elasticsearch.annotations; /** + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak */ public enum FieldType { - String, Integer, Long, Date, Float, Double, Boolean, Object, Auto + String, Integer, Long, Date, Float, Double, Boolean, Object, Auto, Nested } diff --git a/src/test/java/org/springframework/data/elasticsearch/Car.java b/src/test/java/org/springframework/data/elasticsearch/Car.java new file mode 100644 index 000000000..03b7d0f2f --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/Car.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + * @author Artur Konczak + */ +public class Car { + + private String name; + private String model; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } +} diff --git a/src/test/java/org/springframework/data/elasticsearch/NestedObjectTests.java b/src/test/java/org/springframework/data/elasticsearch/NestedObjectTests.java index 3a6756922..0e7c37cff 100644 --- a/src/test/java/org/springframework/data/elasticsearch/NestedObjectTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/NestedObjectTests.java @@ -15,18 +15,29 @@ */ package org.springframework.data.elasticsearch; +import org.elasticsearch.index.query.QueryBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +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.data.elasticsearch.repositories.SampleElasticSearchBookRepository; 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; @@ -34,13 +45,14 @@ import static org.junit.Assert.assertThat; /** * @author Rizwan Idrees * @author Mohsin Husen + * @author Artur Konczak */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:/repository-test-nested-object.xml") public class NestedObjectTests { @Resource - private SampleElasticSearchBookRepository repository; + private SampleElasticSearchBookRepository bookRepository; @Autowired private ElasticsearchTemplate elasticsearchTemplate; @@ -51,10 +63,75 @@ public class NestedObjectTests { 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); + } + + @Test + public void shouldIndexNestedObject(){ + + List cars = new ArrayList(); + + 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 indexQueries = new ArrayList(); + 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 persons = elasticsearchTemplate.queryForList(searchQuery, Person.class); + + assertThat(persons.size() , is(1)); + } @Test - public void shouldIndexNestedObject() { + public void shouldIndexInnerObject() { // given String id = randomAlphanumeric(5); Book book = new Book(); @@ -65,8 +142,8 @@ public class NestedObjectTests { author.setName("ABC"); book.setAuthor(author); // when - repository.save(book); + bookRepository.save(book); // then - assertThat(repository.findOne(id), is(notNullValue())); + assertThat(bookRepository.findOne(id), is(notNullValue())); } } diff --git a/src/test/java/org/springframework/data/elasticsearch/Person.java b/src/test/java/org/springframework/data/elasticsearch/Person.java new file mode 100644 index 000000000..e977c4966 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/Person.java @@ -0,0 +1,66 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch; + + +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" , shards = 1, replicas = 0) +public class Person { + + @Id + private String id; + + private String name; + + @Field( type = FieldType.Nested) + private List car; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCar() { + return car; + } + + public void setCar(List car) { + this.car = car; + } +}