mirror of https://github.com/apache/lucene.git
SOLR-1367: simple callback for modifying the Document when using the SolrPluginUtils
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@805528 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c573d6ea8c
commit
f93b220b37
|
@ -267,6 +267,8 @@ New Features
|
||||||
HTMLStripStandardTokenizerFactory deprecated. To strip HTML tags, HTMLStripCharFilter can be used
|
HTMLStripStandardTokenizerFactory deprecated. To strip HTML tags, HTMLStripCharFilter can be used
|
||||||
with an arbitrary Tokenizer. (koji)
|
with an arbitrary Tokenizer. (koji)
|
||||||
|
|
||||||
|
68. SOLR-1367: Added callback mechanism for converting DocList to SolrDocumentList in SolrPluginUtils (gsingers)
|
||||||
|
|
||||||
|
|
||||||
Optimizations
|
Optimizations
|
||||||
----------------------
|
----------------------
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package org.apache.solr.util;
|
||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.solr.common.SolrDocument;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback capability for modifying a SolrDocument in the {@link SolrPluginUtils#docListToSolrDocumentList(org.apache.solr.search.DocList, org.apache.solr.search.SolrIndexSearcher, java.util.Set, java.util.Map)}
|
||||||
|
*
|
||||||
|
* <p/>
|
||||||
|
* NOTE: This API is subject to change.
|
||||||
|
* Due to https://issues.apache.org/jira/browse/SOLR-1298 and https://issues.apache.org/jira/browse/SOLR-705, this interface may change in the future.
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
public interface SolrDocumentModifier {
|
||||||
|
/**
|
||||||
|
* Implement this method to allow for changes to be made to the {@link org.apache.solr.common.SolrDocument} in the {@link SolrPluginUtils#docListToSolrDocumentList(org.apache.solr.search.DocList, org.apache.solr.search.SolrIndexSearcher, java.util.Set, SolrDocumentModifier, java.util.Map)}
|
||||||
|
* call.
|
||||||
|
* @param doc The {@link org.apache.solr.common.SolrDocument} that can be modified.
|
||||||
|
*/
|
||||||
|
void process(SolrDocument doc);
|
||||||
|
}
|
|
@ -887,26 +887,36 @@ public class SolrPluginUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a DocList to a SolrDocumentList
|
|
||||||
*
|
|
||||||
* The optional param "ids" is populated with the lucene document id
|
|
||||||
* for each SolrDocument.
|
|
||||||
*
|
|
||||||
* @param docs The {@link org.apache.solr.search.DocList} to convert
|
|
||||||
* @param searcher The {@link org.apache.solr.search.SolrIndexSearcher} to use to load the docs from the Lucene index
|
|
||||||
* @param fields The names of the Fields to load
|
|
||||||
* @param ids A map to store the ids of the docs
|
|
||||||
* @return The new {@link org.apache.solr.common.SolrDocumentList} containing all the loaded docs
|
|
||||||
* @throws java.io.IOException if there was a problem loading the docs
|
|
||||||
* @since solr 1.4
|
|
||||||
*/
|
|
||||||
public static SolrDocumentList docListToSolrDocumentList(
|
public static SolrDocumentList docListToSolrDocumentList(
|
||||||
DocList docs,
|
DocList docs,
|
||||||
SolrIndexSearcher searcher,
|
SolrIndexSearcher searcher,
|
||||||
Set<String> fields,
|
Set<String> fields,
|
||||||
Map<SolrDocument, Integer> ids ) throws IOException
|
Map<SolrDocument, Integer> ids ) throws IOException
|
||||||
{
|
{
|
||||||
|
return docListToSolrDocumentList(docs, searcher, fields, null, ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a DocList to a SolrDocumentList
|
||||||
|
*
|
||||||
|
* The optional param "ids" is populated with the lucene document id
|
||||||
|
* for each SolrDocument.
|
||||||
|
*
|
||||||
|
* @param docs The {@link org.apache.solr.search.DocList} to convert
|
||||||
|
* @param searcher The {@link org.apache.solr.search.SolrIndexSearcher} to use to load the docs from the Lucene index
|
||||||
|
* @param fields The names of the Fields to load
|
||||||
|
* @param docModifier The {@link SolrDocumentModifier}
|
||||||
|
* @param ids A map to store the ids of the docs
|
||||||
|
* @return The new {@link org.apache.solr.common.SolrDocumentList} containing all the loaded docs
|
||||||
|
* @throws java.io.IOException if there was a problem loading the docs
|
||||||
|
* @since solr 1.4
|
||||||
|
*/
|
||||||
|
public static SolrDocumentList docListToSolrDocumentList(
|
||||||
|
DocList docs,
|
||||||
|
SolrIndexSearcher searcher,
|
||||||
|
Set<String> fields, SolrDocumentModifier docModifier,
|
||||||
|
Map<SolrDocument, Integer> ids ) throws IOException{
|
||||||
DocumentBuilder db = new DocumentBuilder(searcher.getSchema());
|
DocumentBuilder db = new DocumentBuilder(searcher.getSchema());
|
||||||
SolrDocumentList list = new SolrDocumentList();
|
SolrDocumentList list = new SolrDocumentList();
|
||||||
list.setNumFound(docs.matches());
|
list.setNumFound(docs.matches());
|
||||||
|
@ -917,7 +927,7 @@ public class SolrPluginUtils {
|
||||||
|
|
||||||
while (dit.hasNext()) {
|
while (dit.hasNext()) {
|
||||||
int docid = dit.nextDoc();
|
int docid = dit.nextDoc();
|
||||||
|
|
||||||
Document luceneDoc = searcher.doc(docid, fields);
|
Document luceneDoc = searcher.doc(docid, fields);
|
||||||
SolrDocument doc = new SolrDocument();
|
SolrDocument doc = new SolrDocument();
|
||||||
db.loadStoredFields(doc, luceneDoc);
|
db.loadStoredFields(doc, luceneDoc);
|
||||||
|
@ -927,10 +937,14 @@ public class SolrPluginUtils {
|
||||||
if (docs.hasScores()) {
|
if (docs.hasScores()) {
|
||||||
doc.addField("score", dit.score());
|
doc.addField("score", dit.score());
|
||||||
} else {
|
} else {
|
||||||
doc.addField("score", 0.0f);
|
doc.addField("score", 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (docModifier != null) {
|
||||||
|
docModifier.process(doc);
|
||||||
}
|
}
|
||||||
list.add( doc );
|
list.add( doc );
|
||||||
|
|
||||||
if( ids != null ) {
|
if( ids != null ) {
|
||||||
ids.put( doc, new Integer(docid) );
|
ids.put( doc, new Integer(docid) );
|
||||||
}
|
}
|
||||||
|
@ -938,6 +952,7 @@ public class SolrPluginUtils {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a SolrQueryResponse replace the DocList if it is in the result.
|
* Given a SolrQueryResponse replace the DocList if it is in the result.
|
||||||
* Otherwise add it to the response
|
* Otherwise add it to the response
|
||||||
|
|
|
@ -19,6 +19,14 @@ package org.apache.solr.util;
|
||||||
|
|
||||||
import org.apache.solr.util.SolrPluginUtils;
|
import org.apache.solr.util.SolrPluginUtils;
|
||||||
import org.apache.solr.util.SolrPluginUtils.DisjunctionMaxQueryParser;
|
import org.apache.solr.util.SolrPluginUtils.DisjunctionMaxQueryParser;
|
||||||
|
import org.apache.solr.core.SolrCore;
|
||||||
|
import org.apache.solr.search.SolrIndexSearcher;
|
||||||
|
import org.apache.solr.search.DocList;
|
||||||
|
import org.apache.solr.search.DocSlice;
|
||||||
|
import org.apache.solr.request.SolrQueryResponse;
|
||||||
|
import org.apache.solr.common.util.*;
|
||||||
|
import org.apache.solr.common.SolrDocument;
|
||||||
|
import org.apache.solr.common.SolrDocumentList;
|
||||||
|
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
|
@ -27,12 +35,15 @@ import org.apache.lucene.search.PhraseQuery;
|
||||||
import org.apache.lucene.search.DisjunctionMaxQuery;
|
import org.apache.lucene.search.DisjunctionMaxQuery;
|
||||||
import org.apache.lucene.search.BooleanQuery;
|
import org.apache.lucene.search.BooleanQuery;
|
||||||
import org.apache.lucene.search.BooleanClause;
|
import org.apache.lucene.search.BooleanClause;
|
||||||
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
import org.apache.lucene.search.BooleanClause.Occur;
|
import org.apache.lucene.search.BooleanClause.Occur;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that the functions in SolrPluginUtils work as advertised.
|
* Tests that the functions in SolrPluginUtils work as advertised.
|
||||||
|
@ -42,6 +53,36 @@ public class SolrPluginUtilsTest extends AbstractSolrTestCase {
|
||||||
public String getSchemaFile() { return "schema.xml"; }
|
public String getSchemaFile() { return "schema.xml"; }
|
||||||
public String getSolrConfigFile() { return "solrconfig.xml"; }
|
public String getSolrConfigFile() { return "solrconfig.xml"; }
|
||||||
|
|
||||||
|
|
||||||
|
public void testDocModifier() 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();
|
||||||
|
SolrIndexSearcher srchr = h.getCore().getSearcher().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");
|
||||||
|
|
||||||
|
SolrDocumentModifier docMod = new SolrDocumentModifier() {
|
||||||
|
public void process(SolrDocument doc) {
|
||||||
|
doc.addField("junk", "foo");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SolrDocumentList list = SolrPluginUtils.docListToSolrDocumentList(docs, srchr, fields, docMod, null);
|
||||||
|
assertTrue("list Size: " + list.size() + " is not: " + docs.size(), list.size() == docs.size());
|
||||||
|
for (SolrDocument document : list) {
|
||||||
|
assertNotNull(document.get("junk"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void testPartialEscape() {
|
public void testPartialEscape() {
|
||||||
|
|
||||||
assertEquals("",pe(""));
|
assertEquals("",pe(""));
|
||||||
|
|
Loading…
Reference in New Issue