mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-29 15:22:11 +00:00
DATAES-73 - NullPointerException while persist a Map as part of persistent entity
This commit is contained in:
parent
07f5fab022
commit
5a78ba31f4
@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -119,13 +119,15 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||
super.addPersistentProperty(property);
|
||||
|
||||
Parent parent = property.getField().getAnnotation(Parent.class);
|
||||
if (parent != null) {
|
||||
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
|
||||
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
|
||||
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
|
||||
this.parentIdProperty = property;
|
||||
this.parentType = parent.type();
|
||||
if (property.getField() != null) {
|
||||
Parent parent = property.getField().getAnnotation(Parent.class);
|
||||
if (parent != null) {
|
||||
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
|
||||
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
|
||||
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
|
||||
this.parentIdProperty = property;
|
||||
this.parentType = parent.type();
|
||||
}
|
||||
}
|
||||
|
||||
if (property.isVersionProperty()) {
|
||||
|
@ -55,7 +55,7 @@ public class SimpleElasticsearchPersistentProperty extends
|
||||
|
||||
@Override
|
||||
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
|
||||
|
@ -49,6 +49,7 @@ public class InnerObjectTests {
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(Book.class);
|
||||
elasticsearchTemplate.createIndex(Book.class);
|
||||
elasticsearchTemplate.putMapping(Book.class);
|
||||
elasticsearchTemplate.refresh(Book.class, true);
|
||||
}
|
||||
|
||||
|
@ -15,13 +15,13 @@
|
||||
*/
|
||||
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.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
@ -56,6 +56,7 @@ public class NestedObjectTests {
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(Book.class);
|
||||
elasticsearchTemplate.createIndex(Book.class);
|
||||
elasticsearchTemplate.putMapping(Book.class);
|
||||
elasticsearchTemplate.refresh(Book.class, true);
|
||||
elasticsearchTemplate.deleteIndex(Person.class);
|
||||
elasticsearchTemplate.createIndex(Person.class);
|
||||
@ -301,4 +302,53 @@ public class NestedObjectTests {
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,10 @@
|
||||
*/
|
||||
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.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
@ -32,6 +36,8 @@ public class Book {
|
||||
private String name;
|
||||
@Field(type = FieldType.Object)
|
||||
private Author author;
|
||||
@Field(type = FieldType.Nested)
|
||||
private Map<Integer, Collection<String>> buckets = new HashMap<Integer, Collection<String>>();
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
@ -56,4 +62,12 @@ public class Book {
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public Map<Integer, Collection<String>> getBuckets() {
|
||||
return buckets;
|
||||
}
|
||||
|
||||
public void setBuckets(Map<Integer, Collection<String>> buckets) {
|
||||
this.buckets = buckets;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user