From c5f6fd57ff043651e450388e29204a7c8f83b93a Mon Sep 17 00:00:00 2001 From: Roman Chyla Date: Tue, 25 Feb 2020 17:30:30 -0500 Subject: [PATCH] Added fields transformer --- .../batch/BatchProviderDumpAuthorNames.java | 4 +- .../transform/FieldTransformerFactory.java | 108 ++++++++++++++++++ .../conf/schema-field-transformer.xml | 28 +++++ .../conf/solrconfig-field-transformer.xml | 15 +++ .../transform/TestFieldTransformer.java | 66 +++++++++++ 5 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 contrib/adsabs/src/java/org/apache/solr/response/transform/FieldTransformerFactory.java create mode 100644 contrib/adsabs/src/test-files/solr/collection1/conf/schema-field-transformer.xml create mode 100644 contrib/adsabs/src/test-files/solr/collection1/conf/solrconfig-field-transformer.xml create mode 100644 contrib/adsabs/src/test/org/apache/solr/response/transform/TestFieldTransformer.java diff --git a/contrib/adsabs/src/java/org/apache/solr/handler/batch/BatchProviderDumpAuthorNames.java b/contrib/adsabs/src/java/org/apache/solr/handler/batch/BatchProviderDumpAuthorNames.java index 02312038b..d0b916f14 100644 --- a/contrib/adsabs/src/java/org/apache/solr/handler/batch/BatchProviderDumpAuthorNames.java +++ b/contrib/adsabs/src/java/org/apache/solr/handler/batch/BatchProviderDumpAuthorNames.java @@ -99,8 +99,8 @@ public void collect(int i) { String[] vals = d.getValues(f); for (String s: vals) { - System.out.println(s); - System.out.println(AuthorUtils.normalizeAuthor(s)); + //System.out.println(s); + //System.out.println(AuthorUtils.normalizeAuthor(s)); s = s.toLowerCase(); TokenStream ts = analyzer.tokenStream(targetAnalyzer, new StringReader(s)); diff --git a/contrib/adsabs/src/java/org/apache/solr/response/transform/FieldTransformerFactory.java b/contrib/adsabs/src/java/org/apache/solr/response/transform/FieldTransformerFactory.java new file mode 100644 index 000000000..9d51f48ff --- /dev/null +++ b/contrib/adsabs/src/java/org/apache/solr/response/transform/FieldTransformerFactory.java @@ -0,0 +1,108 @@ +/* + * 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. + */ +package org.apache.solr.response.transform; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.lucene.index.BinaryDocValues; +import org.apache.lucene.util.BytesRef; +import org.apache.solr.common.SolrDocument; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.search.CitationCache; +import org.apache.solr.search.SolrIndexSearcher; + +/** + * + * @since v63.1.1.15 + */ +public class FieldTransformerFactory extends TransformerFactory +{ + int defaultV = 10; + + @Override + public void init(NamedList args) { + String defaultValue = (String) args.get("default"); + if (defaultValue != null) { + defaultV = Integer.getInteger(defaultValue); + } + } + + @Override + public DocTransformer create(String field, SolrParams params, SolrQueryRequest req) { + return new FieldTransform(params, defaultV); + } +} + +class FieldTransform extends DocTransformer +{ + + private SolrParams fields; + private int defaultV; + + public FieldTransform(SolrParams params, int defaultV) { + fields = params; + this.defaultV = defaultV; + } + + @Override + public String getName() + { + return "[fields]"; + } + + @Override + public void transform(SolrDocument doc, int docid, float score) { + if( docid >= 0) { + Iterator it = fields.getParameterNamesIterator(); + Map> docMap = doc.getFieldValuesMap(); + String key; + + while (it.hasNext()) { + key = it.next(); + if (docMap.containsKey(key)) { + Object v = docMap.get(key); + int c = fields.getInt(key, defaultV); + + if (v instanceof List) { + List x = (List) v; + while (x.size() > c) { + x.remove(x.size()-1); + } + } + + + } + } + + + } + } + +} + + diff --git a/contrib/adsabs/src/test-files/solr/collection1/conf/schema-field-transformer.xml b/contrib/adsabs/src/test-files/solr/collection1/conf/schema-field-transformer.xml new file mode 100644 index 000000000..59f479910 --- /dev/null +++ b/contrib/adsabs/src/test-files/solr/collection1/conf/schema-field-transformer.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + id + diff --git a/contrib/adsabs/src/test-files/solr/collection1/conf/solrconfig-field-transformer.xml b/contrib/adsabs/src/test-files/solr/collection1/conf/solrconfig-field-transformer.xml new file mode 100644 index 000000000..3f7db4594 --- /dev/null +++ b/contrib/adsabs/src/test-files/solr/collection1/conf/solrconfig-field-transformer.xml @@ -0,0 +1,15 @@ + + + + ${tests.luceneMatchVersion:LUCENE_CURRENT} + ${solr.data.dir:} + + + + + + + + + + diff --git a/contrib/adsabs/src/test/org/apache/solr/response/transform/TestFieldTransformer.java b/contrib/adsabs/src/test/org/apache/solr/response/transform/TestFieldTransformer.java new file mode 100644 index 000000000..8e39bded2 --- /dev/null +++ b/contrib/adsabs/src/test/org/apache/solr/response/transform/TestFieldTransformer.java @@ -0,0 +1,66 @@ +package org.apache.solr.response.transform; + +import monty.solr.util.MontySolrSetup; + +import org.apache.solr.util.AbstractSolrTestCase; +import org.junit.BeforeClass; + +public class TestFieldTransformer extends AbstractSolrTestCase { + + @BeforeClass + public static void beforeClass() throws Exception { + + System.setProperty("solr.allow.unsafe.resourceloading", "true"); + schemaString = MontySolrSetup.getMontySolrHome() + + "/contrib/adsabs/src/test-files/solr/collection1/conf/schema-field-transformer.xml"; + + configString = MontySolrSetup.getMontySolrHome() + + "/contrib/adsabs/src/test-files/solr/collection1/conf/solrconfig-field-transformer.xml"; + + initCore(configString, schemaString, MontySolrSetup.getSolrHome() + "/example/solr"); + } + + + @Override + public String getSolrHome() { + return MontySolrSetup.getMontySolrHome(); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + + assertU(adoc("id", "3", "bibcode", "b3", + "reference", "b2", "reference", "b3", "reference", "b4", "reference", "foo", + "body", "foo bar baz")); + + assertU(commit()); + } + + + + + + public void test() throws Exception { + + assertQ(req("q", "bibcode:b3", "fl", "id,reference"), + "//*[@numFound='1']", + "//doc/int[@name='id'][.='3']", + "//doc/arr[@name='reference']/str[.='b2']", + "//doc/arr[@name='reference']/str[.='b3']", + "//doc/arr[@name='reference']/str[.='b4']" + ); + + assertQ(req("q", "bibcode:b3", + "fl", "id,reference,body,[fields reference=2 body=3]", + "indent", "true"), + "//*[@numFound='1']", + "//doc/int[@name='id'][.='3']", + "//doc/arr[@name='reference']/str[.='b2']", + "//doc/arr[@name='reference']/str[.='b3']", + "not(//doc/arr[@name='reference']/str[.='b4'])", + "//doc/str[@name='body'][.='foo bar baz']" + ); + + } +}