-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
InnerHitsIntegrationTests.java
220 lines (179 loc) · 6.74 KB
/
InnerHitsIntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright 2020-2023 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
*
* https://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.core;
import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.data.elasticsearch.core.document.NestedMetaData;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
import org.springframework.lang.Nullable;
/**
* Testing the querying and parsing of inner_hits.
*
* @author Peter-Josef Meisch
*/
@SpringIntegrationTest
public abstract class InnerHitsIntegrationTests {
@Autowired ElasticsearchOperations operations;
@Autowired IndexNameProvider indexNameProvider;
@Nullable IndexOperations indexOps;
@BeforeEach
void setUp() {
indexNameProvider.increment();
indexOps = operations.indexOps(City.class);
indexOps.createWithMapping();
Inhabitant john = new Inhabitant("John", "Smith");
Inhabitant carla = new Inhabitant("Carla", "Miller");
House cornerHouse = new House("Round the corner", "7", Arrays.asList(john, carla));
City metropole = new City("Metropole", Arrays.asList(cornerHouse));
Inhabitant jack = new Inhabitant("Jack", "Wayne");
Inhabitant emmy = new Inhabitant("Emmy", "Stone");
House mainStreet = new House("Main Street", "42", Arrays.asList(jack, emmy));
City village = new City("Village", Arrays.asList(mainStreet));
operations.save(Arrays.asList(metropole, village));
}
@Test
@Order(java.lang.Integer.MAX_VALUE)
void cleanup() {
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
}
@Test
void shouldReturnInnerHits() {
Query query = buildQueryForInnerHits("inner_hit_name", "hou-ses.in-habi-tants", "hou-ses.in-habi-tants.first-name",
"Carla");
SoftAssertions softly = new SoftAssertions();
SearchHits<City> searchHits = operations.search(query, City.class);
softly.assertThat(searchHits.getTotalHits()).isEqualTo(1);
SearchHit<City> searchHit = searchHits.getSearchHit(0);
softly.assertThat(searchHit.getInnerHits()).hasSize(1);
SearchHits<?> innerHits = searchHit.getInnerHits("inner_hit_name");
softly.assertThat(innerHits).hasSize(1);
SearchHit<?> innerHit = innerHits.getSearchHit(0);
Object content = innerHit.getContent();
assertThat(content).isInstanceOf(Inhabitant.class);
Inhabitant inhabitant = (Inhabitant) content;
softly.assertThat(inhabitant.getFirstName()).isEqualTo("Carla");
softly.assertThat(inhabitant.getLastName()).isEqualTo("Miller");
NestedMetaData nestedMetaData = innerHit.getNestedMetaData();
softly.assertThat(nestedMetaData.getField()).isEqualTo("houses");
softly.assertThat(nestedMetaData.getOffset()).isEqualTo(0);
softly.assertThat(nestedMetaData.getChild().getField()).isEqualTo("inhabitants");
softly.assertThat(nestedMetaData.getChild().getOffset()).isEqualTo(1);
softly.assertAll();
}
abstract protected Query buildQueryForInnerHits(String innerHitName, String nestedQueryPath, String matchField,
String matchValue);
@Document(indexName = "#{@indexNameProvider.indexName()}")
static class City {
@Nullable
@Id private String name;
// NOTE: using a custom names here to cover property name matching
@Nullable
@Field(name = "hou-ses", type = FieldType.Nested) private Collection<House> houses = new ArrayList<>();
public City(@Nullable String name, @Nullable Collection<House> houses) {
this.name = name;
this.houses = houses;
}
@Nullable
public String getName() {
return name;
}
public void setName(@Nullable String name) {
this.name = name;
}
@Nullable
public Collection<House> getHouses() {
return houses;
}
public void setHouses(@Nullable Collection<House> houses) {
this.houses = houses;
}
}
static class House {
@Nullable
@Field(type = FieldType.Text) private String street;
@Nullable
@Field(type = FieldType.Text) private String streetNumber;
// NOTE: using custom names here to cover property name matching
@Nullable
@Field(name = "in-habi-tants", type = FieldType.Nested) private List<Inhabitant> inhabitants = new ArrayList<>();
public House(@Nullable String street, @Nullable String streetNumber, @Nullable List<Inhabitant> inhabitants) {
this.street = street;
this.streetNumber = streetNumber;
this.inhabitants = inhabitants;
}
@Nullable
public String getStreet() {
return street;
}
public void setStreet(@Nullable String street) {
this.street = street;
}
@Nullable
public String getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(@Nullable String streetNumber) {
this.streetNumber = streetNumber;
}
@Nullable
public List<Inhabitant> getInhabitants() {
return inhabitants;
}
public void setInhabitants(@Nullable List<Inhabitant> inhabitants) {
this.inhabitants = inhabitants;
}
}
static class Inhabitant {
// NOTE: using custom names here to cover property name matching
@Nullable
@Field(name = "first-name", type = FieldType.Text) private String firstName;
@Nullable
@Field(name = "last-name", type = FieldType.Text) private String lastName;
public Inhabitant(@Nullable String firstName, @Nullable String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Nullable
public String getFirstName() {
return firstName;
}
public void setFirstName(@Nullable String firstName) {
this.firstName = firstName;
}
@Nullable
public String getLastName() {
return lastName;
}
public void setLastName(@Nullable String lastName) {
this.lastName = lastName;
}
}
}