Skip to content

Commit

Permalink
Added fields transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed Feb 25, 2020
1 parent bc2ce6b commit c5f6fd5
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> it = fields.getParameterNamesIterator();
Map<String, Collection<Object>> 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);
}
}


}
}


}
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="minimal" version="1.6">
<types>
<fieldType name="string" class="solr.StrField" />
<fieldType name="text" class="solr.TextField" />
<fieldType name="int" class="solr.TrieIntField"
precisionStep="0" omitNorms="true" positionIncrementGap="0" />
<fieldType name="date" class="solr.TrieDateField" omitNorms="true"
precisionStep="0" positionIncrementGap="0" />
<fieldType name="float" class="solr.TrieFloatField"
precisionStep="0" omitNorms="true" positionIncrementGap="0" />
</types>
<fields>
<field name="id" type="int" indexed="true" stored="true"
required="true" />
<field name="bibcode" type="string" indexed="true" stored="true"
required="false" />
<field name="reference" type="string" indexed="true" stored="true"
required="false" multiValued="true"/>
<field name="date" type="date" indexed="true" stored="true"
multiValued="true" />
<field name="indexstamp" type="date" indexed="true" stored="true"
default="NOW" multiValued="false" />
<field name="body" type="text" indexed="true" stored="true"
required="false" multiValued="false"/>
</fields>
<uniqueKey>id</uniqueKey>
</schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>

<config>
<luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion>
<dataDir>${solr.data.dir:}</dataDir>
<schemaFactory class="ClassicIndexSchemaFactory"/>
<directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.RAMDirectoryFactory}"/>
<requestHandler name="standard" class="solr.StandardRequestHandler"></requestHandler>

<query>
</query>

<transformer name="fields" class="org.apache.solr.response.transform.FieldTransformerFactory" >
</transformer>
</config>
Original file line number Diff line number Diff line change
@@ -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']"
);

}
}

0 comments on commit c5f6fd5

Please sign in to comment.