diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index a0b112a7d07..2f17f7b307b 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -40,6 +40,9 @@ Bug Fixes * SOLR-9310: PeerSync fails on a node restart due to IndexFingerPrint mismatch (Pushkar Raste, noble) +* SOLR-6744: fl renaming / alias of uniqueKey field generates null pointer exception in SolrCloud configuration + (Mike Drob via Tomás Fernández Löbbe) + Optimizations ---------------------- diff --git a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java index 722241a8d06..fe4bcebaead 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java @@ -1306,6 +1306,10 @@ public class QueryComponent extends SearchComponent String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName(); boolean removeKeyField = !rb.rsp.getReturnFields().wantsField(keyFieldName); + if (rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName) != null) { + // if id was renamed we need to use the new name + keyFieldName = rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName); + } for (ShardResponse srsp : sreq.responses) { if (srsp.getException() != null) { @@ -1331,7 +1335,6 @@ public class QueryComponent extends SearchComponent continue; } SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response"); - for (SolrDocument doc : docs) { Object id = doc.getFieldValue(keyFieldName); ShardDoc sdoc = rb.resultIds.get(id.toString()); diff --git a/solr/core/src/java/org/apache/solr/search/ReturnFields.java b/solr/core/src/java/org/apache/solr/search/ReturnFields.java index dabfdd661ee..ec2b8787b75 100644 --- a/solr/core/src/java/org/apache/solr/search/ReturnFields.java +++ b/solr/core/src/java/org/apache/solr/search/ReturnFields.java @@ -16,6 +16,7 @@ */ package org.apache.solr.search; +import java.util.Map; import java.util.Set; import org.apache.solr.response.transform.DocTransformer; @@ -53,6 +54,12 @@ public abstract class ReturnFields { */ public abstract Set getRequestedFieldNames(); + /** + * Get the fields which have been renamed + * @return a mapping of renamed fields + */ + public abstract Map getFieldRenames(); + /** Returns true if the specified field should be returned. */ public abstract boolean wantsField(String name); diff --git a/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java b/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java index 6382f45583a..2b1b3038fda 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java +++ b/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java @@ -35,9 +35,11 @@ import org.apache.solr.response.transform.TransformerFactory; import org.apache.solr.response.transform.ValueSourceAugmenter; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -64,6 +66,7 @@ public class SolrReturnFields extends ReturnFields { protected DocTransformer transformer; protected boolean _wantsScore = false; protected boolean _wantsAllFields = false; + protected Map renameFields = Collections.emptyMap(); public SolrReturnFields() { _wantsAllFields = true; @@ -129,6 +132,9 @@ public class SolrReturnFields extends ReturnFields { } augmenters.addTransformer( new RenameFieldTransformer( from, to, copy ) ); } + if (rename.size() > 0 ) { + renameFields = rename.asShallowMap(); + } if( !_wantsAllFields && !globs.isEmpty() ) { // TODO??? need to fill up the fields with matching field names in the index // and add them to okFieldNames? @@ -145,6 +151,11 @@ public class SolrReturnFields extends ReturnFields { } } + @Override + public Map getFieldRenames() { + return renameFields; + } + // like getId, but also accepts dashes for legacy fields public static String getFieldName(StrParser sp) { sp.eatws(); diff --git a/solr/core/src/test/org/apache/solr/handler/component/DistributedQueryComponentCustomSortTest.java b/solr/core/src/test/org/apache/solr/handler/component/DistributedQueryComponentCustomSortTest.java index 9e9401b8d18..4b3e92a77ec 100644 --- a/solr/core/src/test/org/apache/solr/handler/component/DistributedQueryComponentCustomSortTest.java +++ b/solr/core/src/test/org/apache/solr/handler/component/DistributedQueryComponentCustomSortTest.java @@ -60,12 +60,19 @@ public class DistributedQueryComponentCustomSortTest extends BaseDistributedSear index(id, "12", "text", "d", "payload", ByteBuffer.wrap(new byte[] { 0x34, (byte)0xdd, 0x4d })); // 7 index(id, "13", "text", "d", "payload", ByteBuffer.wrap(new byte[] { (byte)0x80, 0x11, 0x33 })); // 12 commit(); - + QueryResponse rsp; + rsp = query("q", "*:*", "fl", "id", "sort", "payload asc", "rows", "20"); assertFieldValues(rsp.getResults(), id, 7, 1, 6, 4, 2, 10, 12, 3, 5, 9, 8, 13, 11); rsp = query("q", "*:*", "fl", "id", "sort", "payload desc", "rows", "20"); assertFieldValues(rsp.getResults(), id, 11, 13, 8, 9, 5, 3, 12, 10, 2, 4, 6, 1, 7); + + // SOLR-6744 + rsp = query("q", "*:*", "fl", "key:id", "sort", "payload asc", "rows", "20"); + assertFieldValues(rsp.getResults(), "key", 7, 1, 6, 4, 2, 10, 12, 3, 5, 9, 8, 13, 11); + rsp = query("q", "*:*", "fl", "key:id,id:text", "sort", "payload asc", "rows", "20"); + assertFieldValues(rsp.getResults(), "key", 7, 1, 6, 4, 2, 10, 12, 3, 5, 9, 8, 13, 11); rsp = query("q", "text:a", "fl", "id", "sort", "payload asc", "rows", "20"); assertFieldValues(rsp.getResults(), id, 1, 3, 5, 9); @@ -76,7 +83,11 @@ public class DistributedQueryComponentCustomSortTest extends BaseDistributedSear assertFieldValues(rsp.getResults(), id, 4, 2, 10); rsp = query("q", "text:b", "fl", "id", "sort", "payload desc", "rows", "20"); assertFieldValues(rsp.getResults(), id, 10, 2, 4); - + + // SOLR-6744 + rsp = query("q", "text:b", "fl", "key:id", "sort", "payload asc", "rows", "20"); + assertFieldValues(rsp.getResults(), id, null, null, null); + rsp = query("q", "text:c", "fl", "id", "sort", "payload asc", "rows", "20"); assertFieldValues(rsp.getResults(), id, 7, 6, 8); rsp = query("q", "text:c", "fl", "id", "sort", "payload desc", "rows", "20");