public static string[] SuggestSilmilarWords(string term, int count = 10) { IndexReader indexReader = IndexReader.Open(FSDirectory.Open(_luceneDir), true); // Create the SpellChecker var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(FSDirectory.Open(_luceneDir + "\\Spell")); // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title))); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Description))); //Suggest Similar Words return(spellChecker.SuggestSimilar(term, count, null, null, true)); }
private static LuceneSearchModel _mapLuceneDocumentToData(Document doc) { return(new LuceneSearchModel { PostId = Convert.ToInt32(doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.PostId))), ProductId = Convert.ToInt32(doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductId))), Description = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Description)), Title = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title)), Image = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Image)), Category = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Category)), SlugUrl = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.SlugUrl)), ProductStatus = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductStatus)), Price = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Price)) }); }
public static void ClearLucenePostIndexRecord(int postId) { // init lucene var analyzer = new StandardAnalyzer(_version); using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) { var searchQuery = new TermQuery(new Term(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.PostId), postId.ToString(CultureInfo.InvariantCulture))); // remove older index entry writer.DeleteDocuments(searchQuery); // close handles analyzer.Close(); writer.Dispose(); } }
public static IList <ProductWidgetViewModel> GetMoreLikeThisPostItems(int postId) { Query query = CreateMoreLikeThisQuery(postId); if (query == null) { return(new List <ProductWidgetViewModel>()); } using (var searcher = new IndexSearcher(_directory, false)) { TopDocs hits = searcher.Search(query, 10); return(hits.ScoreDocs.Select(item => searcher.Doc(item.Doc)).Select(doc => new ProductWidgetViewModel { Name = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title)), Id = Convert.ToInt32(doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.PostId))), Image = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Image)), Category = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Category)), SlugUrl = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.SlugUrl)) }).ToList()); } }
private static void _addToLuceneIndex(LuceneSearchModel modelData, IndexWriter writer) { // remove older index entry TermQuery searchQuery; if (modelData.PostId.HasValue) { searchQuery = new TermQuery(new Term(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.PostId), modelData.PostId.Value.ToString(CultureInfo.InvariantCulture))); } else { searchQuery = new TermQuery(new Term(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductId), modelData.ProductId.Value.ToString(CultureInfo.InvariantCulture))); } writer.DeleteDocuments(searchQuery); // add new index entry var document = new Document(); // add lucene fields mapped to db fields if (modelData.PostId.HasValue) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.PostId), modelData.PostId.Value.ToString(CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED)); } else { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductId), modelData.ProductId.Value.ToString(CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED)); } if (modelData.Title != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title), modelData.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS) { Boost = 3 }); } if (modelData.Description != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Description), modelData.Description, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } if (modelData.SlugUrl != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.SlugUrl), modelData.SlugUrl, Field.Store.YES, Field.Index.NOT_ANALYZED)); } if (modelData.Image != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Image), modelData.Image, Field.Store.YES, Field.Index.NOT_ANALYZED)); } if (modelData.Category != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Category), modelData.Category, Field.Store.YES, Field.Index.NOT_ANALYZED)); } if (modelData.Price != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Price), modelData.Price, Field.Store.YES, Field.Index.NOT_ANALYZED)); } if (modelData.ProductStatus != null) { document.Add(new Field(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductStatus), modelData.ProductStatus, Field.Store.YES, Field.Index.NOT_ANALYZED)); } // add entry to index writer.AddDocument(document); }
public static IList <ProductWidgetViewModel> GetMoreLikeThisProjectItems(int projectId) { Query query = CreateMoreProjectsLikeThisQuery(projectId); if (query == null) { return(new List <ProductWidgetViewModel>()); } using (var searcher = new IndexSearcher(_directory, false)) { TopDocs hits = searcher.Search(query, 10); var docs = hits.ScoreDocs.Select(item => searcher.Doc(item.Doc)).ToList(); return((from doc in docs where !string.IsNullOrEmpty(doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductStatus))) select new ProductWidgetViewModel { Name = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title)), Id = Convert.ToInt32(doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductId))), Image = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Image)), SlugUrl = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.SlugUrl)), ProductStatus = (ProductStatus)Enum.Parse(typeof(ProductStatus), doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductStatus)), true), Category = doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Category)), Price = decimal.Parse(doc.Get(StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Price))) }).ToList()); } }