public IEnumerable <Document> Search(Func <IndexSearcher, IEnumerable <Document> > searchMethod) { lock (syncRoot) { if (_searcher != null && !_searcher.GetIndexReader().IsCurrent() && _activeSearches == 0) { _searcher.Close(); _searcher = null; } if (_searcher == null) { _searcher = new IndexSearcher((_writer ?? (_writer = CreateWriter(indexName))).GetReader()); } } IEnumerable <Document> results; Interlocked.Increment(ref _activeSearches); try { results = searchMethod(_searcher); } finally { Interlocked.Decrement(ref _activeSearches); } return(results); }
public void MrsJones() { var dir = new RAMDirectory(); var analyzer = new LowerCaseKeywordAnalyzer(); var writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); var document = new Lucene.Net.Documents.Document(); document.Add(new Field("Name", "MRS. SHABA", Field.Store.NO, Field.Index.ANALYZED_NO_NORMS)); writer.AddDocument(document); writer.Close(true); var searcher = new IndexSearcher(dir, true); var termEnum = searcher.GetIndexReader().Terms(); while (termEnum.Next()) { var buffer = termEnum.Term().Text(); Console.WriteLine(buffer); } var queryParser = new RangeQueryParser(Version.LUCENE_29, "", analyzer); var query = queryParser.Parse("Name:\"MRS. S*\""); Console.WriteLine(query); var result = searcher.Search(query, 10); Assert.NotEqual(0, result.TotalHits); }
//http://www.d80.co.uk/post/2011/03/29/LuceneNet-Tutorial.aspx Lucene.Net Tutorial with Lucene 2.9.2 public void SearchV2(string indexDir, string q, int pageSize, int pageIndex) { indexDir = HttpContext.Current.Server.MapPath("~/Search/"); string keywords = q; var search = new IndexSearcher(indexDir); q = GetKeyWordsSplitBySpace(q, new PanGuTokenizer()); // 需要查询的域名称 string[] fields = { "title", "Category", "Desc" }; //将每个域Field所查询的结果设为“或”的关系,也就是取并集 BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD }; //构造一个多Field查询 Query query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, q, fields, clauses, new PanGuAnalyzer(true)); // 新的查询 TopDocs newHits = search.Search(query, 100); ScoreDoc[] scoreDocs = newHits.ScoreDocs; Console.WriteLine("符合条件记录:{0}; 索引库记录总数:{1}", scoreDocs.Length, search.GetIndexReader().NumDocs()); foreach (var hit in newHits.ScoreDocs) { var documentFromSearcher = search.Doc(hit.doc); Console.WriteLine(documentFromSearcher.Get("Make") + " " + documentFromSearcher.Get("Model")); } search.Close(); }
public IEnumerable <Hit> Search(string query, int maxResults) { var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); QueryParser qp = new QueryParser( Lucene.Net.Util.Version.LUCENE_29, "contents", analyzer ); Query q = qp.Parse(query); TopDocs top = searcher.Search(q, maxResults); List <Hit> result = new List <Hit>(top.totalHits); for (int index = 0; index < top.totalHits; index++) { var doc = searcher.Doc(top.scoreDocs[index].doc); string contents = doc.Get("contents"); var scorer = new QueryScorer(q, searcher.GetIndexReader(), "contents"); var highlighter = new Highlighter(scorer); result.Add(new Hit() { Relevance = top.scoreDocs[index].score, Title = doc.Get("title"), Url = doc.Get("path"), Excerpt = highlighter.GetBestFragment(analyzer, "contents", contents) }); } return(result); }
public static Dictionary <string, int> Facet(Query query, IndexSearcher s, string field) { StringIndex stringIndex = FieldCache_Fields.DEFAULT.GetStringIndex(s.GetIndexReader(), field); int[] c = new int[stringIndex.lookup.Length]; SimpleFacets.FacetCollector facetCollector = new SimpleFacets.FacetCollector(c, stringIndex); s.Search(query, (HitCollector)facetCollector); SimpleFacets.DictionaryEntryQueue dictionaryEntryQueue = new SimpleFacets.DictionaryEntryQueue(stringIndex.lookup.Length); for (int index = 1; index < stringIndex.lookup.Length; ++index) { if (c[index] > 0 && stringIndex.lookup[index] != null && stringIndex.lookup[index] != "0") { dictionaryEntryQueue.Insert((object)new SimpleFacets.FacetEntry(stringIndex.lookup[index], -c[index])); } } int num = dictionaryEntryQueue.Size(); Dictionary <string, int> dictionary = new Dictionary <string, int>(); for (int index = num - 1; index >= 0; --index) { SimpleFacets.FacetEntry facetEntry = dictionaryEntryQueue.Pop() as SimpleFacets.FacetEntry; dictionary.Add(facetEntry.Value, -facetEntry.Count); } return(dictionary); }
public static Dictionary <string, int> Facet(Query query, IndexSearcher s, string field) { StringIndex stringIndex = FieldCache_Fields.DEFAULT.GetStringIndex(s.GetIndexReader(), field); int[] array = new int[stringIndex.lookup.Length]; SimpleFacets.FacetCollector results = new SimpleFacets.FacetCollector(array, stringIndex); s.Search(query, results); SimpleFacets.DictionaryEntryQueue dictionaryEntryQueue = new SimpleFacets.DictionaryEntryQueue(stringIndex.lookup.Length); for (int i = 1; i < stringIndex.lookup.Length; i++) { if (array[i] > 0 && stringIndex.lookup[i] != null && stringIndex.lookup[i] != "0") { dictionaryEntryQueue.Insert(new SimpleFacets.FacetEntry(stringIndex.lookup[i], -array[i])); } } int num = dictionaryEntryQueue.Size(); Dictionary <string, int> dictionary = new Dictionary <string, int>(); for (int j = num - 1; j >= 0; j--) { SimpleFacets.FacetEntry facetEntry = dictionaryEntryQueue.Pop() as SimpleFacets.FacetEntry; dictionary.Add(facetEntry.Value, -facetEntry.Count); } return(dictionary); }
public void Dispose() { _baseEnumerator.Dispose(); if (_searcher != null) { _searcher.GetIndexReader().DecRef(); } }
public string GetHighlight(string value, IndexSearcher searcher, string highlightField, Query luceneQuery) { var scorer = new QueryScorer(luceneQuery.Rewrite(searcher.GetIndexReader())); var highlighter = new Highlighter(HighlightFormatter, scorer); var tokenStream = HighlightAnalyzer.TokenStream(highlightField, new StringReader(value)); return(highlighter.GetBestFragments(tokenStream, value, MaxNumHighlights, Separator)); }
public StoryCollection Find(int hostId, int storyId) { int?docId = ConvertStoryIdtoDocId(hostId, storyId); if (docId.HasValue) { IndexSearcher indexSearch = SearchQuery.GetSearcher(hostId); IndexReader indexReader = indexSearch.GetIndexReader(); MoreLikeThis mlt = new MoreLikeThis(indexReader); mlt.SetAnalyzer(new DnkAnalyzer()); //mlt.SetFieldNames(new string[] { "title", "description" }); //these values control the query used to find related/similar stories // //-we are only using the title and tags fields, //-the term must appear 1 or more times, //-the query will only have 3 terms //-a word less than 3 char in len with be ignored //-the term must appear at in at least 4 doc mlt.SetFieldNames(new string[] { "title", "tags" }); mlt.SetMinTermFreq(1); mlt.SetMaxQueryTerms(5); mlt.SetMinWordLen(3); mlt.SetMinDocFreq(4); mlt.SetStopWords(StopWords()); mlt.SetBoost(true); Query mltQuery = mlt.Like(docId.Value); Hits hits = indexSearch.Search(mltQuery); List <int> results = new List <int>(); for (int i = 0; i < hits.Length(); i++) { Document d = hits.Doc(i); int hitStoryId = int.Parse(d.GetField("id").StringValue()); if (hitStoryId != storyId) { results.Add(hitStoryId); if (results.Count == NUMBER_OF_RELATED_STORIES_TO_RETURN) { break; } } } return(SearchQuery.LoadStorySearchResults(results)); } else { return(null); } }
public DecrementReaderResult(IEnumerator <SearchResult> baseEnumerator, Searcher searcher) { _baseEnumerator = baseEnumerator; _searcher = searcher as IndexSearcher; if (_searcher != null) { _searcher.GetIndexReader().IncRef(); } }
private void DisposeRudely() { var indexReader = IndexSearcher.GetIndexReader(); if (indexReader != null) { indexReader.Close(); } IndexSearcher.Close(); }
public static IEnumerable <object> GetIds(SearchFactoryImpl sf, IndexSearcher searcher, Type type, Query query) { var doccount = searcher.GetIndexReader().MaxDoc(); if (doccount == 0) { return(new List <object>()); } var docs = searcher.Search(query, doccount); return(docs.ScoreDocs.Select(t => DocumentBuilder.GetDocumentId(sf, type, searcher.Doc(t.doc)))); }
protected override internal string[] GetSearchFields() { ValidateSearcher(false); var reader = _searcher.GetIndexReader(); var fields = reader.GetFieldNames(IndexReader.FieldOption.ALL); //exclude the special index fields var searchFields = fields .Where(x => !x.StartsWith(LuceneIndexer.SpecialFieldPrefix)) .ToArray(); return(searchFields); }
/// <summary> /// Get terms starting with the given prefix /// </summary> /// <param name="prefix"></param> /// <param name="maxItems"></param> /// <returns></returns> public static IEnumerable <string> GetTerms(string prefix, int maxItems = 10) { if (string.IsNullOrWhiteSpace(prefix)) { yield break; } var counter = 0; var tagsEnum = new PrefixTermEnum(Searcher.GetIndexReader(), new Term("Title", prefix)); while (tagsEnum.Next()) { yield return(tagsEnum.Term().Text()); if (++counter == maxItems) { yield break; } } tagsEnum.Close(); }
public void TestForDuplicatesInTheIndex() { ApplicationSettings applicationSettings = new ApplicationSettings(); ArachnodeDAO arachnodeDAO = new ArachnodeDAO(applicationSettings.ConnectionString); IndexSearcher _indexSearcher = new IndexSearcher(FSDirectory.Open(new DirectoryInfo("M:\\LDNI")), true); StandardAnalyzer standardAnalyzer = new StandardAnalyzer(); QueryParser queryParser = new QueryParser("discoveryid", standardAnalyzer); Dictionary <string, List <string> > dictionary = new Dictionary <string, List <string> >(); for (int i = 0; i < _indexSearcher.GetIndexReader().NumDocs(); i++) { Debug.Print(i.ToString()); Document document = _indexSearcher.Doc(i); string absoluteUri = document.GetField("absoluteuri").StringValue(); if (!dictionary.ContainsKey(absoluteUri)) { try { dictionary.Add(absoluteUri, new List <string>()); dictionary[absoluteUri].Add(document.GetField("discoveryid").ToString()); } catch (Exception) { throw; } } else { try { dictionary[absoluteUri].Add(document.GetField("discoveryid").ToString()); //Assert.Fail("Each AbsoluteUri should be present only once in the Lucene.net index."); } catch (Exception) { throw; } } } object someting = dictionary.Where(d => (d.Value).Count > 1).ToArray(); }
/// <summary> /// Gets all distinct values (values are the vertex IDs). /// </summary> /// /// <param name="select">A predicate which takes a LuceneEntry and returns whether a LuceneEntry should be taken into account when looking for vertex ids. /// If this parameter is NULL, no Lucene entry is ignored.</param> /// /// <returns> /// A collection containing a single set of Int64 values, representing the distinct vertex ids within the Lucene index; /// or a collection containing an empty set, if no entries are within the index. /// </returns> public LuceneValueList GetValues(Predicate <LuceneEntry> select = null) { var searcher = new IndexSearcher(_IndexDirectory, true); var reader = searcher.GetIndexReader(); if (select == null) { var ret = new LuceneValueList(reader); return(ret); } else { var ret = new LuceneValueList(reader, select); return(ret); } }
internal int Update0(string searchQuery, string myText, WordExpander expandWithWordNet) { checkDbLock(); // Searching: ulong[] ids; string[] results; float[] scores; int numHits; // find it writeToLog("LUCENE:Replacing best \"{0}\"...", searchQuery); //Search(query, out ids, out results, out scores); IndexSearcher indexSearcher = new IndexSearcher(_directory); try { QueryParser queryParser = new QueryParser(_fieldName, _analyzer); Query query = queryParser.Parse(searchQuery); Hits hits = indexSearcher.Search(query); numHits = hits.Length(); // if we want to do something smarter later //ids = new ulong[numHits]; //results = new string[numHits]; //scores = new float[numHits]; //for (int i = 0; i < numHits; ++i) //{ // float score = hits.Score(i); // string text = hits.Doc(i).Get(_fieldName); // string idAsText = hits.Doc(i).Get(MyLuceneIndexer.DOC_ID_FIELD_NAME); // ids[i] = UInt64.Parse(idAsText); // results[i] = text; // scores[i] = score; //} if (numHits > 0) { indexSearcher.GetIndexReader().DeleteDocument(0); } } finally { indexSearcher.Close(); } return(Insert0(myText, expandWithWordNet)); }
public static Document ExactSearch(IndexSearcher searcher, string queryStr, string queryField) { var docIds = Search(searcher, queryStr, queryField); var reader = searcher.GetIndexReader(); foreach (var docId in docIds) { var document = reader.Document(docId); if (document.Get(queryField) == queryStr) { //reader.Close(); return(document); } } //reader.Close(); return(null); }
public Highlight(SummarizerParameters parameters) : base(parameters) { var searchProvider = ExamineManager.Instance.SearchProviderCollection[parameters.SearchProvider]; if (searchProvider is LuceneSearcher) { _searcher = (searchProvider as LuceneSearcher).GetSearcher() as IndexSearcher; _analyzer = (searchProvider as LuceneSearcher).IndexingAnalyzer; _reader = _searcher.GetIndexReader(); } else { throw new ArgumentException("Supplied search provider not found, or is not a valid LuceneSearcher"); } _formatter = new SimpleHTMLFormatter(parameters.HighlightPreTag, parameters.HighlightPostTag); // fall back to plain summary if no highlight found _plainSummariser = new Plain(parameters); }
/** * 获得IndexSearcher对象,判断当前的Searcher中reader是否为最新,如果不是,则重新创建IndexSearcher(省略了try...catch) * * @param reader * * IndexReader对象 * @return IndexSearcher对象 */ public static IndexSearcher getIndexSearcher(IndexReader reader) { lock (so) { if (searcher == null) { searcher = new IndexSearcher(reader); } else { IndexReader r = searcher.GetIndexReader(); if (!r.IsCurrent()) { searcher = new IndexSearcher(reader); } } return(searcher); } }
internal Identity(IndexSearcher searcher, Query query, Type type /*, Type[] otherTypes (for MultiMap)*/) { var reader = searcher.GetIndexReader(); var fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL); this.type = type; unchecked { hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode()); } foreach (var fieldName in fieldNames) { unchecked { hashCode = hashCode * 23 + fieldName.GetHashCode(); } } }
/// <summary> /// Main searching method /// </summary> /// <param name="lookQuery"></param> /// <returns>an IEnumerableWithTotal</returns> public static IEnumerableWithTotal <LookMatch> Query(LookQuery lookQuery) { IEnumerableWithTotal <LookMatch> lookMatches = null; // prepare return value if (lookQuery == null) { LogHelper.Warn(typeof(LookService), "Supplied search query was null"); } else { var searchProvider = LookService.Searcher; var searchCriteria = searchProvider.CreateSearchCriteria(); var query = searchCriteria.Field(string.Empty, string.Empty); // Text if (!string.IsNullOrWhiteSpace(lookQuery.TextQuery.SearchText)) { if (lookQuery.TextQuery.Fuzzyness > 0) { query.And().Field(LookService.TextField, lookQuery.TextQuery.SearchText.Fuzzy(lookQuery.TextQuery.Fuzzyness)); } else { query.And().Field(LookService.TextField, lookQuery.TextQuery.SearchText); } } // Tags if (lookQuery.TagQuery != null) { var allTags = new List <string>(); var anyTags = new List <string>(); if (lookQuery.TagQuery.AllTags != null) { allTags.AddRange(lookQuery.TagQuery.AllTags); allTags.RemoveAll(x => string.IsNullOrWhiteSpace(x)); } if (lookQuery.TagQuery.AnyTags != null) { anyTags.AddRange(lookQuery.TagQuery.AnyTags); anyTags.RemoveAll(x => string.IsNullOrWhiteSpace(x)); } if (allTags.Any()) { query.And().GroupedAnd(allTags.Select(x => LookService.TagsField), allTags.ToArray()); } if (anyTags.Any()) { query.And().GroupedOr(allTags.Select(x => LookService.TagsField), anyTags.ToArray()); } } // TODO: Date // TODO: Name // Nodes if (lookQuery.NodeQuery != null) { if (lookQuery.NodeQuery.TypeAliases != null) { var typeAliases = new List <string>(); typeAliases.AddRange(lookQuery.NodeQuery.TypeAliases); typeAliases.RemoveAll(x => string.IsNullOrWhiteSpace(x)); if (typeAliases.Any()) { query.And().GroupedOr(typeAliases.Select(x => UmbracoContentIndexer.NodeTypeAliasFieldName), typeAliases.ToArray()); } } if (lookQuery.NodeQuery.ExcludeIds != null) { foreach (var excudeId in lookQuery.NodeQuery.ExcludeIds.Distinct()) { query.Not().Id(excudeId); } } } try { searchCriteria = query.Compile(); } catch (Exception exception) { LogHelper.WarnWithException(typeof(LookService), "Could not compile the Examine query", exception); } if (searchCriteria != null && searchCriteria is LuceneSearchCriteria) { Sort sort = null; Filter filter = null; Func <int, double?> getDistance = x => null; Func <string, IHtmlString> getHighlight = null; TopDocs topDocs = null; switch (lookQuery.SortOn) { case SortOn.Date: // newest -> oldest sort = new Sort(new SortField(LuceneIndexer.SortedFieldNamePrefix + LookService.DateField, SortField.LONG, true)); break; case SortOn.Name: // a -> z sort = new Sort(new SortField(LuceneIndexer.SortedFieldNamePrefix + LookService.NameField, SortField.STRING)); break; } if (lookQuery.LocationQuery != null && lookQuery.LocationQuery.Location != null) { double maxDistance = LookService.MaxDistance; if (lookQuery.LocationQuery.MaxDistance != null) { maxDistance = Math.Min(lookQuery.LocationQuery.MaxDistance.GetMiles(), maxDistance); } var distanceQueryBuilder = new DistanceQueryBuilder( lookQuery.LocationQuery.Location.Latitude, lookQuery.LocationQuery.Location.Longitude, maxDistance, LookService.LocationField + "_Latitude", LookService.LocationField + "_Longitude", CartesianTierPlotter.DefaltFieldPrefix, true); // update filter filter = distanceQueryBuilder.Filter; if (lookQuery.SortOn == SortOn.Distance) { // update sort sort = new Sort( new SortField( LookService.DistanceField, new DistanceFieldComparatorSource(distanceQueryBuilder.DistanceFilter))); } // raw data for the getDistance func var distances = distanceQueryBuilder.DistanceFilter.Distances; // update getDistance func getDistance = new Func <int, double?>(x => { if (distances.ContainsKey(x)) { return(distances[x]); } return(null); }); } var indexSearcher = new IndexSearcher(((LuceneIndexer)LookService.Indexer).GetLuceneDirectory(), false); var luceneSearchCriteria = (LuceneSearchCriteria)searchCriteria; // Do the Lucene search topDocs = indexSearcher.Search( luceneSearchCriteria.Query, // the query build by Examine filter ?? new QueryWrapperFilter(luceneSearchCriteria.Query), LookService.MaxLuceneResults, sort ?? new Sort(SortField.FIELD_SCORE)); if (topDocs.TotalHits > 0) { // setup the highlighing func if required if (lookQuery.TextQuery.HighlightFragments > 0 && !string.IsNullOrWhiteSpace(lookQuery.TextQuery.SearchText)) { var version = Lucene.Net.Util.Version.LUCENE_29; Analyzer analyzer = new StandardAnalyzer(version); var queryParser = new QueryParser(version, LookService.TextField, analyzer); var queryScorer = new QueryScorer(queryParser .Parse(lookQuery.TextQuery.SearchText) .Rewrite(indexSearcher.GetIndexReader())); Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<strong>", "</strong>"), queryScorer); // update the func so it does real highlighting work getHighlight = (x) => { var tokenStream = analyzer.TokenStream(LookService.TextField, new StringReader(x)); var highlight = highlighter.GetBestFragments( tokenStream, x, lookQuery.TextQuery.HighlightFragments, // max number of fragments lookQuery.TextQuery.HighlightSeparator); // fragment separator return(new HtmlString(highlight)); }; } lookMatches = new EnumerableWithTotal <LookMatch>( LookSearchService.GetLookMatches( lookQuery, indexSearcher, topDocs, getHighlight, getDistance), topDocs.TotalHits); } } } return(lookMatches ?? new EnumerableWithTotal <LookMatch>(Enumerable.Empty <LookMatch>(), 0)); }
internal override int Size() { SetIndexReaderSearcher(); return(searcher.GetIndexReader().NumDocs()); }
public void DisposeRudely() { currentIndexSearcher.GetIndexReader().Close(); currentIndexSearcher.Close(); }
private void DisposeRudely() { IndexSearcher.GetIndexReader().Close(); IndexSearcher.Close(); }
/// <summary> /// Reloads info panel. /// </summary> protected void ReloadInfoPanel() { if (sii != null) { // Keep flag if is in action status bool isInAction = (sii.IndexStatus == IndexStatusEnum.REBUILDING || sii.IndexStatus == IndexStatusEnum.OPTIMIZING); // Keep flag if index is not usable bool isNotReady = (!isInAction && sii.IndexStatus != IndexStatusEnum.READY); // get status name string statusName = GetString("srch.status." + sii.IndexStatus.ToString()); // Set progress if is action status ltrProgress.Text = String.Empty; if (isInAction) { ltrProgress.Text = "<img style=\"width:12px;height:12px;\" src=\"" + UIHelper.GetImageUrl(this.Page, "Design/Preloaders/preload16.gif") + "\" alt=\"" + statusName + "\" tooltip=\"" + statusName + "\" />"; } // Fill panel info with informations about index lblNumberOfItemsValue.Text = ValidationHelper.GetString(sii.NumberOfIndexedItems, "0"); lblIndexFileSizeValue.Text = DataHelper.GetSizeString(sii.IndexFileSize); lblIndexStatusValue.Text = statusName; // use coloring for status name if (isNotReady) { lblIndexStatusValue.Text = "<span class=\"StatusDisabled\">" + statusName + "</span>"; } else if (sii.IndexStatus == IndexStatusEnum.READY) { lblIndexStatusValue.Text = "<span class=\"StatusEnabled\">" + statusName + "</span>"; } lblLastRebuildTimeValue.Text = GetString("general.notavailable"); lblLastUpdateValue.Text = sii.IndexLastUpdate.ToString(); if (sii.IndexLastRebuildTime != DateTimeHelper.ZERO_TIME) { lblLastRebuildTimeValue.Text = ValidationHelper.GetString(sii.IndexLastRebuildTime, ""); } lblIndexIsOptimizedValue.Text = UniGridFunctions.ColoredSpanYesNo(false); if (sii.IndexStatus == IndexStatusEnum.READY) { IndexSearcher searcher = sii.GetSearcher(); if (searcher != null) { IndexReader reader = searcher.GetIndexReader(); if (reader != null) { if (reader.IsOptimized()) { lblIndexIsOptimizedValue.Text = UniGridFunctions.ColoredSpanYesNo(true); } } } } } }
private void ValidateSearcher(bool forceReopen) { EnsureIndex(); if (!forceReopen) { if (_searcher == null) { lock (Locker) { //double check if (_searcher == null) { try { _searcher = new IndexSearcher(GetLuceneDirectory(), true); } catch (IOException ex) { throw new ApplicationException("Could not create an index searcher with the supplied lucene directory", ex); } } } } else { if (_searcher.GetReaderStatus() != ReaderStatus.Current) { lock (Locker) { //double check, now, we need to find out if it's closed or just not current switch (_searcher.GetReaderStatus()) { case ReaderStatus.Current: break; case ReaderStatus.Closed: _searcher = new IndexSearcher(GetLuceneDirectory(), true); break; case ReaderStatus.NotCurrent: //yes, this is actually the way the Lucene wants you to work... //normally, i would have thought just calling Reopen() on the underlying reader would suffice... but it doesn't. //here's references: // http://stackoverflow.com/questions/1323779/lucene-indexreader-reopen-doesnt-seem-to-work-correctly // http://gist.github.com/173978 var oldReader = _searcher.GetIndexReader(); var newReader = oldReader.Reopen(true); if (newReader != oldReader) { _searcher.Close(); oldReader.Close(); _searcher = new IndexSearcher(newReader); } break; } } } } } else { //need to close the searcher and force a re-open if (_searcher != null) { lock (Locker) { //double check if (_searcher != null) { try { _searcher.Close(); } catch (IOException) { //this will happen if it's already closed ( i think ) } finally { //set to null in case another call to this method has passed the first lock and is checking for null _searcher = null; } try { _searcher = new IndexSearcher(GetLuceneDirectory(), true); } catch (IOException ex) { throw new ApplicationException("Could not create an index searcher with the supplied lucene directory", ex); } } } } } }
/// <summary> /// Searches the specified criteria. /// </summary> /// <param name="criteria">The criteria.</param> /// <returns></returns> public SearchResults Search(ISearchCriteria criteria) { if (_SearchList == null || _SearchList.Count == 0) { foreach (IndexerDefinition indexer in SearchConfiguration.Instance.Indexers) { try { _SearchList.Add(indexer.Name, new IndexSearcher(GetApplicationPath(indexer))); } catch (FileNotFoundException ex) { string msg = String.Format("No search indexers found for \"{0}\" indexer.", indexer.Name); IndexNotFoundException ne = new IndexNotFoundException(msg, ex); Logger.Error(msg, ne); throw ne; } } } if (_SearchList.Count == 0) { string msg = String.Format("No Search Indexers defined in the configuration file."); Logger.Error(msg); throw new IndexNotFoundException(msg); } if (!_SearchList.ContainsKey(criteria.Scope)) { string msg = String.Format("Specified scope \"{0}\" not declared in the configuration file.", criteria.Scope); Logger.Error(msg); throw new IndexNotFoundException(msg); } IndexSearcher searcher = _SearchList[criteria.Scope]; if (_SearchList.Count == 0) { string msg = String.Format("No Search Indexers defined in the configuration file."); Logger.Error(msg); throw new IndexNotFoundException(msg); } Hits hits = null; try { if (criteria.Sort != null) { hits = searcher.Search(criteria.Query, new Sort(criteria.Sort.FieldName, criteria.Sort.IsDescending)); } else { hits = searcher.Search(criteria.Query); } } catch (SystemException ex) { string msg = String.Format("Search failed."); Logger.Error(msg, ex); throw; } SearchResults results = new SearchResults(searcher.GetIndexReader(), hits, criteria); return(results); }
public static ReaderStatus GetReaderStatus(this IndexSearcher searcher) { return(searcher.GetIndexReader().GetReaderStatus()); }