Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

more SOLR-264 work: add example to schema.xml; fix equals bug in SolrComparatorSource that Koji found; make ValueSource use IndexReader version in it's computation; refactor getSeed to use both the fieldname and the reader version

Chris M. Hostetter89bf821efec6
diff --git a/example/solr/conf/schema.xml b/example/solr/conf/schema.xml
index 1d6b740c8d8..76f36266f42 100755
--- a/example/solr/conf/schema.xml
+++ b/example/solr/conf/schema.xml
@@ -111,6 +111,20 @@
-->
<fieldType name="date" class="solr.DateField" sortMissingLast="true" omitNorms="true"/>
+
+ <!-- The "RandomSortField" is not used to store or search any
+ data. You can declare fields of this type it in your schema
+ to generate psuedo-random orderings of your docs for sorting
+ purposes. The ordering is generated based on the field name
+ and the version of the index, As long as the index version
+ remains unchanged, and the same field name is reused,
+ the ordering of the docs will be consistent.
+ If you want differend psuedo-random orderings of documents,
+ for the same version of the index, use a dynamicField and
+ change the name
+ -->
+ <fieldType name="random" class="solr.RandomSortField" indexed="true" />
+
<!-- solr.TextField allows the specification of custom text analyzers
specified as a tokenizer and a list of token filters. Different
analyzers may be specified for indexing and querying.
@@ -298,6 +312,8 @@
<dynamicField name="*_d" type="sdouble" indexed="true" stored="true"/>
<dynamicField name="*_dt" type="date" indexed="true" stored="true"/>
+ <dynamicField name="random*" type="random" />
+
<!-- uncomment the following to ignore any fields that don't already match an existing
field name or dynamic field, rather than reporting them as an error.
alternately, change the type="ignored" to some other type e.g. "text" if you want
diff --git a/src/java/org/apache/solr/schema/RandomSortField.java b/src/java/org/apache/solr/schema/RandomSortField.java
index 115a92c8adf..b6ee21546e9 100644
--- a/src/java/org/apache/solr/schema/RandomSortField.java
+++ b/src/java/org/apache/solr/schema/RandomSortField.java
@@ -73,6 +73,30 @@ public class RandomSortField extends FieldType {
return key >>> 1;
}
+ /**
+ * Given a field name and an IndexReader, get a random hash seed.
+ * Using dynamic fields, you can force the random order to change
+ */
+ private static int getSeed(String fieldName, IndexReader r) {
+ return (int) (fieldName.hashCode()^r.getVersion() );
+ }
+
+ @Override
+ public SortField getSortField(SchemaField field, boolean reverse) {
+ return new RandomSort(field.getName(), reverse);
+ }
+
+ @Override
+ public ValueSource getValueSource(SchemaField field) {
+ return new RandomValueSource(field.getName());
+ }
+
+ @Override
+ public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException { }
+
+ @Override
+ public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException { }
+
private static class RandomComparator implements ScoreDocComparator {
final int seed;
@@ -93,45 +117,45 @@ public class RandomSortField extends FieldType {
}
};
- /** given a field name, get a random hash seed -- using dynamic fields, you can force the random order to change */
- private static int getSeed(String fieldName) {
- return fieldName.hashCode();
- }
-
private static class RandomSort extends SortField {
public RandomSort(String n, boolean reverse) {
super(n, SortField.CUSTOM, reverse);
}
+
+ static class RandomComparatorSource implements SortComparatorSource {
+ final String field;
+ public RandomComparatorSource( String field ){
+ this.field = field;
+ }
+ public ScoreDocComparator newComparator(IndexReader reader, String fieldname) throws IOException {
+ return new RandomComparator( getSeed(field, reader) );
+ }
+
+ @Override
+ public int hashCode() {
+ return field.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if( !(o instanceof RandomComparatorSource ) ) return false;
+ RandomComparatorSource other = (RandomComparatorSource)o;
+ if( !field.equals( other.field ) ) return false;
+ return true;
+ }
+ }
@Override
public SortComparatorSource getFactory() {
- final int seed = getSeed(getField());
-
- return new SortComparatorSource() {
- public ScoreDocComparator newComparator(IndexReader reader, String fieldname) throws IOException {
- return new RandomComparator( (int)(seed^reader.getVersion()) );
- }
-
- @Override
- public int hashCode() {
- return getField().hashCode();
- }
-
- @Override
- public boolean equals(Object o) {
- return (o instanceof RandomSort) && getField().equals(((RandomSort) o).getField());
- }
- };
+ return new RandomComparatorSource( getField() );
}
}
-
+
public class RandomValueSource extends ValueSource {
private final String field;
- final int seed;
public RandomValueSource(String field) {
this.field=field;
- seed = getSeed(field);
}
@Override
@@ -140,38 +164,39 @@ public class RandomSortField extends FieldType {
}
@Override
- public DocValues getValues(IndexReader reader) throws IOException {
+ public DocValues getValues(final IndexReader reader) throws IOException {
return new DocValues() {
- @Override
- public float floatVal(int doc) {
- return (float)hash(doc+seed);
- }
-
- @Override
- public int intVal(int doc) {
- return (int)hash(doc+seed);
- }
-
- @Override
- public long longVal(int doc) {
- return (long)hash(doc+seed);
- }
-
- @Override
- public double doubleVal(int doc) {
- return (double)hash(doc+seed);
- }
-
- @Override
- public String strVal(int doc) {
- return Integer.toString(hash(doc+seed));
- }
-
- @Override
- public String toString(int doc) {
- return description() + '=' + intVal(doc);
- }
- };
+ private final int seed = getSeed(field, reader);
+ @Override
+ public float floatVal(int doc) {
+ return (float)hash(doc+seed);
+ }
+
+ @Override
+ public int intVal(int doc) {
+ return (int)hash(doc+seed);
+ }
+
+ @Override
+ public long longVal(int doc) {
+ return (long)hash(doc+seed);
+ }
+
+ @Override
+ public double doubleVal(int doc) {
+ return (double)hash(doc+seed);
+ }
+
+ @Override
+ public String strVal(int doc) {
+ return Integer.toString(hash(doc+seed));
+ }
+
+ @Override
+ public String toString(int doc) {
+ return description() + '=' + intVal(doc);
+ }
+ };
}
@Override
@@ -186,22 +211,6 @@ public class RandomSortField extends FieldType {
return field.hashCode();
};
}
-
- @Override
- public SortField getSortField(SchemaField field, boolean reverse) {
- return new RandomSort(field.getName(), reverse);
- }
-
- @Override
- public ValueSource getValueSource(SchemaField field) {
- return new RandomValueSource(field.getName());
- }
-
- @Override
- public void write(XMLWriter xmlWriter, String name, Fieldable f) throws IOException { }
-
- @Override
- public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException { }
}