/// <summary> /// Build a basic index writer for the index located at the given index path. /// </summary> /// <param name="indexDirectoryPath">The directory of the index.</param> /// <returns>Default <see cref="IndexWriter"/> object </returns> private IndexWriter BuildIndexWriter(string indexDirectoryPath) { // the index directoryFactory var fsDirectory = _directoryFactory.GetDirectory(indexDirectoryPath); // the index analyzer var baseAnalyzer = _analyzerFactory.GetAnalyzer(); // the actual index writter var indexWriter = _indexWriterFactory.BuildIndexWritter(fsDirectory, baseAnalyzer); return(indexWriter); }
public SingleFieldSearchResult SingleFieldSearch(string fieldName, string query, string directoryPath) { // Create the index reader var directory = _directoryFactory.GetDirectory(directoryPath); var indexReader = _indexReaderFactorty.GetIndexReader(directory); // create a index searcher var searcher = _indexSearcherFactory.GetIndexSearcher(indexReader); // Create the query to be executed var contentQuery = _singleFieldQueryFactory.GetSingleFieldQuery(fieldName, query, _analyzerFactory.GetAnalyzer()); // Execute the query. var results = _simpleQueryExecutor.ExecuteQuery(contentQuery, searcher, indexReader); // Create the SingleFieldSearchResul wrapper and return it as the result of the search var resultWrapper = new SingleFieldSearchResult(searcher) { TopDocs = results }; return(resultWrapper); }
public StandardSearchResultsList Query(string searchTerm) { searchTerm = searchTerm.ToLowerInvariant().Trim(); var ngramSearchTerms = TokenizeQuery(new EdgeNGramAnalyzer(), searchTerm); var boolQuery = new BooleanQuery(); //phrase boolQuery.Add(new PhraseQuery { new Term(SearchableStandard.TitlePhrase, searchTerm) }, Occur.SHOULD); boolQuery.Add(new PhraseQuery { new Term(SearchableStandard.TypicalJobTitlesPhrase, searchTerm) }, Occur.SHOULD); boolQuery.Add(new PhraseQuery { new Term(SearchableStandard.KeywordsPhrase, searchTerm) }, Occur.SHOULD); //term foreach (var term in searchTerm.Split(' ', StringSplitOptions.RemoveEmptyEntries)) { boolQuery.Add(new TermQuery(new Term(SearchableStandard.TitleTerm, term)), Occur.SHOULD); boolQuery.Add(new TermQuery(new Term(SearchableStandard.TypicalJobTitlesTerm, term)), Occur.SHOULD); boolQuery.Add(new TermQuery(new Term(SearchableStandard.KeywordsTerm, term)), Occur.SHOULD); } //ngram foreach (var term in ngramSearchTerms) { boolQuery.Add(new TermQuery(new Term(SearchableStandard.TitleNGram, term)), Occur.SHOULD); } var directory = _directoryFactory.GetDirectory(); var reader = DirectoryReader.Open(directory); var searcher = new IndexSearcher(reader); var topDocs = searcher.Search(boolQuery, 1000); var results = new List <StandardSearchResult>(); foreach (var scoreDoc in topDocs.ScoreDocs) { var doc = searcher.Doc(scoreDoc.Doc); results.Add(new StandardSearchResult(doc, scoreDoc.Score)); } return(new StandardSearchResultsList { TotalCount = topDocs.TotalHits, Standards = results }); }
public void Build() { var standardAnalyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); var pipeAnalyzer = new PipeAnalyzer(); var ngramAnalyzer = new EdgeNGramAnalyzer(); var fieldAnalyzers = new Dictionary <string, Analyzer> { //phrase { SearchableStandard.TitlePhrase, pipeAnalyzer }, { SearchableStandard.TypicalJobTitlesPhrase, pipeAnalyzer }, { SearchableStandard.KeywordsPhrase, pipeAnalyzer }, //term { SearchableStandard.TitleTerm, standardAnalyzer }, { SearchableStandard.TypicalJobTitlesTerm, standardAnalyzer }, { SearchableStandard.KeywordsTerm, standardAnalyzer }, //ngram { SearchableStandard.TitleNGram, ngramAnalyzer } }; var perFieldAnalyzerWrapper = new PerFieldAnalyzerWrapper(standardAnalyzer, fieldAnalyzers); var config = new IndexWriterConfig(LuceneVersion.LUCENE_48, perFieldAnalyzerWrapper); var directory = _directoryFactory.GetDirectory(); using (var writer = new IndexWriter(directory, config)) { writer.DeleteAll(); writer.Commit(); foreach (var standard in _coursesDataContext.Standards) { var doc = new Document(); var searchable = new SearchableStandard(standard); foreach (var indexableField in searchable.GetFields()) { doc.Add(indexableField); } writer.AddDocument(doc); writer.Commit(); } } }