Пример #1
0
        protected internal virtual Query GetRangeQuery(string field,
                                                       string part1,
                                                       string part2,
                                                       bool startInclusive,
                                                       bool endInclusive)
        {
            if (LowercaseExpandedTerms)
            {
                part1 = part1 == null ? null : part1.ToLower(Locale);
                part2 = part2 == null ? null : part2.ToLower(Locale);
            }

            try
            {
                DateTime d1, d2;
                if (_useJavaStyleDateRangeParsing)
                {
                    // TODO: This doesn't emulate java perfectly.
                    // Java allows parsing of the string up to the end of the pattern
                    // and then ignores everything else.  .NET will throw an exception,
                    // so this will fail in those cases, though the code below is clear
                    // that users can only specify the date, not the time.
                    var shortFormat = Locale.DateTimeFormat.ShortDatePattern;
                    d1 = DateTime.ParseExact(part1, shortFormat, Locale);
                    d2 = DateTime.ParseExact(part2, shortFormat, Locale);
                }
                else
                {
                    d1 = DateTime.Parse(part1, Locale);
                    d2 = DateTime.Parse(part2, Locale);
                }

                if (endInclusive)
                {
                    // The user can only specify the date, not the time, so make sure
                    // the time is set to the latest possible time of that date to really
                    // include all documents:

                    // TODO: Try to work out if the Time Zone is pertinent here or
                    // whether it should just be removed from the API entirely.
                    // In Java:
                    // Calendar cal = Calendar.getInstance(timeZone, locale);

                    var cal = Locale.Calendar;
                    d2 = cal.AddHours(d2, 23);
                    d2 = cal.AddMinutes(d2, 59);
                    d2 = cal.AddSeconds(d2, 59);
                    d2 = cal.AddMilliseconds(d2, 999);
                }
                DateTools.Resolution resolution = GetDateResolution(field);

                part1 = DateTools.DateToString(d1, resolution);
                part2 = DateTools.DateToString(d2, resolution);
            }
            catch (Exception)
            {
            }

            return(NewRangeQuery(field, part1, part2, startInclusive, endInclusive));
        }
Пример #2
0
        private FieldCacheRangeFilter <string> BuildDateFilter(string startDate, string endDate)
        {
            var    includeLower = false;
            var    includeUpper = false;
            string start        = null;
            string end          = null;

            if (string.IsNullOrEmpty(startDate) == false)
            {
                includeLower = true;

                start = DateTools.DateToString(
                    Convert.ToDateTime(startDate),
                    DateTools.Resolution.DAY);
            }

            if (string.IsNullOrEmpty(endDate) == false)
            {
                includeUpper = true;

                end = DateTools.DateToString(
                    Convert.ToDateTime(endDate),
                    DateTools.Resolution.DAY);
            }

            return(FieldCacheRangeFilter.NewStringRange(
                       "dataCriacao",
                       start,
                       includeLower: includeLower,
                       upperVal: end,
                       includeUpper: includeUpper));
        }
Пример #3
0
        public void FillDocument(Song song, Document doc)
        {
            if (!string.IsNullOrWhiteSpace(song.RelativePath))
            {
                doc.Add(new Field(nameof(song.RelativePath), song.RelativePath, Field.Store.YES, Field.Index.ANALYZED));
            }

            if (!string.IsNullOrWhiteSpace(song.Name))
            {
                doc.Add(new Field(nameof(song.Name), song.Name, Field.Store.YES, Field.Index.ANALYZED));
            }

            if (!string.IsNullOrWhiteSpace(song.Artist))
            {
                doc.Add(new Field(nameof(song.Artist), song.Artist, Field.Store.YES, Field.Index.ANALYZED));
            }

            var fieldFileLenght = new NumericField(nameof(song.FileLength), Field.Store.YES, false);

            fieldFileLenght.SetIntValue(song.FileLength);
            doc.Add(fieldFileLenght);

            var fieldLenght = new NumericField(nameof(song.Length), Field.Store.YES, false);

            fieldLenght.SetIntValue((int)song.Length.TotalMilliseconds);
            doc.Add(fieldLenght);

            doc.Add(new Field(nameof(song.LastModified), DateTools.DateToString(song.LastModified, DateTools.Resolution.SECOND), Field.Store.YES,
                              Field.Index.NOT_ANALYZED));
        }
Пример #4
0
        public void DeleteDocumentsFrom(DateTime fromDate)
        {
            string sFrom = DateTools.DateToString(fromDate, DateTools.Resolution.MINUTE);

            Lucene.Net.Search.TermRangeQuery queryFrom = new Lucene.Net.Search.TermRangeQuery("LastWriteTime", sFrom, null, false, false);
            writer.DeleteDocuments(queryFrom);
        }
Пример #5
0
        public static IEnumerable <Document> CreateDocuments()
        {
            var docs = new List <Document>();

            var doc = new Document();

            doc.Add(new Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("date", DateTools.DateToString(new DateTime(2000, 1, 1), DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            //doc.Add(new Field("date", "2000/01/01", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("title", "Mr", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            docs.Add(doc);

            doc = new Document();
            doc.Add(new Field("id", "2", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("date", DateTools.DateToString(new DateTime(2001, 1, 1), DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            //doc.Add(new Field("date", "2001/01/01", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("title", "Mr", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            docs.Add(doc);

            doc = new Document();
            doc.Add(new Field("id", "3", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("date", DateTools.DateToString(new DateTime(2002, 1, 1), DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            //doc.Add(new Field("date", "2002/01/01", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("title", "Ms", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            docs.Add(doc);

            doc = new Document();
            doc.Add(new Field("id", "4", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("date", DateTools.DateToString(new DateTime(2002, 1, 2), DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            //doc.Add(new Field("date", "2001/01/02", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            doc.Add(new Field("title", "Mrs", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            docs.Add(doc);

            return(docs);
        }
Пример #6
0
        public Document Transform(Ticket ticket)
        {
            var searchDoc = new Document
            {
                new StringField(nameof(ticket._id), NormalizeForIndex(ticket._id), Field.Store.YES),
                new StringField(InMemoryLuceneSearch.DOC_TYPE_FIELD, NormalizeForIndex(typeof(Ticket).Name), Field.Store.YES),

                new StringField(nameof(ticket.assignee_id), NormalizeForIndex(ticket.assignee_id), Field.Store.NO),
                new StringField(nameof(ticket.created_at), DateTools.DateToString(ticket.created_at.DateTime, DateTools.Resolution.SECOND), Field.Store.NO),
                new TextField(nameof(ticket.description), NormalizeForIndex(ticket.description), Field.Store.NO),
                new StringField(nameof(ticket.due_at), DateTools.DateToString(ticket.due_at.DateTime, DateTools.Resolution.SECOND), Field.Store.NO),
                new StringField(nameof(ticket.external_id), NormalizeForIndex(ticket.external_id), Field.Store.NO),
                new StringField(nameof(ticket.has_incidents), NormalizeForIndex(ticket.has_incidents), Field.Store.NO),
                new StringField(nameof(ticket.priority), NormalizeForIndex(ticket.priority), Field.Store.NO),
                new StringField(nameof(ticket.organization_id), NormalizeForIndex(ticket.organization_id), Field.Store.NO),
                new StringField(nameof(ticket.status), NormalizeForIndex(ticket.status), Field.Store.NO),
                new TextField(nameof(ticket.subject), NormalizeForIndex(ticket.subject), Field.Store.NO),
                new StringField(nameof(ticket.submitter_id), NormalizeForIndex(ticket.submitter_id), Field.Store.NO),
                new StringField(nameof(ticket.type), NormalizeForIndex(ticket.type), Field.Store.NO),
                new TextField(nameof(ticket.url), NormalizeForIndex(ticket.url), Field.Store.NO),
                new StringField(nameof(ticket.via), NormalizeForIndex(ticket.via), Field.Store.NO),

                new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(ticket.description), Field.Store.NO),
                new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(ticket.subject), Field.Store.NO),
                new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(ticket.external_id), Field.Store.NO),
            };

            AddTagFields(searchDoc, ticket.tags);

            return(searchDoc);
        }
Пример #7
0
        public void Range(string fieldName, object lowerValue, object upperValue, bool includeLower = true, bool includeUpper = true, ClauseOccur occur = ClauseOccur.SHOULD, float boost = 1.0f)
        {
            Query q = null;
            var   f = DocumentBuilder.GetField(TypeName, fieldName);

            switch (f.FieldType)
            {
            case FieldType.Int:
                q = NumericRangeQuery.NewIntRange(fieldName, (int)lowerValue, (int)upperValue, includeLower, includeUpper);
                break;

            case FieldType.Long:
                q = NumericRangeQuery.NewLongRange(fieldName, (long)lowerValue, (long)upperValue, includeLower, includeUpper);
                break;

            case FieldType.Float:
                q = NumericRangeQuery.NewFloatRange(fieldName, (float)lowerValue, (float)upperValue, includeLower, includeUpper);
                break;

            case FieldType.Double:
                q = NumericRangeQuery.NewDoubleRange(fieldName, (double)lowerValue, (double)upperValue, includeLower, includeUpper);
                break;

            case FieldType.DateTime:
                q = new TermRangeQuery(fieldName, DateTools.DateToString((DateTime)lowerValue, DateTools.Resolution.MILLISECOND), DateTools.DateToString((DateTime)upperValue, DateTools.Resolution.MILLISECOND), includeLower, includeUpper);
                break;

            case FieldType.String:
            default:
                q = new TermRangeQuery(fieldName, lowerValue.ToString(), upperValue.ToString(), includeLower, includeUpper);
                break;
            }
            q.Boost = boost;
            query.Add(q, (Occur)((int)occur));
        }
Пример #8
0
        /// <summary>
        /// BarPost转换成<see cref="Lucene.Net.Documents.Document"/>
        /// </summary>
        /// <param name="BarPost">回帖实体</param>
        /// <returns>Lucene.Net.Documents.Document</returns>
        public static Document Convert(BarPost barPost)
        {
            Document doc = new Document();

            //索引回帖基本信息
            doc.Add(new Field(BarIndexDocument.SectionId, barPost.SectionId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.ThreadId, "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.PostId, barPost.PostId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            //如果回帖没有主题存储其主贴的主题
            if (string.IsNullOrEmpty(barPost.Subject))
            {
                string subject = barThreadService.Get(barPostService.Get(barPost.PostId).ThreadId).Subject.ToLower();
                doc.Add(new Field(BarIndexDocument.Subject, subject, Field.Store.YES, Field.Index.ANALYZED));
            }
            else
            {
                doc.Add(new Field(BarIndexDocument.Subject, barPost.Subject, Field.Store.YES, Field.Index.ANALYZED));
            }
            doc.Add(new Field(BarIndexDocument.Body, HtmlUtility.TrimHtml(barPost.GetBody(), 0).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(BarIndexDocument.Author, barPost.Author, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(BarIndexDocument.IsPost, "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.DateCreated, DateTools.DateToString(barPost.DateCreated, DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.TenantTypeId, barPost.TenantTypeId, Field.Store.YES, Field.Index.NOT_ANALYZED));

            return(doc);
        }
Пример #9
0
        static void Main(string[] args)
        {
            Lucene.Net.Util.Version version = Lucene.Net.Util.Version.LUCENE_29;

            Directory dir      = new RAMDirectory();
            Analyzer  analyzer = new StandardAnalyzer(version);

            var docs = CreateDocuments();

            AddToIndex(docs, dir, analyzer);

            // Search for the content
            var   parser = new MultiFieldQueryParser(version, new[] { "name" }, analyzer);
            Query q      = parser.Parse("An*");

            Filter filter = TermRangeFilter.More("date", DateTools.DateToString(new DateTime(2011, 1, 1), DateTools.Resolution.DAY));

            var searcher = new IndexSearcher(dir, true);

            TopDocs hits = searcher.Search(q, filter, 5, Sort.RELEVANCE);

            Console.WriteLine("Found {0} document(s) that matched query '{1}':", hits.TotalHits, q);
            foreach (ScoreDoc match in hits.ScoreDocs)
            {
                Document doc = searcher.Doc(match.Doc);
                Console.WriteLine("Matched id = {0}, Name = {1}", doc.Get("id"), doc.Get("name"));
            }
            searcher.Close();
        }
Пример #10
0
        private bool isInIndex(IndexableFileInfo fileInfo)
        {
            IndexSearcher searcher = new IndexSearcher(this.luceneIndexDir);

            try
            {
                BooleanQuery bq = new BooleanQuery();
                bq.Add(new TermQuery(new Term("filename", fileInfo.Filename)), BooleanClause.Occur.MUST);

                bq.Add(new TermQuery(new Term("LastModified", DateTools.DateToString(fileInfo.LastModified, DateTools.Resolution.SECOND))), BooleanClause.Occur.MUST);

                Hits hits  = searcher.Search(bq);
                int  count = hits.Length();

                if (count > 0)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
                searcher.Close();
            }
            return(false);
        }
Пример #11
0
        public Document Transform(Organization org)
        {
            var searchDoc = new Document
            {
                new StringField(nameof(org._id), NormalizeForIndex(org._id), Field.Store.YES),
                new StringField(InMemoryLuceneSearch.DOC_TYPE_FIELD, NormalizeForIndex(typeof(Organization).Name), Field.Store.YES),

                new StringField(nameof(org.created_at), DateTools.DateToString(org.created_at.DateTime, DateTools.Resolution.SECOND), Field.Store.NO),
                new TextField(nameof(org.details), NormalizeForIndex(org.details), Field.Store.NO),
                new StringField(nameof(org.external_id), NormalizeForIndex(org.external_id), Field.Store.NO),
                new TextField(nameof(org.name), NormalizeForIndex(org.name), Field.Store.NO),
                new StringField(nameof(org.shared_tickets), NormalizeForIndex(org.shared_tickets), Field.Store.NO),
                new TextField(nameof(org.url), NormalizeForIndex(org.url), Field.Store.NO),

                new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(org.details), Field.Store.NO),
                new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(org.name), Field.Store.NO),
                new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(org.external_id), Field.Store.NO)
            };

            AddTagFields(searchDoc, org.tags);

            foreach (var domain in org.domain_names ?? new List <string>())
            {
                searchDoc.Add(new StringField(nameof(org.domain_names), NormalizeForIndex(domain), Field.Store.NO));
                searchDoc.Add(new TextField(InMemoryLuceneSearch.GLOBAL_SEARCH_FIELD, NormalizeForIndex(domain), Field.Store.NO));
            }


            return(searchDoc);
        }
Пример #12
0
        /// <summary>
        /// TopicEntity转换成<see cref="Lucene.Net.Documents.Document"/>
        /// </summary>
        /// <param name="TopicEntity">专题实体</param>
        /// <returns>Lucene.Net.Documents.Document</returns>
        public static Document Convert(TopicEntity group)
        {
            Document doc = new Document();

            //索引专题基本信息
            doc.Add(new Field(TopicIndexDocument.TopicId, group.TopicId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.TopicName, group.TopicName, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(TopicIndexDocument.Description, group.Description, Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(TopicIndexDocument.IsPublic, group.IsPublic == true ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.AreaCode, group.AreaCode, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.UserId, group.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.AuditStatus, ((int)group.AuditStatus).ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.DateCreated, DateTools.DateToString(group.DateCreated, DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.MemberCount, group.MemberCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(TopicIndexDocument.GrowthValue, group.GrowthValue.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            if (group.Category != null)
            {
                doc.Add(new Field(TopicIndexDocument.CategoryName, group.Category.CategoryName, Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field(TopicIndexDocument.CategoryId, group.Category.CategoryId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            }

            //索引专题tag
            foreach (string tagName in group.TagNames)
            {
                doc.Add(new Field(TopicIndexDocument.Tag, tagName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            }

            return(doc);
        }
Пример #13
0
        /// <summary>
        /// WikiPage转换成Document
        /// </summary>
        /// <param name="question">WikiPage</param>
        /// <returns>WikiPage对应的document对象</returns>
        public static Document Convert(WikiPage wikiPage)
        {
            //文档初始权重
            Document doc = new Document();

            //不索引问题审核状态为不通过的、已取消的
            if (wikiPage.AuditStatus != AuditStatus.Fail && wikiPage.IsLogicalDelete != true)
            {
                //索引基本信息
                doc.Add(new Field(WikiIndexDocument.PageId, wikiPage.PageId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field(WikiIndexDocument.Author, wikiPage.Author.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field(WikiIndexDocument.Title, wikiPage.Title.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                if (!string.IsNullOrWhiteSpace(wikiPage.Body))
                {
                    string body = wikiPage.Body;
                    if (wikiPage.LastestVersion != null && !string.IsNullOrEmpty(wikiPage.LastestVersion.Body))
                    {
                        body = wikiPage.LastestVersion.Body;
                    }
                    doc.Add(new Field(WikiIndexDocument.Body, HtmlUtility.TrimHtml(body, 0).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
                }
                doc.Add(new Field(WikiIndexDocument.PinYin, Pinyin.GetPinyin(wikiPage.Title.ToLower()), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.Add(new Field(WikiIndexDocument.DateCreated, DateTools.DateToString(wikiPage.DateCreated, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.NOT_ANALYZED));

                //索引问题标签
                foreach (var tag in wikiPage.TagNames)
                {
                    doc.Add(new Field(WikiIndexDocument.Tag, tag.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                }
                doc.Add(new Field(WikiIndexDocument.CategoryId, wikiPage.SiteCategory.CategoryId.ToString(), Field.Store.YES, Field.Index.ANALYZED));
                doc.Add(new Field(WikiIndexDocument.DateCreated, DateTools.DateToString(wikiPage.DateCreated, DateTools.Resolution.DAY), Field.Store.YES, Field.Index.ANALYZED));
            }

            return(doc);
        }
Пример #14
0
        public void LastModifiedIsQueryable()
        {
            var frozen = DateTime.UtcNow;

            SystemTime.UtcDateTime = () => frozen;
            using (var store = NewDocumentStore())
            {
                new RavenDocumentsByEntityName().Execute(store);

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "John Doe"
                    });
                    session.SaveChanges();

                    var dateTime = DateTools.DateToString(frozen.AddSeconds(1), DateTools.Resolution.MILLISECOND);

                    var results = session.Advanced.DocumentQuery <object>(new RavenDocumentsByEntityName().IndexName)
                                  .Where("LastModified:[* TO " + dateTime + "]")
                                  .WaitForNonStaleResults()
                                  .ToArray();

                    Assert.NotEqual(0, results.Count());
                }
            }
        }
Пример #15
0
        private static string TransformToEqualValue(WhereEqualsParams whereEqualsParams)
        {
            if (whereEqualsParams.Value == null)
            {
                return("[[NULL_VALUE]]");
            }

            if (whereEqualsParams.Value is bool)
            {
                return((bool)whereEqualsParams.Value ? "true" : "false");
            }

            if (whereEqualsParams.Value is DateTime)
            {
                return(DateTools.DateToString((DateTime)whereEqualsParams.Value, DateTools.Resolution.MILLISECOND));
            }

            var escaped = RavenQuery.Escape(Convert.ToString(whereEqualsParams.Value, CultureInfo.InvariantCulture),
                                            whereEqualsParams.AllowWildcards && whereEqualsParams.IsAnalyzed);

            if (whereEqualsParams.Value is string == false)
            {
                return(escaped);
            }

            return(whereEqualsParams.IsAnalyzed ? escaped : String.Concat("[[", escaped, "]]"));
        }
Пример #16
0
        /// <summary>
        /// BarThread转换成<see cref="Lucene.Net.Documents.Document"/>
        /// </summary>
        /// <param name="BarThread">发帖实体</param>
        /// <returns>Lucene.Net.Documents.Document</returns>
        public static Document Convert(BarThread barThread)
        {
            Document doc = new Document();

            //索引发帖基本信息
            doc.Add(new Field(BarIndexDocument.SectionId, barThread.SectionId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.ThreadId, barThread.ThreadId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.PostId, "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.Subject, barThread.Subject.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(BarIndexDocument.Body, HtmlUtility.StripHtml(barThread.GetBody(), true, false).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(BarIndexDocument.Author, barThread.Author, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(BarIndexDocument.IsPost, "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.DateCreated, DateTools.DateToString(barThread.DateCreated, DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BarIndexDocument.TenantTypeId, barThread.TenantTypeId, Field.Store.YES, Field.Index.NOT_ANALYZED));

            //索引发帖tag
            TagService tagService = new TagService(TenantTypeIds.Instance().BarThread());

            IEnumerable <ItemInTag> itemInTags = tagService.GetItemInTagsOfItem(barThread.ThreadId);

            foreach (ItemInTag itemInTag in itemInTags)
            {
                doc.Add(new Field(BarIndexDocument.Tag, itemInTag.TagName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            }

            return(doc);
        }
Пример #17
0
        private static string TransformToEqualValue(object value, bool isAnalyzed, bool allowWildcards)
        {
            if (value == null)
            {
                return("NULL_VALUE");
            }

            if (value is bool)
            {
                return((bool)value ? "true" : "false");
            }

            if (value is DateTime)
            {
                return(DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND));
            }

            if (!(value is string))
            {
                return(Convert.ToString(value, CultureInfo.InvariantCulture));
            }

            string escaped = LuceneEscape(Convert.ToString(value, CultureInfo.InvariantCulture), allowWildcards && isAnalyzed);

            return(isAnalyzed ? escaped : String.Concat("[[", escaped, "]]"));
        }
Пример #18
0
        public void LastModifiedUsesCorrectDateTimeFormatInIndex()
        {
            using (var store = NewDocumentStore())
            {
                new RavenDocumentsByEntityName().Execute(store);

                var user = new User {
                    Name = "John Doe"
                };
                using (var session = store.OpenSession())
                {
                    session.Store(user);
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    user = session.Load <User>("users/1");
                    var ravenJObject = session.Advanced.GetMetadataFor(user);
                    var dateTime     = ravenJObject.Value <DateTime>("Last-Modified").ToUniversalTime();
                    var results      = session.Advanced.LuceneQuery <object>(new RavenDocumentsByEntityName().IndexName)
                                       .WhereEquals("LastModified", DateTools.DateToString(dateTime, DateTools.Resolution.MILLISECOND))
                                       .WaitForNonStaleResults()
                                       .ToArray();
                    Assert.Equal(1, results.Count());
                }
            }
        }
Пример #19
0
        private static string TransformToRangeValue(object value)
        {
            if (value == null)
            {
                return("NULL_VALUE");
            }

            if (value is int)
            {
                return(NumberUtil.NumberToString((int)value));
            }
            if (value is long)
            {
                return(NumberUtil.NumberToString((long)value));
            }
            if (value is decimal)
            {
                return(NumberUtil.NumberToString((double)(decimal)value));
            }
            if (value is double)
            {
                return(NumberUtil.NumberToString((double)value));
            }
            if (value is float)
            {
                return(NumberUtil.NumberToString((float)value));
            }
            if (value is DateTime)
            {
                return(DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND));
            }

            return(LuceneEscape(value.ToString(), false));
        }
Пример #20
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);
        }
        public Document Convert(Offers entity)
        {
            var document = new Document();

            document.Add(new Field("OfferID", entity.OfferID, Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("Name", entity.Name, Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field("User", entity.Users.UserID, Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("Photo1URL", entity.Photo1URL ?? "v6ac3uaxfe5gtlqtrbr7", Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("Price", entity.Price, Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("OfferedBy", entity.OfferedBy, Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("Tags", String.Join(" ", entity.Tags.Select(m => m.Name)), Field.Store.YES, Field.Index.ANALYZED));
            document.Add(new Field("Rating", String.Join(" ", entity.OfferRaiting.Select(m => m.Raiting)), Field.Store.YES, Field.Index.NOT_ANALYZED));
            if (!string.IsNullOrEmpty(entity.Description))
            {
                document.Add(new Field("Description", entity.Description, Field.Store.YES, Field.Index.ANALYZED));
            }

            document.Add(new Field("CreationDate", DateTools.DateToString(entity.DateAndTime.GetValueOrDefault(), DateTools.Resolution.DAY),
                                   Field.Store.YES, Field.Index.NOT_ANALYZED));

            //if (entity.Price != null)
            //{
            //    var priceField = new NumericField("Price", Field.Store.YES, true);
            //    priceField.SetIntValue(Int32.Parse(entity.Price.Substring(0, entity.Price.Length - 3)));
            //    document.Add(priceField);
            //}


            return(document);
        }
Пример #22
0
        private static Document CreateDocument(Post t)
        {
            Document doc = new Document();

            doc.Add(new Field("postid", t.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("title", t.Title, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("body", RemoveHtml(t.Body, 0), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("exact", RemoveHtml(t.Body, 0), Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES));
            doc.Add(new Field("rawbody", t.Body, Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("category", t.Category.Name, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("categoryid", t.Category.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("username", t.UserName, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("modifiedby", t.ModifiedBy, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("createdby", t.CreatedBy, Field.Store.YES, Field.Index.NOT_ANALYZED));
            foreach (string tag in Util.ConvertStringToList(t.TagList))
            {
                doc.Add(new Field("tag", tag, Field.Store.YES, Field.Index.ANALYZED));
            }
            doc.Add(new Field("author", t.CreatedBy, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("date", DateTools.DateToString(t.Published, DateTools.Resolution.HOUR), Field.Store.YES,
                              Field.Index.NOT_ANALYZED));
            doc.Add(new Field("createddate", DateTools.DateToString(t.CreatedOn, DateTools.Resolution.HOUR), Field.Store.YES,
                              Field.Index.NOT_ANALYZED));
            doc.Add(new Field("modifieddate", DateTools.DateToString(t.ModifiedOn, DateTools.Resolution.HOUR), Field.Store.YES,
                              Field.Index.NOT_ANALYZED));
            doc.Add(new Field("name", t.Name, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("image", t.ImageUrl ?? string.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("commentcount", t.CommentCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("pendingcommentcount", t.PendingCommentCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field("propertykeys", t.PropertyKeys, Field.Store.YES, Field.Index.NO));
            doc.Add(new Field("propertyvalues", t.PropertyValues, Field.Store.YES, Field.Index.NO));
            return(doc);
        }
Пример #23
0
        /// <summary>
        /// MicroblogEntity转换成<see cref="Lucene.Net.Documents.Document"/>
        /// </summary>
        /// <param name="microblog">微博实体</param>
        /// <returns>Lucene.Net.Documents.Document</returns>
        public static Document Convert(MicroblogEntity microblog)
        {
            Document doc = new Document();

            //索引微博基本信息
            doc.Add(new Field(MicroblogIndexDocument.MicroblogId, microblog.MicroblogId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            if (microblog.OriginalMicroblog != null)
            {
                doc.Add(new Field(MicroblogIndexDocument.Body, HtmlUtility.StripHtml(microblog.Body, true, false).ToLower() + HtmlUtility.StripHtml(microblog.OriginalMicroblog.Body, true, false).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
            }
            else
            {
                doc.Add(new Field(MicroblogIndexDocument.Body, HtmlUtility.StripHtml(microblog.Body, true, false).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
            }
            doc.Add(new Field(MicroblogIndexDocument.DateCreated, DateTools.DateToString(microblog.DateCreated, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(MicroblogIndexDocument.HasMusic, microblog.HasMusic ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(MicroblogIndexDocument.HasPhoto, microblog.HasPhoto ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(MicroblogIndexDocument.HasVideo, microblog.HasVideo ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(MicroblogIndexDocument.IsOriginality, microblog.ForwardedMicroblogId == 0 ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(MicroblogIndexDocument.TenantTypeId, microblog.TenantTypeId, Field.Store.YES, Field.Index.NOT_ANALYZED));


            TagService tagService = new TagService(TenantTypeIds.Instance().Microblog());

            IEnumerable <ItemInTag> itemInTags = tagService.GetItemInTagsOfItem(microblog.MicroblogId);

            foreach (ItemInTag itemInTag in itemInTags)
            {
                doc.Add(new Field(MicroblogIndexDocument.Topic, itemInTag.TagName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            }

            return(doc);
        }
Пример #24
0
        public void CanQueryByDateRange_GreaterThan()
        {
            using (var store = NewDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new Record
                    {
                        Date = new DateTime(2001, 1, 1)
                    });
                    session.SaveChanges();
                }

                store.DatabaseCommands.PutIndex("Date",
                                                new IndexDefinition
                {
                    Map = "from doc in docs select new { doc.Date}"
                });


                using (var session = store.OpenSession())
                {
                    var result = session.Advanced.DocumentQuery <Record>("Date")
                                 .Where("Date:[" + DateTools.DateToString(new DateTime(2000, 1, 1), DateTools.Resolution.MILLISECOND) + " TO NULL]")
                                 .WaitForNonStaleResults()
                                 .ToList();

                    Assert.Equal(1, result.Count);
                }
            }
        }
Пример #25
0
        /// <summary>
        /// contentItem转换成<see cref="Lucene.Net.Documents.Document"/>
        /// </summary>
        /// <param name="contentItem">资讯实体</param>
        /// <returns>Lucene.Net.Documents.Document</returns>
        public static Document Convert(ContentItem contentItem)
        {
            Document doc = new Document();

            //索引资讯基本信息
            doc.Add(new Field(CmsIndexDocument.ContentItemId, contentItem.ContentItemId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(CmsIndexDocument.IsEssential, contentItem.IsEssential ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(CmsIndexDocument.Summary, contentItem.Summary ?? "", Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(CmsIndexDocument.UserId, contentItem.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(CmsIndexDocument.Title, contentItem.Title.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(CmsIndexDocument.Body, HtmlUtility.StripHtml(contentItem.AdditionalProperties.Get <string>("Body", string.Empty), true, false).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(CmsIndexDocument.Author, contentItem.Author, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(CmsIndexDocument.DateCreated, DateTools.DateToString(contentItem.DateCreated, DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(CmsIndexDocument.AuditStatus, ((int)contentItem.AuditStatus).ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            //索引资讯tag
            foreach (string tagName in contentItem.TagNames)
            {
                doc.Add(new Field(CmsIndexDocument.Tag, tagName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            }

            //索引资讯用户分类名称
            if (contentItem.ContentFolder != null)
            {
                doc.Add(new Field(CmsIndexDocument.FolderName, contentItem.ContentFolder.FolderName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            }

            //索引资讯站点分类Id
            doc.Add(new Field(CmsIndexDocument.ContentFolderId, contentItem.ContentFolderId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            return(doc);
        }
Пример #26
0
        protected override bool HandleWork()
        {
            var         currentTime = ExpirationReadTrigger.GetCurrentUtcDate();
            var         nowAsStr    = DateTools.DateToString(currentTime, DateTools.Resolution.SECOND);
            int         retries     = 0;
            QueryResult queryResult;

            do
            {
                queryResult = Database.Query(RavenDocumentsByExpirationDate, new IndexQuery
                {
                    Cutoff        = currentTime,
                    Query         = "Expiry:[* TO " + nowAsStr + "]",
                    FieldsToFetch = new [] { "__document_id" }
                });

                if (queryResult.IsStale)
                {
                    if (++retries > 25)
                    {
                        return(false);
                    }
                    Thread.Sleep(100);
                }
            } while (queryResult.IsStale);

            foreach (var result in queryResult.Results)
            {
                var docId = result.Value <string>("__document_id");
                Database.Delete(docId, null, null);
            }

            return(queryResult.Results.Count > 0);
        }
Пример #27
0
        private Document CreateLuceneDocument(DocumentIndex documentIndex)
        {
            var doc = new Document();

            // Always store the content item id
            doc.Add(new StringField("ContentItemId", documentIndex.ContentItemId.ToString(), Field.Store.YES));

            foreach (var entry in documentIndex.Entries)
            {
                var store = entry.Value.Options.HasFlag(DocumentIndexOptions.Store)
                            ? Field.Store.YES
                            : Field.Store.NO
                ;

                if (entry.Value.Value == null)
                {
                    continue;
                }

                switch (entry.Value.Type)
                {
                case DocumentIndex.Types.Boolean:
                    // store "true"/"false" for booleans
                    doc.Add(new StringField(entry.Key, Convert.ToString(entry.Value.Value).ToLowerInvariant(), store));
                    break;

                case DocumentIndex.Types.DateTime:
                    if (entry.Value.Value is DateTimeOffset)
                    {
                        doc.Add(new StringField(entry.Key, DateTools.DateToString(((DateTimeOffset)entry.Value.Value).UtcDateTime, DateTools.Resolution.SECOND), store));
                    }
                    else
                    {
                        doc.Add(new StringField(entry.Key, DateTools.DateToString(((DateTime)entry.Value.Value).ToUniversalTime(), DateTools.Resolution.SECOND), store));
                    }
                    break;

                case DocumentIndex.Types.Integer:
                    doc.Add(new IntField(entry.Key, Convert.ToInt32(entry.Value.Value), store));
                    break;

                case DocumentIndex.Types.Number:
                    doc.Add(new DoubleField(entry.Key, Convert.ToDouble(entry.Value.Value), store));
                    break;

                case DocumentIndex.Types.Text:
                    if (entry.Value.Options.HasFlag(DocumentIndexOptions.Analyze))
                    {
                        doc.Add(new TextField(entry.Key, Convert.ToString(entry.Value.Value), store));
                    }
                    else
                    {
                        doc.Add(new StringField(entry.Key, Convert.ToString(entry.Value.Value), store));
                    }
                    break;
                }
            }

            return(doc);
        }
Пример #28
0
        public void CanQueryByDateRange_GreaterThan()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new Record
                    {
                        Date = new DateTime(2001, 1, 1)
                    });
                    session.SaveChanges();
                }

                store.Maintenance.Send(new PutIndexesOperation(new[] { new IndexDefinition {
                                                                           Maps = { "from doc in docs select new { doc.Date}" },
                                                                           Name = "Date"
                                                                       } }));

                using (var session = store.OpenSession())
                {
                    var result = session.Advanced.DocumentQuery <Record>("Date")
                                 .WhereLucene("Date", "[" + DateTools.DateToString(new DateTime(2000, 1, 1), DateTools.Resolution.MILLISECOND) + " TO NULL]")
                                 .WaitForNonStaleResults()
                                 .ToList();

                    Assert.Equal(1, result.Count);
                }
            }
        }
Пример #29
0
        /// <summary>
        /// Recursively tokenize the object into fields whose names are available via dot notation.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="prefix"></param>
        /// <param name="dictionary"></param>
        private static void TokenizeObject(JObject obj, string prefix, ref Dictionary <string, AbstractField> dictionary)
        {
            if (obj == null)
            {
                return;
            }

            //TODO: Add property-based ("$propertyName") conventions that allow the parameters to be customized.
            foreach (var property in obj.Properties().Where(p => p.Value != null))
            {
                var fieldName = String.IsNullOrEmpty(prefix) ? property.Name : prefix + "." + property.Name;
                switch (property.Value.Type)
                {
                case JTokenType.Date:
                    var dateString = DateTools.DateToString((DateTime)property.Value, DateTools.Resolution.MILLISECOND);
                    var dateField  = new Field(fieldName, dateString, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES);

                    dictionary.Add(fieldName, dateField);
                    break;

                case JTokenType.TimeSpan:
                    var timeSpanField = new NumericField(fieldName, Field.Store.YES, true);
                    timeSpanField.SetLongValue(((TimeSpan)property.Value).Ticks);
                    dictionary.Add(fieldName, timeSpanField);
                    break;

                case JTokenType.Integer:
                    var intField = new NumericField(fieldName, Field.Store.YES, true);
                    intField.SetIntValue((int)property.Value);
                    dictionary.Add(fieldName, intField);
                    break;

                case JTokenType.Float:
                    var floatField = new NumericField(fieldName, Field.Store.YES, true);
                    floatField.SetFloatValue((float)property.Value);
                    dictionary.Add(fieldName, floatField);
                    break;

                case JTokenType.Guid:
                    var guidField = new Field(fieldName, property.Value.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED,
                                              Field.TermVector.NO);
                    dictionary.Add(fieldName, guidField);
                    break;

                case JTokenType.Object:
                    TokenizeObject(property.Value as JObject, fieldName, ref dictionary);
                    break;

                default:
                    if (String.IsNullOrEmpty(property.Value.ToString()) == false)
                    {
                        var stringField = new Field(fieldName, property.Value.ToString(), Field.Store.YES, Field.Index.ANALYZED,
                                                    Field.TermVector.WITH_POSITIONS_OFFSETS);
                        dictionary.Add(fieldName, stringField);
                    }
                    break;
                }
            }
        }
        public Field MapToField(PropertyInfo propertyInfo, object value, string name)
        {
            DateTime convertedValue = (DateTime)value;

            return(new StringField(name,
                                   DateTools.DateToString(convertedValue, DateTools.Resolution.MILLISECOND),
                                   GetStore(propertyInfo)));
        }