Skip to content

Apache Lucene open-source search software

SOLR-1285: Add DelimitedPayloadTokenFilterFactory, tests and short example

Grant Ingersollb5620d861460
diff --git a/example/exampledocs/payload.xml b/example/exampledocs/payload.xml
new file mode 100644
index 00000000000..f5fe613596c
--- /dev/null
+++ b/example/exampledocs/payload.xml
@@ -0,0 +1,57 @@
+<!--
+ 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.
+-->
+
+<add>
+<doc>
+ <field name="id">TWINX2048-3200PRO-payload</field>
+ <field name="name">CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail</field>
+ <field name="manu">Corsair Microsystems Inc.</field>
+ <field name="cat">electronics</field>
+ <field name="cat">memory</field>
+ <field name="features">CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader</field>
+ <field name="price">185</field>
+ <field name="popularity">5</field>
+ <field name="inStock">true</field>
+ <field name="catPay">electronics|6.0 memory|3.0</field>
+</doc>
+
+<doc>
+ <field name="id">VS1GB400C3-payload</field>
+ <field name="name">CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail</field>
+ <field name="manu">Corsair Microsystems Inc.</field>
+ <field name="cat">electronics</field>
+ <field name="cat">memory</field>
+ <field name="price">74.99</field>
+ <field name="popularity">7</field>
+ <field name="inStock">true</field>
+ <field name="catPay">electronics|4.0 memory|2.0</field>
+</doc>
+
+<doc>
+ <field name="id">VDBDB1A16-payload</field>
+ <field name="name">A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM</field>
+ <field name="manu">A-DATA Technology Inc.</field>
+ <field name="cat">electronics</field>
+ <field name="cat">memory</field>
+ <field name="features">CAS latency 3, 2.7v</field>
+ <!-- note: price is missing on this one -->
+ <field name="popularity">5</field>
+ <field name="inStock">true</field>
+ <field name="catPay">electronics|0.9 memory|0.1</field>
+</doc>
+</add>
+
diff --git a/example/solr/conf/schema.xml b/example/solr/conf/schema.xml
index 46dc062abb4..b6c4d78e8d2 100755
--- a/example/solr/conf/schema.xml
+++ b/example/solr/conf/schema.xml
@@ -300,7 +300,22 @@
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.DoubleMetaphoneFilterFactory" inject="false"/>
</analyzer>
- </fieldtype>
+ </fieldtype>
+
+ <fieldtype name="payloads" stored="false" indexed="true" class="solr.TextField" >
+ <analyzer>
+ <tokenizer class="solr.WhitespaceTokenizerFactory"/>
+ <!--
+ Attributes:
+ delimiter - a one character delimiter - will throw an exception if the delim is more than one char. Default is | (pipe)
+ encoder - Values: float -> org.apache.lucene.analysis.payloads.FloatEncoder, integer -> o.a.l.a.p.IntegerEncoder
+ identity -> o.a.l.a.p.IdentityEncoder, Fully Qualified class name implementing PayloadEncoder
+ Encoder must have a no arg constructor.
+ -->
+ <filter class="solr.DelimitedPayloadTokenFilterFactory" encoder="float"/>
+ </analyzer>
+ </fieldtype>
+
<!-- since fields of this type are by default not stored or indexed, any data added to
@@ -366,6 +381,8 @@
results by manufacturer. copied from "manu" via copyField -->
<field name="manu_exact" type="string" indexed="true" stored="false"/>
+ <field name="catPay" type="payloads" indexed="true" stored="true"/>
+
<!-- Here, default is used to create a "timestamp" field indicating
When each document was indexed.
-->
diff --git a/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java b/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java
new file mode 100644
index 00000000000..51cb85ab8ed
--- /dev/null
+++ b/src/java/org/apache/solr/analysis/DelimitedPayloadTokenFilterFactory.java
@@ -0,0 +1,73 @@
+package org.apache.solr.analysis;
+/**
+ * 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.
+ */
+
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.payloads.DelimitedPayloadTokenFilter;
+import org.apache.lucene.analysis.payloads.PayloadEncoder;
+import org.apache.lucene.analysis.payloads.FloatEncoder;
+import org.apache.lucene.analysis.payloads.IntegerEncoder;
+import org.apache.lucene.analysis.payloads.IdentityEncoder;
+import org.apache.solr.common.ResourceLoader;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.util.plugin.ResourceLoaderAware;
+
+import java.util.Map;
+
+
+/**
+ *
+ *
+ **/
+public class DelimitedPayloadTokenFilterFactory extends BaseTokenFilterFactory implements ResourceLoaderAware {
+ public static final String ENCODER_ATTR = "encoder";
+ public static final String DELIMITER_ATTR = "delimiter";
+
+ private PayloadEncoder encoder;
+ private char delimiter = '|';
+
+ public DelimitedPayloadTokenFilter create(TokenStream input) {
+ return new DelimitedPayloadTokenFilter(input, delimiter, encoder);
+ }
+
+ @Override
+ public void init(Map<String, String> args) {
+ super.init(args);
+ }
+
+ public void inform(ResourceLoader loader) {
+ String encoderClass = args.get(ENCODER_ATTR);
+ if (encoderClass.equals("float")){
+ encoder = new FloatEncoder();
+ } else if (encoderClass.equals("integer")){
+ encoder = new IntegerEncoder();
+ } else if (encoderClass.equals("identity")){
+ encoder = new IdentityEncoder();
+ } else {
+ encoder = (PayloadEncoder) loader.newInstance(encoderClass);
+ }
+
+ String delim = args.get(DELIMITER_ATTR);
+ if (delim != null){
+ if (delim.length() == 1) {
+ delimiter = delim.charAt(0);
+ } else{
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Delimiter must be one character only");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/org/apache/solr/analysis/TestDelimitedPayloadTokenFilterFactory.java b/src/test/org/apache/solr/analysis/TestDelimitedPayloadTokenFilterFactory.java
new file mode 100644
index 00000000000..e2185fb5147
--- /dev/null
+++ b/src/test/org/apache/solr/analysis/TestDelimitedPayloadTokenFilterFactory.java
@@ -0,0 +1,92 @@
+/**
+ * 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.analysis;
+
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.lucene.analysis.Token;
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.WhitespaceTokenizer;
+import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
+import org.apache.lucene.analysis.payloads.FloatEncoder;
+import org.apache.lucene.analysis.payloads.DelimitedPayloadTokenFilter;
+import org.apache.lucene.analysis.payloads.PayloadHelper;
+import org.apache.lucene.util.Attribute;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.core.SolrResourceLoader;
+import org.apache.solr.common.ResourceLoader;
+import org.apache.solr.util.AbstractSolrTestCase;
+
+public class TestDelimitedPayloadTokenFilterFactory extends AbstractSolrTestCase{
+
+
+ public String getSchemaFile() {
+ return "schema.xml";
+ }
+
+ public String getSolrConfigFile() {
+ return "solrconfig.xml";
+ }
+
+ public void testEncoder() throws Exception {
+ Map<String,String> args = new HashMap<String, String>();
+ args.put(DelimitedPayloadTokenFilterFactory.ENCODER_ATTR, "float");
+ DelimitedPayloadTokenFilterFactory factory = new DelimitedPayloadTokenFilterFactory();
+ factory.init(args);
+ ResourceLoader loader = h.getCore().getResourceLoader();
+ factory.inform(loader);
+
+ TokenStream input = new WhitespaceTokenizer(new StringReader("the|0.1 quick|0.1 red|0.1"));
+ DelimitedPayloadTokenFilter tf = factory.create(input);
+ while (tf.incrementToken()){
+ PayloadAttribute payAttr = (PayloadAttribute) tf.getAttribute(PayloadAttribute.class);
+ assertTrue("payAttr is null and it shouldn't be", payAttr != null);
+ byte[] payData = payAttr.getPayload().getData();
+ assertTrue("payData is null and it shouldn't be", payData != null);
+ assertTrue("payData is null and it shouldn't be", payData != null);
+ float payFloat = PayloadHelper.decodeFloat(payData);
+ assertTrue(payFloat + " does not equal: " + 0.1f, payFloat == 0.1f);
+ }
+ }
+
+ public void testDelim() throws Exception {
+ Map<String,String> args = new HashMap<String, String>();
+ args.put(DelimitedPayloadTokenFilterFactory.ENCODER_ATTR, FloatEncoder.class.getName());
+ args.put(DelimitedPayloadTokenFilterFactory.DELIMITER_ATTR, "*");
+ DelimitedPayloadTokenFilterFactory factory = new DelimitedPayloadTokenFilterFactory();
+ factory.init(args);
+ ResourceLoader loader = h.getCore().getResourceLoader();
+ factory.inform(loader);
+
+ TokenStream input = new WhitespaceTokenizer(new StringReader("the*0.1 quick*0.1 red*0.1"));
+ DelimitedPayloadTokenFilter tf = factory.create(input);
+ while (tf.incrementToken()){
+ PayloadAttribute payAttr = (PayloadAttribute) tf.getAttribute(PayloadAttribute.class);
+ assertTrue("payAttr is null and it shouldn't be", payAttr != null);
+ byte[] payData = payAttr.getPayload().getData();
+ assertTrue("payData is null and it shouldn't be", payData != null);
+ float payFloat = PayloadHelper.decodeFloat(payData);
+ assertTrue(payFloat + " does not equal: " + 0.1f, payFloat == 0.1f);
+ }
+ }
+}
+