private static Query CreateMoreProjectsLikeThisQuery(int projectId) { int docNum = GetLuceneProjectDocumentNumber(projectId); if (docNum == 0) { return(null); } var analyzer = new StandardAnalyzer(_version); using (var searcher = new IndexSearcher(_directory, false)) { IndexReader reader = searcher.IndexReader; var moreLikeThis = new MoreLikeThis(reader) { Analyzer = analyzer }; moreLikeThis.SetFieldNames(new[] { StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductId), StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title), StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Description), StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Price), StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductStatus), StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Category) }); moreLikeThis.MinDocFreq = 1; moreLikeThis.MinTermFreq = 1; moreLikeThis.Boost = true; return(moreLikeThis.Like(docNum)); } }
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()); } }
private static int GetLuceneProjectDocumentNumber(int projectId) { var analyzer = new StandardAnalyzer(_version); var parser = new QueryParser(_version, StronglyTyped.PropertyName <LuceneSearchModel>(x => x.ProductId), analyzer); Query query = parser.Parse(projectId.ToString(CultureInfo.InvariantCulture)); using (var searcher = new IndexSearcher(_directory, false)) { TopDocs doc = searcher.Search(query, 1); return(doc.TotalHits == 0 ? 0 : doc.ScoreDocs[0].Doc); } }
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); }
/// <summary> /// Checks the equality of the inner values. /// </summary> protected bool Equals(StronglyTyped <TInnerType> other) { return(string.Equals(Value, other.Value)); }
public void WP8_StronglyTypedTest() { StronglyTyped.RunTest(new DebugWriter(), TestFolder.Path, OutputFolder.Path); }
public void StronglyTypedTest() { StronglyTyped.RunTest(Console.Out, TestFolder, OutputFolder); }