Skip to content

Apache Lucene open-source search software

SOLR-3074: fix SolrPluginUtils.docListToSolrDocumentList

Chris M. Hostettera78109cdf23c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a420258abf7..48349f6facf 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -716,6 +716,12 @@ Bug Fixes
* SOLR-2959: edismax now respects the magic fields '_val_' and '_query_'
(Michael Watts, hossman)
+* SOLR-3074: fix SolrPluginUtils.docListToSolrDocumentList to respect the
+ list of fields specified. This fix also deprecates
+ DocumentBuilder.loadStoredFields which is not used anywhere in Solr,
+ and was fundamentally broken/bizarre.
+ (hossman, Ahmet Arslan)
+
Other Changes
----------------------
* SOLR-2922: Upgrade commons-io and commons-lang to 2.1 and 2.6, respectively. (koji)
diff --git a/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java b/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java
index 91bac620ad2..06e8b540779 100644
--- a/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java
+++ b/solr/core/src/java/org/apache/solr/update/DocumentBuilder.java
@@ -324,28 +324,4 @@ public class DocumentBuilder {
}
return out;
}
-
-
- /**
- * Add fields from the solr document
- *
- * TODO: /!\ NOTE /!\ This semantics of this function are still in flux.
- * Something somewhere needs to be able to fill up a SolrDocument from
- * a lucene document - this is one place that may happen. It may also be
- * moved to an independent function
- *
- * @since solr 1.3
- */
- public SolrDocument loadStoredFields( SolrDocument doc, Document luceneDoc )
- {
- for( IndexableField field : luceneDoc) {
- if( field.fieldType().stored() ) {
- SchemaField sf = schema.getField( field.name() );
- if( !schema.isCopyFieldTarget( sf ) ) {
- doc.addField( field.name(), sf.getType().toObject( field ) );
- }
- }
- }
- return doc;
- }
}
diff --git a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java
index c91afaa7cfa..d204d181072 100644
--- a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java
+++ b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java
@@ -18,6 +18,7 @@
package org.apache.solr.util;
import org.apache.lucene.document.Document;
+import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
@@ -789,7 +790,8 @@ public class SolrPluginUtils {
Set<String> fields,
Map<SolrDocument, Integer> ids ) throws IOException
{
- DocumentBuilder db = new DocumentBuilder(searcher.getSchema());
+ IndexSchema schema = searcher.getSchema();
+
SolrDocumentList list = new SolrDocumentList();
list.setNumFound(docs.matches());
list.setMaxScore(docs.maxScore());
@@ -802,14 +804,15 @@ public class SolrPluginUtils {
Document luceneDoc = searcher.doc(docid, fields);
SolrDocument doc = new SolrDocument();
- db.loadStoredFields(doc, luceneDoc);
-
- // this may be removed if XMLWriter gets patched to
- // include score from doc iterator in solrdoclist
- if (docs.hasScores()) {
+
+ for( IndexableField field : luceneDoc) {
+ if (null == fields || fields.contains(field.name())) {
+ SchemaField sf = schema.getField( field.name() );
+ doc.addField( field.name(), sf.getType().toObject( field ) );
+ }
+ }
+ if (docs.hasScores() && (null == fields || fields.contains("score"))) {
doc.addField("score", dit.score());
- } else {
- doc.addField("score", 0.0f);
}
list.add( doc );
diff --git a/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java b/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java
index b824d55dbfd..6be5f37947f 100644
--- a/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java
+++ b/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java
@@ -58,28 +58,48 @@ public class SolrPluginUtilsTest extends SolrTestCaseJ4 {
@Test
public void testDocListConversion() throws Exception {
- assertU("", adoc("id", "3234", "val_t", "quick red fox"));
- assertU("", adoc("id", "3235", "val_t", "quick green fox"));
- assertU("", adoc("id", "3236", "val_t", "quick brown fox"));
- commit();
- RefCounted<SolrIndexSearcher> holder = h.getCore().getSearcher();
- SolrIndexSearcher srchr = holder.get();
- SolrIndexSearcher.QueryResult qr = new SolrIndexSearcher.QueryResult();
- SolrIndexSearcher.QueryCommand cmd = new SolrIndexSearcher.QueryCommand();
- cmd.setQuery(new MatchAllDocsQuery());
- qr = srchr.search(qr, cmd);
-
- DocList docs = qr.getDocList();
- Set<String> fields = new HashSet<String>();
- fields.add("val_t");
+ assertU("", adoc("id", "3234", "val_i", "1",
+ "val_dynamic", "quick red fox"));
+ assertU("", adoc("id", "3235", "val_i", "1",
+ "val_dynamic", "quick green fox"));
+ assertU("", adoc("id", "3236", "val_i", "1",
+ "val_dynamic", "quick brown fox"));
+ assertU("", commit());
+ RefCounted<SolrIndexSearcher> holder = h.getCore().getSearcher();
+ try {
+ SolrIndexSearcher srchr = holder.get();
+ SolrIndexSearcher.QueryResult qr = new SolrIndexSearcher.QueryResult();
+ SolrIndexSearcher.QueryCommand cmd = new SolrIndexSearcher.QueryCommand();
+ cmd.setQuery(new MatchAllDocsQuery());
+ cmd.setLen(10);
+ qr = srchr.search(qr, cmd);
+
+ DocList docs = qr.getDocList();
+ assertEquals("wrong docs size", 3, docs.size());
+ Set<String> fields = new HashSet<String>();
+ fields.add("val_dynamic");
+ fields.add("dynamic_val");
+ fields.add("range_facet_l"); // copied from id
+
+ SolrDocumentList list = SolrPluginUtils.docListToSolrDocumentList(docs, srchr, fields, null);
+ assertEquals("wrong list Size", docs.size(), list.size());
+ for (SolrDocument document : list) {
+
+ assertTrue("unexpected field", ! document.containsKey("val_i"));
+ assertTrue("unexpected id field", ! document.containsKey("id"));
- SolrDocumentList list = SolrPluginUtils.docListToSolrDocumentList(docs, srchr, fields, null);
- assertTrue("list Size: " + list.size() + " is not: " + docs.size(), list.size() == docs.size());
- for (SolrDocument document : list) {
- assertNotNull(document.get("val_t"));
+ assertTrue("original field", document.containsKey("val_dynamic"));
+ assertTrue("dyn copy field", document.containsKey("dynamic_val"));
+ assertTrue("copy field", document.containsKey("range_facet_l"));
+
+ assertNotNull("original field null", document.get("val_dynamic"));
+ assertNotNull("dyn copy field null", document.get("dynamic_val"));
+ assertNotNull("copy field null", document.get("range_facet_l"));
+ }
+ } finally {
+ if (null != holder) holder.decref();
}
- holder.decref();
}
@Test