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-6744: fl renaming / alias of uniqueKey field generates null pointer exception in SolrCloud configuration
(Mike Drob via Tomás Fernández Löbbe)
Optimizations
----------------------

View File

@ -1305,6 +1305,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) {
@ -1330,7 +1334,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());

View File

@ -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<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. */
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 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<String,String> 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<String,String> getFieldRenames() {
return renameFields;
}
// like getId, but also accepts dashes for legacy fields
public static String getFieldName(StrParser sp) {
sp.eatws();

View File

@ -62,11 +62,18 @@ public class DistributedQueryComponentCustomSortTest extends BaseDistributedSear
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);
rsp = query("q", "text:a", "fl", "id", "sort", "payload desc", "rows", "20");
@ -77,6 +84,10 @@ public class DistributedQueryComponentCustomSortTest extends BaseDistributedSear
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");