Ejemplo n.º 1
0
        /// <summary>
        /// Determines whether the Document object contains the special _categories field, which is used for faceted search.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <returns>
        ///   <c>true</c> if the specified Document object contains the special _categories field.; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasCategories(this Document document)
        {
            if (document == null)
                throw new ArgumentNullException(nameof(document));

            return document.AsDictionary().ContainsKey(Schema.StandardField.CATEGORIES);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Copies the Document object to a new Dictionary object.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <returns></returns>        
        public static IDictionary<string, object> ToDictionary(this Document document)
        {
            if (document == null)
                throw new ArgumentNullException(nameof(document));

            var dictionary = new Dictionary<string, object>(document.AsDictionary());
            return dictionary;
        }
Ejemplo n.º 3
0
		public static TimeSpan? GetTimeSpan( this MessagePackObject source, MessagePackObject key )
		{
			if ( source.IsDictionary )
			{
				MessagePackObject value;
				if ( source.AsDictionary().TryGetValue( key, out value ) && value.IsTypeOf<Int64>().GetValueOrDefault() )
				{
					return new TimeSpan( value.AsInt64() );
				}
			}

			return null;
		}
Ejemplo n.º 4
0
		public static string GetString( this MessagePackObject source, MessagePackObject key )
		{
			if ( source.IsDictionary )
			{
				MessagePackObject value;
				if ( source.AsDictionary().TryGetValue( key, out value ) && value.IsTypeOf<string>().GetValueOrDefault() )
				{
					return value.AsString();
				}
			}

			return null;
		}
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
0
        public static Content Select(this Content content, IList<string> selectedFields)
        {
            if (content == null)
                throw new ArgumentNullException("content");
            if (selectedFields == null)
                throw new ArgumentNullException("selectedFields");

            if (selectedFields.Count == 0)
                return content;

            var contentDictionary = content.AsDictionary();

            // Remove fields that are not in the selectedFields
            contentDictionary.Keys.Except(new[] { Content.ID_FIELD_NAME })
                             .Where(fieldName => !selectedFields.Contains(fieldName))
                             .ToList()
                             .ForEach(fieldName => contentDictionary.Remove(fieldName));

            // Content should now only contain the fields in the projectedFields list, plus the _id field (which is always included)
            return content;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Generates the Lucene full-text representation of the Document object.
        /// </summary>
        /// <param name="document">The Document object.</param>
        /// <returns></returns>        
        public static string ToLuceneFullTextString(this Document document)
        {
            if (document == null)
                throw new ArgumentNullException(nameof(document));

            var buffer = new StringBuilder();

            var dictionary = document.AsDictionary();
            var keys = dictionary.Keys.Except(new[] { Schema.StandardField.ID, Schema.StandardField.CREATED_TIMESTAMP, Schema.StandardField.MODIFIED_TIMESTAMP });

            foreach (var fieldName in keys)
            {
                var fieldValue = dictionary[fieldName];
                if (fieldValue == null)
                    continue;

                buffer.Append(fieldValue.ToLuceneFullTextString());
            }

            return buffer.ToString();
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Converts all date values inside the Document object to UTC format.
 /// </summary>
 /// <param name="document">The document to process.</param>
 public static void ConvertDatesToUtc(this Document document)
 {
     document.AsDictionary().ConvertDatesToUtc();
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Converts the Document object to a Schema object.
 /// </summary>
 /// <param name="document">The Document.</param>
 /// <returns></returns>
 public static Schema ToSchema(this Document document)
 {
     var schema = new Schema().PopulateWith(document.AsDictionary());
     return schema;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Selects only the specified list of fields.
        /// </summary>
        /// <param name="document">The Document object.</param>
        /// <param name="selectedFields">The list of fields to be selected.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        public static Document Select(this Document document, IList<string> selectedFields)
        {
            if (document == null)
                throw new ArgumentNullException(nameof(document));
            if (selectedFields == null)
                throw new ArgumentNullException(nameof(selectedFields));

            if (selectedFields.Count == 0)
                return document;

            selectedFields = selectedFields.Distinct().ToList();

            var documentDictionary = document.AsDictionary();

            // Remove fields that are not in the selectedFields
            var keysToRemove = documentDictionary.Keys
                                                .Where(fieldName => !selectedFields.Contains(fieldName))
                                                .ToList();

            keysToRemove.ForEach(fieldName => documentDictionary.Remove(fieldName));

            // Document should now only contain the fields in the selectedFields list
            return document;
        }