분산 Lucene에서 검색
데이터를 인덱싱한 후 검색이 수행됩니다. 데이터를 검색하려면, NCacheDirectory
전달됩니다 IndexReader
. 인덱스에 대해 여러 인덱스 리더를 열 수 있습니다. IndexWriter
또는 NCacheDirectory
. 의 사례 IndexReader
전달됩니다 IndexSearcher
. 그만큼 IndexSearcher
주어진 쿼리에 따라 데이터를 검색할 책임이 있습니다. Lucene은 광범위한 쿼리를 제공하며 Distributed Lucene은 모든 Lucene 쿼리를 지원합니다.
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. 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
.
Distributed Lucene에서 Document
전화 commit
, 삭제된 콘텐츠는 계속 읽을 수 있습니다. Document
의 인스턴스를 통해 IndexReader
, 이 경우 IndexReader
당신이 만들기 전에 열렸습니다 commit
전화 (삭제 후). 이걸 처분하면 IndexReader
당신의 후 commit
전화를 걸고 다시 열면 삭제된 내용을 읽을 수 없습니다. Document
.
클러스터가 있는 경우에도 결과를 검색할 수 있습니다. 일부의. NCache 분산 Lucene은 다음을 제공합니다. AllowPartialResults
설정할 수 있는 플래그 TRUE
부분 데이터 검색용. 기본적으로 그 값은 FALSE
.
사전 조건
- 모든 작업에 필요한 표준 전제 조건에 대해 알아보려면 NCache 클라이언트측 기능은 다음 페이지를 참조하세요. 클라이언트 측 API 전제 조건.
- 다음을 통해 Lucene 캐시를 만들고 시작했는지 확인하십시오. NCache 관리 센터 or 명령 행 인터페이스.
- 그 확인 클라이언트 알림 사용할 수 있습니다.
- 애플리케이션이 기본 Lucene DLL/참조를 사용하고 있지 않은지 확인하십시오.
- API 세부 정보는 다음을 참조하세요. 인덱스 리더, 인덱스 검색자, 인덱스 라이터, NCache명부 담당, GetReader, Lucene 버전, 분석기, 공백분석기, 쿼리 파서, 색인 작성기 구성.
데이터 검색
아래 코드 샘플은 Distributed 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();
주의 사항
당신은 전화해야합니다 Dispose
마지막에 reader
그렇지 않으면 메모리에 상주하며 메모리 누수를 유발합니다.
추가 자료
NCache 의 Distributed Lucene에서 문서를 검색하기 위한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
.그물: Lucene.Net.검색 네임 스페이스.