Example #1
0
        protected static RavenPerFieldAnalyzerWrapper CreateAnalyzer(Func <Analyzer> createDefaultAnalyzer, Dictionary <string, IndexField> fields, bool forQuerying = false)
        {
            if (fields.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 defaultAnalyzer = createDefaultAnalyzer();

            RavenStandardAnalyzer standardAnalyzer = null;
            KeywordAnalyzer       keywordAnalyzer  = null;
            var perFieldAnalyzerWrapper            = new RavenPerFieldAnalyzerWrapper(defaultAnalyzer);

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

                switch (field.Value.Indexing)
                {
                case FieldIndexing.Exact:
                    if (keywordAnalyzer == null)
                    {
                        keywordAnalyzer = new KeywordAnalyzer();
                    }

                    perFieldAnalyzerWrapper.AddAnalyzer(fieldName, keywordAnalyzer);
                    break;

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

                    break;
                }
            }

            return(perFieldAnalyzerWrapper);

            void AddStandardAnalyzer(string fieldName)
            {
                if (standardAnalyzer == null)
                {
                    standardAnalyzer = new RavenStandardAnalyzer(global::Lucene.Net.Util.Version.LUCENE_29);
                }

                perFieldAnalyzerWrapper.AddAnalyzer(fieldName, standardAnalyzer);
            }
        }
        protected static RavenPerFieldAnalyzerWrapper CreateAnalyzer(Func <Analyzer> createDefaultAnalyzer, IndexDefinitionBase 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      hasDefaultFieldOptions        = false;
            Analyzer defaultAnalyzerToUse          = null;
            RavenStandardAnalyzer standardAnalyzer = null;
            KeywordAnalyzer       keywordAnalyzer  = 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 = keywordAnalyzer = new KeywordAnalyzer();
                        break;

                    case FieldIndexing.Search:
                        if (value.Analyzer != null)
                        {
                            defaultAnalyzerToUse = GetAnalyzer(Constants.Documents.Indexing.Fields.AllFields, value.Analyzer, forQuerying);
                        }
                        if (defaultAnalyzerToUse == null)
                        {
                            defaultAnalyzerToUse = standardAnalyzer = new RavenStandardAnalyzer(Version.LUCENE_29);
                        }
                        break;

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

            if (defaultAnalyzerToUse == null)
            {
                defaultAnalyzerToUse = defaultAnalyzer = createDefaultAnalyzer();
            }

            var perFieldAnalyzerWrapper = new RavenPerFieldAnalyzerWrapper(defaultAnalyzerToUse);

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

                switch (field.Value.Indexing)
                {
                case FieldIndexing.Exact:
                    if (keywordAnalyzer == null)
                    {
                        keywordAnalyzer = new KeywordAnalyzer();
                    }

                    perFieldAnalyzerWrapper.AddAnalyzer(fieldName, keywordAnalyzer);
                    break;

                case FieldIndexing.Search:
                    var analyzer = GetAnalyzer(fieldName, field.Value.Analyzer, forQuerying);
                    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();
                        }

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

            return(perFieldAnalyzerWrapper);

            void AddStandardAnalyzer(string fieldName)
            {
                if (standardAnalyzer == null)
                {
                    standardAnalyzer = new RavenStandardAnalyzer(Version.LUCENE_29);
                }

                perFieldAnalyzerWrapper.AddAnalyzer(fieldName, standardAnalyzer);
            }
        }