Ejemplo n.º 1
0
        public virtual void IncludeContentField(Document document, IContent content, ContentTypeDocument indexModel)
        {
            if (!indexModel.IndexAllFields)
            {
                return;
            }
            var baseClass  = content.GetOriginalType();
            var properties = baseClass.GetProperties().Where(x => x.GetCustomAttributes(typeof(ExcludeFromIndexAttribute), true).Count() == 0 &&
                                                             x.GetIndexParameters().Length == 0).ToList();

            foreach (var property in properties)
            {
                var field = GetFieldFromProperty(property, content);
                if (field != null)
                {
                    if (document.GetField(field.Name) != null)
                    {
                        document.RemoveField(field.Name);
                    }
                    document.Add(field);
                }
            }
        }
        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;
            }
        }