分散Luceneファセット
ファセット検索では、特定のカテゴリのサブカテゴリで構成されるファセット結果が得られます。 NCache では、Distributed Lucene によるファセットがサポートされるようになりました。これにより、目的のドキュメントを効率的かつ効果的に検索できるようになります。
Distributed Lucene は、によってサポートされるすべてのファセット タイプをサポートします。 ルセン. これらのファセットは、次の XNUMX つのタイプに分類されます。
- 分類ベースのファセット: これらのファセットは、分類法と呼ばれるカテゴリの階層を使用して定義されます。
- 範囲ベースのファセット: これらのファセットは、
Numeric
orDateTime
フィールドが落ちる。 - ソートされたセットファセット: これらのファセットは別個の分類インデックスを必要とせず、代わりにソートされたセットのドキュメント値フィールドに基づいてカウントを計算します。
これらのファセットの種類について、それぞれのコード例とともに以下で説明します。
前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- Luceneキャッシュを作成して開始したことを確認してください。 NCache 管理センター or コマンドラインインターフェイス.
- アプリケーションがネイティブのLuceneDLL/リファレンスを使用していないことを確認してください。
- API の詳細については、こちらを参照してください。 NCacheディレクトリ, ファセットコレクター, ディレクトリ分類ライター, タクソライター, ディレクトリ分類リーダー, IndexWriterConfig, Luceneバージョン, スタンダードアナライザー, インデックスライター, ファセット構成, インデックスサーチャー, 高速分類法ファセットカウント, ファセット結果, GetTopChildren, GetSpecificValue, GetAllDims, 処分, NumericDocValuesフィールド, SortedSetDocValuesFacetCounts, DefaultSortedSetDocValuesReaderState.
分類法に基づくファセット
ディレクトリ分類ライター クラスを使用すると、分類ライターを作成して開くことができます。 分類ディレクトリ内で最初に分類ディレクトリを開き、 ディレクトリ分類リーダー クラスを使用すると、タクソノミーライターでタクソノミーリーダーを開くことができます。
Note
分散 Lucene は、Lucene によってサポートされるすべての分類ベースのファセットをサポートします。
次の例では、ファセットは FastTaxonomoyFacetCounts クラス。 次に、それらは構成され、ファセット フィールドの形式でドキュメントに追加されます。 その後、結果が取得され、 ファセット結果 とに提供されます。
FacetsCollector facetCollector = null;
Facets facets = null;
DirectoryTaxonomyWriter taxoWriter = null;
Directory directory = null;
Directory taxoDir = null;
DirectoryTaxonomyReader taxoReader = null;
// Precondition: Lucene cache is already connected
string cache = "luceneCache";
string index = "luceneIndex";
string taxoIndex = "taxonomyIndex";
// Open NCache directory
directory = NCacheDirectory.Open(cache, index);
// Open taxonomy directory
taxoDir = NCacheDirectory.Open(cache, taxoIndex);
// Initialize taxonomy writer
taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
// Configure index writer and initialize it
var indexWriterConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, new StandardAnalyzer(LuceneVersion.LUCENE_48));
IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
// Configure facets
FacetsConfig config = new FacetsConfig(cache);
config.SetHierarchical("Publish Date", true);
// Initialize document and add facet fields to it
Document document = new Document();
document.Add(new FacetField("Author", "Bob"));
document.Add(new FacetField("Publish Date", "2010", "10", "15"));
// Add document with facets config and taxonomy writer
writer.AddDocument(document, config, taxoWriter);
// Initialize index searcher
IndexSearcher indexSearcher = new IndexSearcher(writer.GetReader(true));
// Initialize taxonomy reader
taxoReader = new DirectoryTaxonomyReader(taxoWriter);
// Initialize facets collector
facetCollector = new FacetsCollector(false, cache);
// Search docs that match query and collect facets with facets collector
indexSearcher.Search(new MatchAllDocsQuery(), facetCollector);
// Initialize facets
facets = new FastTaxonomyFacetCounts(taxoReader, config, facetCollector);
// Retrieve & verify results:
FacetResult facetResult = facets.GetTopChildren(10, "Publish Date");
float Val = facets.GetSpecificValue("Publish Date");
IList<FacetResult> facetResults = facets.GetAllDims(10);
// Dispose all instances
facetCollector?.Dispose();
facets?.Dispose();
taxoWriter?.Dispose();
taxoReader?.Dispose();
taxoDir?.Dispose();
directory?.Dispose();
範囲ベースのファセット
次の例では、数値ファセットは Int64RangeFacetCounts クラス。 これらのファセットは、 IndexWriter
。 これらのファセットに基づいて文書が検索され、結果が後で検証されます。
Note
Distributed Lucene は、Lucene がサポートするすべての範囲ベースのファセットをサポートします。
Directory directory = null;
IndexWriter writer = null;
IndexReader reader = null;
FacetsCollector facetCollector = null;
IndexSearcher indexSearcher = null;
Facets facets = null;
string cache = "luceneCache";
string index = "luceneIndex";
// Open NCache directory
directory = NCacheDirectory.Open(cache, index);
// Configure index writer and initialize it
var indexWriterConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, new StandardAnalyzer(LuceneVersion.LUCENE_48));
writer = new IndexWriter(directory, indexWriterConfig);
// Create a document
Document document = new Document();
// Create and add numeric fields to the document
NumericDocValuesField field = new NumericDocValuesField
("field", 0L);
document.Add(field);
for (long l = 0; l < 100; l++)
{
field.SetInt64Value(l);
writer.AddDocument(document);
}
// Also add Long.MAX_VALUE
field.SetInt64Value(long.MaxValue);
writer.AddDocument(document);
// Initialize index reader
reader = writer.GetReader(true);
// Initialize facets collector
facetCollector = new FacetsCollector(false, cache);
// Initialize index searcher
indexSearcher = new IndexSearcher(reader);
// Search docs that match query and collect facets with facets collector
indexSearcher.Search(new MatchAllDocsQuery(), facetCollector);
// Intialize facets
facets = new Int64RangeFacetCounts("field",
facetCollector, new Int64Range("less than 10", 0L, true, 10L, false),
new Int64Range("less than or equal to 10", 0L, true, 10L, true),
new Int64Range("over 90", 90L, false, 100L, false),
new Int64Range("90 or above", 90L, true, 100L, false),
new Int64Range("over 1000", 1000L,
false, long.MaxValue, true));
// Retrieve & verify results:
FacetResult facetResult = facets.GetTopChildren(10, "field");
finally
{
// Dispose all instances
facets?.Dispose();
indexSearcher?.Dispose();
facetCollector?.Dispose();
reader?.Dispose();
writer?.Dispose();
directory?.Dispose();
}
ソートされたセットファセット
次の例では、Sorted Set ファセットは SortedSetDocValuesFacetCounts
クラス。 次に、これらのファセットがドキュメントに追加され、検索され、後で結果が検証されます。
Directory directory = null;
IndexWriter writer = null;
IndexSearcher indexSearcher = null;
FacetsCollector facetCollector = null;
SortedSetDocValuesFacetCounts facets = null;
string cache = "luceneCache";
string index = "normalIndex";
// Open NCache directory
directory = NCacheDirectory.Open(cache, index);
// Initialize facets config
FacetsConfig config = new FacetsConfig(cache);
config.SetMultiValued("a", true);
// Initialize index writer
var indexWriterConfig = new IndexWriterConfig
(LuceneVersion.LUCENE_48, new StandardAnalyzer
(LuceneVersion.LUCENE_48));
writer = new IndexWriter(directory, indexWriterConfig);
// Create a document and add fields
Document document = new Document();
document.Add(new SortedSetDocValuesFacetField("a", "foo"));
document.Add(new SortedSetDocValuesFacetField("b", "foo"));
// Add sorted set facet field to document with facet config
writer.AddDocument(document, config);
// Commit writer
writer.Commit();
// Initialize index searcher
indexSearcher = new IndexSearcher(writer.GetReader(true));
// Per-top-reader state:
SortedSetDocValuesReaderState state = new
DefaultSortedSetDocValuesReaderState(indexSearcher.IndexReader);
// Initialize facets collector
facetCollector = new FacetsCollector(false, cache);
// Searcher docs that match query and collect facets with facet collector
indexSearcher.Search(new MatchAllDocsQuery(), facetCollector);
facets = new SortedSetDocValuesFacetCounts(state,
facetCollector);
// Retrieve & verify results:
FacetResult facetResult = facets.GetTopChildren(10, "a");
IList<FacetResult> facetResults = facets.GetAllDims(10);
finally
{
// Dipose all instances
facets?.Dispose();
facetCollector?.Dispose();
indexSearcher?.Dispose();
writer?.Dispose();
directory?.Dispose();
}
警告
処分しないのは、 Facets
, FacetCollector
, NCacheDirectory
, IndexReader
, IndexWriter
, DirectoryTaxonomyReader
サーバー側で余分なメモリ消費につながります。
その他のリソース
NCache 上の分散 Lucene のファセットのサンプル アプリケーションも提供します。 GitHubの
も参照してください
。ネット: Lucene.Net.ファセット 名前空間