SOLR-1127 -- Add support for field name to be templatized

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@769751 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-04-29 11:23:28 +00:00
parent 3dc6f5e2f2
commit 9f5b8ad035
5 changed files with 44 additions and 5 deletions

View File

@ -136,6 +136,9 @@ New Features
for processing with transformers and child entities.
(Fergus McMenemie, Noble Paul, shalin)
31.SOLR-1127: Add support for field name to be templatized.
(Noble Paul, shalin)
Optimizations
----------------------
1. SOLR-846: Reduce memory consumption during delta import by removing keys when used

View File

@ -190,6 +190,9 @@ public class DataConfig {
public boolean multiValued = false;
boolean dynamicName;
public Map<String, String> allAttributes = new HashMap<String, String>() {
public String put(String key, String value) {
if (super.containsKey(key))

View File

@ -181,6 +181,10 @@ public class DataImporter {
if (e.fields != null) {
for (DataConfig.Field f : e.fields) {
if (schema != null) {
if(f.name != null && f.name.contains("${")){
f.dynamicName = true;
continue;
}
SchemaField schemaField = schema.getFieldOrNull(f.getName());
if (schemaField == null) {
schemaField = config.lowerNameVsSchemaField.get(f.getName().toLowerCase());

View File

@ -327,7 +327,7 @@ public class DocBuilder {
DataConfig.Entity e = entity;
while (e.parentEntity != null) {
addFields(e.parentEntity, doc, (Map<String, Object>) vr
.resolve(e.parentEntity.name));
.resolve(e.parentEntity.name), vr);
e = e.parentEntity;
}
}
@ -335,7 +335,7 @@ public class DocBuilder {
Map<String, Object> arow = entityProcessor.nextRow();
if (arow == null) {
entityProcessor.destroy();
break;
break;
}
// Support for start parameter in debug mode
@ -354,7 +354,7 @@ public class DocBuilder {
importStatistics.rowsCount.incrementAndGet();
if (doc != null) {
handleSpecialCommands(arow, doc);
addFields(entity, doc, arow);
addFields(entity, doc, arow, vr);
}
if (isRoot)
vr.removeNamespace(null);
@ -480,7 +480,8 @@ public class DocBuilder {
}
@SuppressWarnings("unchecked")
private void addFields(DataConfig.Entity entity, DocWrapper doc, Map<String, Object> arow) {
private void addFields(DataConfig.Entity entity, DocWrapper doc,
Map<String, Object> arow, VariableResolver vr) {
for (Map.Entry<String, Object> entry : arow.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
@ -500,7 +501,11 @@ public class DocBuilder {
} else {
if (field != null) {
for (DataConfig.Field f : field) {
if (f.toWrite) addFieldToDoc(entry.getValue(), f.getName(), f.boost, f.multiValued, doc);
String name = f.getName();
if(f.dynamicName){
name = vr.replaceTokens(name);
}
if (f.toWrite) addFieldToDoc(entry.getValue(), name, f.boost, f.multiValued, doc);
}
}
}

View File

@ -110,6 +110,21 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
assertQ(req("desc:ApacheSolr"), "//*[@numFound='1']");
}
@Test
@SuppressWarnings("unchecked")
public void testRequestParamsAsFieldName() throws Exception {
List rows = new ArrayList();
rows.add(createMap("mypk", "101", "text", "ApacheSolr"));
MockDataSource.setIterator("select * from x", rows.iterator());
LocalSolrQueryRequest request = lrf.makeRequest("command", "full-import",
"debug", "on", "clean", "true", "commit", "true",
"mypk", "id", "text", "desc",
"dataConfig", dataConfigWithTemplatizedFieldNames);
h.query("/dataimport", request);
assertQ(req("id:101"), "//*[@numFound='1']");
}
@Test
@SuppressWarnings("unchecked")
public void testContext() throws Exception {
@ -299,4 +314,13 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
" </entity>\n" +
" </document>\n" +
"</dataConfig>";
private final String dataConfigWithTemplatizedFieldNames = "<dataConfig>\n" +
" <document>\n" +
" <entity name=\"books\" query=\"select * from x\">\n" +
" <field column=\"mypk\" name=\"${dih.request.mypk}\" />\n" +
" <field column=\"text\" name=\"${dih.request.text}\" />\n" +
" </entity>\n" +
" </document>\n" +
"</dataConfig>";
}