分散Luceneでの検索
検索はデータにインデックスを付けた後に実行されます。 データを検索するには、 NCacheDirectory
に渡されます IndexReader
。 複数のインデックスリーダーは、インデックスに対して開くことができます。 IndexWriter
または NCacheDirectory
。 のインスタンス IndexReader
に渡されます IndexSearcher
を選択します。 IndexSearcher
指定されたクエリに従ってデータを検索する責任があります。 Lucene は幅広いクエリを提供し、Distributed Lucene はすべての Lucene クエリをサポートします。
Note
この機能は以下でも利用できます NCache Professional.
Lucene.Net.Search.TopDocs.TotalHits
は、実行された検索の結果として得られた上位の結果の数を表します。 Distributed Lucene の場合、この値は integer
〜へ long
。 同様に、 Lucene.Net.Search.ScoreDocs.Doc
, Lucene.Net.Index.IndexReader.NumDoc
, Lucene.Net.Index.IndexReader.MaxDocs
から変換されることもあります integer
〜へ long
.
分散 Lucene で、 Document
呼び出し commit
、削除された内容は引き続き読むことができます Document
のインスタンスを介して IndexReader
、これが IndexReader
あなたが作る前に開かれました commit
(削除後)呼び出します。 これを処分したら IndexReader
あなたの後に commit
呼び出してから再度開くと、削除された内容を読み取ることができなくなります Document
.
クラスタが停止している場合でも結果を取得できます 部分的な. NCache 分散 Lucene は、 AllowPartialResults
に設定できるフラグ TRUE
部分データを取得します。 デフォルトでは、その値は FALSE
.
前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- Luceneキャッシュを作成して開始したことを確認してください。 NCache 管理センター or コマンドラインインターフェイス.
- ていることを確認してください クライアントへの通知 有効になります。
- アプリケーションがネイティブのLuceneDLL/リファレンスを使用していないことを確認してください。
- APIの詳細については、以下を参照してください。 索引リーダー, インデックスサーチャー, インデックスライター, NCacheディレクトリ, GetReader, Luceneバージョン, アナライザ, ホワイトスペースアナライザー, QueryParser, IndexWriterConfig.
データの検索
以下のコード サンプルは、分散 Lucene を使用してインデックス付きドキュメントを検索する方法を示しています。
IndexReader reader = null;
IndexSearcher indexSearcher = null;
// Create a directory and open it on the cache and the index path
NCacheDirectory ncacheDirectory = NCacheDirectory.Open("demoCache",
"luceneIndex");
// Specify lucene version and analyzer type
LuceneVersion version = LuceneVersion.LUCENE_48;
Analyzer analyzer = new WhitespaceAnalyzer(version);
// Create an indexWriterConfig which holds all the configurations to create an instance of the writer
IndexWriterConfig config = new IndexWriterConfig(LuceneVersion.
LUCENE_48, analyzer);
// Create an instance of the writer
IndexWriter indexWriter = new IndexWriter(ncacheDirectory, new
IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer));
// Open a new reader instance
// The 'applyAllDeletes' is set to true which means all enqueued deletes will be applied on the writer
reader = indexWriter.GetReader(true);
// OR
// reader = DirectoryReader.Open(indexWriter, true);
// OR
// reader = DirectoryReader.Open(ncacheDirectory, 1);
// Allow partial results in case of partial connectivity with cluster
reader.AllowPartialResults = true;
// A searcher is opened to perform searching
indexSearcher = new IndexSearcher(reader);
// Specify the searchTerm and the fieldName
string searchTerm = "Beverages";
string fieldName = "Category";
// Specify lucene version and analyzer type
LuceneVersion version = LuceneVersion.LUCENE_48;
Analyzer analyzer = new WhitespaceAnalyzer(version);
// Create a query parser and parse the query with the parser
QueryParser parser = new QueryParser(version, fieldName,
analyzer);
Query query = parser.Parse(searchTerm);
// Returns the top 10000 hits from the result set
ScoreDoc[] docsFound = indexSearcher.Search(query, 10000).
ScoreDocs;
// Please make sure that no operations should be performed after calling this
if (indexSearcher != null) indexSearcher.Dispose();
if (reader != null) reader.Dispose();
Note
あなたは電話する必要があります Dispose
すべての終わりに reader
たとえば、そうでない場合、メモリ内に存在し、メモリ リークが発生します。
その他のリソース
NCache 上の分散 Lucene 内のドキュメントを検索するためのサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Lucene.Net.Search 名前空間