Esempio n. 1
0
        private static void _addToLuceneIndex(LuceneSearchModel searchModel, IndexWriter writer)
        {
            // remove older index entry
            var searchQuery = new TermQuery(new Term(AppConstants.LucId, searchModel.Id.ToString()));

            writer.DeleteDocuments(searchQuery);

            // add new index entry
            var doc = new Document();

            // add lucene fields mapped to db fields
            // Posts
            doc.Add(new Field(AppConstants.LucId, searchModel.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(AppConstants.LucPostContent, searchModel.PostContent, Field.Store.YES, Field.Index.ANALYZED));

            //Topics
            if (!string.IsNullOrEmpty(searchModel.TopicName))
            {
                doc.Add(new Field(AppConstants.LucTopicName, searchModel.TopicName, Field.Store.YES, Field.Index.ANALYZED));
            }
            doc.Add(new Field(AppConstants.LucTopicUrl, searchModel.TopicUrl, Field.Store.YES, Field.Index.NOT_ANALYZED));

            // Chnage the date so we can query in date order
            var dateValue = DateTools.DateToString(searchModel.DateCreated, DateTools.Resolution.MILLISECOND);

            doc.Add(new Field(AppConstants.LucDateCreated, dateValue, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(AppConstants.LucTopicId, searchModel.TopicId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            //User
            doc.Add(new Field(AppConstants.LucUsername, searchModel.Username, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(AppConstants.LucUserId, searchModel.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            // add entry to index
            writer.AddDocument(doc);
        }
Esempio n. 2
0
 private IEnumerable <IIndexableField> GetFields(LuceneSearchModel model)
 {
     return(new Field[]
     {
         new TextField(nameof(model.PageId), model.PageId?.ToString(), Lucene.Net.Documents.Field.Store.YES),
         new TextField(nameof(model.Title), model.Title, Lucene.Net.Documents.Field.Store.YES)
         {
             Boost = 4.0f
         },
         new StringField(nameof(model.BriefDescription), model.BriefDescription, Lucene.Net.Documents.Field.Store.YES),
         //new StringField(nameof(model.Body), model.Body??"", Lucene.Net.Documents.Field.Store.NO),
         new StringField(nameof(model.SlugUrl), model.SlugUrl ?? "", Lucene.Net.Documents.Field.Store.YES),
         new StringField(nameof(model.Image), model.Image ?? "", Lucene.Net.Documents.Field.Store.YES),
         new StringField(nameof(model.CategoryId), model.CategoryId?.ToString() ?? "", Lucene.Net.Documents.Field.Store.YES),
         new StringField(nameof(model.CategoryName), model.CategoryName ?? "", Lucene.Net.Documents.Field.Store.YES),
         new StringField(nameof(model.IsShow), Convert.ToString(model.IsShow), Lucene.Net.Documents.Field.Store.YES),
     });
 }
Esempio n. 3
0
        /// <summary>
        /// Map a post to the Lucene Model
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public LuceneSearchModel MapToModel(Post post)
        {
            var model = new LuceneSearchModel
            {
                Id          = post.Id,
                PostContent = post.PostContent,
                DateCreated = post.DateCreated,
                TopicId     = post.Topic.Id,
                TopicUrl    = post.Topic.NiceUrl,
                Username    = post.User.UserName,
                UserId      = post.User.Id
            };

            if (post.IsTopicStarter)
            {
                model.TopicName = post.Topic.Name;
            }
            return(model);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 public static void AddUpdateLuceneIndex(LuceneSearchModel modelData)
 {
     AddUpdateLuceneIndex(new List <LuceneSearchModel> {
         modelData
     });
 }
Esempio n. 6
0
 /// <summary>
 /// Add / Update an item in the index
 /// </summary>
 /// <param name="luceneSearchModel"></param>
 public void AddUpdate(LuceneSearchModel luceneSearchModel)
 {
     GoLucene.AddUpdateLuceneIndex(luceneSearchModel);
 }