DATAES-73 - NullPointerException while persist a Map as part of persistent entity

This commit is contained in:
Mohsin Husen 2014-03-23 19:06:47 +00:00
parent 07f5fab022
commit 5a78ba31f4
5 changed files with 80 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -119,6 +119,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
public void addPersistentProperty(ElasticsearchPersistentProperty property) { public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property); super.addPersistentProperty(property);
if (property.getField() != null) {
Parent parent = property.getField().getAnnotation(Parent.class); Parent parent = property.getField().getAnnotation(Parent.class);
if (parent != null) { if (parent != null) {
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation"); Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
@ -127,6 +128,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.parentIdProperty = property; this.parentIdProperty = property;
this.parentType = parent.type(); this.parentType = parent.type();
} }
}
if (property.isVersionProperty()) { if (property.isVersionProperty()) {
Assert.isTrue(property.getType() == Long.class, "Version property should be Long"); Assert.isTrue(property.getType() == Long.class, "Version property should be Long");

View File

@ -55,7 +55,7 @@ public class SimpleElasticsearchPersistentProperty extends
@Override @Override
public boolean isIdProperty() { public boolean isIdProperty() {
return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()); return super.isIdProperty() || field != null ? SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()) : false;
} }
@Override @Override

View File

@ -49,6 +49,7 @@ public class InnerObjectTests {
public void before() { public void before() {
elasticsearchTemplate.deleteIndex(Book.class); elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class); elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class, true); elasticsearchTemplate.refresh(Book.class, true);
} }

View File

@ -15,13 +15,13 @@
*/ */
package org.springframework.data.elasticsearch; package org.springframework.data.elasticsearch;
import static org.apache.commons.lang.RandomStringUtils.*;
import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
@ -56,6 +56,7 @@ public class NestedObjectTests {
public void before() { public void before() {
elasticsearchTemplate.deleteIndex(Book.class); elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class); elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class, true); elasticsearchTemplate.refresh(Book.class, true);
elasticsearchTemplate.deleteIndex(Person.class); elasticsearchTemplate.deleteIndex(Person.class);
elasticsearchTemplate.createIndex(Person.class); elasticsearchTemplate.createIndex(Person.class);
@ -301,4 +302,53 @@ public class NestedObjectTests {
assertThat(persons.size(), is(1)); assertThat(persons.size(), is(1));
} }
/*
DATAES-73
*/
@Test
public void shouldIndexAndSearchMapAsNestedType() {
//given
Book book1 = new Book();
Book book2 = new Book();
book1.setId(randomNumeric(5));
book1.setName("testBook1");
book2.setId(randomNumeric(5));
book2.setName("testBook2");
Map<Integer, Collection<String>> map1 = new HashMap<Integer, Collection<String>>();
map1.put(1, Arrays.asList("test1", "test2"));
Map<Integer, Collection<String>> map2 = new HashMap<Integer, Collection<String>>();
map2.put(1, Arrays.asList("test3", "test4"));
book1.setBuckets(map1);
book2.setBuckets(map2);
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
IndexQuery indexQuery1 = new IndexQuery();
indexQuery1.setId(book1.getId());
indexQuery1.setObject(book1);
IndexQuery indexQuery2 = new IndexQuery();
indexQuery2.setId(book2.getId());
indexQuery2.setObject(book2);
indexQueries.add(indexQuery1);
indexQueries.add(indexQuery2);
//when
elasticsearchTemplate.bulkIndex(indexQueries);
elasticsearchTemplate.refresh(Book.class, true);
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(nestedQuery("buckets", termQuery("buckets.1", "test3")))
.build();
Page<Book> books = elasticsearchTemplate.queryForPage(searchQuery, Book.class);
assertThat(books.getContent().size(), is(1));
assertThat(books.getContent().get(0).getId(), is(book2.getId()));
} }
}

View File

@ -15,6 +15,10 @@
*/ */
package org.springframework.data.elasticsearch.entities; package org.springframework.data.elasticsearch.entities;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.Field;
@ -32,6 +36,8 @@ public class Book {
private String name; private String name;
@Field(type = FieldType.Object) @Field(type = FieldType.Object)
private Author author; private Author author;
@Field(type = FieldType.Nested)
private Map<Integer, Collection<String>> buckets = new HashMap<Integer, Collection<String>>();
public String getId() { public String getId() {
return id; return id;
@ -56,4 +62,12 @@ public class Book {
public void setAuthor(Author author) { public void setAuthor(Author author) {
this.author = author; this.author = author;
} }
public Map<Integer, Collection<String>> getBuckets() {
return buckets;
}
public void setBuckets(Map<Integer, Collection<String>> buckets) {
this.buckets = buckets;
}
} }