SOLR-1032: add literal.field_name=val support to CSV update handler

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1153166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2011-08-02 15:33:52 +00:00
parent cffd01664b
commit 89bceaaaf0
3 changed files with 42 additions and 2 deletions

View File

@ -161,6 +161,9 @@ New Features
* SOLR-2399: Updated Solr Admin interface. New look and feel with per core administration
and many new options. (Stefan Matheis via ryan)
* SOLR-1032: CSV handler now supports "literal.field_name=value" parameters.
(Simon Rosenthal, ehatcher)
Optimizations
----------------------

View File

@ -34,13 +34,18 @@ import org.apache.commons.io.IOUtils;
import java.util.regex.Pattern;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.io.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class CSVRequestHandler extends ContentStreamHandlerBase {
public static Logger log = LoggerFactory.getLogger(CSVRequestHandler.class);
@Override
protected ContentStreamLoader newLoader(SolrQueryRequest req, UpdateRequestProcessor processor) {
@ -83,16 +88,20 @@ abstract class CSVLoader extends ContentStreamLoader {
public static final String ENCAPSULATOR="encapsulator";
public static final String ESCAPE="escape";
public static final String OVERWRITE="overwrite";
public static final String LITERALS_PREFIX = "literal.";
private static Pattern colonSplit = Pattern.compile(":");
private static Pattern commaSplit = Pattern.compile(",");
public static Logger log = LoggerFactory.getLogger(CSVRequestHandler.class);
final IndexSchema schema;
final SolrParams params;
final CSVStrategy strategy;
final UpdateRequestProcessor processor;
// hashmap to save any literal fields and their values
HashMap <SchemaField, String> literals;
String[] fieldnames;
SchemaField[] fields;
CSVLoader.FieldAdder[] adders;
@ -189,6 +198,7 @@ abstract class CSVLoader extends ContentStreamLoader {
this.processor = processor;
this.params = req.getParams();
schema = req.getSchema();
this.literals = new HashMap<SchemaField, String>();
templateAdd = new AddUpdateCommand(req);
templateAdd.overwrite=params.getBool(OVERWRITE,true);
@ -289,7 +299,7 @@ abstract class CSVLoader extends ContentStreamLoader {
adders[i] = new CSVLoader.FieldMapperSingle(mapArgs[0], mapArgs[1], adders[i]);
}
}
if (params.getFieldBool(fname,TRIM,false)) {
adders[i] = new CSVLoader.FieldTrimmer(adders[i]);
}
@ -306,6 +316,18 @@ abstract class CSVLoader extends ContentStreamLoader {
adders[i] = new CSVLoader.FieldSplitter(fstrat, adders[i]);
}
}
// look for any literal fields - literal.foo=xyzzy
Iterator<String> paramNames = params.getParameterNamesIterator();
while (paramNames.hasNext()) {
String pname = paramNames.next();
if (!pname.startsWith(LITERALS_PREFIX)) continue;
String name = pname.substring(LITERALS_PREFIX.length());
SchemaField sf = schema.getFieldOrNull(name);
if(sf == null)
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Invalid field name for literal:'"+ name +"'");
literals.put(sf, params.get(pname));
}
}
private void input_err(String msg, String[] line, int lineno) {
@ -397,6 +419,13 @@ abstract class CSVLoader extends ContentStreamLoader {
adders[i].add(doc, line, i, val);
}
// add any literals
for (SchemaField sf : literals.keySet()) {
String fn = sf.getName();
String val = literals.get(sf);
doc.addField(fn, val);
}
template.solrDoc = doc;
processor.processAdd(template);
}

View File

@ -120,6 +120,14 @@ public class TestCSVLoader extends SolrTestCaseJ4 {
assertQ(req("id:[100 TO 110]"),"//*[@numFound='3']");
}
@Test
public void testLiteral() throws Exception {
makeFile("id\n100");
loadLocal("stream.file",filename,"commit","true", "literal.name","LITERAL_VALUE");
assertQ(req("*:*"),"//doc/str[@name='name'][.='LITERAL_VALUE']");
}
@Test
public void testCSV() throws Exception {
lrf.args.put(CommonParams.VERSION,"2.2");