Skip to content

Commit 997ddbe

Browse files
Both id and query is working and basic unittests
1 parent 90f90c7 commit 997ddbe

File tree

3 files changed

+285
-145
lines changed

3 files changed

+285
-145
lines changed

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@
6565

6666
import java.io.IOException;
6767
import java.nio.CharBuffer;
68-
import java.util.*;
68+
import java.util.AbstractList;
69+
import java.util.ArrayList;
70+
import java.util.Arrays;
71+
import java.util.Base64;
72+
import java.util.Collections;
73+
import java.util.HashSet;
74+
import java.util.List;
75+
import java.util.Map;
76+
import java.util.Objects;
77+
import java.util.Set;
6978
import java.util.function.Supplier;
7079
import java.util.stream.Collectors;
7180
import java.util.stream.IntStream;
@@ -427,7 +436,6 @@ public static TermsQueryBuilder fromXContent(XContentParser parser) throws IOExc
427436
TermsLookup termsLookup = null;
428437
QueryBuilder nestedQuery = null;
429438

430-
431439
String queryName = null;
432440
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
433441

@@ -506,15 +514,9 @@ public static TermsQueryBuilder fromXContent(XContentParser parser) throws IOExc
506514
}
507515

508516
if (termsLookup != null) {
509-
System.out.println("TermsLookup Details:");
510-
System.out.println("Index: " + termsLookup.index());
511-
System.out.println("ID: " + termsLookup.id());
512-
System.out.println("Path: " + termsLookup.path());
513-
System.out.println("Query: " + termsLookup.query());
514517
termsLookup = new TermsLookup(termsLookup.index(), termsLookup.id(), termsLookup.path(), termsLookup.query());
515518
}
516519
return new TermsQueryBuilder(fieldName, values, termsLookup).boost(boost).queryName(queryName).valueType(valueType);
517-
//return new TermsQueryBuilder(fieldName, values, termsLookup).boost(boost).queryName(queryName).valueType(valueType);
518520
}
519521

520522
static List<Object> parseValues(XContentParser parser) throws IOException {
@@ -537,17 +539,16 @@ public String getWriteableName() {
537539
@Override
538540
protected Query doToQuery(QueryShardContext context) throws IOException {
539541
if (termsLookup != null && termsLookup.query() != null) {
540-
System.out.println("Executing subquery: " + termsLookup.query());
542+
// System.out.println("Executing subquery: " + termsLookup.query());
541543
QueryBuilder rewrittenQuery = termsLookup.query().rewrite(context);
542-
//Query query = rewrittenQuery.toQuery(context);
543-
//QueryBuilder query = termsLookup.query().rewrite(context);
544+
// Query query = rewrittenQuery.toQuery(context);
545+
// QueryBuilder query = termsLookup.query().rewrite(context);
544546

545-
SearchResponse response = context.getClient().search(
546-
new SearchRequest(termsLookup.index())
547-
.source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(true))
548-
).actionGet();
547+
SearchResponse response = context.getClient()
548+
.search(new SearchRequest(termsLookup.index()).source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(true)))
549+
.actionGet();
549550

550-
System.out.println("Subquery Response: " + response);
551+
// System.out.println("Subquery Response: " + response);
551552

552553
List<Object> terms = new ArrayList<>();
553554
for (SearchHit hit : response.getHits().getHits()) {
@@ -637,14 +638,15 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
637638
}
638639
// Rewrite the subquery using the shard context
639640
QueryBuilder rewrittenQuery = termsLookup.query().rewrite(shardContext);
640-
System.out.println("Rewritten Query: " + rewrittenQuery);
641+
// System.out.println("Rewritten Query: " + rewrittenQuery);
641642

642643
SearchResponse response;
643644
try {
644-
response = shardContext.getClient().search(
645-
new SearchRequest(termsLookup.index())
646-
.source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(true))
647-
).actionGet();
645+
response = shardContext.getClient()
646+
.search(
647+
new SearchRequest(termsLookup.index()).source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(true))
648+
)
649+
.actionGet();
648650
} catch (Exception e) {
649651
throw new IllegalStateException("Failed to execute subquery: " + e.getMessage(), e);
650652
}
@@ -656,13 +658,14 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
656658
try {
657659
List<Object> extracted = XContentMapValues.extractRawValues(termsLookup.path(), source);
658660
terms.addAll(extracted);
659-
System.out.println("Extracted Terms: " + extracted);
661+
// System.out.println("Extracted Terms: " + extracted);
660662
} catch (Exception ex) {
661-
System.err.println("Error extracting path '" + termsLookup.path() + "' from source: " + source);
662-
ex.printStackTrace();
663+
// System.err.println("Error extracting path '" + termsLookup.path() + "' from source: " + source);
664+
throw new IllegalStateException("Failed to execute subquery: " + ex.getMessage(), ex);
663665
}
664666
} else {
665-
System.err.println("Source is null for hit: " + hit);
667+
// System.err.println("Source is null for hit: " + hit);
668+
throw new IllegalStateException("Source is null for hit: " + hit);
666669
}
667670
}
668671
// Return a new TermsQueryBuilder with the fetched terms
@@ -705,6 +708,6 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
705708
}
706709

707710
return this;
708-
//return super.doRewrite(queryRewriteContext);
711+
// return super.doRewrite(queryRewriteContext);
709712
}
710713
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class TermsLookup implements Writeable, ToXContentFragment {
6969
public TermsLookup(String index, String id, String path) {
7070
this(index, id, path, null); // Delegate to the existing constructor
7171
}
72+
7273
public TermsLookup(String index, String id, String path, QueryBuilder query) {
7374
if (Strings.isEmpty(index)) {
7475
throw new IllegalArgumentException("index cannot be null or empty for TermsLookup");
@@ -77,7 +78,9 @@ public TermsLookup(String index, String id, String path, QueryBuilder query) {
7778
throw new IllegalArgumentException("path cannot be null or empty for TermsLookup");
7879
}
7980
if (id == null && query == null) {
80-
throw new IllegalArgumentException("[" + TermsQueryBuilder.NAME + "] query lookup element requires specifying either the id or the query.");
81+
throw new IllegalArgumentException(
82+
"[" + TermsQueryBuilder.NAME + "] query lookup element requires specifying either the id or the query."
83+
);
8184
}
8285
if (id != null && query != null) {
8386
throw new IllegalArgumentException("[" + TermsQueryBuilder.NAME + "] query lookup element cannot specify both id and query.");
@@ -210,7 +213,7 @@ public String toString() {
210213
@Override
211214
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
212215
builder.field("index", index);
213-
//builder.field("id", id);
216+
// builder.field("id", id);
214217
if (id != null) {
215218
builder.field("id", id);
216219
}

0 commit comments

Comments
 (0)