Skip to content

Commit 7b91b89

Browse files
committed
Addressing PR comments, adding parameter to the tryGetFromParentDocument
1 parent 4a9e954 commit 7b91b89

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/main/java/com/yelp/nrtsearch/server/doc/SegmentDocLookup.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ public LoadedDocValues<?> get(Object key) {
111111
Objects.requireNonNull(key);
112112
String fieldName = key.toString();
113113
LoadedDocValues<?> docValues = loaderCache.get(fieldName);
114+
FieldDef fieldDef = null;
114115
if (docValues == null) {
115-
FieldDef fieldDef = fieldDefLookup.apply(fieldName);
116+
fieldDef = fieldDefLookup.apply(fieldName);
116117
if (fieldDef == null) {
117118
throw new IllegalArgumentException("Field does not exist: " + fieldName);
118119
}
@@ -133,7 +134,10 @@ public LoadedDocValues<?> get(Object key) {
133134
"Could not set doc: " + docId + ", field: " + fieldName, e);
134135
}
135136
if (docValues.isEmpty()) {
136-
LoadedDocValues<?> parentDocValues = tryGetFromParentDocument(fieldName);
137+
if (fieldDef == null) {
138+
fieldDef = fieldDefLookup.apply(fieldName);
139+
}
140+
LoadedDocValues<?> parentDocValues = tryGetFromParentDocument(fieldName, fieldDef);
137141
if (parentDocValues != null) {
138142
return parentDocValues;
139143
}
@@ -145,11 +149,12 @@ public LoadedDocValues<?> get(Object key) {
145149
* Attempt to retrieve the field from the parent document using NESTED_DOCUMENT_OFFSET.
146150
*
147151
* @param fieldName the name of the field to retrieve
152+
* @param fieldDef the definition of the field to retrieve
148153
* @return LoadedDocValues from parent document, or null if not found or not a nested document
149154
* @throws IllegalArgumentException if there are issues accessing the offset field or parent
150155
* document
151156
*/
152-
private LoadedDocValues<?> tryGetFromParentDocument(String fieldName) {
157+
private LoadedDocValues<?> tryGetFromParentDocument(String fieldName, FieldDef fieldDef) {
153158
FieldDef offsetFieldDef;
154159
try {
155160
offsetFieldDef = IndexState.getMetaField(IndexState.NESTED_DOCUMENT_OFFSET);
@@ -178,6 +183,7 @@ private LoadedDocValues<?> tryGetFromParentDocument(String fieldName) {
178183
"Could not set doc: " + docId + " for NESTED_DOCUMENT_OFFSET field", e);
179184
}
180185

186+
// If there's no offset value, this is not a nested document and therefore we should terminate
181187
if (offsetDocValues.isEmpty()) {
182188
return null;
183189
}
@@ -187,11 +193,6 @@ private LoadedDocValues<?> tryGetFromParentDocument(String fieldName) {
187193

188194
// The offset represents the exact number of documents to jump forward to reach the parent
189195
int parentDocId = docId + offset;
190-
191-
FieldDef fieldDef = fieldDefLookup.apply(fieldName);
192-
if (fieldDef == null) {
193-
throw new IllegalArgumentException("Field does not exist: " + fieldName);
194-
}
195196
if (!(fieldDef instanceof IndexableFieldDef<?> indexableFieldDef)) {
196197
throw new IllegalArgumentException("Field cannot have doc values: " + fieldName);
197198
}

src/test/java/com/yelp/nrtsearch/server/doc/ParentDocLookupTest.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.gson.Gson;
2323
import com.google.gson.GsonBuilder;
2424
import com.yelp.nrtsearch.server.ServerTestCase;
25+
import com.yelp.nrtsearch.server.field.FieldDef;
2526
import com.yelp.nrtsearch.server.grpc.AddDocumentRequest;
2627
import com.yelp.nrtsearch.server.grpc.FieldDefRequest;
2728
import com.yelp.nrtsearch.server.index.IndexState;
@@ -102,13 +103,15 @@ public void testParentAccessWithValidNestedDocs() throws Exception {
102103
SegmentDocLookup docLookup = new SegmentDocLookup(indexState::getField, leafContext);
103104

104105
Method tryGetMethod =
105-
SegmentDocLookup.class.getDeclaredMethod("tryGetFromParentDocument", String.class);
106+
SegmentDocLookup.class.getDeclaredMethod(
107+
"tryGetFromParentDocument", String.class, FieldDef.class);
106108
tryGetMethod.setAccessible(true);
107109

108110
// Test 1: Child document should be able to access parent field
109111
docLookup.setDocId(0);
112+
FieldDef docIdFieldDef = indexState.getField("doc_id");
110113
LoadedDocValues<?> parentField =
111-
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "doc_id");
114+
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "doc_id", docIdFieldDef);
112115

113116
if (parentField != null && !parentField.isEmpty()) {
114117
assertNotNull("Should find parent field from child document", parentField);
@@ -119,8 +122,10 @@ public void testParentAccessWithValidNestedDocs() throws Exception {
119122
// Test 2: Non-existent field should return null
120123
LoadedDocValues<?> nonExistentField = null;
121124
try {
125+
FieldDef nonExistentFieldDef = indexState.getField("non_existent_field");
122126
nonExistentField =
123-
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "non_existent_field");
127+
(LoadedDocValues<?>)
128+
tryGetMethod.invoke(docLookup, "non_existent_field", nonExistentFieldDef);
124129
} catch (Exception e) {
125130
if (e.getCause() instanceof IllegalArgumentException) {
126131
nonExistentField = null;
@@ -133,7 +138,7 @@ public void testParentAccessWithValidNestedDocs() throws Exception {
133138
// Test 3: Parent document (no offset field) should return null
134139
docLookup.setDocId(2);
135140
LoadedDocValues<?> fromParentDoc =
136-
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "doc_id");
141+
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "doc_id", docIdFieldDef);
137142
assertNull("Parent document should return null (no offset field)", fromParentDoc);
138143
} finally {
139144
if (s != null) {
@@ -155,7 +160,8 @@ public void testParentAccessWithDifferentFieldTypes() throws Exception {
155160
SegmentDocLookup docLookup = new SegmentDocLookup(indexState::getField, leafContext);
156161

157162
Method tryGetMethod =
158-
SegmentDocLookup.class.getDeclaredMethod("tryGetFromParentDocument", String.class);
163+
SegmentDocLookup.class.getDeclaredMethod(
164+
"tryGetFromParentDocument", String.class, FieldDef.class);
159165
tryGetMethod.setAccessible(true);
160166

161167
docLookup.setDocId(0);
@@ -164,8 +170,9 @@ public void testParentAccessWithDifferentFieldTypes() throws Exception {
164170

165171
for (String fieldName : fieldsToTest) {
166172
try {
173+
FieldDef fieldDef = indexState.getField(fieldName);
167174
LoadedDocValues<?> result =
168-
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, fieldName);
175+
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, fieldName, fieldDef);
169176
assertTrue("Field access should not throw unexpected exceptions: " + fieldName, true);
170177
} catch (Exception e) {
171178
assertTrue(
@@ -193,14 +200,17 @@ public void testParentAccessWithoutNestedDocuments() throws Exception {
193200
SegmentDocLookup docLookup = new SegmentDocLookup(indexState::getField, leafContext);
194201

195202
Method tryGetMethod =
196-
SegmentDocLookup.class.getDeclaredMethod("tryGetFromParentDocument", String.class);
203+
SegmentDocLookup.class.getDeclaredMethod(
204+
"tryGetFromParentDocument", String.class, FieldDef.class);
197205
tryGetMethod.setAccessible(true);
198206

199207
int maxDoc = leafContext.reader().maxDoc();
200208
if (maxDoc > 1) {
201209
docLookup.setDocId(maxDoc - 1);
202210

203-
LoadedDocValues<?> result = (LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "doc_id");
211+
FieldDef docIdFieldDef = indexState.getField("doc_id");
212+
LoadedDocValues<?> result =
213+
(LoadedDocValues<?>) tryGetMethod.invoke(docLookup, "doc_id", docIdFieldDef);
204214
assertNull("Document without nested structure should return null", result);
205215
}
206216
} finally {

0 commit comments

Comments
 (0)