private Document BuildDocument(string file) { if (string.IsNullOrEmpty(file)) { return(null); } Document doc = new Document(); try { using (StreamReader sr = new StreamReader(file)) { string fileDirectory = Path.GetDirectoryName(file); doc.Add(new Field(Constants.IndexFields.Directory, fileDirectory, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(Constants.IndexFields.Filename, Path.GetFileNameWithoutExtension(file), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(Constants.IndexFields.Extension, Path.GetExtension(file), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(Constants.IndexFields.LastModified, new FileInfo(file).LastWriteTime.Ticks.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); //TODO split content when too long ( > int.max ) string content = sr.ReadToEnd(); doc.Add(new Field(Constants.IndexFields.AnalizedDirectory, LuceneHelper.ExpandTokenBreak(fileDirectory.ToLower()), Field.Store.NO, Field.Index.ANALYZED)); doc.Add(new Field(Constants.IndexFields.Content, LuceneHelper.ExpandTokenBreak(content), Field.Store.NO, Field.Index.ANALYZED)); doc.Add(new Field(Constants.IndexFields.ContentCaseInsensitive, LuceneHelper.ExpandTokenBreak(content.ToLower()), Field.Store.NO, Field.Index.ANALYZED)); } } catch { return(null); } return(doc); }
private IEnumerable <BooleanClause> BuildBlacklistQueryClauses(IEnumerable <string> blacklist) { if (blacklist == null || !blacklist.Any()) { return(Enumerable.Empty <BooleanClause>()); } List <BooleanClause> clauses = new List <BooleanClause>(); foreach (var cur in blacklist) { string adjustedString = LuceneHelper.ExpandTokenBreak(cur.ToLower()).TrimEnd(Path.DirectorySeparatorChar); clauses.Add(new BooleanClause(BuildMatchWholeWordQuery(Constants.IndexFields.AnalizedDirectory, adjustedString), Occur.MUST_NOT)); } return(clauses); }
private Query BuildMatchAnywhereQuery(IndexReader indexReader, string searchString, bool matchCase, bool useWildcards) { if (useWildcards) { BooleanQuery resultQuery = new BooleanQuery(); var patternParts = GetWildcardPatternParts(searchString); foreach (var part in patternParts) { resultQuery.Add(BuildMatchAnywhereQuery(indexReader, LuceneHelper.ExpandTokenBreak(part), matchCase), Occur.MUST); } return(resultQuery); } else { return(BuildMatchAnywhereQuery(indexReader, LuceneHelper.ExpandTokenBreak(searchString), matchCase)); } }