Exemplo n.º 1
0
        private string GetIndexFieldName(PropertyInfo property)
        {
            var fieldName = ((IndexFieldNameAttribute)property.GetCustomAttributes(typeof(IndexFieldNameAttribute), true).FirstOrDefault())?.IndexFieldName
                            ?? property.Name;

            return(ContentIndexHelpers.GetIndexFieldName(fieldName));
        }
Exemplo n.º 2
0
        public virtual List <SearchFacet> GetSearchFacets(IQueryExpression expression, string[] groupByFields, Directory directory = null)
        {
            if (directory == null)
            {
                directory = LuceneContext.Directory;
            }
            groupByFields = groupByFields.Select(x => ContentIndexHelpers.GetIndexFieldName(x)).ToArray();
            var result = new List <SearchFacet>();

            using (IndexReader indexReader = IndexReader.Open(directory, true))
            {
                var queryParser = new MultiFieldQueryParser(LuceneConfiguration.LuceneVersion, expression.GetFieldName()
                                                            , LuceneConfiguration.Analyzer);
                queryParser.AllowLeadingWildcard = true;
                var query = queryParser.Parse(expression.GetExpression());
                SimpleFacetedSearch      facetSearch = new SimpleFacetedSearch(indexReader, groupByFields);
                SimpleFacetedSearch.Hits hits        = facetSearch.Search(query, int.MaxValue);
                long totalHits = hits.TotalHitCount;
                foreach (SimpleFacetedSearch.HitsPerFacet hitPerGroup in hits.HitsPerFacet)
                {
                    long hitCountPerGroup = hitPerGroup.HitCount;
                    result.Add(new SearchFacet()
                    {
                        Count = hitPerGroup.HitCount,
                        Term  = hitPerGroup.Name.ToString()
                    });
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        public virtual Collection <SearchDocument> SearchByField(string fieldname, string value, int maxHits, out int totalHits)
        {
            Collection <SearchDocument> collection = new Collection <SearchDocument>();

            totalHits = 0;
            try
            {
                Query query = new QueryParser(LuceneConfiguration.LuceneVersion, fieldname, LuceneConfiguration.Analyzer).Parse(value);
                using (IndexSearcher indexSearcher = new IndexSearcher(LuceneContext.Directory, true))
                {
                    TopDocs topDocs = indexSearcher.Search(query, maxHits);
                    totalHits = topDocs.TotalHits;
                    ScoreDoc[] scoreDocs = topDocs.ScoreDocs;
                    for (int index = 0; index < scoreDocs.Length; ++index)
                    {
                        var document = indexSearcher.Doc(scoreDocs[index].Doc);
                        collection.Add(new SearchDocument(document.GetField(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID)).StringValue, document));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Lucene Search Error", ex);
            }
            return(collection);
        }
Exemplo n.º 4
0
 public void AddContentNodes(ContentReference contentLink)
 {
     if (ContentReference.IsNullOrEmpty(contentLink))
     {
         return;
     }
     foreach (string virtualPathNode in ContentIndexHelpers.GetVirtualPathNodes(contentLink))
     {
         _virtualPathNodes.Add(virtualPathNode);
     }
 }
 public static IQueryExpression FilterByIdsToInclude(this IQueryExpression expression, IList <int> IdsToInclude)
 {
     if (IdsToInclude != null && IdsToInclude.Any())
     {
         foreach (var id in IdsToInclude)
         {
             var fieldQuery = new FieldQuery(Constants.INDEX_FIELD_NAME_ID, ContentIndexHelpers.GetIndexFieldId(new ContentReference(id)), true);
             expression = expression.Or(fieldQuery);
         }
     }
     return(expression);
 }
Exemplo n.º 6
0
        public virtual Document GetDocumentById(string id)
        {
            int totalHits = 0;
            Collection <SearchDocument> collection = this.SearchByField(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID)
                                                                        , QueryParser.Escape(id), 1, out totalHits);

            if (collection.Count > 0)
            {
                return(collection[0].Document);
            }
            return(null);
        }
        public string GetExpression()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(ContentIndexHelpers.GetIndexFieldName(Field));
            stringBuilder.Append(":");
            stringBuilder.Append(this.Inclusive ? "[" : "{");
            stringBuilder.Append(LuceneQueryHelper.Escape(this.Start));
            stringBuilder.Append(" TO ");
            stringBuilder.Append(LuceneQueryHelper.Escape(this.End));
            stringBuilder.Append(this.Inclusive ? "]" : "}");
            return(stringBuilder.ToString());
        }
 public static void AddDefaultFieldAnalyzer()
 {
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID), new KeywordAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_NAME), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_STATUS), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_VIRTUAL_PATH), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ACL), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_TYPE), new StandardAnalyzer(LuceneVersion));
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_LANGUAGE), new KeywordAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_START_PUBLISHDATE), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_STOP_PUBLISHDATE), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_REFERENCE), new KeywordAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_CREATED), new WhitespaceAnalyzer());
     AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_CHANGED), new WhitespaceAnalyzer());
 }
Exemplo n.º 9
0
        public virtual string GetExpression()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_VIRTUAL_PATH) + ":(");
            foreach (string virtualPathNode in this.VirtualPathNodes)
            {
                stringBuilder.Append(LuceneQueryHelper.Escape(virtualPathNode.Replace(" ", "")));
                stringBuilder.Append("|");
            }
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            stringBuilder.Append("*)");
            return(stringBuilder.ToString());
        }
 public override void ReindexSite(List <SearchDocument> documents, Guid siteRootId)
 {
     lock (_writeLock)
     {
         BooleanQuery.MaxClauseCount = int.MaxValue;
         var fieldName     = ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_VIRTUAL_PATH);
         var siteRoot      = "*" + LuceneQueryHelper.Escape(siteRootId.ToString().ToLower().Replace(" ", "")) + "*";
         var siteRootQuery = $"{fieldName}:{siteRoot}";
         var queryParser   = new QueryParser(LuceneConfiguration.LuceneVersion, fieldName, LuceneConfiguration.Analyzer);
         queryParser.AllowLeadingWildcard = true;
         var deleteQuery = queryParser.Parse(siteRootQuery);
         _indexWriter.DeleteDocuments(deleteQuery);
         foreach (var document in documents)
         {
             _indexWriter.AddDocument(document.Document);
         }
     }
 }
Exemplo n.º 11
0
        public virtual void ReindexSite(List <SearchDocument> documents, Guid siteRootId)
        {
            if (!LuceneContext.AllowIndexing)
            {
                return;
            }
            var deletedList = new List <SearchDocument>();

            try
            {
                lock (_writeLock)
                {
                    BooleanQuery.MaxClauseCount = int.MaxValue;
                    var fieldName     = ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_VIRTUAL_PATH);
                    var siteRoot      = "*" + LuceneQueryHelper.Escape(siteRootId.ToString().ToLower().Replace(" ", "")) + "*";
                    var siteRootQuery = $"{fieldName}:{siteRoot}";
                    var queryParser   = new QueryParser(LuceneConfiguration.LuceneVersion, fieldName, LuceneConfiguration.Analyzer);
                    queryParser.AllowLeadingWildcard = true;
                    var deleteQuery = queryParser.Parse(siteRootQuery);
                    using (IndexWriter indexWriter = new IndexWriter(LuceneContext.Directory, LuceneConfiguration.Analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        indexWriter.SetMergeScheduler(new SerialMergeScheduler());
                        indexWriter.DeleteDocuments(deleteQuery);
                        foreach (var document in documents)
                        {
                            indexWriter.AddDocument(document.Document);
                        }
                        indexWriter.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Lucene Search Error", ex);
            }
            finally
            {
                if (IndexWriter.IsLocked(LuceneContext.Directory))
                {
                    IndexWriter.Unlock(LuceneContext.Directory);
                }
            }
        }
Exemplo n.º 12
0
        public string GetExpression()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(ContentIndexHelpers.GetIndexFieldName(FieldName));
            stringBuilder.Append(":(");
            stringBuilder.Append(LuceneQueryHelper.EscapeParenthesis(Value));
            if (UseWildCard)
            {
                stringBuilder.Append("*");
            }
            if (Boost.HasValue)
            {
                stringBuilder.Append($"^{Boost}");
            }
            stringBuilder.Append(")");

            return(stringBuilder.ToString());
        }
Exemplo n.º 13
0
        public T ParseFromDocument(Document document)
        {
            var instance          = Activator.CreateInstance <T>();
            var indexedProperties = typeof(T).GetProperties()
                                    .Where(x => x.GetCustomAttributes(typeof(IndexFieldNameAttribute), true).Any())
                                    .ToList();

            foreach (var property in indexedProperties)
            {
                var indexFieldNameAttrs = (IndexFieldNameAttribute[])property.GetCustomAttributes(typeof(IndexFieldNameAttribute), true);
                var indexFieldName      = ContentIndexHelpers.GetIndexFieldName(indexFieldNameAttrs.FirstOrDefault().IndexFieldName);
                var documentField       = document.GetField(indexFieldName);
                if (documentField != null)
                {
                    var           fieldValue    = documentField.StringValue;
                    TypeConverter typeConverter = TypeDescriptor.GetConverter(property.PropertyType);
                    object        propValue     = typeConverter.ConvertFromString(fieldValue);
                    property.SetValue(instance, propValue, null);
                }
            }
            return(instance);
        }
        public override void UpdateBatchIndex(List <SearchDocument> documents)
        {
            var deletedList = new List <SearchDocument>();
            var itemIds     = documents.Select(x => x.Id).ToList();

            lock (_writeLock)
            {
                BooleanQuery.MaxClauseCount = int.MaxValue;
                var deleteQueries = new List <Query>();
                foreach (var deletedDoc in itemIds)
                {
                    var deleteQuery = new QueryParser(LuceneConfiguration.LuceneVersion, ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID), LuceneConfiguration.Analyzer)
                                      .Parse(deletedDoc);
                    deleteQueries.Add(deleteQuery);
                }
                _indexWriter.DeleteDocuments(deleteQueries.ToArray());
                foreach (var document in documents)
                {
                    _indexWriter.AddDocument(document.Document);
                }
            }
        }
Exemplo n.º 15
0
 public virtual void DeleteFromIndex(List <string> itemIds)
 {
     if (!LuceneContext.AllowIndexing)
     {
         return;
     }
     try
     {
         lock (_writeLock)
         {
             BooleanQuery.MaxClauseCount = int.MaxValue;
             var deleteQueries = new List <Query>();
             foreach (var deletedDoc in itemIds)
             {
                 var deleteQuery = new QueryParser(LuceneConfiguration.LuceneVersion, ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID), LuceneConfiguration.Analyzer)
                                   .Parse(deletedDoc);
                 deleteQueries.Add(deleteQuery);
             }
             using (IndexWriter indexWriter = new IndexWriter(LuceneContext.Directory, LuceneConfiguration.Analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED))
             {
                 indexWriter.SetMergeScheduler(new SerialMergeScheduler());
                 indexWriter.DeleteDocuments(deleteQueries.ToArray());
                 indexWriter.Commit();
             }
         }
     }
     catch (Exception ex)
     {
         _logger.Error("Lucene Search Error", ex);
     }
 }
        private static void Initialize()
        {
            if (_initialized)
            {
                return;
            }
            lock (_lock)
            {
                if (_initialized)
                {
                    return;
                }
                try
                {
                    LuceneSection section = (LuceneSection)ConfigurationManager.GetSection("lucene.indexing");
                    _indexAllTypes = section.IndexAllTypes;
                    _isActive      = section.Active;
                    if (_isActive == null || !_isActive.Value)
                    {
                        _initialized = true;
                        return;
                    }
                    _fieldPrefix   = section.Prefix;
                    _luceneVersion = section.LuceneVersion;
                    _includedTypes = new Dictionary <string, ContentTypeDocument>();
                    string[] strArray             = new string[0];
                    var      fieldAnalyzerWrapper = new PerFieldAnalyzerWrapper((Analyzer) new StandardAnalyzer(LuceneVersion, StopFilter.MakeStopSet(strArray)));
                    if (!IndexAllTypes)
                    {
                        foreach (IncludedTypeElement typeSetting in section.IncludedTypes)
                        {
                            Type contentType = Type.GetType(typeSetting.Type, true, true);
                            if (contentType == null)
                            {
                                continue;
                            }
                            if (!_includedTypes.TryGetValue(typeSetting.Name, out var tmp))
                            {
                                var documentIndexModel = new ContentTypeDocument();
                                documentIndexModel.ContentType    = contentType;
                                documentIndexModel.IndexAllFields = typeSetting.IndexAllFields;
                                _includedTypes.Add(typeSetting.Name, documentIndexModel);
                                foreach (IncludedFieldElement fieldSetting in typeSetting.IncludedFields)
                                {
                                    Type fieldType;
                                    if (string.IsNullOrEmpty(fieldSetting.Type))
                                    {
                                        fieldType = typeof(DefaultComputedField);
                                    }
                                    else
                                    {
                                        fieldType = Type.GetType(fieldSetting.Type, true, true);
                                    }
                                    if (!typeof(IComputedField).IsAssignableFrom(fieldType))
                                    {
                                        continue;
                                    }
                                    var  instance     = (IComputedField)Activator.CreateInstance(fieldType);
                                    Type analyzerType = Type.GetType(fieldSetting.Analyzer, true, true);
                                    if (!typeof(Analyzer).IsAssignableFrom(analyzerType))
                                    {
                                        continue;
                                    }
                                    if (analyzerType == typeof(StandardAnalyzer))
                                    {
                                        instance.Analyzer = new StandardAnalyzer(LuceneVersion, StopFilter.MakeStopSet(strArray));
                                    }
                                    else
                                    {
                                        instance.Analyzer = (Analyzer)Activator.CreateInstance(analyzerType);
                                    }
                                    instance.Index    = fieldSetting.Index;
                                    instance.Store    = fieldSetting.Store;
                                    instance.Vector   = fieldSetting.Vector;
                                    instance.DataType = fieldSetting.DataType;
                                    if (!documentIndexModel.IndexedFields.TryGetValue(fieldSetting.Name, out var tmp2))
                                    {
                                        documentIndexModel.IndexedFields.Add(fieldSetting.Name, instance);
                                        fieldAnalyzerWrapper.AddAnalyzer(ContentIndexHelpers.GetIndexFieldName(fieldSetting.Name), instance.Analyzer);
                                    }
                                }
                            }
                        }
                    }
                    _analyzer = fieldAnalyzerWrapper;
                    AddDefaultFieldAnalyzer();
                    var shardingStrategy = section.Sharding?.Strategy;
                    if (!string.IsNullOrEmpty(shardingStrategy))
                    {
                        var shardingType = Type.GetType(shardingStrategy, true, true);
                        LuceneContext.IndexShardingStrategy = (IIndexShardingStrategy)Activator.CreateInstance(shardingType);
                        LuceneContext.IndexShardingStrategy.WarmupShards();
                        return;
                    }
                    Directory directory;
                    var       directoryConnectionString = ConfigurationManager.AppSettings["lucene:BlobConnectionString"] ?? "App_Data/My_Index";
                    var       directoryContainerName    = ConfigurationManager.AppSettings["lucene:ContainerName"] ?? "lucene";
                    var       directoryType             = (ConfigurationManager.AppSettings["lucene:DirectoryType"] ?? "Filesystem").ToLower();
                    switch (directoryType)
                    {
                    case Constants.ContainerType.Azure:
                        var connectionString = directoryConnectionString;
                        var containerName    = directoryContainerName;
                        var storageAccount   = CloudStorageAccount.Parse(connectionString);
                        var azureDir         = new FastAzureDirectory(storageAccount, containerName, new RAMDirectory());
                        directory = azureDir;
                        break;

                    default:
                        var folderPath  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directoryConnectionString);
                        var fsDirectory = FSDirectory.Open(folderPath);
                        directory = fsDirectory;
                        break;
                    }
                    _directory = directory;
                    InitDirectory(_directory);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                _initialized = true;
            }
        }
Exemplo n.º 17
0
        public virtual SearchResults <T> GetSearchResults <T>(IQueryExpression expression, int pageIndex, int pageSize, SortOptions sortOption = null, Directory directory = null)
            where T : DocumentIndexModel
        {
            if (directory == null)
            {
                directory = LuceneContext.Directory;
            }
            var result = new SearchResults <T>();

            using (IndexSearcher indexSearcher = new IndexSearcher(directory, true))
            {
                Sort luceneSortOption = new Sort();
                if (sortOption != null)
                {
                    luceneSortOption = new Sort(sortOption.Fields.Select(x =>
                                                                         new Lucene.Net.Search.SortField(ContentIndexHelpers.GetIndexFieldName(x.FieldName), x.FieldType, x.Reverse)).ToArray());
                }
                var queryParser = new MultiFieldQueryParser(LuceneConfiguration.LuceneVersion, expression.GetFieldName()
                                                            , LuceneConfiguration.Analyzer);
                queryParser.AllowLeadingWildcard = true;
                var     query   = queryParser.Parse(expression.GetExpression());
                TopDocs topDocs = indexSearcher.Search(query, null, int.MaxValue, luceneSortOption);
                result.TotalHits = topDocs.TotalHits;
                ScoreDoc[] scoreDocs      = topDocs.ScoreDocs;
                var        documentParser = new DocumentParser <T>();
                var        startIndex     = (pageIndex - 1);
                var        endIndex       = (pageIndex - 1) + pageSize;
                var        count          = scoreDocs.Count();
                for (int index = startIndex; index < endIndex; index++)
                {
                    if (index >= 0 && index < count)
                    {
                        var document = indexSearcher.Doc(scoreDocs[index].Doc);
                        result.Results.Add(documentParser.ParseFromDocument(document));
                    }
                }
            }
            return(result);
        }
 public string[] GetFieldName()
 {
     return(new string[] { ContentIndexHelpers.GetIndexFieldName(Field) });
 }
Exemplo n.º 19
0
 public string[] GetFieldName()
 {
     return(new string[] { ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_VIRTUAL_PATH) });
 }
Exemplo n.º 20
0
 public AccessControlListQuery(LuceneOperator innerOperator)
     : base(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ACL), innerOperator)
 {
 }
Exemplo n.º 21
0
        public virtual void IncludeDefaultField(Document document, IContent content)
        {
            var versionable      = (content as IVersionable);
            var changeTrackable  = content as IChangeTrackable;
            var contentStatus    = ((int)versionable.Status).ToString();
            var virtualPath      = ContentIndexHelpers.GetContentVirtualpath(content);
            var acl              = ContentIndexHelpers.GetContentACL(content);
            var contentType      = ContentIndexHelpers.GetContentType(content);
            var language         = (content as ILocalizable).Language.Name;
            var startPublishDate = versionable.StartPublish.HasValue ? DateTools.DateToString(versionable.StartPublish.Value.ToUniversalTime(), DateTools.Resolution.SECOND) : string.Empty;
            var stopPublishDate  = versionable.StopPublish.HasValue ? DateTools.DateToString(versionable.StopPublish.Value.ToUniversalTime(), DateTools.Resolution.SECOND) : string.Empty;
            var expired          = "";

            if ((versionable.StopPublish.HasValue && versionable.StopPublish.Value.ToUniversalTime() < DateTime.UtcNow) ||
                (versionable.StartPublish.HasValue && versionable.StartPublish.Value.ToUniversalTime() > DateTime.UtcNow))
            {
                expired = "true";
            }
            var createdDate = DateTools.DateToString(changeTrackable.Created.ToUniversalTime(), DateTools.Resolution.SECOND);
            var updatedDate = DateTools.DateToString(changeTrackable.Changed.ToUniversalTime(), DateTools.Resolution.SECOND);
            var idField     = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID),
                                        SearchDocument.FormatDocumentId(content), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var nameField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_NAME),
                                      content.Name, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var statusField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_STATUS),
                                        contentStatus, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var virtualPathField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_VIRTUAL_PATH),
                                             virtualPath, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var aclField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ACL),
                                     acl, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var typeField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_TYPE),
                                      contentType, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var langField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_LANGUAGE),
                                      language, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var startPublishField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_START_PUBLISHDATE),
                                              startPublishDate, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var stopPublishField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_STOP_PUBLISHDATE),
                                             stopPublishDate, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var referenceField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_REFERENCE),
                                           content.ContentLink.ID.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var createdField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_CREATED),
                                         createdDate, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var updatedField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_CHANGED),
                                         updatedDate, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);
            var expiredField = new Field(ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_EXPIRED),
                                         expired, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES);

            document.Add(idField);
            document.Add(nameField);
            document.Add(statusField);
            document.Add(virtualPathField);
            document.Add(aclField);
            document.Add(typeField);
            document.Add(langField);
            document.Add(startPublishField);
            document.Add(stopPublishField);
            document.Add(referenceField);
            document.Add(createdField);
            document.Add(updatedField);
            document.Add(expiredField);
        }
Exemplo n.º 22
0
        public virtual SearchDocument GetDocFromContent(IContent content)
        {
            try
            {
                var document = new Document();
                IncludeDefaultField(document, content);
                var possibleIndexModels = LuceneConfiguration.IncludedTypes.Where(x => x.Value.ContentType.IsAssignableFrom(content.GetOriginalType())).Select(x => x.Value).ToList();
                foreach (var documentIndexModel in possibleIndexModels)
                {
                    IncludeContentField(document, content, documentIndexModel);
                    var computedFieldList = documentIndexModel.IndexedFields;
                    foreach (var field in computedFieldList)
                    {
                        var fieldInstance = field.Value;
                        var value         = fieldInstance.GetValue(content, field.Key);
                        if (value != null)
                        {
                            AbstractField luceneField;
                            var           indexFieldName   = ContentIndexHelpers.GetIndexFieldName(field.Key);
                            var           existedFieldName = document.GetField(indexFieldName);
                            if (existedFieldName != null)
                            {
                                document.RemoveField(indexFieldName);
                            }
                            if (fieldInstance.DataType == LuceneFieldType.Multilist)
                            {
                                var listValue = (List <string>)value;
                                foreach (var item in listValue)
                                {
                                    document.Add(new Field(indexFieldName, item, fieldInstance.Store, fieldInstance.Index, fieldInstance.Vector));
                                }
                                continue;
                            }
                            switch (fieldInstance.DataType)
                            {
                            case LuceneFieldType.Datetime:
                                DateTime d1 = Convert.ToDateTime(value);
                                luceneField = new Field(indexFieldName, DateTools.DateToString(d1.ToUniversalTime(), DateTools.Resolution.SECOND), fieldInstance.Store, fieldInstance.Index, fieldInstance.Vector);
                                break;

                            case LuceneFieldType.Numeric:
                                luceneField = new NumericField(indexFieldName, fieldInstance.Store, true).SetLongValue(string.IsNullOrEmpty(value + "") ? 0 : long.Parse(value + ""));
                                break;

                            default:
                                luceneField = new Field(indexFieldName, value.ToString(), fieldInstance.Store, fieldInstance.Index, fieldInstance.Vector);
                                break;
                            }
                            document.Add(luceneField);
                        }
                    }
                }
                var result = new SearchDocument()
                {
                    Id       = SearchDocument.FormatDocumentId(content),
                    Document = document
                };
                return(result);
            }
            catch (Exception ex)
            {
                _logger.Error("Fetch Document error", ex);
                return(null);
            }
        }
 public override void DeleteFromIndex(List <string> itemIds)
 {
     lock (_writeLock)
     {
         BooleanQuery.MaxClauseCount = int.MaxValue;
         var deleteQueries = new List <Query>();
         foreach (var deletedDoc in itemIds)
         {
             var deleteQuery = new QueryParser(LuceneConfiguration.LuceneVersion, ContentIndexHelpers.GetIndexFieldName(Constants.INDEX_FIELD_NAME_ID), LuceneConfiguration.Analyzer)
                               .Parse(deletedDoc);
             deleteQueries.Add(deleteQuery);
         }
         _indexWriter.DeleteDocuments(deleteQueries.ToArray());
     }
 }