private Document BuildDocumentFromSearchContent(T searchContent) { Document doc = new Document(); IList <SearchContentFieldInfo> fields = SearchGenericUtils.GetSearchContentFields(typeof(T), searchContent); for (int i = 0; i < fields.Count; i++) { SearchContentFieldInfo fi = fields[i]; switch (fi.FieldType) { case SearchContentFieldType.Text: doc.Add(Field.Text(fi.Name, fi.Value)); break; case SearchContentFieldType.UnStored: doc.Add(Field.UnStored(fi.Name, fi.Value)); break; case SearchContentFieldType.UnIndexed: doc.Add(Field.UnIndexed(fi.Name, fi.Value)); break; case SearchContentFieldType.Keyword: doc.Add(Field.Keyword(fi.Name, fi.Value)); break; default: break; } } return(doc); }
/// <summary> /// Searches the index. /// </summary> /// <param name="queryText"></param> /// <param name="keywordFilter">A Hashtable where the key is the fieldname of the keyword and /// the value the keyword itself.</param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public SearchResultCollection <T> Find(string queryText, Hashtable keywordFilter, int pageIndex, int pageSize) { long startTicks = DateTime.Now.Ticks; string[] qryFields = SearchGenericUtils.GetSearchContentQueryFields(typeof(T)); if (qryFields.Length == 0) { throw new Exception("No query field specified on target class!"); } Query query = MultiFieldQueryParser.Parse(queryText, qryFields, new StandardAnalyzer()); IndexSearcher searcher = new IndexSearcher(this._indexDirectory); Hits hits; if (keywordFilter != null && keywordFilter.Count > 0) { QueryFilter qf = BuildQueryFilterFromKeywordFilter(keywordFilter); hits = searcher.Search(query, qf); } else { hits = searcher.Search(query); } int start = pageIndex * pageSize; int end = (pageIndex + 1) * pageSize; if (hits.Length() <= end) { end = hits.Length(); } SearchResultCollection <T> results = new SearchResultCollection <T>(); results.TotalCount = hits.Length(); results.PageIndex = pageIndex; string[] resultFields = SearchGenericUtils.GetSearchContentResultFields(typeof(T)); for (int i = start; i < end; i++) { T instance = Activator.CreateInstance <T>(); for (int j = 0; j < resultFields.Length; j++) { SearchGenericUtils.SetSearchResultField(instance, resultFields[j], hits.Doc(i).Get(resultFields[j])); } if (instance is ISearchResultStat) { SearchGenericUtils.SetSearchResultField(instance, "Boost", hits.Doc(i).GetBoost()); SearchGenericUtils.SetSearchResultField(instance, "Score", hits.Score(i)); } results.Add(instance); } searcher.Close(); results.ExecutionTime = DateTime.Now.Ticks - startTicks; return(results); }
/// <summary> /// Delete existing content from the search index. /// </summary> /// <param name="searchContent"></param> public void DeleteContent(T searchContent) { if (this._rebuildIndex) { throw new InvalidOperationException("Cannot delete documents when rebuilding the index."); } else { this._indexWriter.Close(); this._indexWriter = null; // The SearchContentKey uniquely identifies a document in the index. SearchContentFieldInfo ki = SearchGenericUtils.GetSearchContentKeyFieldInfo(typeof(T), searchContent); if (String.IsNullOrEmpty(ki.Name)) { throw new Exception("SearchContentKey Field not specified on target class!"); } Term term = new Term(ki.Name, ki.Value); IndexReader rdr = IndexReader.Open(this._indexDirectory); rdr.Delete(term); rdr.Close(); } }