public Product[] Search(int catalogId, string text) { var result = new List <Product>(); using (var analyzer = new SimpleAnalyzer(LuceneVersion.LUCENE_48)) using (var indexDirectory = FSDirectory.Open(GetIndexDirectory())) using (var indexReader = DirectoryReader.Open(indexDirectory)) { var query = new FuzzyQuery(new Term("Name", text), 2); var indexSearcher = new IndexSearcher(indexReader); var searchResults = indexSearcher.Search(query, 10).ScoreDocs; foreach (var searchResultItem in searchResults) { var doc = indexSearcher.Doc(searchResultItem.Doc); var product = new Product() { Id = (int)doc.GetField("Id")?.GetInt32Value(), Name = doc.GetField("Name")?.GetStringValue() }; result.Add(product); } return(result.ToArray()); } }
/// <summary> /// Returns an Analyzer for the given AnalyzerType /// </summary> /// <param name="oAnalyzerType">Enumeration value</param> /// <returns>Analyzer</returns> public static Analyzer GetAnalyzer(AnalyzerType oAnalyzerType) { Analyzer oAnalyzer = null; switch (oAnalyzerType) { case AnalyzerType.SimpleAnalyzer: oAnalyzer = new SimpleAnalyzer(); break; case AnalyzerType.StopAnalyzer: oAnalyzer = new StopAnalyzer(); break; case AnalyzerType.WhitespaceAnalyzer: oAnalyzer = new WhitespaceAnalyzer(); break; default: case AnalyzerType.StandardAnalyzer: oAnalyzer = new StandardAnalyzer(); break; } return(oAnalyzer); }
public MainWindowViewModel(IDialogService dialogService, IDispatcher dispatcher) { if (dialogService == null) { throw new ArgumentNullException(nameof(dialogService)); } if (dispatcher == null) { throw new ArgumentNullException(nameof(dispatcher)); } _dialogService = dialogService; _dispatcher = dispatcher; var analyzer = new SimpleAnalyzer(); var store = new InMemoryStore(); _indexer = new Indexer(new SearchIndex(analyzer, store)); _indexer.IndexingProgress += OnIndexingProgress; _addDirectoryCommand = new DelegateCommand(AddDirectory); _addFilesCommand = new DelegateCommand(AddFiles); _searchCommand = new DelegateCommand <string>(Search); _searchResultsSource.Source = _searchResults; _searchResultsSource.SortDescriptions.Add(new SortDescription(string.Empty, ListSortDirection.Ascending)); }
public string GetBestContent(string text) { if (_contentScorer == null) { return(string.Empty); } // Build the TokenStream on top of the text. Analyzer analyzer = new SimpleAnalyzer(); var tokenStream = analyzer.tokenStream(Field, new java.io.StringReader(text)); // Build the highlighter. var highlighter = new LuceneHighlighter( new SimpleHTMLFormatter(_startTag, _endTag), new SimpleHTMLEncoder(), _contentScorer); highlighter.setTextFragmenter(new SimpleSpanFragmenter(_contentScorer, FragmentSize)); // Perform highlighting. var highlightedText = highlighter.getBestFragments(tokenStream, text, MaxFragments, Separator); return(!string.IsNullOrEmpty(highlightedText) ? Separator + highlightedText + Separator : string.Empty); }
public static IEnumerable <ISearchItem> Search(string pattern, DirectoryInfo dataFolder, int page) { using (Analyzer analyzer = new SimpleAnalyzer(LuceneVersion.LUCENE_48)) using (IndexReader reader = new PagesReader(dataFolder)) { Query query = new QueryParser(LuceneVersion.LUCENE_48, string.Empty, analyzer).Parse(pattern); IndexSearcher searcher = new IndexSearcher(reader); TopFieldCollector collector = TopFieldCollector.Create(Sort.INDEXORDER, NumHits, false, false, false, false); searcher.Search(query, collector); /* * IFormatter formatter = new SimpleHTMLFormatter(); * IScorer scorer = new QueryScorer(query); * Highlighter highlighter = new Highlighter(formatter, scorer) * { * TextFragmenter = new SimpleFragmenter(3) * }; */ ScoreDoc[] docs = collector.GetTopDocs(page * PageSize, PageSize).ScoreDocs; return(docs.Select(doc => new SearchItem(doc.Doc.ToString(), doc.Doc))); /* * Document document = searcher.Doc(doc.Doc); * string body = document.Get("body"); * TokenStream stream = TokenSources.GetAnyTokenStream(reader, doc.Doc, "body", analyzer); * //TokenStream stream = analyzer.GetTokenStream("test123", new StringReader("test456")); * string best = highlighter.GetBestFragments(stream, body, 1, " "); */ } }
/// <summary> /// Indexes a file. /// </summary> /// <param name="fileName">The name of the file to be indexed.</param> /// <param name="filePath">The path of the file to be indexed.</param> /// <returns><c>true</c> if the message has been indexed succesfully, <c>false</c> otherwise.</returns> public static bool IndexFile(string fileName, string filePath) { IIndexDirectoryProviderV30 indexDirectoryProvider = Collectors.IndexDirectoryProvider; Analyzer analyzer = new SimpleAnalyzer(); using (IndexWriter writer = new IndexWriter(indexDirectoryProvider.GetDirectory(), analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { try { Document doc = new Document(); doc.Add(new Field(SearchField.Key.AsString(), (DocumentTypeToString(DocumentType.File) + "|" + fileName).Replace(" ", ""), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); doc.Add(new Field(SearchField.DocumentType.AsString(), DocumentTypeToString(DocumentType.File), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchField.FileName.AsString(), fileName, Field.Store.YES, Field.Index.ANALYZED)); string fileContent = SearchEngine.Parser.Parse(filePath); doc.Add(new Field(SearchField.FileContent.AsString(), fileContent, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Commit(); } catch (System.Runtime.InteropServices.COMException ex) { Log.LogEntry(ex.Message, EntryType.Warning, Log.SystemUsername); } } return(true); }
public void RebuildIndex(int catalogId) { if (System.IO.Directory.Exists(GetIndexDirectory().FullName)) { System.IO.Directory.Delete(GetIndexDirectory().FullName, true); } using (var analyzer = new SimpleAnalyzer(LuceneVersion.LUCENE_48)) using (var indexDir = FSDirectory.Open(GetIndexDirectory())) { var config = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, analyzer); using (var indexWriter = new IndexWriter(indexDir, config)) { using (var db = eCommerce.Accessors.EntityFramework.eCommerceDbContext.Create()) { foreach (var p in db.Products.Where(p => p.CatalogId == catalogId)) { var doc = new Document(); doc.Add(new Int32Field("Id", p.Id, Field.Store.YES)); doc.Add(new TextField("Name", p.Name, Field.Store.YES)); indexWriter.AddDocument(doc); } } } } }
public void ShouldRemoveFileWhenRenamed() { var file1 = Path.Combine(_directory, "1.txt"); File.WriteAllText(file1, "hello world"); var analyzer = new SimpleAnalyzer(); var store = new InMemoryStore(); BlockingCollection <IndexingEventArgs> events = new BlockingCollection <IndexingEventArgs>(); var indexer = new Indexer(new SearchIndex(analyzer, store)); indexer.IndexingProgress += (o, e) => events.Add(e); indexer.AddFile(file1); WaitForIndexed(indexer, events); var result = indexer.Search("hello world"); CollectionAssert.AreEquivalent(new[] { file1 }, result); File.Move(file1, Path.Combine(_directory, "2.txt")); WaitForIndexed(indexer, events); result = indexer.Search("hello"); CollectionAssert.IsEmpty(result); indexer.Dispose(); }
public void ShouldNotSearchDeleted() { var file1 = Path.Combine(_directory, "1.txt"); var file2 = Path.Combine(_directory, "2.txt"); var file3 = Path.Combine(_directory, "3.txt"); var file4 = Path.Combine(_directory, "4.pdf"); File.WriteAllText(file1, "hello world"); File.WriteAllText(file2, "hello pretty world"); File.WriteAllText(file3, "just hello"); File.WriteAllText(file4, "hello ugly world"); var analyzer = new SimpleAnalyzer(); var store = new InMemoryStore(); BlockingCollection <IndexingEventArgs> events = new BlockingCollection <IndexingEventArgs>(); var indexer = new Indexer(new SearchIndex(analyzer, store)); indexer.IndexingProgress += (o, e) => events.Add(e); indexer.AddDirectory(_directory, "*.txt"); WaitForIndexed(indexer, events); File.Delete(file1); WaitForIndexed(indexer, events); var result = indexer.Search("hello world"); CollectionAssert.AreEquivalent(new[] { file2 }, result); indexer.Dispose(); }
/// <summary> /// Indexes the page. /// </summary> /// <param name="page">The page page to be intexed.</param> /// <returns><c>true</c> if the page has been indexed succesfully, <c>false</c> otherwise.</returns> public static bool IndexPage(PageContent page) { IIndexDirectoryProviderV60 indexDirectoryProvider = Collectors.CollectorsBox.GetIndexDirectoryProvider(page.Provider.CurrentWiki); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(indexDirectoryProvider.GetDirectory(), analyzer, IndexWriter.MaxFieldLength.UNLIMITED); try { string[] linkedPages; string contentWithHtml = FormattingPipeline.FormatWithPhase1And2(page.Provider.CurrentWiki, page.Content, true, FormattingContext.PageContent, page.FullName, out linkedPages); string content = Tools.RemoveHtmlMarkup(contentWithHtml); Document doc = new Document(); doc.Add(new Field(SearchField.Key.AsString(), (DocumentTypeToString(DocumentType.Page) + "|" + page.Provider.CurrentWiki + "|" + page.FullName).Replace(" ", ""), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); doc.Add(new Field(SearchField.DocumentType.AsString(), DocumentTypeToString(DocumentType.Page), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(SearchField.Wiki.AsString(), page.Provider.CurrentWiki, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(SearchField.PageFullName.AsString(), page.FullName, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchField.Title.AsString(), page.Title, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchField.Content.AsString(), content, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Commit(); } finally { writer.Dispose(); } return(true); }
/** * @param language * @return */ protected Analyzer GetAnalyzerForLanguage(string language) { Analyzer contentAnalyzer = null; Analyzer defaultAnalyzer = null; PerFieldAnalyzerWrapper perFieldAnalyzer = null; switch (language) { case "de": contentAnalyzer = new GermanAnalyzer(); break; case "eo": contentAnalyzer = new EsperantoAnalyzer(); break; case "ru": contentAnalyzer = new RussianAnalyzer(); break; } if (contentAnalyzer == null) { contentAnalyzer = new EnglishAnalyzer(); } if (defaultAnalyzer == null) { defaultAnalyzer = new SimpleAnalyzer(); } perFieldAnalyzer = new PerFieldAnalyzerWrapper(defaultAnalyzer); perFieldAnalyzer.AddAnalyzer("contents", contentAnalyzer); return(perFieldAnalyzer); }
/// <summary> /// Renames a file in the index. /// </summary> /// <param name="oldName">The old attachment name.</param> /// <param name="newName">The new attachment name.</param> public static bool RenameFile(string oldName, string newName) { IIndexDirectoryProviderV30 indexDirectoryProvider = Collectors.IndexDirectoryProvider; Analyzer analyzer = new SimpleAnalyzer(); Term term = new Term(SearchField.Key.AsString(), (DocumentTypeToString(DocumentType.File) + "|" + oldName).Replace(" ", "")); Query query = new TermQuery(term); using (IndexWriter writer = new IndexWriter(indexDirectoryProvider.GetDirectory(), analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) using (IndexSearcher searcher = new IndexSearcher(indexDirectoryProvider.GetDirectory(), false)) { TopDocs topDocs = searcher.Search(query, 100); if (topDocs.ScoreDocs.Length == 0) { return(true); } Document doc = searcher.Doc(topDocs.ScoreDocs[0].Doc); Document newDoc = new Document(); newDoc.Add(new Field(SearchField.Key.AsString(), (DocumentTypeToString(DocumentType.File) + "|" + newName).Replace(" ", ""), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); newDoc.Add(new Field(SearchField.DocumentType.AsString(), DocumentTypeToString(DocumentType.File), Field.Store.YES, Field.Index.ANALYZED)); newDoc.Add(new Field(SearchField.FileName.AsString(), newName, Field.Store.YES, Field.Index.ANALYZED)); newDoc.Add(new Field(SearchField.FileContent.AsString(), doc.GetField(SearchField.FileContent.AsString()).StringValue, Field.Store.YES, Field.Index.ANALYZED)); writer.UpdateDocument(term, newDoc); writer.Commit(); } return(true); }
// This method is printing out the message details given the index document. // NOTE: The field "mainText" must be stored in indexing level. Same goes for any // other field you want to search. private static void DisplayMessage(Document d, string searchTerm) { // THIS IS USED IN THE DATABASE INDEXic //Console.WriteLine("id: " + d.Get("id") + "\n" + "messageBox: " + d.Get("messageBox") + "\n" + "incoming: " + d.Get("incoming") + "\n" + "date: " + d.Get("date") + "\n" + "mainText: " + d.Get("mainText")); // THIS IS USED IN MY TEST FILES //Console.WriteLine("id: " + d.Get("id") + "\n" + "mainText: " + d.Get("mainText")); string text = d.Get("mainText"); TermQuery query = new TermQuery(new Term("mainText", searchTerm)); Lucene.Net.Search.Highlight.IScorer scorer = new QueryScorer(query); Highlighter highlighter = new Highlighter(scorer); System.IO.StringReader reader = new System.IO.StringReader(text); TokenStream tokenStream = new SimpleAnalyzer().TokenStream("mainText", reader); String[] toBePrinted = highlighter.GetBestFragments(tokenStream, text, 5); // 5 is the maximum number of fragments that gets tested foreach (var word in toBePrinted) { Console.Write(word); } Console.WriteLine("====================="); Console.ReadKey(); }
/// <summary> /// Indexes a page attachment. /// </summary> /// <param name="fileName">The name of the attachment to be indexed.</param> /// <param name="filePath">The path of the file to be indexed.</param> /// <param name="page">The page the file is attached to.</param> /// <returns><c>true</c> if the message has been indexed succesfully, <c>false</c> otherwise.</returns> public static bool IndexPageAttachment(string fileName, string filePath, PageContent page) { IIndexDirectoryProviderV40 indexDirectoryProvider = Collectors.CollectorsBox.GetIndexDirectoryProvider(page.Provider.CurrentWiki); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(indexDirectoryProvider.GetDirectory(), analyzer, IndexWriter.MaxFieldLength.UNLIMITED); try { Document doc = new Document(); doc.Add(new Field(SearchField.Key.AsString(), (DocumentTypeToString(DocumentType.Attachment) + "|" + page.Provider.CurrentWiki + "|" + page.FullName + "|" + fileName).Replace(" ", ""), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); doc.Add(new Field(SearchField.DocumentType.AsString(), DocumentTypeToString(DocumentType.Attachment), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchField.Wiki.AsString(), page.Provider.CurrentWiki, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchField.PageFullName.AsString(), page.FullName, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field(SearchField.FileName.AsString(), fileName, Field.Store.YES, Field.Index.ANALYZED)); string fileContent = ScrewTurn.Wiki.SearchEngine.Parser.Parse(filePath); doc.Add(new Field(SearchField.FileContent.AsString(), fileContent, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Commit(); } catch (System.Runtime.InteropServices.COMException ex) { Log.LogEntry(ex.Message, EntryType.Warning, Log.SystemUsername, page.Provider.CurrentWiki); } finally { writer.Close(); } return(true); }
public void ShouldReplaceYeYo() { var analyzer = new SimpleAnalyzer(); string input = "дёготь"; var tokens = analyzer.Analyze(() => input.AsStreamReader()); CollectionAssert.AreEqual(new[] { "деготь" }, tokens); }
public void print_visitor_in_action() { var n = new SimpleAnalyzer().Parse("3+7-4+1?8:9"); var v = new PrintVisitor(); v.VisitNode(n); v.Result.Should().Be(" ((((3 + 7) - 4) + 1) ? 8 : 9) "); }
public void ShouldAnalyze() { string input = "\r\n Лёгок как на помине "; var analyzer = new SimpleAnalyzer(); var tokens = analyzer.Analyze(() => input.AsStreamReader()); CollectionAssert.AreEqual(new[] { "легок", "помине" }, tokens); }
public void ShouldLowercase() { var analyzer = new SimpleAnalyzer(); string input = "HeLLo"; var tokens = analyzer.Analyze(() => input.AsStreamReader()); CollectionAssert.AreEqual(new[] { "hello" }, tokens); }
public void SpecifyAnalyzer() { var analyzer = new SimpleAnalyzer(); map.Property(x => x.Date).AnalyzeWith(analyzer); var mapper = GetMappingInfo("Date"); Assert.That(mapper.Analyzer, Is.SameAs(analyzer)); }
public void with_x_variable(string toParse, double x, double expected) { var n = new SimpleAnalyzer().Parse(toParse); var v = new ComputeVisitor(name => name == "x" ? (double?)x : null); v.VisitNode(n); v.Result.Should().Be(expected); }
} // End Constructor public static void Main1() { SimpleFileIndexer sfi = new SimpleFileIndexer(); SimpleAnalyzer san = new SimpleAnalyzer(Lucene.Net.Util.LuceneVersion.LUCENE_48); Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo("/Users/abhinavjha/Documents/index")); IndexWriterConfig writerConfig = new IndexWriterConfig(Lucene.Net.Util.LuceneVersion.LUCENE_48, san); IndexWriter indexWriter = new IndexWriter(directory, writerConfig); sfi.CreateDocument(sfi.PopulateDatabase(), indexWriter); } // End Sub Main
public void optimizer_in_action(string input, string optimPrint) { var n = new SimpleAnalyzer().Parse(input); var optim = new OptimizationVisitor().VisitNode(n); var printer = new PrintVisitor(); printer.VisitNode(optim); printer.ToString().Should().Be(optimPrint); }
public void PlusMinusInvertMutator_in_action(string input, double result) { var n = new SimpleAnalyzer().Parse(input); var mp = new PlusMinusInvertMutator(); var nV = mp.VisitNode(n); var computer = new ComputeVisitor(); computer.VisitNode(nV); computer.Result.Should().Be(result); }
public static void Main(System.String[] args) { try { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); int MAX_DOCS = 225; for (int j = 0; j < MAX_DOCS; j++) { Document d = new Document(); d.Add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY)); d.Add(Field.Text(ID_FIELD, System.Convert.ToString(j))); writer.AddDocument(d); } writer.Close(); // try a search without OR Searcher searcher = new IndexSearcher(directory); Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer); Query query = parser.Parse(HIGH_PRIORITY); System.Console.Out.WriteLine("Query: " + query.ToString(PRIORITY_FIELD)); hits = searcher.Search(query); PrintHits(hits); searcher.Close(); // try a new search with OR searcher = new IndexSearcher(directory); hits = null; parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer); query = parser.Parse(HIGH_PRIORITY + " OR " + MED_PRIORITY); System.Console.Out.WriteLine("Query: " + query.ToString(PRIORITY_FIELD)); hits = searcher.Search(query); PrintHits(hits); searcher.Close(); } catch (System.Exception e) { System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message); } }
/// <summary> /// Clears the files index. /// </summary> /// <param name="wiki">The wiki.</param> public static void ClearFilesIndex(string wiki) { IIndexDirectoryProviderV40 indexDirectoryProvider = Collectors.CollectorsBox.GetIndexDirectoryProvider(wiki); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(indexDirectoryProvider.GetDirectory(), analyzer, IndexWriter.MaxFieldLength.UNLIMITED); ClearFilesIndex(writer); ClearAttachmentsIndex(writer); writer.Commit(); writer.Close(); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void testSimple() throws Exception public virtual void testSimple() { Analyzer a = new SimpleAnalyzer(TEST_VERSION_CURRENT); assertAnalyzesTo(a, "foo bar FOO BAR", new string[] { "foo", "bar", "foo", "bar" }); assertAnalyzesTo(a, "foo bar . FOO <> BAR", new string[] { "foo", "bar", "foo", "bar" }); assertAnalyzesTo(a, "foo.bar.FOO.BAR", new string[] { "foo", "bar", "foo", "bar" }); assertAnalyzesTo(a, "U.S.A.", new string[] { "u", "s", "a" }); assertAnalyzesTo(a, "C++", new string[] { "c" }); assertAnalyzesTo(a, "B2B", new string[] { "b", "b" }); assertAnalyzesTo(a, "2B", new string[] { "b" }); assertAnalyzesTo(a, "\"QUOTED\" word", new string[] { "quoted", "word" }); }
public void with_x_and_y_variable(string toParse, double x, double y, double expected) { var n = new SimpleAnalyzer().Parse(toParse); var d = new Dictionary <string, double> { { "x", x }, { "y", y } }; var v = new ComputeVisitor(d); v.VisitNode(n); v.Result.Should().Be(expected); }
private void DoTest(System.IO.StringWriter out_Renamed, bool useCompoundFiles) { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); writer.SetUseCompoundFile(useCompoundFiles); int MAX_DOCS = 225; for (int j = 0; j < MAX_DOCS; j++) { Document d = new Document(); d.Add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY)); d.Add(Field.Text(ID_FIELD, System.Convert.ToString(j))); writer.AddDocument(d); } writer.Close(); // try a search without OR Searcher searcher = new IndexSearcher(directory); Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer); Query query = parser.Parse(HIGH_PRIORITY); out_Renamed.WriteLine("Query: " + query.ToString(PRIORITY_FIELD)); hits = searcher.Search(query); PrintHits(out_Renamed, hits); CheckHits(hits, MAX_DOCS); searcher.Close(); // try a new search with OR searcher = new IndexSearcher(directory); hits = null; parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer); query = parser.Parse(HIGH_PRIORITY + " OR " + MED_PRIORITY); out_Renamed.WriteLine("Query: " + query.ToString(PRIORITY_FIELD)); hits = searcher.Search(query); PrintHits(out_Renamed, hits); CheckHits(hits, MAX_DOCS); searcher.Close(); }
public static void Main(System.String[] args) { try { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" }; for (int j = 0; j < docs.Length; j++) { Document d = new Document(); d.Add(Field.Text("contents", docs[j])); writer.AddDocument(d); } writer.Close(); Searcher searcher = new IndexSearcher(directory); System.String[] queries = new System.String[] { "\"a c e\"" }; Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer); parser.SetPhraseSlop(4); for (int j = 0; j < queries.Length; j++) { Query query = parser.Parse(queries[j]); System.Console.Out.WriteLine("Query: " + query.ToString("contents")); //DateFilter filter = // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //System.out.println(filter); hits = searcher.Search(query); System.Console.Out.WriteLine(hits.Length() + " total results"); for (int i = 0; i < hits.Length() && i < 10; i++) { Document d = hits.Doc(i); System.Console.Out.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents")); } } searcher.Close(); } catch (System.Exception e) { System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message); } }
/// <summary> /// Clears the files index. /// </summary> public static void ClearFilesIndex() { IIndexDirectoryProviderV30 indexDirectoryProvider = Collectors.IndexDirectoryProvider; Analyzer analyzer = new SimpleAnalyzer(); using (IndexWriter writer = new IndexWriter(indexDirectoryProvider.GetDirectory(), analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { ClearFilesIndex(writer); ClearAttachmentsIndex(writer); writer.Commit(); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void testSimple() throws Exception public virtual void testSimple() { Analyzer a = new SimpleAnalyzer(TEST_VERSION_CURRENT); assertAnalyzesTo(a, "foo bar FOO BAR", new string[] {"foo", "bar", "foo", "bar"}); assertAnalyzesTo(a, "foo bar . FOO <> BAR", new string[] {"foo", "bar", "foo", "bar"}); assertAnalyzesTo(a, "foo.bar.FOO.BAR", new string[] {"foo", "bar", "foo", "bar"}); assertAnalyzesTo(a, "U.S.A.", new string[] {"u", "s", "a"}); assertAnalyzesTo(a, "C++", new string[] {"c"}); assertAnalyzesTo(a, "B2B", new string[] {"b", "b"}); assertAnalyzesTo(a, "2B", new string[] {"b"}); assertAnalyzesTo(a, "\"QUOTED\" word", new string[] {"quoted", "word"}); }