Ejemplo n.º 1
0
        /// <summary>
        /// Converts a <see cref="Document" /> object to a <see cref="LuceneDocument" /> object.
        /// </summary>
        /// <param name="document">The Document object</param>
        /// <param name="schema">The schema.</param>
        /// <param name="facetBuilder">The Lucene facet builder.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException">Cannot index a Document that does not have an _id.</exception>
        /// <exception cref="SchemaException">The fieldName '{fieldName}'</exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.InvalidOperationException">Cannot index a Document that does not have an _id.</exception>
        public static LuceneDocument ToLuceneDocument(this Document document, Schema schema = null, LuceneFacetBuilder facetBuilder = null)
        {
            if (document == null)
                throw new ArgumentNullException(nameof(document));
            if (schema == null)
                schema = Schema.CreateDefault();

            var documentDictionary = document.AsDictionary();
            if (!documentDictionary.ContainsKey(Schema.StandardField.ID))
                throw new InvalidOperationException("Cannot index a Document that does not have an _id.");

            var luceneDocument = new LuceneDocument();

            // Make sure the _id field is the first field added to the Lucene document
            var keys = documentDictionary.Keys.Except(new[] { Schema.StandardField.ID }).ToList();
            keys.Insert(0, Schema.StandardField.ID);

            foreach (var fieldName in keys)
            {
                // Validate fieldName - must not contain space or Lucene QueryParser illegal characters.
                if (_queryParserIllegalCharsRegex.IsMatch(fieldName))
                    throw new SchemaException($"The fieldName '{fieldName}' contains illegal characters.");

                Schema.Field schemaField = null;
                if (!schema.Fields.TryGetValue(fieldName, out schemaField))
                {
                    schemaField = new Schema.Field
                    {
                        Name = fieldName
                    };
                    schema.Fields.TryAdd(fieldName, schemaField);
                }

                var fieldValue = documentDictionary[fieldName];
                var luceneFields = fieldValue.ToLuceneFields(schemaField);
                foreach (var luceneField in luceneFields)
                    luceneDocument.Add(luceneField);
            }

            // The full-text field is always auto-generated and added to the Lucene document.
            var fullText = document.ToLuceneFullTextString();
            luceneDocument.Add(new TextField(Schema.StandardField.FULL_TEXT, fullText, FieldStore.NO));

            // Check if the document has the special _categories field,
            // which means that we need to create facets for it.
            if (document.HasCategories() && facetBuilder != null)
                luceneDocument = facetBuilder.RebuildDocumentWithFacets(luceneDocument, document, schema);

            return luceneDocument;
        }