public void Index_Protected_Content_Not_Indexed() { var protectedQuery = new BooleanQuery(); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndexer.IndexTypeFieldName, IndexTypes.Content)), BooleanClause.Occur.MUST)); protectedQuery.Add( new BooleanClause( new TermQuery(new Term(LuceneIndexer.IndexNodeIdFieldName, TestContentService.ProtectedNode.ToString())), BooleanClause.Occur.MUST)); var collector = new AllHitsCollector(false, true); var s = _searcher.GetSearcher(); s.Search(protectedQuery, collector); Assert.AreEqual(0, collector.Count, "Protected node should not be indexed"); }
private void DoSearch(Query query, IEnumerable<SortField> sortField, int maxResults) { //This try catch is because analyzers strip out stop words and sometimes leave the query //with null values. This simply tries to extract terms, if it fails with a null //reference then its an invalid null query, NotSupporteException occurs when the query is //valid but the type of query can't extract terms. //This IS a work-around, theoretically Lucene itself should check for null query parameters //before throwing exceptions. try { var set = new Hashtable(); query.ExtractTerms(set); } catch (NullReferenceException) { //this means that an analyzer has stipped out stop words and now there are //no words left to search on TotalItemCount = 0; return; } catch (NotSupportedException) { //swallow this exception, we should continue if this occurs. } maxResults = maxResults > 1 ? maxResults : LuceneSearcher.MaxDoc(); if (sortField.Count() == 0) { var topDocs = LuceneSearcher.Search(query, null, maxResults, new Sort()); _collector = new AllHitsCollector(topDocs.scoreDocs); topDocs = null; } else { var topDocs = LuceneSearcher.Search(query, null, maxResults, new Sort(sortField.ToArray())); _collector = new AllHitsCollector(topDocs.scoreDocs); topDocs = null; } TotalItemCount = _collector.Count; }
public void Index_Reindex_Content() { var s = (IndexSearcher)_searcher.GetSearcher(); //first delete all 'Content' (not media). This is done by directly manipulating the index with the Lucene API, not examine! var r = IndexReader.Open(s.GetIndexReader().Directory(), false); var contentTerm = new Term(LuceneIndexer.IndexTypeFieldName, IndexTypes.Content); var delCount = r.DeleteDocuments(contentTerm); r.Commit(); r.Close(); //make sure the content is gone. This is done with lucene APIs, not examine! var collector = new AllHitsCollector(false, true); var query = new TermQuery(contentTerm); s = (IndexSearcher)_searcher.GetSearcher(); //make sure the searcher is up do date. s.Search(query, collector); Assert.AreEqual(0, collector.Count); //call our indexing methods _indexer.IndexAll(IndexTypes.Content); collector = new AllHitsCollector(false, true); s = (IndexSearcher)_searcher.GetSearcher(); //make sure the searcher is up do date. s.Search(query, collector); //var ids = new List<string>(); //for (var i = 0; i < collector.Count;i++) //{ // ids.Add(s.Doc(collector.GetDocId(i)).GetValues("__NodeId")[0]); //} Assert.AreEqual(20, collector.Count); }