You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2014/09/16 00:47:04 UTC
[06/11] Skeleton porting of Lucene.Net.Queries
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/BoolDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/BoolDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/BoolDocValues.cs
new file mode 100644
index 0000000..1ad0517
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/BoolDocValues.cs
@@ -0,0 +1,122 @@
+using System;
+
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ /*
+ * 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.
+ */
+
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueBool = org.apache.lucene.util.mutable.MutableValueBool;
+
+ /// <summary>
+ /// Abstract <seealso cref="FunctionValues"/> implementation which supports retrieving boolean values.
+ /// Implementations can control how the boolean values are loaded through <seealso cref="#boolVal(int)"/>}
+ /// </summary>
+ public abstract class BoolDocValues : FunctionValues
+ {
+ protected internal readonly ValueSource vs;
+
+ public BoolDocValues(ValueSource vs)
+ {
+ this.vs = vs;
+ }
+
+ public override abstract bool boolVal(int doc);
+
+ public override sbyte byteVal(int doc)
+ {
+ return boolVal(doc) ? (sbyte)1 : (sbyte)0;
+ }
+
+ public override short shortVal(int doc)
+ {
+ return boolVal(doc) ? (short)1 : (short)0;
+ }
+
+ public override float floatVal(int doc)
+ {
+ return boolVal(doc) ? (float)1 : (float)0;
+ }
+
+ public override int intVal(int doc)
+ {
+ return boolVal(doc) ? 1 : 0;
+ }
+
+ public override long longVal(int doc)
+ {
+ return boolVal(doc) ? (long)1 : (long)0;
+ }
+
+ public override double doubleVal(int doc)
+ {
+ return boolVal(doc) ? (double)1 : (double)0;
+ }
+
+ public override string strVal(int doc)
+ {
+ return Convert.ToString(boolVal(doc));
+ }
+
+ public override object objectVal(int doc)
+ {
+ return exists(doc) ? boolVal(doc) : null;
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + '=' + strVal(doc);
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly BoolDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(BoolDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueBool();
+ }
+
+ private readonly MutableValueBool mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.value = outerInstance.boolVal(doc);
+ mval.exists = outerInstance.exists(doc);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs
new file mode 100644
index 0000000..79f490c
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs
@@ -0,0 +1,234 @@
+using System;
+
+/*
+ * 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.
+ */
+
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using IndexReader = org.apache.lucene.index.IndexReader;
+ using SortedDocValues = org.apache.lucene.index.SortedDocValues;
+ using FieldCache = org.apache.lucene.search.FieldCache;
+ using BytesRef = org.apache.lucene.util.BytesRef;
+ using CharsRef = org.apache.lucene.util.CharsRef;
+ using UnicodeUtil = org.apache.lucene.util.UnicodeUtil;
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueStr = org.apache.lucene.util.mutable.MutableValueStr;
+
+ /// <summary>
+ /// Serves as base class for FunctionValues based on DocTermsIndex.
+ /// @lucene.internal
+ /// </summary>
+ public abstract class DocTermsIndexDocValues : FunctionValues
+ {
+ protected internal readonly SortedDocValues termsIndex;
+ protected internal readonly ValueSource vs;
+ protected internal readonly MutableValueStr val = new MutableValueStr();
+ protected internal readonly BytesRef spare = new BytesRef();
+ protected internal readonly CharsRef spareChars = new CharsRef();
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: public DocTermsIndexDocValues(org.apache.lucene.queries.function.ValueSource vs, org.apache.lucene.index.AtomicReaderContext context, String field) throws java.io.IOException
+ public DocTermsIndexDocValues(ValueSource vs, AtomicReaderContext context, string field)
+ {
+ try
+ {
+ termsIndex = FieldCache.DEFAULT.getTermsIndex(context.reader(), field);
+ }
+ catch (Exception e)
+ {
+ throw new DocTermsIndexException(field, e);
+ }
+ this.vs = vs;
+ }
+
+ protected internal abstract string toTerm(string readableValue);
+
+ public override bool exists(int doc)
+ {
+ return ordVal(doc) >= 0;
+ }
+
+ public override int ordVal(int doc)
+ {
+ return termsIndex.getOrd(doc);
+ }
+
+ public override int numOrd()
+ {
+ return termsIndex.ValueCount;
+ }
+
+ public override bool bytesVal(int doc, BytesRef target)
+ {
+ termsIndex.get(doc, target);
+ return target.length > 0;
+ }
+
+ public override string strVal(int doc)
+ {
+ termsIndex.get(doc, spare);
+ if (spare.length == 0)
+ {
+ return null;
+ }
+ UnicodeUtil.UTF8toUTF16(spare, spareChars);
+ return spareChars.ToString();
+ }
+
+ public override bool boolVal(int doc)
+ {
+ return exists(doc);
+ }
+
+ public override abstract object objectVal(int doc); // force subclasses to override
+
+ public override ValueSourceScorer getRangeScorer(IndexReader reader, string lowerVal, string upperVal, bool includeLower, bool includeUpper)
+ {
+ // TODO: are lowerVal and upperVal in indexed form or not?
+ lowerVal = lowerVal == null ? null : toTerm(lowerVal);
+ upperVal = upperVal == null ? null : toTerm(upperVal);
+
+ int lower = int.MinValue;
+ if (lowerVal != null)
+ {
+ lower = termsIndex.lookupTerm(new BytesRef(lowerVal));
+ if (lower < 0)
+ {
+ lower = -lower - 1;
+ }
+ else if (!includeLower)
+ {
+ lower++;
+ }
+ }
+
+ int upper = int.MaxValue;
+ if (upperVal != null)
+ {
+ upper = termsIndex.lookupTerm(new BytesRef(upperVal));
+ if (upper < 0)
+ {
+ upper = -upper - 2;
+ }
+ else if (!includeUpper)
+ {
+ upper--;
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final int ll = lower;
+ int ll = lower;
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final int uu = upper;
+ int uu = upper;
+
+ return new ValueSourceScorerAnonymousInnerClassHelper(this, reader, this, ll, uu);
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper : ValueSourceScorer
+ {
+ private readonly DocTermsIndexDocValues outerInstance;
+
+ private int ll;
+ private int uu;
+
+ public ValueSourceScorerAnonymousInnerClassHelper(DocTermsIndexDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.DocTermsIndexDocValues this, int ll, int uu) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.ll = ll;
+ this.uu = uu;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ int ord = outerInstance.termsIndex.getOrd(doc);
+ return ord >= ll && ord <= uu;
+ }
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + '=' + strVal(doc);
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly DocTermsIndexDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(DocTermsIndexDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueStr();
+ }
+
+ private readonly MutableValueStr mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ int ord = outerInstance.termsIndex.getOrd(doc);
+ if (ord == -1)
+ {
+ mval.value.bytes = BytesRef.EMPTY_BYTES;
+ mval.value.offset = 0;
+ mval.value.length = 0;
+ mval.exists = false;
+ }
+ else
+ {
+ outerInstance.termsIndex.lookupOrd(ord, mval.value);
+ mval.exists = true;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Custom Exception to be thrown when the DocTermsIndex for a field cannot be generated
+ /// </summary>
+ public sealed class DocTermsIndexException : Exception
+ {
+
+//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
+//ORIGINAL LINE: public DocTermsIndexException(final String fieldName, final RuntimeException cause)
+ public DocTermsIndexException(string fieldName, Exception cause) : base("Can't initialize DocTermsIndex to generate (function) FunctionValues for field: " + fieldName, cause)
+ {
+ }
+
+ }
+
+
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/DoubleDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/DoubleDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/DoubleDocValues.cs
new file mode 100644
index 0000000..afc2a59
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/DoubleDocValues.cs
@@ -0,0 +1,256 @@
+using System;
+
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ /*
+ * 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.
+ */
+
+ using IndexReader = org.apache.lucene.index.IndexReader;
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueDouble = org.apache.lucene.util.mutable.MutableValueDouble;
+
+ /// <summary>
+ /// Abstract <seealso cref="FunctionValues"/> implementation which supports retrieving double values.
+ /// Implementations can control how the double values are loaded through <seealso cref="#doubleVal(int)"/>}
+ /// </summary>
+ public abstract class DoubleDocValues : FunctionValues
+ {
+ protected internal readonly ValueSource vs;
+
+ public DoubleDocValues(ValueSource vs)
+ {
+ this.vs = vs;
+ }
+
+ public override sbyte byteVal(int doc)
+ {
+ return (sbyte)doubleVal(doc);
+ }
+
+ public override short shortVal(int doc)
+ {
+ return (short)doubleVal(doc);
+ }
+
+ public override float floatVal(int doc)
+ {
+ return (float)doubleVal(doc);
+ }
+
+ public override int intVal(int doc)
+ {
+ return (int)doubleVal(doc);
+ }
+
+ public override long longVal(int doc)
+ {
+ return (long)doubleVal(doc);
+ }
+
+ public override bool boolVal(int doc)
+ {
+ return doubleVal(doc) != 0;
+ }
+
+ public override abstract double doubleVal(int doc);
+
+ public override string strVal(int doc)
+ {
+ return Convert.ToString(doubleVal(doc));
+ }
+
+ public override object objectVal(int doc)
+ {
+ return exists(doc) ? doubleVal(doc) : null;
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + '=' + strVal(doc);
+ }
+
+ public override ValueSourceScorer getRangeScorer(IndexReader reader, string lowerVal, string upperVal, bool includeLower, bool includeUpper)
+ {
+ double lower, upper;
+
+ if (lowerVal == null)
+ {
+ lower = double.NegativeInfinity;
+ }
+ else
+ {
+ lower = Convert.ToDouble(lowerVal);
+ }
+
+ if (upperVal == null)
+ {
+ upper = double.PositiveInfinity;
+ }
+ else
+ {
+ upper = Convert.ToDouble(upperVal);
+ }
+
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final double l = lower;
+ double l = lower;
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final double u = upper;
+ double u = upper;
+
+
+ if (includeLower && includeUpper)
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper(this, reader, this, l, u);
+ }
+ else if (includeLower && !includeUpper)
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper2(this, reader, this, l, u);
+ }
+ else if (!includeLower && includeUpper)
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper3(this, reader, this, l, u);
+ }
+ else
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper4(this, reader, this, l, u);
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper : ValueSourceScorer
+ {
+ private readonly DoubleDocValues outerInstance;
+
+ private double l;
+ private double u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper(DoubleDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.DoubleDocValues this, double l, double u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ double docVal = outerInstance.doubleVal(doc);
+ return docVal >= l && docVal <= u;
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper2 : ValueSourceScorer
+ {
+ private readonly DoubleDocValues outerInstance;
+
+ private double l;
+ private double u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper2(DoubleDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.DoubleDocValues this, double l, double u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ double docVal = outerInstance.doubleVal(doc);
+ return docVal >= l && docVal < u;
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper3 : ValueSourceScorer
+ {
+ private readonly DoubleDocValues outerInstance;
+
+ private double l;
+ private double u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper3(DoubleDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.DoubleDocValues this, double l, double u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ double docVal = outerInstance.doubleVal(doc);
+ return docVal > l && docVal <= u;
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper4 : ValueSourceScorer
+ {
+ private readonly DoubleDocValues outerInstance;
+
+ private double l;
+ private double u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper4(DoubleDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.DoubleDocValues this, double l, double u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ double docVal = outerInstance.doubleVal(doc);
+ return docVal > l && docVal < u;
+ }
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly DoubleDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(DoubleDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueDouble();
+ }
+
+ private readonly MutableValueDouble mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.value = outerInstance.doubleVal(doc);
+ mval.exists = outerInstance.exists(doc);
+ }
+ }
+
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/FloatDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/FloatDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/FloatDocValues.cs
new file mode 100644
index 0000000..72df9b0
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/FloatDocValues.cs
@@ -0,0 +1,117 @@
+using System;
+
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ /*
+ * 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.
+ */
+
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueFloat = org.apache.lucene.util.mutable.MutableValueFloat;
+
+ /// <summary>
+ /// Abstract <seealso cref="FunctionValues"/> implementation which supports retrieving float values.
+ /// Implementations can control how the float values are loaded through <seealso cref="#floatVal(int)"/>}
+ /// </summary>
+ public abstract class FloatDocValues : FunctionValues
+ {
+ protected internal readonly ValueSource vs;
+
+ public FloatDocValues(ValueSource vs)
+ {
+ this.vs = vs;
+ }
+
+ public override sbyte byteVal(int doc)
+ {
+ return (sbyte)floatVal(doc);
+ }
+
+ public override short shortVal(int doc)
+ {
+ return (short)floatVal(doc);
+ }
+
+ public override abstract float floatVal(int doc);
+
+ public override int intVal(int doc)
+ {
+ return (int)floatVal(doc);
+ }
+
+ public override long longVal(int doc)
+ {
+ return (long)floatVal(doc);
+ }
+
+ public override double doubleVal(int doc)
+ {
+ return (double)floatVal(doc);
+ }
+
+ public override string strVal(int doc)
+ {
+ return Convert.ToString(floatVal(doc));
+ }
+
+ public override object objectVal(int doc)
+ {
+ return exists(doc) ? floatVal(doc) : null;
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + '=' + strVal(doc);
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly FloatDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(FloatDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueFloat();
+ }
+
+ private readonly MutableValueFloat mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.value = outerInstance.floatVal(doc);
+ mval.exists = outerInstance.exists(doc);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/IntDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/IntDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/IntDocValues.cs
new file mode 100644
index 0000000..c3f934e
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/IntDocValues.cs
@@ -0,0 +1,183 @@
+using System;
+
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ /*
+ * 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.
+ */
+
+ using IndexReader = org.apache.lucene.index.IndexReader;
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueInt = org.apache.lucene.util.mutable.MutableValueInt;
+
+ /// <summary>
+ /// Abstract <seealso cref="FunctionValues"/> implementation which supports retrieving int values.
+ /// Implementations can control how the int values are loaded through <seealso cref="#intVal(int)"/>
+ /// </summary>
+ public abstract class IntDocValues : FunctionValues
+ {
+ protected internal readonly ValueSource vs;
+
+ public IntDocValues(ValueSource vs)
+ {
+ this.vs = vs;
+ }
+
+ public override sbyte byteVal(int doc)
+ {
+ return (sbyte)intVal(doc);
+ }
+
+ public override short shortVal(int doc)
+ {
+ return (short)intVal(doc);
+ }
+
+ public override float floatVal(int doc)
+ {
+ return (float)intVal(doc);
+ }
+
+ public override abstract int intVal(int doc);
+
+ public override long longVal(int doc)
+ {
+ return (long)intVal(doc);
+ }
+
+ public override double doubleVal(int doc)
+ {
+ return (double)intVal(doc);
+ }
+
+ public override string strVal(int doc)
+ {
+ return Convert.ToString(intVal(doc));
+ }
+
+ public override object objectVal(int doc)
+ {
+ return exists(doc) ? intVal(doc) : null;
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + '=' + strVal(doc);
+ }
+
+ public override ValueSourceScorer getRangeScorer(IndexReader reader, string lowerVal, string upperVal, bool includeLower, bool includeUpper)
+ {
+ int lower, upper;
+
+ // instead of using separate comparison functions, adjust the endpoints.
+
+ if (lowerVal == null)
+ {
+ lower = int.MinValue;
+ }
+ else
+ {
+ lower = Convert.ToInt32(lowerVal);
+ if (!includeLower && lower < int.MaxValue)
+ {
+ lower++;
+ }
+ }
+
+ if (upperVal == null)
+ {
+ upper = int.MaxValue;
+ }
+ else
+ {
+ upper = Convert.ToInt32(upperVal);
+ if (!includeUpper && upper > int.MinValue)
+ {
+ upper--;
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final int ll = lower;
+ int ll = lower;
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final int uu = upper;
+ int uu = upper;
+
+ return new ValueSourceScorerAnonymousInnerClassHelper(this, reader, this, ll, uu);
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper : ValueSourceScorer
+ {
+ private readonly IntDocValues outerInstance;
+
+ private int ll;
+ private int uu;
+
+ public ValueSourceScorerAnonymousInnerClassHelper(IntDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.IntDocValues this, int ll, int uu) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.ll = ll;
+ this.uu = uu;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ int val = outerInstance.intVal(doc);
+ // only check for deleted if it's the default value
+ // if (val==0 && reader.isDeleted(doc)) return false;
+ return val >= ll && val <= uu;
+ }
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly IntDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(IntDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueInt();
+ }
+
+ private readonly MutableValueInt mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.value = outerInstance.intVal(doc);
+ mval.exists = outerInstance.exists(doc);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/LongDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/LongDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/LongDocValues.cs
new file mode 100644
index 0000000..da27a4c
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/LongDocValues.cs
@@ -0,0 +1,193 @@
+using System;
+
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ /*
+ * 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.
+ */
+
+ using IndexReader = org.apache.lucene.index.IndexReader;
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueLong = org.apache.lucene.util.mutable.MutableValueLong;
+
+ /// <summary>
+ /// Abstract <seealso cref="FunctionValues"/> implementation which supports retrieving long values.
+ /// Implementations can control how the long values are loaded through <seealso cref="#longVal(int)"/>}
+ /// </summary>
+ public abstract class LongDocValues : FunctionValues
+ {
+ protected internal readonly ValueSource vs;
+
+ public LongDocValues(ValueSource vs)
+ {
+ this.vs = vs;
+ }
+
+ public override sbyte byteVal(int doc)
+ {
+ return (sbyte)longVal(doc);
+ }
+
+ public override short shortVal(int doc)
+ {
+ return (short)longVal(doc);
+ }
+
+ public override float floatVal(int doc)
+ {
+ return (float)longVal(doc);
+ }
+
+ public override int intVal(int doc)
+ {
+ return (int)longVal(doc);
+ }
+
+ public override abstract long longVal(int doc);
+
+ public override double doubleVal(int doc)
+ {
+ return (double)longVal(doc);
+ }
+
+ public override bool boolVal(int doc)
+ {
+ return longVal(doc) != 0;
+ }
+
+ public override string strVal(int doc)
+ {
+ return Convert.ToString(longVal(doc));
+ }
+
+ public override object objectVal(int doc)
+ {
+ return exists(doc) ? longVal(doc) : null;
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + '=' + strVal(doc);
+ }
+
+ protected internal virtual long externalToLong(string extVal)
+ {
+ return Convert.ToInt64(extVal);
+ }
+
+ public override ValueSourceScorer getRangeScorer(IndexReader reader, string lowerVal, string upperVal, bool includeLower, bool includeUpper)
+ {
+ long lower, upper;
+
+ // instead of using separate comparison functions, adjust the endpoints.
+
+ if (lowerVal == null)
+ {
+ lower = long.MinValue;
+ }
+ else
+ {
+ lower = externalToLong(lowerVal);
+ if (!includeLower && lower < long.MaxValue)
+ {
+ lower++;
+ }
+ }
+
+ if (upperVal == null)
+ {
+ upper = long.MaxValue;
+ }
+ else
+ {
+ upper = externalToLong(upperVal);
+ if (!includeUpper && upper > long.MinValue)
+ {
+ upper--;
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final long ll = lower;
+ long ll = lower;
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final long uu = upper;
+ long uu = upper;
+
+ return new ValueSourceScorerAnonymousInnerClassHelper(this, reader, this, ll, uu);
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper : ValueSourceScorer
+ {
+ private readonly LongDocValues outerInstance;
+
+ private long ll;
+ private long uu;
+
+ public ValueSourceScorerAnonymousInnerClassHelper(LongDocValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.docvalues.LongDocValues this, long ll, long uu) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.ll = ll;
+ this.uu = uu;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ long val = outerInstance.longVal(doc);
+ // only check for deleted if it's the default value
+ // if (val==0 && reader.isDeleted(doc)) return false;
+ return val >= ll && val <= uu;
+ }
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly LongDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(LongDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueLong();
+ }
+
+ private readonly MutableValueLong mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.value = outerInstance.longVal(doc);
+ mval.exists = outerInstance.exists(doc);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/DocValues/StrDocValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/DocValues/StrDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/StrDocValues.cs
new file mode 100644
index 0000000..ea3c450
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/DocValues/StrDocValues.cs
@@ -0,0 +1,89 @@
+namespace org.apache.lucene.queries.function.docvalues
+{
+
+ /*
+ * 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.
+ */
+
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueStr = org.apache.lucene.util.mutable.MutableValueStr;
+
+ /// <summary>
+ /// Abstract <seealso cref="FunctionValues"/> implementation which supports retrieving String values.
+ /// Implementations can control how the String values are loaded through <seealso cref="#strVal(int)"/>}
+ /// </summary>
+ public abstract class StrDocValues : FunctionValues
+ {
+ protected internal readonly ValueSource vs;
+
+ public StrDocValues(ValueSource vs)
+ {
+ this.vs = vs;
+ }
+
+ public override abstract string strVal(int doc);
+
+ public override object objectVal(int doc)
+ {
+ return exists(doc) ? strVal(doc) : null;
+ }
+
+ public override bool boolVal(int doc)
+ {
+ return exists(doc);
+ }
+
+ public override string ToString(int doc)
+ {
+ return vs.description() + "='" + strVal(doc) + "'";
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly StrDocValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(StrDocValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueStr();
+ }
+
+ private readonly MutableValueStr mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.exists = outerInstance.bytesVal(doc, mval.value);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/FunctionQuery.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/FunctionQuery.cs b/src/Lucene.Net.Queries/Function/FunctionQuery.cs
new file mode 100644
index 0000000..e0642f1
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/FunctionQuery.cs
@@ -0,0 +1,270 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace org.apache.lucene.queries.function
+{
+
+ /*
+ * 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.
+ */
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using IndexReader = org.apache.lucene.index.IndexReader;
+ using Term = org.apache.lucene.index.Term;
+ using org.apache.lucene.search;
+ using MultiFields = org.apache.lucene.index.MultiFields;
+ using Bits = org.apache.lucene.util.Bits;
+
+
+
+ /// <summary>
+ /// Returns a score for each document based on a ValueSource,
+ /// often some function of the value of a field.
+ ///
+ /// <b>Note: This API is experimental and may change in non backward-compatible ways in the future</b>
+ ///
+ ///
+ /// </summary>
+ public class FunctionQuery : Query
+ {
+ internal readonly ValueSource func;
+
+ /// <param name="func"> defines the function to be used for scoring </param>
+ public FunctionQuery(ValueSource func)
+ {
+ this.func = func;
+ }
+
+ /// <returns> The associated ValueSource </returns>
+ public virtual ValueSource ValueSource
+ {
+ get
+ {
+ return func;
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public Query rewrite(org.apache.lucene.index.IndexReader reader) throws java.io.IOException
+ public override Query rewrite(IndexReader reader)
+ {
+ return this;
+ }
+
+ public override void extractTerms(HashSet<Term> terms)
+ {
+ }
+
+ protected internal class FunctionWeight : Weight
+ {
+ private readonly FunctionQuery outerInstance;
+
+ protected internal readonly IndexSearcher searcher;
+ protected internal float queryNorm;
+ protected internal float queryWeight;
+ protected internal readonly IDictionary context;
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: public FunctionWeight(IndexSearcher searcher) throws java.io.IOException
+ public FunctionWeight(FunctionQuery outerInstance, IndexSearcher searcher)
+ {
+ this.outerInstance = outerInstance;
+ this.searcher = searcher;
+ this.context = ValueSource.newContext(searcher);
+ outerInstance.func.createWeight(context, searcher);
+ }
+
+ public override Query Query
+ {
+ get
+ {
+ return outerInstance;
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public float getValueForNormalization() throws java.io.IOException
+ public override float ValueForNormalization
+ {
+ get
+ {
+ queryWeight = Boost;
+ return queryWeight * queryWeight;
+ }
+ }
+
+ public override void normalize(float norm, float topLevelBoost)
+ {
+ this.queryNorm = norm * topLevelBoost;
+ queryWeight *= this.queryNorm;
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public Scorer scorer(org.apache.lucene.index.AtomicReaderContext context, org.apache.lucene.util.Bits acceptDocs) throws java.io.IOException
+ public override Scorer scorer(AtomicReaderContext context, Bits acceptDocs)
+ {
+ return new AllScorer(outerInstance, context, acceptDocs, this, queryWeight);
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public Explanation explain(org.apache.lucene.index.AtomicReaderContext context, int doc) throws java.io.IOException
+ public override Explanation explain(AtomicReaderContext context, int doc)
+ {
+ return ((AllScorer)scorer(context, context.reader().LiveDocs)).explain(doc);
+ }
+ }
+
+ protected internal class AllScorer : Scorer
+ {
+ private readonly FunctionQuery outerInstance;
+
+ internal readonly IndexReader reader;
+ internal readonly FunctionWeight weight;
+ internal readonly int maxDoc;
+ internal readonly float qWeight;
+ internal int doc = -1;
+ internal readonly FunctionValues vals;
+ internal readonly Bits acceptDocs;
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: public AllScorer(org.apache.lucene.index.AtomicReaderContext context, org.apache.lucene.util.Bits acceptDocs, FunctionWeight w, float qWeight) throws java.io.IOException
+ public AllScorer(FunctionQuery outerInstance, AtomicReaderContext context, Bits acceptDocs, FunctionWeight w, float qWeight) : base(w)
+ {
+ this.outerInstance = outerInstance;
+ this.weight = w;
+ this.qWeight = qWeight;
+ this.reader = context.reader();
+ this.maxDoc = reader.maxDoc();
+ this.acceptDocs = acceptDocs;
+ vals = outerInstance.func.getValues(weight.context, context);
+ }
+
+ public override int docID()
+ {
+ return doc;
+ }
+
+ // instead of matching all docs, we could also embed a query.
+ // the score could either ignore the subscore, or boost it.
+ // Containment: floatline(foo:myTerm, "myFloatField", 1.0, 0.0f)
+ // Boost: foo:myTerm^floatline("myFloatField",1.0,0.0f)
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public int nextDoc() throws java.io.IOException
+ public override int nextDoc()
+ {
+ for (;;)
+ {
+ ++doc;
+ if (doc >= maxDoc)
+ {
+ return doc = NO_MORE_DOCS;
+ }
+ if (acceptDocs != null && !acceptDocs.get(doc))
+ {
+ continue;
+ }
+ return doc;
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public int advance(int target) throws java.io.IOException
+ public override int advance(int target)
+ {
+ // this will work even if target==NO_MORE_DOCS
+ doc = target - 1;
+ return nextDoc();
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public float score() throws java.io.IOException
+ public override float score()
+ {
+ float score = qWeight * vals.floatVal(doc);
+
+ // Current Lucene priority queues can't handle NaN and -Infinity, so
+ // map to -Float.MAX_VALUE. This conditional handles both -infinity
+ // and NaN since comparisons with NaN are always false.
+ return score > float.NegativeInfinity ? score : -float.MaxValue;
+ }
+
+ public override long cost()
+ {
+ return maxDoc;
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public int freq() throws java.io.IOException
+ public override int freq()
+ {
+ return 1;
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: public Explanation explain(int doc) throws java.io.IOException
+ public virtual Explanation explain(int doc)
+ {
+ float sc = qWeight * vals.floatVal(doc);
+
+ Explanation result = new ComplexExplanation(true, sc, "FunctionQuery(" + outerInstance.func + "), product of:");
+
+ result.addDetail(vals.explain(doc));
+ result.addDetail(new Explanation(Boost, "boost"));
+ result.addDetail(new Explanation(weight.queryNorm,"queryNorm"));
+ return result;
+ }
+ }
+
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public Weight createWeight(IndexSearcher searcher) throws java.io.IOException
+ public override Weight createWeight(IndexSearcher searcher)
+ {
+ return new FunctionQuery.FunctionWeight(this, searcher);
+ }
+
+
+ /// <summary>
+ /// Prints a user-readable version of this query. </summary>
+ public override string ToString(string field)
+ {
+ float boost = Boost;
+ return (boost != 1.0?"(":"") + func.ToString() + (boost == 1.0 ? "" : ")^" + boost);
+ }
+
+
+ /// <summary>
+ /// Returns true if <code>o</code> is equal to this. </summary>
+ public override bool Equals(object o)
+ {
+ if (!typeof(FunctionQuery).IsInstanceOfType(o))
+ {
+ return false;
+ }
+ FunctionQuery other = (FunctionQuery)o;
+ return this.Boost == other.Boost && this.func.Equals(other.func);
+ }
+
+ /// <summary>
+ /// Returns a hash code value for this object. </summary>
+ public override int GetHashCode()
+ {
+ return func.GetHashCode() * 31 + float.floatToIntBits(Boost);
+ }
+
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/FunctionValues.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/FunctionValues.cs b/src/Lucene.Net.Queries/Function/FunctionValues.cs
new file mode 100644
index 0000000..862d0ba
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/FunctionValues.cs
@@ -0,0 +1,362 @@
+using System;
+
+namespace org.apache.lucene.queries.function
+{
+
+ /*
+ * 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.
+ */
+
+ using org.apache.lucene.search;
+ using IndexReader = org.apache.lucene.index.IndexReader;
+ using BytesRef = org.apache.lucene.util.BytesRef;
+ using MutableValue = org.apache.lucene.util.mutable.MutableValue;
+ using MutableValueFloat = org.apache.lucene.util.mutable.MutableValueFloat;
+
+ /// <summary>
+ /// Represents field values as different types.
+ /// Normally created via a <seealso cref="ValueSource"/> for a particular field and reader.
+ ///
+ ///
+ /// </summary>
+
+ // FunctionValues is distinct from ValueSource because
+ // there needs to be an object created at query evaluation time that
+ // is not referenced by the query itself because:
+ // - Query objects should be MT safe
+ // - For caching, Query objects are often used as keys... you don't
+ // want the Query carrying around big objects
+ public abstract class FunctionValues
+ {
+
+ public virtual sbyte byteVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual short shortVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ public virtual float floatVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual int intVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual long longVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual double doubleVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+ // TODO: should we make a termVal, returns BytesRef?
+ public virtual string strVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ public virtual bool boolVal(int doc)
+ {
+ return intVal(doc) != 0;
+ }
+
+ /// <summary>
+ /// returns the bytes representation of the string val - TODO: should this return the indexed raw bytes not? </summary>
+ public virtual bool bytesVal(int doc, BytesRef target)
+ {
+ string s = strVal(doc);
+ if (s == null)
+ {
+ target.length = 0;
+ return false;
+ }
+ target.copyChars(s);
+ return true;
+ }
+
+ /// <summary>
+ /// Native Java Object representation of the value </summary>
+ public virtual object objectVal(int doc)
+ {
+ // most FunctionValues are functions, so by default return a Float()
+ return floatVal(doc);
+ }
+
+ /// <summary>
+ /// Returns true if there is a value for this document </summary>
+ public virtual bool exists(int doc)
+ {
+ return true;
+ }
+
+ /// <param name="doc"> The doc to retrieve to sort ordinal for </param>
+ /// <returns> the sort ordinal for the specified doc
+ /// TODO: Maybe we can just use intVal for this... </returns>
+ public virtual int ordVal(int doc)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ /// <returns> the number of unique sort ordinals this instance has </returns>
+ public virtual int numOrd()
+ {
+ throw new System.NotSupportedException();
+ }
+ public abstract string ToString(int doc);
+
+ /// <summary>
+ /// Abstraction of the logic required to fill the value of a specified doc into
+ /// a reusable <seealso cref="MutableValue"/>. Implementations of <seealso cref="FunctionValues"/>
+ /// are encouraged to define their own implementations of ValueFiller if their
+ /// value is not a float.
+ ///
+ /// @lucene.experimental
+ /// </summary>
+ public abstract class ValueFiller
+ {
+ /// <summary>
+ /// MutableValue will be reused across calls </summary>
+ public abstract MutableValue Value {get;}
+
+ /// <summary>
+ /// MutableValue will be reused across calls. Returns true if the value exists. </summary>
+ public abstract void fillValue(int doc);
+ }
+
+ /// <summary>
+ /// @lucene.experimental </summary>
+ public virtual ValueFiller ValueFiller
+ {
+ get
+ {
+ return new ValueFillerAnonymousInnerClassHelper(this);
+ }
+ }
+
+ private class ValueFillerAnonymousInnerClassHelper : ValueFiller
+ {
+ private readonly FunctionValues outerInstance;
+
+ public ValueFillerAnonymousInnerClassHelper(FunctionValues outerInstance)
+ {
+ this.outerInstance = outerInstance;
+ mval = new MutableValueFloat();
+ }
+
+ private readonly MutableValueFloat mval;
+
+ public override MutableValue Value
+ {
+ get
+ {
+ return mval;
+ }
+ }
+
+ public override void fillValue(int doc)
+ {
+ mval.value = outerInstance.floatVal(doc);
+ }
+ }
+
+ //For Functions that can work with multiple values from the same document. This does not apply to all functions
+ public virtual void byteVal(int doc, sbyte[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual void shortVal(int doc, short[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ public virtual void floatVal(int doc, float[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual void intVal(int doc, int[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual void longVal(int doc, long[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+ public virtual void doubleVal(int doc, double[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ // TODO: should we make a termVal, fills BytesRef[]?
+ public virtual void strVal(int doc, string[] vals)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ public virtual Explanation explain(int doc)
+ {
+ return new Explanation(floatVal(doc), ToString(doc));
+ }
+
+ public virtual ValueSourceScorer getScorer(IndexReader reader)
+ {
+ return new ValueSourceScorer(reader, this);
+ }
+
+ // A RangeValueSource can't easily be a ValueSource that takes another ValueSource
+ // because it needs different behavior depending on the type of fields. There is also
+ // a setup cost - parsing and normalizing params, and doing a binary search on the StringIndex.
+ // TODO: change "reader" to AtomicReaderContext
+ public virtual ValueSourceScorer getRangeScorer(IndexReader reader, string lowerVal, string upperVal, bool includeLower, bool includeUpper)
+ {
+ float lower;
+ float upper;
+
+ if (lowerVal == null)
+ {
+ lower = float.NegativeInfinity;
+ }
+ else
+ {
+ lower = Convert.ToSingle(lowerVal);
+ }
+ if (upperVal == null)
+ {
+ upper = float.PositiveInfinity;
+ }
+ else
+ {
+ upper = Convert.ToSingle(upperVal);
+ }
+
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final float l = lower;
+ float l = lower;
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final float u = upper;
+ float u = upper;
+
+ if (includeLower && includeUpper)
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper(this, reader, this, l, u);
+ }
+ else if (includeLower && !includeUpper)
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper2(this, reader, this, l, u);
+ }
+ else if (!includeLower && includeUpper)
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper3(this, reader, this, l, u);
+ }
+ else
+ {
+ return new ValueSourceScorerAnonymousInnerClassHelper4(this, reader, this, l, u);
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper : ValueSourceScorer
+ {
+ private readonly FunctionValues outerInstance;
+
+ private float l;
+ private float u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper(FunctionValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.FunctionValues this, float l, float u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ float docVal = outerInstance.floatVal(doc);
+ return docVal >= l && docVal <= u;
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper2 : ValueSourceScorer
+ {
+ private readonly FunctionValues outerInstance;
+
+ private float l;
+ private float u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper2(FunctionValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.FunctionValues this, float l, float u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ float docVal = outerInstance.floatVal(doc);
+ return docVal >= l && docVal < u;
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper3 : ValueSourceScorer
+ {
+ private readonly FunctionValues outerInstance;
+
+ private float l;
+ private float u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper3(FunctionValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.FunctionValues this, float l, float u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ float docVal = outerInstance.floatVal(doc);
+ return docVal > l && docVal <= u;
+ }
+ }
+
+ private class ValueSourceScorerAnonymousInnerClassHelper4 : ValueSourceScorer
+ {
+ private readonly FunctionValues outerInstance;
+
+ private float l;
+ private float u;
+
+ public ValueSourceScorerAnonymousInnerClassHelper4(FunctionValues outerInstance, IndexReader reader, org.apache.lucene.queries.function.FunctionValues this, float l, float u) : base(reader, this)
+ {
+ this.outerInstance = outerInstance;
+ this.l = l;
+ this.u = u;
+ }
+
+ public override bool matchesValue(int doc)
+ {
+ float docVal = outerInstance.floatVal(doc);
+ return docVal > l && docVal < u;
+ }
+ }
+ }
+
+
+
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource.cs b/src/Lucene.Net.Queries/Function/ValueSource.cs
new file mode 100644
index 0000000..bb131af
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource.cs
@@ -0,0 +1,223 @@
+using System.Collections;
+
+namespace org.apache.lucene.queries.function
+{
+
+ /*
+ * 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.
+ */
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using FieldComparator = org.apache.lucene.search.FieldComparator;
+ using FieldComparatorSource = org.apache.lucene.search.FieldComparatorSource;
+ using IndexSearcher = org.apache.lucene.search.IndexSearcher;
+ using SortField = org.apache.lucene.search.SortField;
+
+
+ /// <summary>
+ /// Instantiates <seealso cref="FunctionValues"/> for a particular reader.
+ /// <br>
+ /// Often used when creating a <seealso cref="FunctionQuery"/>.
+ ///
+ ///
+ /// </summary>
+ public abstract class ValueSource
+ {
+
+ /// <summary>
+ /// Gets the values for this reader and the context that was previously
+ /// passed to createWeight()
+ /// </summary>
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: public abstract FunctionValues getValues(java.util.Map context, org.apache.lucene.index.AtomicReaderContext readerContext) throws java.io.IOException;
+ public abstract FunctionValues getValues(IDictionary context, AtomicReaderContext readerContext);
+
+ public override abstract bool Equals(object o);
+
+ public override abstract int GetHashCode();
+
+ /// <summary>
+ /// description of field, used in explain()
+ /// </summary>
+ public abstract string description();
+
+ public override string ToString()
+ {
+ return description();
+ }
+
+
+ /// <summary>
+ /// Implementations should propagate createWeight to sub-ValueSources which can optionally store
+ /// weight info in the context. The context object will be passed to getValues()
+ /// where this info can be retrieved.
+ /// </summary>
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: public void createWeight(java.util.Map context, org.apache.lucene.search.IndexSearcher searcher) throws java.io.IOException
+ public virtual void createWeight(IDictionary context, IndexSearcher searcher)
+ {
+ }
+
+ /// <summary>
+ /// Returns a new non-threadsafe context map.
+ /// </summary>
+ public static IDictionary newContext(IndexSearcher searcher)
+ {
+ IDictionary context = new IdentityHashMap();
+ context["searcher"] = searcher;
+ return context;
+ }
+
+
+ //
+ // Sorting by function
+ //
+
+ /// <summary>
+ /// EXPERIMENTAL: This method is subject to change.
+ /// <para>
+ /// Get the SortField for this ValueSource. Uses the <seealso cref="#getValues(java.util.Map, AtomicReaderContext)"/>
+ /// to populate the SortField.
+ ///
+ /// </para>
+ /// </summary>
+ /// <param name="reverse"> true if this is a reverse sort. </param>
+ /// <returns> The <seealso cref="org.apache.lucene.search.SortField"/> for the ValueSource </returns>
+ public virtual SortField getSortField(bool reverse)
+ {
+ return new ValueSourceSortField(this, reverse);
+ }
+
+ internal class ValueSourceSortField : SortField
+ {
+ private readonly ValueSource outerInstance;
+
+ public ValueSourceSortField(ValueSource outerInstance, bool reverse) : base(outerInstance.description(), SortField.Type.REWRITEABLE, reverse)
+ {
+ this.outerInstance = outerInstance;
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.search.SortField rewrite(org.apache.lucene.search.IndexSearcher searcher) throws java.io.IOException
+ public override SortField rewrite(IndexSearcher searcher)
+ {
+ IDictionary context = newContext(searcher);
+ outerInstance.createWeight(context, searcher);
+ return new SortField(Field, new ValueSourceComparatorSource(outerInstance, context), Reverse);
+ }
+ }
+
+ internal class ValueSourceComparatorSource : FieldComparatorSource
+ {
+ private readonly ValueSource outerInstance;
+
+ internal readonly IDictionary context;
+
+ public ValueSourceComparatorSource(ValueSource outerInstance, IDictionary context)
+ {
+ this.outerInstance = outerInstance;
+ this.context = context;
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.search.FieldComparator<Double> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws java.io.IOException
+ public override FieldComparator<double?> newComparator(string fieldname, int numHits, int sortPos, bool reversed)
+ {
+ return new ValueSourceComparator(outerInstance, context, numHits);
+ }
+ }
+
+ /// <summary>
+ /// Implement a <seealso cref="org.apache.lucene.search.FieldComparator"/> that works
+ /// off of the <seealso cref="FunctionValues"/> for a ValueSource
+ /// instead of the normal Lucene FieldComparator that works off of a FieldCache.
+ /// </summary>
+ internal class ValueSourceComparator : FieldComparator<double?>
+ {
+ private readonly ValueSource outerInstance;
+
+ internal readonly double[] values;
+ internal FunctionValues docVals;
+ internal double bottom;
+ internal readonly IDictionary fcontext;
+ internal double topValue;
+
+ internal ValueSourceComparator(ValueSource outerInstance, IDictionary fcontext, int numHits)
+ {
+ this.outerInstance = outerInstance;
+ this.fcontext = fcontext;
+ values = new double[numHits];
+ }
+
+ public override int compare(int slot1, int slot2)
+ {
+ return values[slot1].CompareTo(values[slot2]);
+ }
+
+ public override int compareBottom(int doc)
+ {
+ return bottom.CompareTo(docVals.doubleVal(doc));
+ }
+
+ public override void copy(int slot, int doc)
+ {
+ values[slot] = docVals.doubleVal(doc);
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.search.FieldComparator setNextReader(org.apache.lucene.index.AtomicReaderContext context) throws java.io.IOException
+ public override FieldComparator setNextReader(AtomicReaderContext context)
+ {
+ docVals = outerInstance.getValues(fcontext, context);
+ return this;
+ }
+
+//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
+//ORIGINAL LINE: @Override public void setBottom(final int bottom)
+ public override int Bottom
+ {
+ set
+ {
+ this.bottom = values[value];
+ }
+ }
+
+//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
+//ORIGINAL LINE: @Override public void setTopValue(final Double value)
+ public override double? TopValue
+ {
+ set
+ {
+ this.topValue = (double)value;
+ }
+ }
+
+ public override double? value(int slot)
+ {
+ return values[slot];
+ }
+
+ public override int compareTop(int doc)
+ {
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final double docValue = docVals.doubleVal(doc);
+ double docValue = docVals.doubleVal(doc);
+ return topValue.CompareTo(docValue);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource/BoolFunction.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource/BoolFunction.cs b/src/Lucene.Net.Queries/Function/ValueSource/BoolFunction.cs
new file mode 100644
index 0000000..85399df
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource/BoolFunction.cs
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+
+namespace org.apache.lucene.queries.function.valuesource
+{
+
+ /// <summary>
+ /// Abstract parent class for those <seealso cref="ValueSource"/> implementations which
+ /// apply boolean logic to their values
+ /// </summary>
+ public abstract class BoolFunction : ValueSource
+ {
+ // TODO: placeholder to return type, among other common future functionality
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource/ByteFieldSource.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource/ByteFieldSource.cs b/src/Lucene.Net.Queries/Function/ValueSource/ByteFieldSource.cs
new file mode 100644
index 0000000..0e2ffc6
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource/ByteFieldSource.cs
@@ -0,0 +1,142 @@
+using System;
+using System.Collections;
+
+namespace org.apache.lucene.queries.function.valuesource
+{
+ /*
+ * 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.
+ */
+
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using FieldCache = org.apache.lucene.search.FieldCache;
+
+ /// <summary>
+ /// Obtains int field values from the <seealso cref="org.apache.lucene.search.FieldCache"/>
+ /// using <code>getInts()</code>
+ /// and makes those values available as other numeric types, casting as needed. *
+ ///
+ ///
+ /// </summary>
+ [Obsolete]
+ public class ByteFieldSource : FieldCacheSource
+ {
+
+ private readonly FieldCache.ByteParser parser;
+
+ public ByteFieldSource(string field) : this(field, null)
+ {
+ }
+
+ public ByteFieldSource(string field, FieldCache.ByteParser parser) : base(field)
+ {
+ this.parser = parser;
+ }
+
+ public override string description()
+ {
+ return "byte(" + field + ')';
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.queries.function.FunctionValues getValues(java.util.Map context, org.apache.lucene.index.AtomicReaderContext readerContext) throws java.io.IOException
+ public override FunctionValues getValues(IDictionary context, AtomicReaderContext readerContext)
+ {
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final org.apache.lucene.search.FieldCache.Bytes arr = cache.getBytes(readerContext.reader(), field, parser, false);
+ FieldCache.Bytes arr = cache.getBytes(readerContext.reader(), field, parser, false);
+
+ return new FunctionValuesAnonymousInnerClassHelper(this, arr);
+ }
+
+ private class FunctionValuesAnonymousInnerClassHelper : FunctionValues
+ {
+ private readonly ByteFieldSource outerInstance;
+
+ private FieldCache.Bytes arr;
+
+ public FunctionValuesAnonymousInnerClassHelper(ByteFieldSource outerInstance, FieldCache.Bytes arr)
+ {
+ this.outerInstance = outerInstance;
+ this.arr = arr;
+ }
+
+ public override sbyte byteVal(int doc)
+ {
+ return arr.get(doc);
+ }
+
+ public override short shortVal(int doc)
+ {
+ return (short) arr.get(doc);
+ }
+
+ public override float floatVal(int doc)
+ {
+ return (float) arr.get(doc);
+ }
+
+ public override int intVal(int doc)
+ {
+ return (int) arr.get(doc);
+ }
+
+ public override long longVal(int doc)
+ {
+ return (long) arr.get(doc);
+ }
+
+ public override double doubleVal(int doc)
+ {
+ return (double) arr.get(doc);
+ }
+
+ public override string strVal(int doc)
+ {
+ return Convert.ToString(arr.get(doc));
+ }
+
+ public override string ToString(int doc)
+ {
+ return outerInstance.description() + '=' + byteVal(doc);
+ }
+
+ public override object objectVal(int doc)
+ {
+ return arr.get(doc); // TODO: valid?
+ }
+
+ }
+
+ public override bool Equals(object o)
+ {
+ if (o.GetType() != typeof(ByteFieldSource))
+ {
+ return false;
+ }
+ ByteFieldSource other = (ByteFieldSource) o;
+ return base.Equals(other) && (this.parser == null ? other.parser == null : this.parser.GetType() == other.parser.GetType());
+ }
+
+ public override int GetHashCode()
+ {
+ int h = parser == null ? typeof(sbyte?).GetHashCode() : parser.GetType().GetHashCode();
+ h += base.GetHashCode();
+ return h;
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource/BytesRefFieldSource.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource/BytesRefFieldSource.cs b/src/Lucene.Net.Queries/Function/ValueSource/BytesRefFieldSource.cs
new file mode 100644
index 0000000..f091aee
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource/BytesRefFieldSource.cs
@@ -0,0 +1,140 @@
+using System.Collections;
+
+namespace org.apache.lucene.queries.function.valuesource
+{
+
+ /*
+ * 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.
+ */
+
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using BinaryDocValues = org.apache.lucene.index.BinaryDocValues;
+ using FieldInfo = org.apache.lucene.index.FieldInfo;
+ using DocValuesType = org.apache.lucene.index.FieldInfo.DocValuesType;
+ using DocTermsIndexDocValues = org.apache.lucene.queries.function.docvalues.DocTermsIndexDocValues;
+ using FieldCache = org.apache.lucene.search.FieldCache;
+ using Bits = org.apache.lucene.util.Bits;
+ using BytesRef = org.apache.lucene.util.BytesRef;
+
+ /// <summary>
+ /// An implementation for retrieving <seealso cref="FunctionValues"/> instances for string based fields.
+ /// </summary>
+ public class BytesRefFieldSource : FieldCacheSource
+ {
+
+ public BytesRefFieldSource(string field) : base(field)
+ {
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.queries.function.FunctionValues getValues(java.util.Map context, org.apache.lucene.index.AtomicReaderContext readerContext) throws java.io.IOException
+ public override FunctionValues getValues(IDictionary context, AtomicReaderContext readerContext)
+ {
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final org.apache.lucene.index.FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field);
+ FieldInfo fieldInfo = readerContext.reader().FieldInfos.fieldInfo(field);
+ // To be sorted or not to be sorted, that is the question
+ // TODO: do it cleaner?
+ if (fieldInfo != null && fieldInfo.DocValuesType == FieldInfo.DocValuesType.BINARY)
+ {
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final org.apache.lucene.index.BinaryDocValues binaryValues = org.apache.lucene.search.FieldCache.DEFAULT.getTerms(readerContext.reader(), field, true);
+ BinaryDocValues binaryValues = FieldCache.DEFAULT.getTerms(readerContext.reader(), field, true);
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final org.apache.lucene.util.Bits docsWithField = org.apache.lucene.search.FieldCache.DEFAULT.getDocsWithField(readerContext.reader(), field);
+ Bits docsWithField = FieldCache.DEFAULT.getDocsWithField(readerContext.reader(), field);
+ return new FunctionValuesAnonymousInnerClassHelper(this, binaryValues, docsWithField);
+ }
+ else
+ {
+ return new DocTermsIndexDocValuesAnonymousInnerClassHelper(this, this, readerContext, field);
+ }
+ }
+
+ private class FunctionValuesAnonymousInnerClassHelper : FunctionValues
+ {
+ private readonly BytesRefFieldSource outerInstance;
+
+ private BinaryDocValues binaryValues;
+ private Bits docsWithField;
+
+ public FunctionValuesAnonymousInnerClassHelper(BytesRefFieldSource outerInstance, BinaryDocValues binaryValues, Bits docsWithField)
+ {
+ this.outerInstance = outerInstance;
+ this.binaryValues = binaryValues;
+ this.docsWithField = docsWithField;
+ }
+
+
+ public override bool exists(int doc)
+ {
+ return docsWithField.get(doc);
+ }
+
+ public override bool bytesVal(int doc, BytesRef target)
+ {
+ binaryValues.get(doc, target);
+ return target.length > 0;
+ }
+
+ public override string strVal(int doc)
+ {
+//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
+//ORIGINAL LINE: final org.apache.lucene.util.BytesRef bytes = new org.apache.lucene.util.BytesRef();
+ BytesRef bytes = new BytesRef();
+ return bytesVal(doc, bytes) ? bytes.utf8ToString() : null;
+ }
+
+ public override object objectVal(int doc)
+ {
+ return strVal(doc);
+ }
+
+ public override string ToString(int doc)
+ {
+ return outerInstance.description() + '=' + strVal(doc);
+ }
+ }
+
+ private class DocTermsIndexDocValuesAnonymousInnerClassHelper : DocTermsIndexDocValues
+ {
+ private readonly BytesRefFieldSource outerInstance;
+
+ public DocTermsIndexDocValuesAnonymousInnerClassHelper(BytesRefFieldSource outerInstance, org.apache.lucene.queries.function.valuesource.BytesRefFieldSource this, AtomicReaderContext readerContext, string field) : base(this, readerContext, field)
+ {
+ this.outerInstance = outerInstance;
+ }
+
+
+ protected internal override string toTerm(string readableValue)
+ {
+ return readableValue;
+ }
+
+ public override object objectVal(int doc)
+ {
+ return strVal(doc);
+ }
+
+ public override string ToString(int doc)
+ {
+ return outerInstance.description() + '=' + strVal(doc);
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource/ConstNumberSource.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource/ConstNumberSource.cs b/src/Lucene.Net.Queries/Function/ValueSource/ConstNumberSource.cs
new file mode 100644
index 0000000..33a0aa0
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource/ConstNumberSource.cs
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+namespace org.apache.lucene.queries.function.valuesource
+{
+
+ /// <summary>
+ /// <code>ConstNumberSource</code> is the base class for all constant numbers
+ /// </summary>
+ public abstract class ConstNumberSource : ValueSource
+ {
+ public abstract int Int {get;}
+ public abstract long Long {get;}
+ public abstract float Float {get;}
+ public abstract double Double {get;}
+ public abstract Number Number {get;}
+ public abstract bool Bool {get;}
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource/ConstValueSource.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource/ConstValueSource.cs b/src/Lucene.Net.Queries/Function/ValueSource/ConstValueSource.cs
new file mode 100644
index 0000000..9933f13
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource/ConstValueSource.cs
@@ -0,0 +1,156 @@
+using System.Collections;
+
+/*
+ * 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.
+ */
+
+namespace org.apache.lucene.queries.function.valuesource
+{
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using FloatDocValues = org.apache.lucene.queries.function.docvalues.FloatDocValues;
+
+
+ /// <summary>
+ /// <code>ConstValueSource</code> returns a constant for all documents
+ /// </summary>
+ public class ConstValueSource : ConstNumberSource
+ {
+ internal readonly float constant;
+ private readonly double dv;
+
+ public ConstValueSource(float constant)
+ {
+ this.constant = constant;
+ this.dv = constant;
+ }
+
+ public override string description()
+ {
+ return "const(" + constant + ")";
+ }
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.queries.function.FunctionValues getValues(java.util.Map context, org.apache.lucene.index.AtomicReaderContext readerContext) throws java.io.IOException
+ public override FunctionValues getValues(IDictionary context, AtomicReaderContext readerContext)
+ {
+ return new FloatDocValuesAnonymousInnerClassHelper(this, this);
+ }
+
+ private class FloatDocValuesAnonymousInnerClassHelper : FloatDocValues
+ {
+ private readonly ConstValueSource outerInstance;
+
+ public FloatDocValuesAnonymousInnerClassHelper(ConstValueSource outerInstance, org.apache.lucene.queries.function.valuesource.ConstValueSource this) : base(this)
+ {
+ this.outerInstance = outerInstance;
+ }
+
+ public override float floatVal(int doc)
+ {
+ return outerInstance.constant;
+ }
+ public override int intVal(int doc)
+ {
+ return (int)outerInstance.constant;
+ }
+ public override long longVal(int doc)
+ {
+ return (long)outerInstance.constant;
+ }
+ public override double doubleVal(int doc)
+ {
+ return outerInstance.dv;
+ }
+ public override string ToString(int doc)
+ {
+ return outerInstance.description();
+ }
+ public override object objectVal(int doc)
+ {
+ return outerInstance.constant;
+ }
+ public override bool boolVal(int doc)
+ {
+ return outerInstance.constant != 0.0f;
+ }
+ }
+
+ public override int GetHashCode()
+ {
+ return float.floatToIntBits(constant) * 31;
+ }
+
+ public override bool Equals(object o)
+ {
+ if (!(o is ConstValueSource))
+ {
+ return false;
+ }
+ ConstValueSource other = (ConstValueSource)o;
+ return this.constant == other.constant;
+ }
+
+ public override int Int
+ {
+ get
+ {
+ return (int)constant;
+ }
+ }
+
+ public override long Long
+ {
+ get
+ {
+ return (long)constant;
+ }
+ }
+
+ public override float Float
+ {
+ get
+ {
+ return constant;
+ }
+ }
+
+ public override double Double
+ {
+ get
+ {
+ return dv;
+ }
+ }
+
+ public override Number Number
+ {
+ get
+ {
+ return constant;
+ }
+ }
+
+ public override bool Bool
+ {
+ get
+ {
+ return constant != 0.0f;
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/882f487d/src/Lucene.Net.Queries/Function/ValueSource/DefFunction.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Queries/Function/ValueSource/DefFunction.cs b/src/Lucene.Net.Queries/Function/ValueSource/DefFunction.cs
new file mode 100644
index 0000000..48a67d4
--- /dev/null
+++ b/src/Lucene.Net.Queries/Function/ValueSource/DefFunction.cs
@@ -0,0 +1,153 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace org.apache.lucene.queries.function.valuesource
+{
+ /*
+ * 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.
+ */
+
+ using AtomicReaderContext = org.apache.lucene.index.AtomicReaderContext;
+ using IndexSearcher = org.apache.lucene.search.IndexSearcher;
+ using BytesRef = org.apache.lucene.util.BytesRef;
+
+
+ /// <summary>
+ /// <seealso cref="ValueSource"/> implementation which only returns the values from the provided
+ /// ValueSources which are available for a particular docId. Consequently, when combined
+ /// with a <seealso cref="ConstValueSource"/>, this function serves as a way to return a default
+ /// value when the values for a field are unavailable.
+ /// </summary>
+ public class DefFunction : MultiFunction
+ {
+ public DefFunction(IList<ValueSource> sources) : base(sources)
+ {
+ }
+
+ protected internal override string name()
+ {
+ return "def";
+ }
+
+
+//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
+//ORIGINAL LINE: @Override public org.apache.lucene.queries.function.FunctionValues getValues(java.util.Map fcontext, org.apache.lucene.index.AtomicReaderContext readerContext) throws java.io.IOException
+ public override FunctionValues getValues(IDictionary fcontext, AtomicReaderContext readerContext)
+ {
+
+
+ return new ValuesAnonymousInnerClassHelper(this, valsArr(sources, fcontext, readerContext));
+ }
+
+ private class ValuesAnonymousInnerClassHelper : Values
+ {
+ private readonly DefFunction outerInstance;
+
+ public ValuesAnonymousInnerClassHelper(DefFunction outerInstance, FunctionValues[] valsArr) : base(outerInstance, valsArr)
+ {
+ this.outerInstance = outerInstance;
+ upto = valsArr.Length - 1;
+ }
+
+ internal readonly int upto;
+
+ private FunctionValues get(int doc)
+ {
+ for (int i = 0; i < upto; i++)
+ {
+ FunctionValues vals = valsArr[i];
+ if (vals.exists(doc))
+ {
+ return vals;
+ }
+ }
+ return valsArr[upto];
+ }
+
+ public override sbyte byteVal(int doc)
+ {
+ return get(doc).byteVal(doc);
+ }
+
+ public override short shortVal(int doc)
+ {
+ return get(doc).shortVal(doc);
+ }
+
+ public override float floatVal(int doc)
+ {
+ return get(doc).floatVal(doc);
+ }
+
+ public override int intVal(int doc)
+ {
+ return get(doc).intVal(doc);
+ }
+
+ public override long longVal(int doc)
+ {
+ return get(doc).longVal(doc);
+ }
+
+ public override double doubleVal(int doc)
+ {
+ return get(doc).doubleVal(doc);
+ }
+
+ public override string strVal(int doc)
+ {
+ return get(doc).strVal(doc);
+ }
+
+ public override bool boolVal(int doc)
+ {
+ return get(doc).boolVal(doc);
+ }
+
+ public override bool bytesVal(int doc, BytesRef target)
+ {
+ return get(doc).bytesVal(doc, target);
+ }
+
+ public override object objectVal(int doc)
+ {
+ return get(doc).objectVal(doc);
+ }
+
+ public override bool exists(int doc)
+ {
+ // return true if any source is exists?
+ foreach (FunctionValues vals in valsArr)
+ {
+ if (vals.exists(doc))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public override ValueFiller ValueFiller
+ {
+ get
+ {
+ // TODO: need ValueSource.type() to determine correct type
+ return base.ValueFiller;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file