-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathMultiCategoryListsFacetsExample.cs
149 lines (126 loc) · 5.78 KB
/
MultiCategoryListsFacetsExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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.
*/
// Add NuGet References:
// Lucene.Net.Analysis.Common
// Lucene.Net.Facet
using Lucene.Net.Analysis.Core;
using Lucene.Net.Documents;
using Lucene.Net.Facet;
using Lucene.Net.Facet.Taxonomy;
using Lucene.Net.Facet.Taxonomy.Directory;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
namespace Lucene.Net.Demo.Facet
{
/// <summary>
/// Demonstrates indexing categories into different indexed fields.
/// </summary>
public class MultiCategoryListsFacetsExample
{
/// <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 readonly Directory taxoDir = new RAMDirectory();
private readonly FacetsConfig config = new FacetsConfig();
/// <summary>Creates a new instance and populates the catetory list params mapping.</summary>
public MultiCategoryListsFacetsExample()
{
config.SetIndexFieldName("Author", "author");
config.SetIndexFieldName("Publish Date", "pubdate");
config.SetHierarchical("Publish Date", true);
}
/// <summary>Build the example index.</summary>
private void Index()
{
using IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(EXAMPLE_VERSION,
new WhitespaceAnalyzer(EXAMPLE_VERSION)));
// Writes facet ords to a separate directory from the main index
using DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
indexWriter.AddDocument(config.Build(taxoWriter, new Document
{
new FacetField("Author", "Bob"),
new FacetField("Publish Date", "2010", "10", "15")
}));
indexWriter.AddDocument(config.Build(taxoWriter, new Document
{
new FacetField("Author", "Lisa"),
new FacetField("Publish Date", "2010", "10", "20")
}));
indexWriter.AddDocument(config.Build(taxoWriter, new Document
{
new FacetField("Author", "Lisa"),
new FacetField("Publish Date", "2012", "1", "1")
}));
indexWriter.AddDocument(config.Build(taxoWriter, new Document
{
new FacetField("Author", "Susan"),
new FacetField("Publish Date", "2012", "1", "7")
}));
indexWriter.AddDocument(config.Build(taxoWriter, new Document
{
new FacetField("Author", "Frank"),
new FacetField("Publish Date", "1999", "5", "5")
}));
}
/// <summary>User runs a query and counts facets.</summary>
private IList<FacetResult> Search()
{
IList<FacetResult> results = new List<FacetResult>();
using DirectoryReader indexReader = DirectoryReader.Open(indexDir);
using TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
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);
// Retrieve results
// Count both "Publish Date" and "Author" dimensions
Facets author = new FastTaxonomyFacetCounts("author", taxoReader, config, fc);
results.Add(author.GetTopChildren(10, "Author"));
Facets pubDate = new FastTaxonomyFacetCounts("pubdate", taxoReader, config, fc);
results.Add(pubDate.GetTopChildren(10, "Publish Date"));
return results;
}
/// <summary>Runs the search example.</summary>
public IList<FacetResult> RunSearch()
{
Index();
return Search();
}
/// <summary>Runs the search example and prints the results.</summary>
/// <param name="args">The command line arguments</param>
public static void Main(string[] args)
{
Console.WriteLine("Facet counting over multiple category lists example:");
Console.WriteLine("-----------------------");
IList<FacetResult> results = new MultiCategoryListsFacetsExample().RunSearch();
Console.WriteLine("Author: " + results[0]);
Console.WriteLine("Publish Date: " + results[1]);
}
}
}