SOLR-6744: Consider uniqueKey rename when handling shard responses in distributed search

This commit is contained in:
Tomas Fernandez Lobbe 2016-08-26 16:10:48 -07:00
parent 6bff06ce4f
commit b3d12d265b
5 changed files with 38 additions and 3 deletions

View File

@ -76,6 +76,9 @@ Bug Fixes
* SOLR-9389: HDFS Transaction logs stay open for writes which leaks Xceivers. (Tim Owen via Mark Miller) * SOLR-9389: HDFS Transaction logs stay open for writes which leaks Xceivers. (Tim Owen via Mark Miller)
* 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 Optimizations
---------------------- ----------------------

View File

@ -1305,6 +1305,10 @@ public class QueryComponent extends SearchComponent
String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName(); String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName();
boolean removeKeyField = !rb.rsp.getReturnFields().wantsField(keyFieldName); 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) { for (ShardResponse srsp : sreq.responses) {
if (srsp.getException() != null) { if (srsp.getException() != null) {
@ -1330,7 +1334,6 @@ public class QueryComponent extends SearchComponent
continue; continue;
} }
SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response"); SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response");
for (SolrDocument doc : docs) { for (SolrDocument doc : docs) {
Object id = doc.getFieldValue(keyFieldName); Object id = doc.getFieldValue(keyFieldName);
ShardDoc sdoc = rb.resultIds.get(id.toString()); ShardDoc sdoc = rb.resultIds.get(id.toString());

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.solr.search; package org.apache.solr.search;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.solr.response.transform.DocTransformer; import org.apache.solr.response.transform.DocTransformer;
@ -53,6 +54,12 @@ public abstract class ReturnFields {
*/ */
public abstract Set<String> getRequestedFieldNames(); public abstract Set<String> getRequestedFieldNames();
/**
* Get the fields which have been renamed
* @return a mapping of renamed fields
*/
public abstract Map<String,String> getFieldRenames();
/** Returns <code>true</code> if the specified field should be returned. */ /** Returns <code>true</code> if the specified field should be returned. */
public abstract boolean wantsField(String name); public abstract boolean wantsField(String name);

View File

@ -35,9 +35,11 @@ import org.apache.solr.response.transform.TransformerFactory;
import org.apache.solr.response.transform.ValueSourceAugmenter; import org.apache.solr.response.transform.ValueSourceAugmenter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
@ -64,6 +66,7 @@ public class SolrReturnFields extends ReturnFields {
protected DocTransformer transformer; protected DocTransformer transformer;
protected boolean _wantsScore = false; protected boolean _wantsScore = false;
protected boolean _wantsAllFields = false; protected boolean _wantsAllFields = false;
protected Map<String,String> renameFields = Collections.emptyMap();
public SolrReturnFields() { public SolrReturnFields() {
_wantsAllFields = true; _wantsAllFields = true;
@ -129,6 +132,9 @@ public class SolrReturnFields extends ReturnFields {
} }
augmenters.addTransformer( new RenameFieldTransformer( from, to, copy ) ); augmenters.addTransformer( new RenameFieldTransformer( from, to, copy ) );
} }
if (rename.size() > 0 ) {
renameFields = rename.asShallowMap();
}
if( !_wantsAllFields && !globs.isEmpty() ) { if( !_wantsAllFields && !globs.isEmpty() ) {
// TODO??? need to fill up the fields with matching field names in the index // TODO??? need to fill up the fields with matching field names in the index
// and add them to okFieldNames? // and add them to okFieldNames?
@ -145,6 +151,11 @@ public class SolrReturnFields extends ReturnFields {
} }
} }
@Override
public Map<String,String> getFieldRenames() {
return renameFields;
}
// like getId, but also accepts dashes for legacy fields // like getId, but also accepts dashes for legacy fields
public static String getFieldName(StrParser sp) { public static String getFieldName(StrParser sp) {
sp.eatws(); sp.eatws();

View File

@ -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, "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 index(id, "13", "text", "d", "payload", ByteBuffer.wrap(new byte[] { (byte)0x80, 0x11, 0x33 })); // 12
commit(); commit();
QueryResponse rsp; QueryResponse rsp;
rsp = query("q", "*:*", "fl", "id", "sort", "payload asc", "rows", "20"); 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); 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"); 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); 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"); rsp = query("q", "text:a", "fl", "id", "sort", "payload asc", "rows", "20");
assertFieldValues(rsp.getResults(), id, 1, 3, 5, 9); assertFieldValues(rsp.getResults(), id, 1, 3, 5, 9);
@ -76,7 +83,11 @@ public class DistributedQueryComponentCustomSortTest extends BaseDistributedSear
assertFieldValues(rsp.getResults(), id, 4, 2, 10); assertFieldValues(rsp.getResults(), id, 4, 2, 10);
rsp = query("q", "text:b", "fl", "id", "sort", "payload desc", "rows", "20"); rsp = query("q", "text:b", "fl", "id", "sort", "payload desc", "rows", "20");
assertFieldValues(rsp.getResults(), id, 10, 2, 4); 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"); rsp = query("q", "text:c", "fl", "id", "sort", "payload asc", "rows", "20");
assertFieldValues(rsp.getResults(), id, 7, 6, 8); assertFieldValues(rsp.getResults(), id, 7, 6, 8);
rsp = query("q", "text:c", "fl", "id", "sort", "payload desc", "rows", "20"); rsp = query("q", "text:c", "fl", "id", "sort", "payload desc", "rows", "20");