API: Lucene.Net.Support.Document: Added extension methods to make casting to the correct field type simpler.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/9c5085c4 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/9c5085c4 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/9c5085c4 Branch: refs/heads/master Commit: 9c5085c4b6c6e16aaa10eb79ac645789af5b2b6c Parents: 2fedc57 Author: Shad Storhaug <[email protected]> Authored: Fri Jul 14 01:34:03 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Fri Jul 14 01:34:03 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net/Document/Document.cs | 2 +- src/Lucene.Net/Lucene.Net.csproj | 1 + .../Support/Document/DocumentExtensions.cs | 39 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9c5085c4/src/Lucene.Net/Document/Document.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Document/Document.cs b/src/Lucene.Net/Document/Document.cs index d19f874..e67946f 100644 --- a/src/Lucene.Net/Document/Document.cs +++ b/src/Lucene.Net/Document/Document.cs @@ -192,7 +192,7 @@ namespace Lucene.Net.Documents /// <summary> /// Returns an array of <see cref="IIndexableField"/>s with the given name. - /// this method returns an empty array when there are no + /// This method returns an empty array when there are no /// matching fields. It never returns <c>null</c>. /// </summary> /// <param name="name"> the name of the field </param> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9c5085c4/src/Lucene.Net/Lucene.Net.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj index ff96872..d2b8e6e 100644 --- a/src/Lucene.Net/Lucene.Net.csproj +++ b/src/Lucene.Net/Lucene.Net.csproj @@ -394,6 +394,7 @@ <Compile Include="Index\TwoStoredFieldsConsumers.cs" /> <Compile Include="Index\UpgradeIndexMergePolicy.cs" /> <Compile Include="LucenePackage.cs" /> + <Compile Include="Support\Document\DocumentExtensions.cs" /> <Compile Include="Support\IO\Compression\LZOCompressor.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Search\AutomatonQuery.cs" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9c5085c4/src/Lucene.Net/Support/Document/DocumentExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/Document/DocumentExtensions.cs b/src/Lucene.Net/Support/Document/DocumentExtensions.cs new file mode 100644 index 0000000..c04dc21 --- /dev/null +++ b/src/Lucene.Net/Support/Document/DocumentExtensions.cs @@ -0,0 +1,39 @@ +using Lucene.Net.Index; +using System.Linq; + +namespace Lucene.Net.Documents +{ + /// <summary> + /// Extension methods to the <see cref="Document"/> class. + /// </summary> + public static class DocumentExtensions + { + /// <summary> + /// Returns a field with the given name if any exist in this document cast to type <typeparam name="T"/>, or + /// <c>null</c>. If multiple fields exists with this name, this method returns the + /// first value added. + /// <para/> + /// LUCENENET specific + /// </summary> + /// <exception cref="InvalidCastException">If the field type cannot be cast to <typeparam name="T"/>.</exception> + public static T GetField<T>(this Document document, string name) where T : IIndexableField + { + return (T)document.GetField(name); + } + + /// <summary> + /// Returns an array of <see cref="IIndexableField"/>s with the given name, cast to type <typeparam name="T"/>. + /// This method returns an empty array when there are no + /// matching fields. It never returns <c>null</c>. + /// <para/> + /// LUCENENET specific + /// </summary> + /// <param name="name"> the name of the field </param> + /// <returns> a <see cref="T:IndexableField[]"/> array </returns> + /// <exception cref="InvalidCastException">If the field type cannot be cast to <typeparam name="T"/>.</exception> + public static T[] GetFields<T>(this Document document, string name) where T : IIndexableField + { + return document.GetFields(name).Cast<T>().ToArray(); + } + } +}
