Show / Hide Table of Contents

    Class RangeFacetsExample

    Shows simple usage of dynamic range faceting.

    Inheritance
    System.Object
    RangeFacetsExample
    Namespace: Lucene.Net.Demo.Facet
    Assembly: Lucene.Net.Demo.dll
    Syntax
    public class RangeFacetsExample : IDisposable
    Examples
    using Lucene.Net.Analysis.Core;
    using Lucene.Net.Documents;
    using Lucene.Net.Facet;
    using Lucene.Net.Facet.Range;
    using Lucene.Net.Index;
    using Lucene.Net.Search;
    using Lucene.Net.Store;
    using Lucene.Net.Util;
    using System;
    
    namespace Lucene.Net.Demo.Facet
    {
        /// <summary>
        /// Shows simple usage of dynamic range faceting.
        /// </summary>
        public class RangeFacetsExample : IDisposable
        {
            /// <summary>
            /// Using a constant for all functionality related to a specific index
            /// is the best strategy. This allows you to upgrade Lucene.Net first
            /// and plan the upgrade of the index binary format for a later time. 
            /// Once the index is upgraded, you simply need to update the constant 
            /// version and redeploy your application.
            /// </summary>
            private const LuceneVersion EXAMPLE_VERSION = LuceneVersion.LUCENE_48;
    
            private readonly Directory indexDir = new RAMDirectory();
            private IndexSearcher searcher;
            private readonly long nowSec = DateTime.Now.Ticks;
    
            internal readonly Int64Range PAST_HOUR;
            internal readonly Int64Range PAST_SIX_HOURS;
            internal readonly Int64Range PAST_DAY;
    
            /// <summary>Constructor</summary>
            public RangeFacetsExample()
            {
                PAST_HOUR = new Int64Range("Past hour", nowSec - 3600, true, nowSec, true);
                PAST_SIX_HOURS = new Int64Range("Past six hours", nowSec - 6 * 3600, true, nowSec, true);
                PAST_DAY = new Int64Range("Past day", nowSec - 24 * 3600, true, nowSec, true);
            }
    
            /// <summary>Build the example index.</summary>
            public void Index()
            {
                using (IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(EXAMPLE_VERSION,
                    new WhitespaceAnalyzer(EXAMPLE_VERSION))))
                {
                    // Add documents with a fake timestamp, 1000 sec before
                    // "now", 2000 sec before "now", ...:
                    for (int i = 0; i < 100; i++)
                    {
                        Document doc = new Document();
                        long then = nowSec - i * 1000;
                        // Add as doc values field, so we can compute range facets:
                        doc.Add(new NumericDocValuesField("timestamp", then));
                        // Add as numeric field so we can drill-down:
                        doc.Add(new Int64Field("timestamp", then, Field.Store.NO));
                        indexWriter.AddDocument(doc);
                    }
    
                    // Open near-real-time searcher
                    searcher = new IndexSearcher(DirectoryReader.Open(indexWriter, true));
    
                } // Disposes indexWriter
            }
    
            private FacetsConfig GetConfig()
            {
                return new FacetsConfig();
            }
    
            /// <summary>User runs a query and counts facets.</summary>
            public FacetResult Search()
            {
                // Aggregates the facet counts
                FacetsCollector fc = new FacetsCollector();
    
                // MatchAllDocsQuery is for "browsing" (counts facets
                // for all non-deleted docs in the index); normally
                // you'd use a "normal" query:
                FacetsCollector.Search(searcher, new MatchAllDocsQuery(), 10, fc);
    
                Facets facets = new Int64RangeFacetCounts("timestamp", fc,
                                                         PAST_HOUR,
                                                         PAST_SIX_HOURS,
                                                         PAST_DAY);
                return facets.GetTopChildren(10, "timestamp");
            }
    
            /// <summary>User drills down on the specified range.</summary>
            public TopDocs DrillDown(Int64Range range)
            {
                // Passing no baseQuery means we drill down on all
                // documents ("browse only"):
                DrillDownQuery q = new DrillDownQuery(GetConfig());
    
                q.Add("timestamp", NumericRangeQuery.NewInt64Range("timestamp", range.Min, range.Max, range.MinInclusive, range.MaxInclusive));
    
                return searcher.Search(q, 10);
            }
    
            public void Dispose()
            {
                searcher?.IndexReader?.Dispose();
                indexDir?.Dispose();
            }
    
            /// <summary>Runs the search and drill-down examples and prints the results.</summary>
            public static void Main(string[] args)
            {
                using (RangeFacetsExample example = new RangeFacetsExample())
                {
                    example.Index();
    
                    Console.WriteLine("Facet counting example:");
                    Console.WriteLine("-----------------------");
                    Console.WriteLine(example.Search());
    
                    Console.WriteLine("\n");
                    Console.WriteLine("Facet drill-down example (timestamp/Past six hours):");
                    Console.WriteLine("---------------------------------------------");
                    TopDocs hits = example.DrillDown(example.PAST_SIX_HOURS);
                    Console.WriteLine(hits.TotalHits + " TotalHits");
    
                } // Disposes example
            }
        }
    }
    

    Constructors

    | Improve this Doc View Source

    RangeFacetsExample()

    Constructor

    Declaration
    public RangeFacetsExample()

    Methods

    | Improve this Doc View Source

    Dispose()

    Declaration
    public void Dispose()
    | Improve this Doc View Source

    DrillDown(Int64Range)

    User drills down on the specified range.

    Declaration
    public TopDocs DrillDown(Int64Range range)
    Parameters
    Type Name Description
    Int64Range range
    Returns
    Type Description
    TopDocs
    | Improve this Doc View Source

    Index()

    Build the example index.

    Declaration
    public void Index()
    | Improve this Doc View Source

    Main(String[])

    Runs the search and drill-down examples and prints the results.

    Declaration
    public static void Main(string[] args)
    Parameters
    Type Name Description
    System.String[] args
    | Improve this Doc View Source

    Search()

    User runs a query and counts facets.

    Declaration
    public FacetResult Search()
    Returns
    Type Description
    FacetResult
    • Improve this Doc
    • View Source
    Back to top Copyright © 2020 Licensed to the Apache Software Foundation (ASF)