Skip to content

Commit 5a78ba3

Browse files
committed
DATAES-73 - NullPointerException while persist a Map as part of persistent entity
1 parent 07f5fab commit 5a78ba3

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,13 +119,15 @@ public ElasticsearchPersistentProperty getParentIdProperty() {
119119
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
120120
super.addPersistentProperty(property);
121121

122-
Parent parent = property.getField().getAnnotation(Parent.class);
123-
if (parent != null) {
124-
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
125-
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
126-
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
127-
this.parentIdProperty = property;
128-
this.parentType = parent.type();
122+
if (property.getField() != null) {
123+
Parent parent = property.getField().getAnnotation(Parent.class);
124+
if (parent != null) {
125+
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
126+
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
127+
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
128+
this.parentIdProperty = property;
129+
this.parentType = parent.type();
130+
}
129131
}
130132

131133
if (property.isVersionProperty()) {

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public String getFieldName() {
5555

5656
@Override
5757
public boolean isIdProperty() {
58-
return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
58+
return super.isIdProperty() || field != null ? SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()) : false;
5959
}
6060

6161
@Override

src/test/java/org/springframework/data/elasticsearch/InnerObjectTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class InnerObjectTests {
4949
public void before() {
5050
elasticsearchTemplate.deleteIndex(Book.class);
5151
elasticsearchTemplate.createIndex(Book.class);
52+
elasticsearchTemplate.putMapping(Book.class);
5253
elasticsearchTemplate.refresh(Book.class, true);
5354
}
5455

src/test/java/org/springframework/data/elasticsearch/NestedObjectTests.java

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

18+
import static org.apache.commons.lang.RandomStringUtils.*;
1819
import static org.elasticsearch.index.query.QueryBuilders.*;
19-
import static org.hamcrest.Matchers.*;
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.hamcrest.Matchers.notNullValue;
2022
import static org.junit.Assert.*;
2123

22-
import java.util.ArrayList;
23-
import java.util.Arrays;
24-
import java.util.List;
24+
import java.util.*;
2525

2626
import org.elasticsearch.index.query.BoolQueryBuilder;
2727
import org.elasticsearch.index.query.QueryBuilder;
@@ -56,6 +56,7 @@ public class NestedObjectTests {
5656
public void before() {
5757
elasticsearchTemplate.deleteIndex(Book.class);
5858
elasticsearchTemplate.createIndex(Book.class);
59+
elasticsearchTemplate.putMapping(Book.class);
5960
elasticsearchTemplate.refresh(Book.class, true);
6061
elasticsearchTemplate.deleteIndex(Person.class);
6162
elasticsearchTemplate.createIndex(Person.class);
@@ -301,4 +302,53 @@ public void shouldSearchBooksForPersonInitialLevelNestedType() {
301302

302303
assertThat(persons.size(), is(1));
303304
}
305+
306+
/*
307+
DATAES-73
308+
*/
309+
@Test
310+
public void shouldIndexAndSearchMapAsNestedType() {
311+
//given
312+
Book book1 = new Book();
313+
Book book2 = new Book();
314+
315+
book1.setId(randomNumeric(5));
316+
book1.setName("testBook1");
317+
318+
book2.setId(randomNumeric(5));
319+
book2.setName("testBook2");
320+
321+
Map<Integer, Collection<String>> map1 = new HashMap<Integer, Collection<String>>();
322+
map1.put(1, Arrays.asList("test1", "test2"));
323+
324+
Map<Integer, Collection<String>> map2 = new HashMap<Integer, Collection<String>>();
325+
map2.put(1, Arrays.asList("test3", "test4"));
326+
327+
book1.setBuckets(map1);
328+
book2.setBuckets(map2);
329+
330+
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
331+
IndexQuery indexQuery1 = new IndexQuery();
332+
indexQuery1.setId(book1.getId());
333+
indexQuery1.setObject(book1);
334+
335+
IndexQuery indexQuery2 = new IndexQuery();
336+
indexQuery2.setId(book2.getId());
337+
indexQuery2.setObject(book2);
338+
339+
indexQueries.add(indexQuery1);
340+
indexQueries.add(indexQuery2);
341+
//when
342+
elasticsearchTemplate.bulkIndex(indexQueries);
343+
elasticsearchTemplate.refresh(Book.class, true);
344+
//then
345+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
346+
.withQuery(nestedQuery("buckets", termQuery("buckets.1", "test3")))
347+
.build();
348+
Page<Book> books = elasticsearchTemplate.queryForPage(searchQuery, Book.class);
349+
350+
assertThat(books.getContent().size(), is(1));
351+
assertThat(books.getContent().get(0).getId(), is(book2.getId()));
352+
}
304353
}
354+

src/test/java/org/springframework/data/elasticsearch/entities/Book.java

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

18+
import java.util.Collection;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
1822
import org.springframework.data.annotation.Id;
1923
import org.springframework.data.elasticsearch.annotations.Document;
2024
import org.springframework.data.elasticsearch.annotations.Field;
@@ -32,6 +36,8 @@ public class Book {
3236
private String name;
3337
@Field(type = FieldType.Object)
3438
private Author author;
39+
@Field(type = FieldType.Nested)
40+
private Map<Integer, Collection<String>> buckets = new HashMap<Integer, Collection<String>>();
3541

3642
public String getId() {
3743
return id;
@@ -56,4 +62,12 @@ public Author getAuthor() {
5662
public void setAuthor(Author author) {
5763
this.author = author;
5864
}
65+
66+
public Map<Integer, Collection<String>> getBuckets() {
67+
return buckets;
68+
}
69+
70+
public void setBuckets(Map<Integer, Collection<String>> buckets) {
71+
this.buckets = buckets;
72+
}
5973
}

0 commit comments

Comments
 (0)