Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-4035: add docvalues collation field

Robert Muir7456020a3bb2
diff --git a/lucene/analysis/icu/src/java/org/apache/lucene/collation/ICUCollationDocValuesField.java b/lucene/analysis/icu/src/java/org/apache/lucene/collation/ICUCollationDocValuesField.java
new file mode 100644
index 00000000000..0874b37e175
--- /dev/null
+++ b/lucene/analysis/icu/src/java/org/apache/lucene/collation/ICUCollationDocValuesField.java
@@ -0,0 +1,64 @@
+package org.apache.lucene.collation;
+
+/**
+ * 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.document.Field;
+import org.apache.lucene.document.SortedDocValuesField;
+import org.apache.lucene.util.BytesRef;
+
+import com.ibm.icu.text.Collator;
+import com.ibm.icu.text.RawCollationKey;
+
+/**
+ * nocommit
+ */
+public final class ICUCollationDocValuesField extends Field {
+ private final String name;
+ private final Collator collator;
+ private final BytesRef bytes = new BytesRef();
+ private final RawCollationKey key = new RawCollationKey();
+
+ public ICUCollationDocValuesField(String name, Collator collator) {
+ super(name, SortedDocValuesField.TYPE);
+ this.name = name;
+ try {
+ this.collator = (Collator) collator.clone();
+ } catch (CloneNotSupportedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public String name() {
+ return name;
+ }
+
+ public void setStringValue(String value) {
+ collator.getRawCollationKey(value, key);
+ bytes.bytes = key.bytes;
+ bytes.offset = 0;
+ bytes.length = key.size;
+ }
+
+ @Override
+ public BytesRef binaryValue() {
+ return bytes;
+ }
+
+ // nocommit: make this thing trap-free
+}
diff --git a/lucene/analysis/icu/src/test/org/apache/lucene/collation/TestICUCollationDocValuesField.java b/lucene/analysis/icu/src/test/org/apache/lucene/collation/TestICUCollationDocValuesField.java
new file mode 100644
index 00000000000..035ecff6048
--- /dev/null
+++ b/lucene/analysis/icu/src/test/org/apache/lucene/collation/TestICUCollationDocValuesField.java
@@ -0,0 +1,72 @@
+package org.apache.lucene.collation;
+
+/**
+ * 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.codecs.Codec;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+
+import com.ibm.icu.text.Collator;
+import com.ibm.icu.util.ULocale;
+
+/**
+ * trivial test of ICUCollationDocValuesField
+ */
+public class TestICUCollationDocValuesField extends LuceneTestCase {
+ public void test() throws Exception {
+ assumeFalse("3.x codec does not support docvalues", Codec.getDefault().getName().equals("Lucene3x"));
+ Directory dir = newDirectory();
+ RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
+ Document doc = new Document();
+ Field field = newField("field", "", StringField.TYPE_STORED);
+ ICUCollationDocValuesField collationField = new ICUCollationDocValuesField("collated", Collator.getInstance(ULocale.ENGLISH));
+ doc.add(field);
+ doc.add(collationField);
+
+ field.setStringValue("ABC");
+ collationField.setStringValue("ABC");
+ iw.addDocument(doc);
+
+ field.setStringValue("abc");
+ collationField.setStringValue("abc");
+ iw.addDocument(doc);
+
+ IndexReader ir = iw.getReader();
+ iw.close();
+
+ IndexSearcher is = newSearcher(ir);
+
+ SortField sortField = new SortField("collated", SortField.Type.STRING);
+
+ TopDocs td = is.search(new MatchAllDocsQuery(), 5, new Sort(sortField));
+ assertEquals("abc", ir.document(td.scoreDocs[0].doc).get("field"));
+ assertEquals("ABC", ir.document(td.scoreDocs[1].doc).get("field"));
+ ir.close();
+ dir.close();
+ }
+}