Skip to content

Commit 7f83a94

Browse files
committed
Allow updating child fields
1 parent f1313ea commit 7f83a94

40 files changed

+1609
-63
lines changed

src/main/java/com/yelp/nrtsearch/server/field/AtomFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,24 @@ public class AtomFieldDef extends TextBaseFieldDef
4343

4444
public AtomFieldDef(
4545
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
46-
super(name, requestField, context);
46+
this(name, requestField, context, null);
47+
}
48+
49+
/**
50+
* Constructor for creating an instance of this field based on a previous instance. This is used
51+
* when updating field properties.
52+
*
53+
* @param name name of the field
54+
* @param requestField the field definition from the request
55+
* @param context context for creating the field definition
56+
* @param previousField the previous instance of this field definition, or null if there is none
57+
*/
58+
protected AtomFieldDef(
59+
String name,
60+
Field requestField,
61+
FieldDefCreator.FieldDefCreatorContext context,
62+
AtomFieldDef previousField) {
63+
super(name, requestField, context, previousField);
4764
}
4865

4966
@Override
@@ -59,6 +76,12 @@ public String getType() {
5976
return "ATOM";
6077
}
6178

79+
@Override
80+
public FieldDef createUpdatedFieldDef(
81+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
82+
return new AtomFieldDef(name, requestField, context, this);
83+
}
84+
6285
@Override
6386
public Object parseLastValue(String value) {
6487
return new BytesRef(value);

src/main/java/com/yelp/nrtsearch/server/field/BooleanFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,24 @@
3939
public class BooleanFieldDef extends IndexableFieldDef<Boolean> implements TermQueryable {
4040
protected BooleanFieldDef(
4141
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
42-
super(name, requestField, context, Boolean.class);
42+
this(name, requestField, context, null);
43+
}
44+
45+
/**
46+
* Constructor for creating an instance of this field based on a previous instance. This is used
47+
* when updating field properties.
48+
*
49+
* @param name name of the field
50+
* @param requestField the field definition from the request
51+
* @param context context for creating the field definition
52+
* @param previousField the previous instance of this field definition, or null if there is none
53+
*/
54+
protected BooleanFieldDef(
55+
String name,
56+
Field requestField,
57+
FieldDefCreator.FieldDefCreatorContext context,
58+
BooleanFieldDef previousField) {
59+
super(name, requestField, context, Boolean.class, previousField);
4360
}
4461

4562
@Override
@@ -138,6 +155,12 @@ public String getType() {
138155
return "BOOLEAN";
139156
}
140157

158+
@Override
159+
public FieldDef createUpdatedFieldDef(
160+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
161+
return new BooleanFieldDef(name, requestField, context, this);
162+
}
163+
141164
@Override
142165
public Query getTermQueryFromBooleanValue(boolean booleanValue) {
143166
verifySearchable("Term query");

src/main/java/com/yelp/nrtsearch/server/field/ContextSuggestFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,24 @@ public class ContextSuggestFieldDef extends IndexableFieldDef<Void> {
4141
*/
4242
protected ContextSuggestFieldDef(
4343
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
44-
super(name, requestField, context, Void.class);
44+
this(name, requestField, context, null);
45+
}
46+
47+
/**
48+
* Constructor for creating an instance of this field based on a previous instance. This is used
49+
* when updating field properties.
50+
*
51+
* @param name name of the field
52+
* @param requestField the field definition from the request
53+
* @param context context for creating the field definition
54+
* @param previousField the previous instance of this field definition, or null if there is none
55+
*/
56+
protected ContextSuggestFieldDef(
57+
String name,
58+
Field requestField,
59+
FieldDefCreator.FieldDefCreatorContext context,
60+
ContextSuggestFieldDef previousField) {
61+
super(name, requestField, context, Void.class, previousField);
4562
this.indexAnalyzer = this.parseIndexAnalyzer(requestField);
4663
this.searchAnalyzer = this.parseSearchAnalyzer(requestField);
4764
this.postingsFormat =
@@ -64,6 +81,12 @@ public String getType() {
6481
return "CONTEXT_SUGGEST";
6582
}
6683

84+
@Override
85+
public FieldDef createUpdatedFieldDef(
86+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
87+
return new ContextSuggestFieldDef(name, requestField, context, this);
88+
}
89+
6790
@Override
6891
public void parseDocumentField(
6992
Document document, List<String> fieldValues, List<List<String>> facetHierarchyPaths) {

src/main/java/com/yelp/nrtsearch/server/field/DateTimeFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,24 @@ private static DateTimeFormatter createDateTimeFormatter(String dateTimeFormat)
8282

8383
public DateTimeFieldDef(
8484
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
85-
super(name, requestField, context, Instant.class);
85+
this(name, requestField, context, null);
86+
}
87+
88+
/**
89+
* Constructor for creating an instance of this field based on a previous instance. This is used
90+
* when updating field properties.
91+
*
92+
* @param name name of the field
93+
* @param requestField the field definition from the request
94+
* @param context context for creating the field definition
95+
* @param previousField the previous instance of this field definition, or null if there is none
96+
*/
97+
protected DateTimeFieldDef(
98+
String name,
99+
Field requestField,
100+
FieldDefCreator.FieldDefCreatorContext context,
101+
DateTimeFieldDef previousField) {
102+
super(name, requestField, context, Instant.class, previousField);
86103
dateTimeFormat = requestField.getDateTimeFormat();
87104
dateTimeFormatter = createDateTimeFormatter(dateTimeFormat);
88105
}
@@ -369,6 +386,12 @@ public String getType() {
369386
return "DATE_TIME";
370387
}
371388

389+
@Override
390+
public FieldDef createUpdatedFieldDef(
391+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
392+
return new DateTimeFieldDef(name, requestField, context, this);
393+
}
394+
372395
/**
373396
* Get the format used to parse date time string.
374397
*

src/main/java/com/yelp/nrtsearch/server/field/DoubleFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,24 @@ public class DoubleFieldDef extends NumberFieldDef<Double> {
3939

4040
public DoubleFieldDef(
4141
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
42-
super(name, requestField, DOUBLE_PARSER, context, Double.class);
42+
this(name, requestField, context, null);
43+
}
44+
45+
/**
46+
* Constructor for creating an instance of this field based on a previous instance. This is used
47+
* when updating field properties.
48+
*
49+
* @param name name of the field
50+
* @param requestField the field definition from the request
51+
* @param context context for creating the field definition
52+
* @param previousField the previous instance of this field definition, or null if there is none
53+
*/
54+
protected DoubleFieldDef(
55+
String name,
56+
Field requestField,
57+
FieldDefCreator.FieldDefCreatorContext context,
58+
DoubleFieldDef previousField) {
59+
super(name, requestField, DOUBLE_PARSER, context, Double.class, previousField);
4360
}
4461

4562
@Override
@@ -101,6 +118,12 @@ public String getType() {
101118
return "DOUBLE";
102119
}
103120

121+
@Override
122+
public FieldDef createUpdatedFieldDef(
123+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
124+
return new DoubleFieldDef(name, requestField, context, this);
125+
}
126+
104127
@Override
105128
public Query getRangeQuery(RangeQuery rangeQuery) {
106129
verifySearchableOrDocValues("Range query");

src/main/java/com/yelp/nrtsearch/server/field/FieldDef.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.yelp.nrtsearch.server.field;
1717

18+
import com.yelp.nrtsearch.server.grpc.Field;
1819
import java.io.Closeable;
1920

2021
/** Base class for all field definition types. */
@@ -35,6 +36,22 @@ public String getName() {
3536
return name;
3637
}
3738

39+
/**
40+
* Create a new instance of the current {@link FieldDef} that is updated to use the provided
41+
* request field definition. This method must not modify the current instance, but instead return
42+
* a new instance with the updated properties.
43+
*
44+
* @param name name of the field
45+
* @param requestField the field definition from the request
46+
* @param context context for creating the field definition
47+
* @return a new instance of the field definition with updated properties
48+
*/
49+
public FieldDef createUpdatedFieldDef(
50+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
51+
throw new UnsupportedOperationException(
52+
String.format("Field %s does not support update", this.getName()));
53+
}
54+
3855
/** Get String representation of the field type. */
3956
public abstract String getType();
4057

src/main/java/com/yelp/nrtsearch/server/field/FieldDefCreator.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ public FieldDef createFieldDef(String name, Field field, FieldDefCreatorContext
9797
return provider.get(name, field, context);
9898
}
9999

100+
/**
101+
* Create a new {@link FieldDef} instance based on a previous {@link FieldDef}. This is typically
102+
* used when updating a field definition in the index.
103+
*
104+
* @param name name of the field
105+
* @param field grpc request field definition
106+
* @param previousFieldDef previous field definition, or null if there is none
107+
* @param context context for creating the field definition
108+
* @return new field definition instance
109+
*/
110+
public FieldDef createFieldDefFromPrevious(
111+
String name, Field field, FieldDef previousFieldDef, FieldDefCreatorContext context) {
112+
if (previousFieldDef == null) {
113+
return createFieldDef(name, field, context);
114+
}
115+
FieldDef updatedFieldDef = previousFieldDef.createUpdatedFieldDef(name, field, context);
116+
if (updatedFieldDef == null) {
117+
throw new IllegalArgumentException(
118+
"FieldDef " + previousFieldDef.getName() + " cannot be updated");
119+
}
120+
return updatedFieldDef;
121+
}
122+
100123
/**
101124
* Create a new {@link FieldDefCreatorContext} instance.
102125
*

src/main/java/com/yelp/nrtsearch/server/field/FloatFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,24 @@ public class FloatFieldDef extends NumberFieldDef<Float> {
3939

4040
public FloatFieldDef(
4141
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
42-
super(name, requestField, FLOAT_PARSER, context, Float.class);
42+
this(name, requestField, context, null);
43+
}
44+
45+
/**
46+
* Constructor for creating an instance of this field based on a previous instance. This is used
47+
* when updating field properties.
48+
*
49+
* @param name name of the field
50+
* @param requestField the field definition from the request
51+
* @param context context for creating the field definition
52+
* @param previousField the previous instance of this field definition, or null if there is none
53+
*/
54+
protected FloatFieldDef(
55+
String name,
56+
Field requestField,
57+
FieldDefCreator.FieldDefCreatorContext context,
58+
FloatFieldDef previousField) {
59+
super(name, requestField, FLOAT_PARSER, context, Float.class, previousField);
4360
}
4461

4562
@Override
@@ -99,6 +116,12 @@ public String getType() {
99116
return "FLOAT";
100117
}
101118

119+
@Override
120+
public FieldDef createUpdatedFieldDef(
121+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
122+
return new FloatFieldDef(name, requestField, context, this);
123+
}
124+
102125
@Override
103126
public Query getRangeQuery(RangeQuery rangeQuery) {
104127
verifySearchableOrDocValues("Range query");

src/main/java/com/yelp/nrtsearch/server/field/IdFieldDef.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,24 @@ public class IdFieldDef extends IndexableFieldDef<String> implements TermQueryab
4343

4444
protected IdFieldDef(
4545
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
46-
super(name, requestField, context, String.class);
46+
this(name, requestField, context, null);
47+
}
48+
49+
/**
50+
* Constructor for creating an instance of this field based on a previous instance. This is used
51+
* when updating field properties.
52+
*
53+
* @param name name of the field
54+
* @param requestField the field definition from the request
55+
* @param context context for creating the field definition
56+
* @param previousField the previous instance of this field definition, or null if there is none
57+
*/
58+
protected IdFieldDef(
59+
String name,
60+
Field requestField,
61+
FieldDefCreator.FieldDefCreatorContext context,
62+
IdFieldDef previousField) {
63+
super(name, requestField, context, String.class, previousField);
4764
}
4865

4966
/**
@@ -142,6 +159,12 @@ public String getType() {
142159
return "_ID";
143160
}
144161

162+
@Override
163+
public FieldDef createUpdatedFieldDef(
164+
String name, Field requestField, FieldDefCreator.FieldDefCreatorContext context) {
165+
return new IdFieldDef(name, requestField, context, this);
166+
}
167+
145168
/**
146169
* Construct a Term with the given field and value to identify the document to be added or updated
147170
*

0 commit comments

Comments
 (0)