Skip to content

Commit edfa605

Browse files
Address the PR comments
1 parent 085edf2 commit edfa605

File tree

2 files changed

+9
-52
lines changed

2 files changed

+9
-52
lines changed

server/src/main/java/org/opensearch/index/query/TermsQueryBuilder.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -470,42 +470,7 @@ public static TermsQueryBuilder fromXContent(XContentParser parser) throws IOExc
470470
);
471471
}
472472
fieldName = currentFieldName;
473-
// Parse the nested object for termsLookup or query
474-
String index = null;
475-
String id = null;
476-
String path = null;
477-
478-
479-
XContentParser.Token innerToken;
480-
String innerFieldName = null;
481-
while ((innerToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
482-
if (innerToken == XContentParser.Token.FIELD_NAME) {
483-
innerFieldName = parser.currentName();
484-
} else if (innerToken.isValue()) {
485-
if ("index".equals(innerFieldName)) {
486-
index = parser.text();
487-
} else if ("id".equals(innerFieldName)) {
488-
id = parser.text();
489-
} else if ("path".equals(innerFieldName)) {
490-
path = parser.text();
491-
} else {
492-
throw new ParsingException(
493-
parser.getTokenLocation(),
494-
"[" + TermsQueryBuilder.NAME + "] query does not support [" + innerFieldName + "]"
495-
);
496-
}
497-
} else if (innerToken == XContentParser.Token.START_OBJECT && "query".equals(innerFieldName)) {
498-
nestedQuery = AbstractQueryBuilder.parseInnerQueryBuilder(parser);
499-
}
500-
}
501-
502-
if (index != null || id != null || path != null || nestedQuery != null) {
503-
if (path == null) {
504-
throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] missing required field [path]");
505-
}
506-
termsLookup = new TermsLookup(index, id, path, nestedQuery);
507-
}
508-
//termsLookup = TermsLookup.parseTermsLookup(parser);
473+
termsLookup = TermsLookup.parseTermsLookup(parser);
509474
} else if (token.isValue()) {
510475
if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
511476
boost = parser.floatValue();

server/src/main/java/org/opensearch/indices/TermsLookup.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class TermsLookup implements Writeable, ToXContentFragment {
6666
private String routing;
6767
private QueryBuilder query;
6868

69+
public TermsLookup(String index, String id, String path) {
70+
this(index, id, path, null); // Delegate to the existing constructor
71+
}
6972
public TermsLookup(String index, String id, String path, QueryBuilder query) {
7073
if (Strings.isEmpty(index)) {
7174
throw new IllegalArgumentException("index cannot be null or empty for TermsLookup");
@@ -108,8 +111,10 @@ public TermsLookup(StreamInput in) throws IOException {
108111
if (in.getVersion().onOrAfter(Version.V_2_17_0)) {
109112
store = in.readBoolean();
110113
}
111-
if (in.readBoolean()) {
112-
query = in.readOptionalWriteable(inStream -> inStream.readNamedWriteable(QueryBuilder.class));
114+
if (in.getVersion().onOrAfter(Version.V_3_0_0)) {
115+
if (in.readBoolean()) {
116+
query = in.readOptionalWriteable(inStream -> inStream.readNamedWriteable(QueryBuilder.class));
117+
}
113118
}
114119
}
115120

@@ -125,12 +130,7 @@ public void writeTo(StreamOutput out) throws IOException {
125130
if (out.getVersion().onOrAfter(Version.V_2_17_0)) {
126131
out.writeBoolean(store);
127132
}
128-
if (query != null) {
129-
out.writeBoolean(true);
130-
out.writeOptionalWriteable(query);
131-
} else {
132-
out.writeBoolean(false);
133-
}
133+
out.writeOptionalWriteable(query);
134134
}
135135

136136
public String path() {
@@ -181,18 +181,10 @@ public TermsLookup id(String id) {
181181
String path = (String) args[2];
182182
QueryBuilder query = (QueryBuilder) args[3]; // Optional query
183183

184-
// Ensure either `id` or `query` is provided, but not both
185-
if (id == null && query == null) {
186-
throw new IllegalArgumentException("[" + TermsQueryBuilder.NAME + "] query lookup element requires specifying either the id or the query.");
187-
}
188-
if (id != null && query != null) {
189-
throw new IllegalArgumentException("[" + TermsQueryBuilder.NAME + "] query lookup element cannot specify both id and query.");
190-
}
191184
return new TermsLookup(index, id, path, query);
192185
});
193186
static {
194187
PARSER.declareString(constructorArg(), new ParseField("index")); // Required
195-
//PARSER.declareString(constructorArg(), new ParseField("id")); // Optional
196188
PARSER.declareString(optionalConstructorArg(), new ParseField("id")); // Optional
197189
PARSER.declareString(constructorArg(), new ParseField("path")); // Required
198190
PARSER.declareObject(optionalConstructorArg(), (parser, context) -> {

0 commit comments

Comments
 (0)