|
65 | 65 |
|
66 | 66 | import java.io.IOException;
|
67 | 67 | import java.nio.CharBuffer;
|
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.Objects; |
76 |
| -import java.util.Set; |
| 68 | +import java.util.*; |
77 | 69 | import java.util.function.Supplier;
|
78 | 70 | import java.util.stream.Collectors;
|
79 | 71 | import java.util.stream.IntStream;
|
@@ -552,7 +544,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
|
552 | 544 |
|
553 | 545 | SearchResponse response = context.getClient().search(
|
554 | 546 | new SearchRequest(termsLookup.index())
|
555 |
| - .source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(false)) |
| 547 | + .source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(true)) |
556 | 548 | ).actionGet();
|
557 | 549 |
|
558 | 550 | System.out.println("Subquery Response: " + response);
|
@@ -653,23 +645,35 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
|
653 | 645 | try {
|
654 | 646 | response = shardContext.getClient().search(
|
655 | 647 | new SearchRequest(termsLookup.index())
|
656 |
| - .source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(false)) |
| 648 | + .source(new SearchSourceBuilder().query(rewrittenQuery).fetchSource(true)) |
657 | 649 | ).actionGet();
|
658 |
| - |
659 |
| - System.out.println("Extracted Terms: "); |
660 |
| - for (SearchHit hit : response.getHits().getHits()) { |
661 |
| - System.out.println(XContentMapValues.extractRawValues(termsLookup.path(), hit.getSourceAsMap())); |
662 |
| - } |
663 | 650 | } catch (Exception e) {
|
664 | 651 | throw new IllegalStateException("Failed to execute subquery: " + e.getMessage(), e);
|
665 | 652 | }
|
666 |
| - |
667 |
| - // Extract terms from the response |
| 653 | + // Extract terms from search hits |
668 | 654 | List<Object> terms = new ArrayList<>();
|
669 | 655 | for (SearchHit hit : response.getHits().getHits()) {
|
670 |
| - terms.addAll(XContentMapValues.extractRawValues(termsLookup.path(), hit.getSourceAsMap())); |
| 656 | + Map<String, Object> source = hit.getSourceAsMap(); |
| 657 | + if (source != null) { |
| 658 | + try { |
| 659 | + List<Object> extracted = XContentMapValues.extractRawValues(termsLookup.path(), source); |
| 660 | + terms.addAll(extracted); |
| 661 | + System.out.println("Extracted Terms: " + extracted); |
| 662 | + } catch (Exception ex) { |
| 663 | + System.err.println("Error extracting path '" + termsLookup.path() + "' from source: " + source); |
| 664 | + ex.printStackTrace(); |
| 665 | + } |
| 666 | + } else { |
| 667 | + System.err.println("Source is null for hit: " + hit); |
| 668 | + } |
671 | 669 | }
|
672 | 670 |
|
| 671 | +// // Extract terms from the response |
| 672 | +// List<Object> terms = new ArrayList<>(); |
| 673 | +// for (SearchHit hit : response.getHits().getHits()) { |
| 674 | +// terms.addAll(XContentMapValues.extractRawValues(termsLookup.path(), hit.getSourceAsMap())); |
| 675 | +// } |
| 676 | + |
673 | 677 | // Return a new TermsQueryBuilder with the fetched terms
|
674 | 678 | return new TermsQueryBuilder(fieldName, terms);
|
675 | 679 | }
|
|
0 commit comments