forked from adsabs/montysolr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc2ce6b
commit c5f6fd5
Showing
5 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
contrib/adsabs/src/java/org/apache/solr/response/transform/FieldTransformerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
} | ||
|
||
|
28 changes: 28 additions & 0 deletions
28
contrib/adsabs/src/test-files/solr/collection1/conf/schema-field-transformer.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
15 changes: 15 additions & 0 deletions
15
contrib/adsabs/src/test-files/solr/collection1/conf/solrconfig-field-transformer.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
66 changes: 66 additions & 0 deletions
66
contrib/adsabs/src/test/org/apache/solr/response/transform/TestFieldTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']" | ||
); | ||
|
||
} | ||
} |