예제 #1
0
        private static Analyzer GetAnalyzer(string name, IndexField field, bool forQuerying)
        {
            if (string.IsNullOrWhiteSpace(field.Analyzer))
            {
                return(null);
            }

            // TODO [ppekrol] can we use one instance like with KeywordAnalyzer and StandardAnalyzer?
            var analyzerInstance = IndexingExtensions.CreateAnalyzerInstance(name, field.Analyzer);

            if (forQuerying)
            {
                var analyzerType = analyzerInstance.GetType();

                var notForQuerying = NotForQuerying
                                     .GetOrAdd(analyzerType, t => analyzerInstance.GetType().GetTypeInfo().GetCustomAttributes <NotForQueryingAttribute>(false).Any());

                if (notForQuerying)
                {
                    return(null);
                }
            }

            return(analyzerInstance);
        }
예제 #2
0
        private static Analyzer GetAnalyzer(string name, string analyzer, Dictionary <Type, Analyzer> analyzers, bool forQuerying)
        {
            if (string.IsNullOrWhiteSpace(analyzer))
            {
                return(null);
            }

            var analyzerType = IndexingExtensions.GetAnalyzerType(name, analyzer);

            if (forQuerying)
            {
                var notForQuerying = NotForQuerying
                                     .GetOrAdd(analyzerType, t => t.GetCustomAttributes <NotForQueryingAttribute>(false).Any());

                if (notForQuerying)
                {
                    return(null);
                }
            }

            if (analyzers.TryGetValue(analyzerType, out var analyzerInstance) == false)
            {
                analyzers[analyzerType] = analyzerInstance = IndexingExtensions.CreateAnalyzerInstance(name, analyzerType);
            }

            return(analyzerInstance);
        }
예제 #3
0
        private static Analyzer GetAnalyzer(string name, string analyzer, bool forQuerying)
        {
            if (string.IsNullOrWhiteSpace(analyzer))
            {
                return(null);
            }

            var analyzerInstance = IndexingExtensions.CreateAnalyzerInstance(name, analyzer);

            if (forQuerying)
            {
                var analyzerType = analyzerInstance.GetType();

                var notForQuerying = NotForQuerying
                                     .GetOrAdd(analyzerType, t => analyzerInstance.GetType().GetTypeInfo().GetCustomAttributes <NotForQueryingAttribute>(false).Any());

                if (notForQuerying)
                {
                    return(null);
                }
            }

            return(analyzerInstance);
        }
예제 #4
0
        protected static RavenPerFieldAnalyzerWrapper CreateAnalyzer(Index index, IndexDefinitionBaseServerSide indexDefinition, bool forQuerying = false)
        {
            if (indexDefinition.IndexFields.ContainsKey(Constants.Documents.Indexing.Fields.AllFields))
            {
                throw new InvalidOperationException($"Detected '{Constants.Documents.Indexing.Fields.AllFields}'. This field should not be present here, because inheritance is done elsewhere.");
            }

            var analyzers = new Dictionary <Type, Analyzer>();

            var      hasDefaultFieldOptions = false;
            Analyzer defaultAnalyzerToUse   = null;
            Analyzer defaultAnalyzer        = null;

            if (indexDefinition is MapIndexDefinition mid)
            {
                if (mid.IndexDefinition.Fields.TryGetValue(Constants.Documents.Indexing.Fields.AllFields, out var value))
                {
                    hasDefaultFieldOptions = true;

                    switch (value.Indexing)
                    {
                    case FieldIndexing.Exact:
                        defaultAnalyzerToUse = GetOrCreateAnalyzer(Constants.Documents.Indexing.Fields.AllFields, index.Configuration.DefaultExactAnalyzerType.Value.Type, CreateKeywordAnalyzer);
                        break;

                    case FieldIndexing.Search:
                        if (value.Analyzer != null)
                        {
                            defaultAnalyzerToUse = GetAnalyzer(Constants.Documents.Indexing.Fields.AllFields, value.Analyzer, analyzers, forQuerying, index.DocumentDatabase.Name);
                        }

                        if (defaultAnalyzerToUse == null)
                        {
                            defaultAnalyzerToUse = GetOrCreateAnalyzer(Constants.Documents.Indexing.Fields.AllFields, index.Configuration.DefaultSearchAnalyzerType.Value.Type, CreateStandardAnalyzer);
                        }
                        break;

                    default:
                        // explicitly ignore all other values
                        break;
                    }
                }
            }

            if (defaultAnalyzerToUse == null)
            {
                defaultAnalyzerToUse = defaultAnalyzer = CreateDefaultAnalyzer(Constants.Documents.Indexing.Fields.AllFields, index.Configuration.DefaultAnalyzerType.Value.Type);
                analyzers.Add(defaultAnalyzerToUse.GetType(), defaultAnalyzerToUse);
            }

            var perFieldAnalyzerWrapper = forQuerying == false && indexDefinition.HasDynamicFields
                ? new RavenPerFieldAnalyzerWrapper(
                defaultAnalyzerToUse,
                fieldName => GetOrCreateAnalyzer(fieldName, index.Configuration.DefaultSearchAnalyzerType.Value.Type, CreateStandardAnalyzer),
                fieldName => GetOrCreateAnalyzer(fieldName, index.Configuration.DefaultExactAnalyzerType.Value.Type, CreateKeywordAnalyzer))
                : new RavenPerFieldAnalyzerWrapper(defaultAnalyzerToUse);

            foreach (var field in indexDefinition.IndexFields)
            {
                var fieldName = field.Value.Name;

                switch (field.Value.Indexing)
                {
                case FieldIndexing.Exact:
                    var keywordAnalyzer = GetOrCreateAnalyzer(fieldName, index.Configuration.DefaultExactAnalyzerType.Value.Type, CreateKeywordAnalyzer);

                    perFieldAnalyzerWrapper.AddAnalyzer(fieldName, keywordAnalyzer);
                    break;

                case FieldIndexing.Search:
                    var analyzer = GetAnalyzer(fieldName, field.Value.Analyzer, analyzers, forQuerying, index.DocumentDatabase.Name);
                    if (analyzer != null)
                    {
                        perFieldAnalyzerWrapper.AddAnalyzer(fieldName, analyzer);
                        continue;
                    }
                    AddStandardAnalyzer(fieldName);
                    break;

                case FieldIndexing.Default:
                    if (hasDefaultFieldOptions)
                    {
                        // if we have default field options then we need to take into account overrides for regular fields

                        if (defaultAnalyzer == null)
                        {
                            defaultAnalyzer = CreateDefaultAnalyzer(fieldName, index.Configuration.DefaultAnalyzerType.Value.Type);
                        }

                        perFieldAnalyzerWrapper.AddAnalyzer(fieldName, defaultAnalyzer);
                        continue;
                    }
                    break;
                }
            }

            return(perFieldAnalyzerWrapper);

            void AddStandardAnalyzer(string fieldName)
            {
                var standardAnalyzer = GetOrCreateAnalyzer(fieldName, index.Configuration.DefaultSearchAnalyzerType.Value.Type, CreateStandardAnalyzer);

                perFieldAnalyzerWrapper.AddAnalyzer(fieldName, standardAnalyzer);
            }

            Analyzer GetOrCreateAnalyzer(string fieldName, Type analyzerType, Func <string, Type, Analyzer> createAnalyzer)
            {
                if (analyzers.TryGetValue(analyzerType, out var analyzer) == false)
                {
                    analyzers[analyzerType] = analyzer = createAnalyzer(fieldName, analyzerType);
                }

                return(analyzer);
            }

            Analyzer CreateDefaultAnalyzer(string fieldName, Type analyzerType)
            {
                if (analyzerType == typeof(LowerCaseKeywordAnalyzer))
                {
                    return(new LowerCaseKeywordAnalyzer());
                }

                return(IndexingExtensions.CreateAnalyzerInstance(fieldName, analyzerType));
            }

            Analyzer CreateKeywordAnalyzer(string fieldName, Type analyzerType)
            {
                if (analyzerType == typeof(KeywordAnalyzer))
                {
                    return(new KeywordAnalyzer());
                }

                return(IndexingExtensions.CreateAnalyzerInstance(fieldName, analyzerType));
            }

            Analyzer CreateStandardAnalyzer(string fieldName, Type analyzerType)
            {
                if (analyzerType == typeof(RavenStandardAnalyzer))
                {
                    return(new RavenStandardAnalyzer(Version.LUCENE_29));
                }

                return(IndexingExtensions.CreateAnalyzerInstance(fieldName, analyzerType));
            }
        }