From 59ed1b5bf19786a3371c2a18cfd26b331fba031c Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 19 Jun 2007 05:27:47 +0000 Subject: [PATCH] SOLR-264: Added RandomSortField a random order SortComparatorSource. Thanks yonik for the performance hints git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@548594 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 + .../apache/solr/schema/RandomSortField.java | 108 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/java/org/apache/solr/schema/RandomSortField.java diff --git a/CHANGES.txt b/CHANGES.txt index bdaa269b056..63bbdf3d440 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -65,6 +65,9 @@ New Features requests depending on the request attributes. This class will likely replace XmlUpdateRequestHandler. (Thorsten Scherler, ryan) + 9. SOLR-264: Added RandomSortField, a utility field with a random sort order. + This field type should only be used for sorting. (ryan) + Changes in runtime behavior Optimizations diff --git a/src/java/org/apache/solr/schema/RandomSortField.java b/src/java/org/apache/solr/schema/RandomSortField.java new file mode 100644 index 00000000000..13f8712aca3 --- /dev/null +++ b/src/java/org/apache/solr/schema/RandomSortField.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.schema; + +import java.io.IOException; +import java.util.Random; + +import org.apache.lucene.document.Fieldable; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.ScoreDocComparator; +import org.apache.lucene.search.SortComparatorSource; +import org.apache.lucene.search.SortField; +import org.apache.solr.request.TextResponseWriter; +import org.apache.solr.request.XMLWriter; +import org.apache.solr.search.function.ValueSource; + +/** + * Utility Field used for random sorting. It should not be passed a value. + * + * To enable random sorting, you will need to add something like this + * to the schema.xml + * + * + * ... + * + * ... + * + * + * ... + * + * ... + * + * + * @author ryan + * @version $Id$ + * @since solr 1.3 + */ +public class RandomSortField extends FieldType +{ + /** Special comparator for sorting hits in random order */ + private static final ScoreDocComparator COMPARE = new ScoreDocComparator() { + final Random rand = new Random(); + + public int compare (ScoreDoc i, ScoreDoc j) { + return (rand.nextBoolean()) ? 1 : -1; //rand.nextInt() >>> 31; ?? + } + public Comparable sortValue (ScoreDoc i) { + return new Float(rand.nextFloat()); + } + public int sortType() { + return SortField.CUSTOM; + } + }; + + /** use random sorting order. */ + private static class RandomSort extends SortField { + public RandomSort( String n ) + { + super( n, SortField.CUSTOM ); + } + + @Override + public SortComparatorSource getFactory() { + return new SortComparatorSource() { + public ScoreDocComparator newComparator(IndexReader reader, String fieldname) throws IOException { + return COMPARE; + } + }; + } + } + + @Override + public SortField getSortField(SchemaField field,boolean reverse) { + return new RandomSort(field.getName()); + } + + @Override + public ValueSource getValueSource(SchemaField field) { + throw new UnsupportedOperationException("Random field does not have a value source"); + } + + @Override + public void write(XMLWriter xmlWriter, String name, Fieldable f) {} + + @Override + public void write(TextResponseWriter writer, String name, Fieldable f) {} +} + + + + +