This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 84517cc02ef7afd9df1db18349b4d70b9b50f4e8 Author: Semirk0 <[email protected]> AuthorDate: Wed Nov 4 12:34:19 2020 +0100 Adding overloads to Document for GetValues and Get --- src/Lucene.Net.Misc/Document/LazyDocument.cs | 5 + .../Index/TestIndexWriterExceptions.cs | 5 + src/Lucene.Net.Tests/Index/TestIndexableField.cs | 5 + src/Lucene.Net/Document/Document.cs | 192 ++++++++++++++++++++- src/Lucene.Net/Document/Field.cs | 20 +++ src/Lucene.Net/Index/IndexableField.cs | 6 + 6 files changed, 229 insertions(+), 4 deletions(-) diff --git a/src/Lucene.Net.Misc/Document/LazyDocument.cs b/src/Lucene.Net.Misc/Document/LazyDocument.cs index 1436cae..92c886b 100644 --- a/src/Lucene.Net.Misc/Document/LazyDocument.cs +++ b/src/Lucene.Net.Misc/Document/LazyDocument.cs @@ -354,6 +354,11 @@ namespace Lucene.Net.Documents { return GetRealValue().GetTokenStream(analyzer); } + + public virtual string ToString(IFormatProvider provider) + { + return GetRealValue().ToString(provider); + } } } } \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs index c9ca8a4..8833cb8 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs @@ -2043,6 +2043,11 @@ namespace Lucene.Net.Index { return null; } + + public virtual string ToString(IFormatProvider provider) + { + return GetStringValue().ToString(provider); + } } // See LUCENE-4870 TooManyOpenFiles errors are thrown as diff --git a/src/Lucene.Net.Tests/Index/TestIndexableField.cs b/src/Lucene.Net.Tests/Index/TestIndexableField.cs index 06b39ba..89e9efa 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexableField.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexableField.cs @@ -225,6 +225,11 @@ namespace Lucene.Net.Index { return GetReaderValue() != null ? analyzer.GetTokenStream(Name, GetReaderValue()) : analyzer.GetTokenStream(Name, new StringReader(GetStringValue())); } + + public virtual string ToString(IFormatProvider provider) + { + return GetStringValue().ToString(provider); + } } // Silly test showing how to index documents w/o using Lucene's core diff --git a/src/Lucene.Net/Document/Document.cs b/src/Lucene.Net/Document/Document.cs index 4e5f186..0f1a081 100644 --- a/src/Lucene.Net/Document/Document.cs +++ b/src/Lucene.Net/Document/Document.cs @@ -235,9 +235,101 @@ namespace Lucene.Net.Documents var result = new List<string>(); foreach (IIndexableField field in fields) { - if (field.Name.Equals(name, StringComparison.Ordinal) && field.GetStringValue() != null) + string fieldStringValue = field.GetStringValue(); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) { - result.Add(field.GetStringValue()); + result.Add(fieldStringValue); + } + } + + if (result.Count == 0) + { + return NO_STRINGS; + } + + return result.ToArray(); + } + + /// <summary> + /// Returns an array of values of the field specified as the method parameter. + /// This method returns an empty array when there are no + /// matching fields. It never returns <c>null</c>. + /// For <see cref="Int32Field"/>, <see cref="Int64Field"/>, + /// <see cref="SingleField"/> and <seealso cref="DoubleField"/> it returns the string value of the number. If you want + /// the actual numeric field instances back, use <see cref="GetFields(string)"/>. </summary> + /// <param name="name"> the name of the field </param> + /// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param> + /// <returns> a <see cref="T:string[]"/> of field values </returns> + public string[] GetValues(string name, string format) + { + var result = new List<string>(); + foreach (IIndexableField field in fields) + { + string fieldStringValue = field.GetStringValue(format); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) + { + result.Add(fieldStringValue); + } + } + + if (result.Count == 0) + { + return NO_STRINGS; + } + + return result.ToArray(); + } + + /// <summary> + /// Returns an array of values of the field specified as the method parameter. + /// This method returns an empty array when there are no + /// matching fields. It never returns <c>null</c>. + /// For <see cref="Int32Field"/>, <see cref="Int64Field"/>, + /// <see cref="SingleField"/> and <seealso cref="DoubleField"/> it returns the string value of the number. If you want + /// the actual numeric field instances back, use <see cref="GetFields(string)"/>. </summary> + /// <param name="name"> the name of the field </param> + /// <param name = "provider" > An object that supplies culture-specific formatting information.This parameter has no effect if this field is non-numeric.</param> + /// <returns> a <see cref="T:string[]"/> of field values </returns> + public string[] GetValues(string name, IFormatProvider provider) + { + var result = new List<string>(); + foreach (IIndexableField field in fields) + { + string fieldStringValue = field.GetStringValue(provider); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) + { + result.Add(fieldStringValue); + } + } + + if (result.Count == 0) + { + return NO_STRINGS; + } + + return result.ToArray(); + } + + /// <summary> + /// Returns an array of values of the field specified as the method parameter. + /// This method returns an empty array when there are no + /// matching fields. It never returns <c>null</c>. + /// For <see cref="Int32Field"/>, <see cref="Int64Field"/>, + /// <see cref="SingleField"/> and <seealso cref="DoubleField"/> it returns the string value of the number. If you want + /// the actual numeric field instances back, use <see cref="GetFields(string)"/>. </summary> + /// <param name="name"> the name of the field </param> + /// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param> + /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param> + /// <returns> a <see cref="T:string[]"/> of field values </returns> + public string[] GetValues(string name, string format, IFormatProvider provider) + { + var result = new List<string>(); + foreach (IIndexableField field in fields) + { + string fieldStringValue = field.GetStringValue(format, provider); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) + { + result.Add(fieldStringValue); } } @@ -262,9 +354,80 @@ namespace Lucene.Net.Documents { foreach (IIndexableField field in fields) { - if (field.Name.Equals(name, StringComparison.Ordinal) && field.GetStringValue() != null) + string fieldStringValue = field.GetStringValue(); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) { - return field.GetStringValue(); + return fieldStringValue; + } + } + return null; + } + + /// <summary> + /// Returns the string value of the field with the given name if any exist in + /// this document, or <c>null</c>. If multiple fields exist with this name, this + /// method returns the first value added. If only binary fields with this name + /// exist, returns <c>null</c>. + /// For <see cref="Int32Field"/>, <see cref="Int64Field"/>, + /// <see cref="SingleField"/> and <seealso cref="DoubleField"/> it returns the string value of the number. If you want + /// the actual numeric field instance back, use <see cref="GetField(string)"/>. + /// </summary> + /// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param> + public string Get(string name, string format) + { + foreach (IIndexableField field in fields) + { + string fieldStringValue = field.GetStringValue(format); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) + { + return fieldStringValue; + } + } + return null; + } + + /// <summary> + /// Returns the string value of the field with the given name if any exist in + /// this document, or <c>null</c>. If multiple fields exist with this name, this + /// method returns the first value added. If only binary fields with this name + /// exist, returns <c>null</c>. + /// For <see cref="Int32Field"/>, <see cref="Int64Field"/>, + /// <see cref="SingleField"/> and <seealso cref="DoubleField"/> it returns the string value of the number. If you want + /// the actual numeric field instance back, use <see cref="GetField(string)"/>. + /// </summary> + /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param> + public string Get(string name, IFormatProvider provider) + { + foreach (IIndexableField field in fields) + { + string fieldStringValue = field.GetStringValue(provider); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) + { + return fieldStringValue; + } + } + return null; + } + + /// <summary> + /// Returns the string value of the field with the given name if any exist in + /// this document, or <c>null</c>. If multiple fields exist with this name, this + /// method returns the first value added. If only binary fields with this name + /// exist, returns <c>null</c>. + /// For <see cref="Int32Field"/>, <see cref="Int64Field"/>, + /// <see cref="SingleField"/> and <seealso cref="DoubleField"/> it returns the string value of the number. If you want + /// the actual numeric field instance back, use <see cref="GetField(string)"/>. + /// </summary> + /// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param> + /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param> + public string Get(string name, string format, IFormatProvider provider) + { + foreach (IIndexableField field in fields) + { + string fieldStringValue = field.GetStringValue(format, provider); + if (field.Name.Equals(name, StringComparison.Ordinal) && fieldStringValue != null) + { + return fieldStringValue; } } return null; @@ -288,5 +451,26 @@ namespace Lucene.Net.Documents buffer.Append(">"); return buffer.ToString(); } + + /// <summary> + /// Prints the fields of a document for human consumption. + /// </summary> + /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param> + public string ToString(IFormatProvider provider) + { + var buffer = new StringBuilder(); + buffer.Append("Document<"); + for (int i = 0; i < fields.Count; i++) + { + IIndexableField field = fields[i]; + buffer.Append(field.ToString(provider)); + if (i != fields.Count - 1) + { + buffer.Append(" "); + } + } + buffer.Append(">"); + return buffer.ToString(); + } } } \ No newline at end of file diff --git a/src/Lucene.Net/Document/Field.cs b/src/Lucene.Net/Document/Field.cs index a620ebc..801fcb4 100644 --- a/src/Lucene.Net/Document/Field.cs +++ b/src/Lucene.Net/Document/Field.cs @@ -796,6 +796,26 @@ namespace Lucene.Net.Documents } /// <summary> + /// Prints a <see cref="Field"/> for human consumption. + /// </summary> + /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param> + public virtual string ToString(IFormatProvider provider) + { + StringBuilder result = new StringBuilder(); + + if(FieldsData != null) + { + result.AppendFormat(provider, "{0}<{1}:{3}>", m_type.ToString(), m_name.ToString(), FieldsData); + } + else + { + result.AppendFormat(provider, "{0}<{1}:>", m_type.ToString(), m_name.ToString()); + } + + return result.ToString(); + } + + /// <summary> /// Returns the <see cref="Documents.FieldType"/> for this field as type <see cref="Documents.FieldType"/>. </summary> // LUCENENET specific property to prevent the need to cast. The FieldType property was renamed IndexableFieldType // in order to accommodate this (more Lucene like) property. diff --git a/src/Lucene.Net/Index/IndexableField.cs b/src/Lucene.Net/Index/IndexableField.cs index 18fc0ee..93f0b75 100644 --- a/src/Lucene.Net/Index/IndexableField.cs +++ b/src/Lucene.Net/Index/IndexableField.cs @@ -201,5 +201,11 @@ namespace Lucene.Net.Index /// a non-null value if the field is to be indexed </returns> /// <exception cref="IOException"> Can be thrown while creating the <see cref="TokenStream"/> </exception> TokenStream GetTokenStream(Analyzer analyzer); + + /// <summary> + /// Prints a <see cref="Field"/> for human consumption. + /// </summary> + /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param> + string ToString(IFormatProvider provider); } } \ No newline at end of file
