Lucene.Net.Facet: Added extension methods to Document class for adding facet-related Field types
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/a032177a Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/a032177a Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/a032177a Branch: refs/heads/master Commit: a032177a1a300d01df1fbe28766c56b4aeec21ad Parents: 62c6407 Author: Shad Storhaug <[email protected]> Authored: Thu Oct 5 00:29:23 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Thu Oct 5 03:49:22 2017 +0700 ---------------------------------------------------------------------- .../Facet/AssociationsFacetsExample.cs | 13 +-- .../Support/Document/DocumentExtensions.cs | 111 +++++++++++++++++++ 2 files changed, 117 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a032177a/src/Lucene.Net.Demo/Facet/AssociationsFacetsExample.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Demo/Facet/AssociationsFacetsExample.cs b/src/Lucene.Net.Demo/Facet/AssociationsFacetsExample.cs index f3649d0..1641bf1 100644 --- a/src/Lucene.Net.Demo/Facet/AssociationsFacetsExample.cs +++ b/src/Lucene.Net.Demo/Facet/AssociationsFacetsExample.cs @@ -76,21 +76,20 @@ namespace Lucene.Net.Demo.Facet Document doc = new Document(); // 3 occurrences for tag 'lucene' - // LUCENENET TODO: Create extension methods for built-in field types ( .AddFacetField("", ""), .AddInt32AssociationsFacetField(), etc ). - doc.Add(new Int32AssociationFacetField(3, "tags", "lucene")); + doc.AddInt32AssociationFacetField(3, "tags", "lucene"); // 87% confidence level of genre 'computing' - doc.Add(new SingleAssociationFacetField(0.87f, "genre", "computing")); + doc.AddSingleAssociationFacetField(0.87f, "genre", "computing"); indexWriter.AddDocument(config.Build(taxoWriter, doc)); doc = new Document(); // 1 occurrence for tag 'lucene' - doc.Add(new Int32AssociationFacetField(1, "tags", "lucene")); + doc.AddInt32AssociationFacetField(1, "tags", "lucene"); // 2 occurrence for tag 'solr' - doc.Add(new Int32AssociationFacetField(2, "tags", "solr")); + doc.AddInt32AssociationFacetField(2, "tags", "solr"); // 75% confidence level of genre 'computing' - doc.Add(new SingleAssociationFacetField(0.75f, "genre", "computing")); + doc.AddSingleAssociationFacetField(0.75f, "genre", "computing"); // 34% confidence level of genre 'software' - doc.Add(new SingleAssociationFacetField(0.34f, "genre", "software")); + doc.AddSingleAssociationFacetField(0.34f, "genre", "software"); indexWriter.AddDocument(config.Build(taxoWriter, doc)); } // Disposes indexWriter and taxoWriter http://git-wip-us.apache.org/repos/asf/lucenenet/blob/a032177a/src/Lucene.Net.Facet/Support/Document/DocumentExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Facet/Support/Document/DocumentExtensions.cs b/src/Lucene.Net.Facet/Support/Document/DocumentExtensions.cs new file mode 100644 index 0000000..3ce202e --- /dev/null +++ b/src/Lucene.Net.Facet/Support/Document/DocumentExtensions.cs @@ -0,0 +1,111 @@ +using Lucene.Net.Facet; +using Lucene.Net.Facet.SortedSet; +using Lucene.Net.Facet.Taxonomy; +using Lucene.Net.Util; + +namespace Lucene.Net.Documents +{ + /* + * 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. + */ + + /// <summary> + /// LUCENENET specific extensions to the <see cref="Document"/> class. + /// </summary> + public static class DocumentExtensions + { + /// <summary> + /// Adds a new <see cref="SortedSetDocValuesFacetField"/>. + /// </summary> + /// <remarks> + /// Add a <see cref="SortedSetDocValuesFacetField"/> to your <see cref="Documents.Document"/> for every facet + /// label to be indexed via <see cref="Index.SortedSetDocValues"/>. + /// </remarks> + /// <param name="document">This <see cref="Document"/>.</param> + /// <param name="dim">Dimension for this field.</param> + /// <param name="label">Label for this field.</param> + /// <returns>The field that was added to this <see cref="Document"/>.</returns> + public static SortedSetDocValuesFacetField AddSortedSetDocValuesFacetField(this Document document, string dim, string label) + { + var field = new SortedSetDocValuesFacetField(dim, label); + document.Add(field); + return field; + } + + /// <summary> + /// Adds a new <see cref="AssociationFacetField"/> using <paramref name="dim"/> and <paramref name="path"/> and an + /// association. + /// </summary> + /// <param name="document">This <see cref="Document"/>.</param> + /// <param name="assoc">Associated value.</param> + /// <param name="dim">Dimension for this field.</param> + /// <param name="path">Facet path for this field.</param> + /// <returns>The field that was added to this <see cref="Document"/>.</returns> + public static AssociationFacetField AddAssociationFacetField(this Document document, BytesRef assoc, string dim, params string[] path) + { + var field = new AssociationFacetField(assoc, dim, path); + document.Add(field); + return field; + } + + /// <summary> + /// Adds a new <see cref="Int32AssociationFacetField"/> using <paramref name="dim"/> and <paramref name="path"/> and an + /// <see cref="int"/> association. + /// </summary> + /// <param name="document">This <see cref="Document"/>.</param> + /// <param name="assoc">Associated value.</param> + /// <param name="dim">Dimension for this field.</param> + /// <param name="path">Facet path for this field.</param> + /// <returns>The field that was added to this <see cref="Document"/>.</returns> + public static Int32AssociationFacetField AddInt32AssociationFacetField(this Document document, int assoc, string dim, params string[] path) + { + var field = new Int32AssociationFacetField(assoc, dim, path); + document.Add(field); + return field; + } + + /// <summary> + /// Adds a new <see cref="SingleAssociationFacetField"/> using <paramref name="dim"/> and <paramref name="path"/> and a + /// <see cref="float"/> association. + /// </summary> + /// <param name="document">This <see cref="Document"/>.</param> + /// <param name="assoc">Associated value.</param> + /// <param name="dim">Dimension for this field.</param> + /// <param name="path">Facet path for this field.</param> + /// <returns>The field that was added to this <see cref="Document"/>.</returns> + public static SingleAssociationFacetField AddSingleAssociationFacetField(this Document document, float assoc, string dim, params string[] path) + { + var field = new SingleAssociationFacetField(assoc, dim, path); + document.Add(field); + return field; + } + + /// <summary> + /// Adds a new <see cref="FacetField"/> with the specified <paramref name="dim"/> and + /// <paramref name="path"/>. + /// </summary> + /// <param name="document">This <see cref="Document"/>.</param> + /// <param name="dim">Dimension for this field.</param> + /// <param name="path">Facet path for this field.</param> + /// <returns>The field that was added to this <see cref="Document"/>.</returns> + public static FacetField AddFacetField(this Document document, string dim, params string[] path) + { + var field = new FacetField(dim, path); + document.Add(field); + return field; + } + } +}
