private IEnumerable <AbstractField> CreateRegularFields(string name, object value, Field.Store defaultStorage, bool nestedArray = false, Field.TermVector defaultTermVector = Field.TermVector.NO, Field.Index?analyzed = null)
        {
            var fieldIndexingOptions = analyzed ?? indexDefinition.GetIndex(name, null);
            var storage    = indexDefinition.GetStorage(name, defaultStorage);
            var termVector = indexDefinition.GetTermVector(name, defaultTermVector);

            if (fieldIndexingOptions == Field.Index.NO && storage == Field.Store.NO && termVector == Field.TermVector.NO)
            {
                yield break;
            }

            if (fieldIndexingOptions == Field.Index.NO && storage == Field.Store.NO)
            {
                fieldIndexingOptions = Field.Index.ANALYZED; // we have some sort of term vector, forcing index to be analyzed, then.
            }

            if (value == null)
            {
                yield return(CreateFieldWithCaching(name, Constants.NullValue, storage,
                                                    Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));

                yield break;
            }

            CheckIfSortOptionsAndInputTypeMatch(name, value);

            var attachmentFoIndexing = value as AttachmentForIndexing;

            if (attachmentFoIndexing != null)
            {
                if (database == null)
                {
                    throw new InvalidOperationException(
                              "Cannot use attachment for indexing if the database parameter is null. This is probably a RavenDB bug");
                }

                var attachment = database.Attachments.GetStatic(attachmentFoIndexing.Key);
                if (attachment == null)
                {
                    yield break;
                }

                var fieldWithCaching = CreateFieldWithCaching(name, string.Empty, Field.Store.NO, fieldIndexingOptions, termVector);

                if (database.TransactionalStorage.IsAlreadyInBatch)
                {
                    var streamReader = new StreamReader(attachment.Data());
                    fieldWithCaching.SetValue(streamReader);
                }
                else
                {
                    // we are not in batch operation so we have to create it be able to read attachment's data
                    database.TransactionalStorage.Batch(accessor =>
                    {
                        var streamReader = new StreamReader(attachment.Data());
                        // we have to read it into memory because we after exiting the batch an attachment's data stream will be closed
                        fieldWithCaching.SetValue(streamReader.ReadToEnd());
                    });
                }

                yield return(fieldWithCaching);

                yield break;
            }
            if (Equals(value, string.Empty))
            {
                yield return(CreateFieldWithCaching(name, Constants.EmptyString, storage,
                                                    Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));

                yield break;
            }
            var dynamicNullObject = value as DynamicNullObject;

            if (ReferenceEquals(dynamicNullObject, null) == false)
            {
                if (dynamicNullObject.IsExplicitNull)
                {
                    var sortOptions = indexDefinition.GetSortOption(name, query: null);
                    if (sortOptions == null ||
                        sortOptions.Value == SortOptions.String ||
                        sortOptions.Value == SortOptions.None ||
                        sortOptions.Value == SortOptions.StringVal ||
                        sortOptions.Value == SortOptions.Custom)
                    {
                        yield return(CreateFieldWithCaching(name, Constants.NullValue, storage,
                                                            Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
                    }

                    foreach (var field in CreateNumericFieldWithCaching(name, GetNullValueForSorting(sortOptions), storage, termVector))
                    {
                        yield return(field);
                    }
                }
                yield break;
            }
            var boostedValue = value as BoostedValue;

            if (boostedValue != null)
            {
                foreach (var field in CreateFields(name, boostedValue.Value, storage, false, termVector))
                {
                    field.Boost     = boostedValue.Boost;
                    field.OmitNorms = false;
                    yield return(field);
                }
                yield break;
            }


            var abstractField = value as AbstractField;

            if (abstractField != null)
            {
                yield return(abstractField);

                yield break;
            }
            var bytes = value as byte[];

            if (bytes != null)
            {
                yield return(CreateBinaryFieldWithCaching(name, bytes, storage, fieldIndexingOptions, termVector));

                yield break;
            }

            var itemsToIndex = value as IEnumerable;

            if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex))
            {
                int count = 1;

                if (nestedArray == false)
                {
                    yield return(new Field(name + "_IsArray", "true", storage, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
                }

                foreach (var itemToIndex in itemsToIndex)
                {
                    if (!CanCreateFieldsForNestedArray(itemToIndex, fieldIndexingOptions))
                    {
                        continue;
                    }

                    multipleItemsSameFieldCount.Add(count++);
                    foreach (var field in CreateFields(name, itemToIndex, storage, nestedArray: true, defaultTermVector: defaultTermVector, analyzed: analyzed))
                    {
                        yield return(field);
                    }

                    multipleItemsSameFieldCount.RemoveAt(multipleItemsSameFieldCount.Count - 1);
                }

                yield break;
            }

            if (Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED) ||
                Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED_NO_NORMS))// explicitly not analyzed
            {
                // date time, time span and date time offset have the same structure fo analyzed and not analyzed.
                if (!(value is DateTime) && !(value is DateTimeOffset) && !(value is TimeSpan))
                {
                    yield return(CreateFieldWithCaching(name, value.ToString(), storage,
                                                        indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));

                    yield break;
                }
            }
            if (value is string)
            {
                var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED);
                yield return(CreateFieldWithCaching(name, value.ToString(), storage, index, termVector));

                yield break;
            }

            if (value is TimeSpan)
            {
                var val = (TimeSpan)value;
                yield return(CreateFieldWithCaching(name, val.ToString("c", CultureInfo.InvariantCulture), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is DateTime)
            {
                var val          = (DateTime)value;
                var dateAsString = val.GetDefaultRavenFormat();
                if (val.Kind == DateTimeKind.Utc)
                {
                    dateAsString += "Z";
                }
                yield return(CreateFieldWithCaching(name, dateAsString, storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is DateTimeOffset)
            {
                var val = (DateTimeOffset)value;

                string dtoStr;
                if (Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED) || Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED_NO_NORMS))
                {
                    dtoStr = val.ToString(Default.DateTimeOffsetFormatsToWrite, CultureInfo.InvariantCulture);
                }
                else
                {
                    dtoStr = val.UtcDateTime.GetDefaultRavenFormat(true);
                }
                yield return(CreateFieldWithCaching(name, dtoStr, storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is bool)
            {
                yield return(new Field(name, ((bool)value) ? "true" : "false", storage,
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is double)
            {
                var d = (double)value;
                yield return(CreateFieldWithCaching(name, d.ToString("r", CultureInfo.InvariantCulture), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is decimal)
            {
                var d = (decimal)value;
                var s = d.ToString(CultureInfo.InvariantCulture);
                if (s.Contains('.'))
                {
                    s = s.TrimEnd('0');
                    if (s.EndsWith("."))
                    {
                        s = s.Substring(0, s.Length - 1);
                    }
                }
                yield return(CreateFieldWithCaching(name, s, storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is Enum)
            {
                yield return(CreateFieldWithCaching(name, value.ToString(), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.ANALYZED_NO_NORMS), termVector));
            }
            else if (value is IConvertible) // we need this to store numbers in invariant format, so JSON could read them
            {
                var convert = ((IConvertible)value);
                yield return(CreateFieldWithCaching(name, convert.ToString(CultureInfo.InvariantCulture), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else if (value is IDynamicJsonObject)
            {
                var inner = ((IDynamicJsonObject)value).Inner;
                yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));

                yield return(CreateFieldWithCaching(name, inner.ToString(Formatting.None), storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }
            else
            {
                var jsonVal = RavenJToken.FromObject(value).ToString(Formatting.None);
                if (jsonVal.StartsWith("{") || jsonVal.StartsWith("["))
                {
                    yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
                }
                else if (jsonVal.StartsWith("\"") && jsonVal.EndsWith("\"") && jsonVal.Length > 1)
                {
                    jsonVal = jsonVal.Substring(1, jsonVal.Length - 2);
                }
                yield return(CreateFieldWithCaching(name, jsonVal, storage,
                                                    indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS), termVector));
            }


            foreach (var numericField in CreateNumericFieldWithCaching(name, value, storage, termVector))
            {
                yield return(numericField);
            }
        }
        private IEnumerable <AbstractField> CreateNumericFieldWithCaching(string name, object value,
                                                                          Field.Store defaultStorage, Field.TermVector termVector)
        {
            var          fieldName = name + "_Range";
            var          storage   = indexDefinition.GetStorage(name, defaultStorage);
            var          cacheKey  = new FieldCacheKey(name, null, storage, termVector, multipleItemsSameFieldCount.ToArray());
            NumericField numericField;

            if (numericFieldsCache.TryGetValue(cacheKey, out numericField) == false)
            {
                numericFieldsCache[cacheKey] = numericField = new NumericField(fieldName, storage, true);
            }

            if (value is TimeSpan)
            {
                yield return(numericField.SetLongValue(((TimeSpan)value).Ticks));
            }
            else if (value is int)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue((int)value));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Float)
                {
                    yield return(numericField.SetFloatValue((int)value));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Double)
                {
                    yield return(numericField.SetDoubleValue((int)value));
                }
                else
                {
                    yield return(numericField.SetIntValue((int)value));
                }
            }
            else if (value is long)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Double)
                {
                    yield return(numericField.SetDoubleValue((long)value));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Float)
                {
                    yield return(numericField.SetFloatValue((long)value));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Int)
                {
                    yield return(numericField.SetIntValue(Convert.ToInt32((long)value)));
                }
                else
                {
                    yield return(numericField.SetLongValue((long)value));
                }
            }
            else if (value is decimal)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Float)
                {
                    yield return(numericField.SetFloatValue(Convert.ToSingle((decimal)value)));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Int)
                {
                    yield return(numericField.SetIntValue(Convert.ToInt32((decimal)value)));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue(Convert.ToInt64((decimal)value)));
                }
                else
                {
                    yield return(numericField.SetDoubleValue((double)(decimal)value));
                }
            }
            else if (value is float)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Double)
                {
                    yield return(numericField.SetDoubleValue((float)value));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Int)
                {
                    yield return(numericField.SetIntValue(Convert.ToInt32((float)value)));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue(Convert.ToInt64((float)value)));
                }
                else
                {
                    yield return(numericField.SetFloatValue((float)value));
                }
            }
            else if (value is double)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Float)
                {
                    yield return(numericField.SetFloatValue(Convert.ToSingle((double)value)));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Int)
                {
                    yield return(numericField.SetIntValue(Convert.ToInt32((double)value)));
                }
                else if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue(Convert.ToInt64((double)value)));
                }
                else
                {
                    yield return(numericField.SetDoubleValue((double)value));
                }
            }
        }
 public IEnumerable <AbstractField> Index(object val, PropertyDescriptorCollection properties, Field.Store defaultStorage)
 {
     return(from property in properties.Cast <PropertyDescriptor>()
            where property.Name != Constants.DocumentIdFieldName
            from field in CreateFields(property.Name, property.GetValue(val), defaultStorage)
            select field);
 }
        protected Field GetOrCreateField(string name, string value, LazyStringValue lazyValue, BlittableJsonReaderObject blittableValue, Field.Store store, Field.Index index, Field.TermVector termVector)
        {
            int cacheKey = FieldCacheKey.GetHashCode(name, index, store, termVector, _multipleItemsSameFieldCount);

            Field field;

            if (_fieldsCache.TryGetValue(cacheKey, out CachedFieldItem <Field> cached) == false ||
                !cached.Key.IsSame(name, index, store, termVector, _multipleItemsSameFieldCount))
            {
                LazyStringReader      stringReader    = null;
                BlittableObjectReader blittableReader = null;

                if ((lazyValue != null || blittableValue != null) && store.IsStored() == false && index.IsIndexed() && index.IsAnalyzed())
                {
                    TextReader reader;
                    if (lazyValue != null)
                    {
                        stringReader = new LazyStringReader();
                        reader       = stringReader.GetTextReaderFor(lazyValue);
                    }
                    else
                    {
                        blittableReader = Scope.GetBlittableReader();
                        reader          = blittableReader.GetTextReaderFor(blittableValue);
                    }

                    field = new Field(name, reader, termVector);
                }
                else
                {
                    if (value == null && lazyValue == null)
                    {
                        blittableReader = Scope.GetBlittableReader();
                    }

                    field = new Field(name,
                                      value ?? LazyStringReader.GetStringFor(lazyValue) ?? blittableReader.GetStringFor(blittableValue),
                                      store, index, termVector);
                }

                field.Boost     = 1;
                field.OmitNorms = true;

                _fieldsCache[cacheKey] = new CachedFieldItem <Field>
                {
                    Key              = new FieldCacheKey(name, index, store, termVector, _multipleItemsSameFieldCount.ToArray()),
                    Field            = field,
                    LazyStringReader = stringReader
                };
            }
            else
            {
                BlittableObjectReader blittableReader = null;

                field = cached.Field;
                if (lazyValue != null && cached.LazyStringReader == null)
                {
                    cached.LazyStringReader = new LazyStringReader();
                }
                if (blittableValue != null)
                {
                    blittableReader = Scope.GetBlittableReader();
                }

                if ((lazyValue != null || blittableValue != null) && store.IsStored() == false && index.IsIndexed() && index.IsAnalyzed())
                {
                    field.SetValue(lazyValue != null
                        ? cached.LazyStringReader.GetTextReaderFor(lazyValue)
                        : blittableReader.GetTextReaderFor(blittableValue));
                }
                else
                {
                    field.SetValue(value ?? LazyStringReader.GetStringFor(lazyValue) ?? blittableReader.GetStringFor(blittableValue));
                }
            }

            return(field);
        }
        private IEnumerable <AbstractField> GetOrCreateNumericField(IndexField field, object value, Field.Store storage, Field.TermVector termVector = Field.TermVector.NO)
        {
            var fieldNameDouble = field.Name + Constants.Documents.Indexing.Fields.RangeFieldSuffixDouble;
            var fieldNameLong   = field.Name + Constants.Documents.Indexing.Fields.RangeFieldSuffixLong;

            var numericFieldDouble = GetNumericFieldFromCache(fieldNameDouble, null, storage, termVector);
            var numericFieldLong   = GetNumericFieldFromCache(fieldNameLong, null, storage, termVector);

            switch (BlittableNumber.Parse(value, out double doubleValue, out long longValue))
            {
Example #6
0
 private void AddParameterToDocument(string name, dynamic value, Field.Store store, Field.Index index)
 {
     document.Add(new Field(name, value.ToString(), store, index));
 }
        private IEnumerable <AbstractField> GetComplexObjectFields(string path, BlittableJsonReaderObject val, Field.Store storage, Field.Index indexing, Field.TermVector termVector)
        {
            if (_multipleItemsSameFieldCount.Count == 0 || _multipleItemsSameFieldCount[0] == 1)
            {
                yield return(GetOrCreateField(path + ConvertToJsonSuffix, TrueString, null, null, storage, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
            }

            yield return(GetOrCreateField(path, null, null, val, storage, indexing, termVector));
        }
        /// <summary>
        /// This method generate the fields for indexing documents in lucene from the values.
        /// Given a name and a value, it has the following behavior:
        /// * If the value is enumerable, index all the items in the enumerable under the same field name
        /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE'
        /// * If the value is string or was set to not analyzed, create a single field with the supplied name
        /// * If the value is date, create a single field with millisecond precision with the supplied name
        /// * If the value is numeric (int, long, double, decimal, or float) will create two fields:
        ///		1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries
        ///		2. with the name: name +'_Range', containing the numeric value in a form that allows range queries
        /// </summary>
        private static IEnumerable <AbstractField> CreateFields(string name, object value, IndexDefinition indexDefinition, Field.Store defaultStorage)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name");
            }

            if (char.IsLetter(name[0]) == false &&
                name[0] != '_')
            {
                name = "_" + name;
            }

            if (value == null)
            {
                yield return(new Field(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage),
                                       Field.Index.NOT_ANALYZED));

                yield break;
            }
            if (value is DynamicNullObject)
            {
                if (((DynamicNullObject)value).IsExplicitNull)
                {
                    yield return(new Field(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage),
                                           Field.Index.NOT_ANALYZED));
                }
                yield break;
            }

            if (value is AbstractField)
            {
                yield return((AbstractField)value);

                yield break;
            }


            var itemsToIndex = value as IEnumerable;

            if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex))
            {
                yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                foreach (var itemToIndex in itemsToIndex)
                {
                    foreach (var field in CreateFields(name, itemToIndex, indexDefinition, defaultStorage))
                    {
                        yield return(field);
                    }
                }
                yield break;
            }

            if (indexDefinition.GetIndex(name, null) == Field.Index.NOT_ANALYZED)// explicitly not analyzed
            {
                yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));

                yield break;
            }
            if (value is string)
            {
                var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED);
                yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       index));

                yield break;
            }

            if (value is DateTime)
            {
                yield return(new Field(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND),
                                       indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is bool)
            {
                yield return(new Field(name, ((bool)value) ? "true" : "false", indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is IConvertible)            // we need this to store numbers in invariant format, so JSON could read them
            {
                var convert = ((IConvertible)value);
                yield return(new Field(name, convert.ToString(CultureInfo.InvariantCulture), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else if (value is DynamicJsonObject)
            {
                var inner = ((DynamicJsonObject)value).Inner;
                yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(new Field(name, inner.ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }
            else
            {
                yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                yield return(new Field(name, JToken.FromObject(value).ToString(), indexDefinition.GetStorage(name, defaultStorage),
                                       indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED)));
            }


            var numericField = new NumericField(name + "_Range", indexDefinition.GetStorage(name, defaultStorage), true);

            if (value is int)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Long)
                {
                    yield return(numericField.SetLongValue((int)value));
                }
                else
                {
                    yield return(numericField.SetIntValue((int)value));
                }
            }
            if (value is long)
            {
                yield return(numericField
                             .SetLongValue((long)value));
            }
            if (value is decimal)
            {
                yield return(numericField
                             .SetDoubleValue((double)(decimal)value));
            }
            if (value is float)
            {
                if (indexDefinition.GetSortOption(name) == SortOptions.Double)
                {
                    yield return(numericField.SetDoubleValue((float)value));
                }
                else
                {
                    yield return(numericField.SetFloatValue((float)value));
                }
            }
            if (value is double)
            {
                yield return(numericField
                             .SetDoubleValue((double)value));
            }
        }
Example #9
0
        /// <summary>
        /// Adds the field.
        /// </summary>
        /// <param name="doc">The doc.</param>
        /// <param name="metaField">The meta field.</param>
        /// <param name="metaObject">The meta object.</param>
        protected virtual void AddField(Document doc, MetaField metaField, Hashtable metaObject)
        {
            if (metaField.AllowSearch)
            {
                Field.Store store = null;
                Field.Index index = null;
                if (metaField.Attributes["IndexStored"] != null)
                {
                    if (metaField.Attributes["IndexStored"].Equals("true", StringComparison.OrdinalIgnoreCase))
                    {
                        store = Field.Store.YES;
                    }
                    else
                    {
                        store = Field.Store.NO;
                    }
                }

                if (metaField.Attributes["IndexField"] != null)
                {
                    if (metaField.Attributes["IndexField"].Equals("tokenized", StringComparison.OrdinalIgnoreCase))
                    {
                        index = Field.Index.TOKENIZED;
                    }
                    else
                    {
                        index = Field.Index.UN_TOKENIZED;
                    }
                }

                object val = MetaHelper.GetMetaFieldValue(metaField, metaObject[metaField.Name]);
                //object val = metaObject[metaField];
                string valString = String.Empty;
                if (
                    metaField.DataType == MetaDataType.BigInt ||
                    metaField.DataType == MetaDataType.Decimal ||
                    metaField.DataType == MetaDataType.Float ||
                    metaField.DataType == MetaDataType.Int ||
                    metaField.DataType == MetaDataType.Money ||
                    metaField.DataType == MetaDataType.Numeric ||
                    metaField.DataType == MetaDataType.SmallInt ||
                    metaField.DataType == MetaDataType.SmallMoney ||
                    metaField.DataType == MetaDataType.TinyInt
                    )
                {
                    if (val != null)
                    {
                        valString = val.ToString();
                        if (!String.IsNullOrEmpty(valString))
                        {
                            if (metaField.DataType == MetaDataType.Decimal)
                            {
                                doc.Add(new Field(metaField.Name, ConvertStringToSortable(Decimal.Parse(valString)), store == null ? Field.Store.NO : store, index == null ? Field.Index.UN_TOKENIZED : index));
                            }
                            else
                            {
                                doc.Add(new Field(metaField.Name, ConvertStringToSortable(valString), store == null ? Field.Store.NO : store, index == null ? Field.Index.UN_TOKENIZED : index));
                            }
                        }
                    }
                }
                else if (val != null)
                {
                    if (metaField.DataType == MetaDataType.DictionaryMultiValue)
                    {
                        foreach (string s in (string[])val)
                        {
                            doc.Add(new Field(metaField.Name, s == null ? String.Empty : s.ToLower(), store == null ? Field.Store.NO : store, index == null ? Field.Index.TOKENIZED : index));
                            doc.Add(new Field("_content", s == null ? String.Empty : s.ToString().ToLower(), store == null ? Field.Store.NO : store, index == null ? Field.Index.TOKENIZED : index));
                        }
                    }
                    else if (metaField.DataType == MetaDataType.StringDictionary)
                    {
                        MetaStringDictionary stringDictionary = val as MetaStringDictionary;
                        ArrayList            strvals          = new ArrayList();
                        if (stringDictionary != null)
                        {
                            foreach (string key in stringDictionary.Keys)
                            {
                                string s = stringDictionary[key];
                                doc.Add(new Field(metaField.Name, s == null ? String.Empty : s.ToLower(), store == null ? Field.Store.NO : store, index == null ? Field.Index.TOKENIZED : index));
                                doc.Add(new Field("_content", s == null ? String.Empty : s.ToString().ToLower(), store == null ? Field.Store.NO : store, index == null ? Field.Index.TOKENIZED : index));
                            }
                        }
                    }
                    else
                    {
                        doc.Add(new Field(metaField.Name, val == null ? String.Empty : val.ToString().ToLower(), store == null ? Field.Store.NO : store, index == null ? Field.Index.TOKENIZED : index));
                        doc.Add(new Field("_content", val == null ? String.Empty : val.ToString().ToLower(), store == null ? Field.Store.NO : store, index == null ? Field.Index.TOKENIZED : index));
                    }
                }
            }
        }
 public static IEnumerable <AbstractField> Index(JObject document, IndexDefinition indexDefinition, Field.Store defaultStorage)
 {
     return(from property in document.Cast <JProperty>()
            let name = property.Name
                       where name != Constants.DocumentIdFieldName
                       let value = GetPropertyValue(property)
                                   from field in CreateFields(name, value, indexDefinition, defaultStorage)
                                   select field);
 }
 public static IEnumerable <AbstractField> Index(object val, PropertyDescriptorCollection properties, IndexDefinition indexDefinition, Field.Store defaultStorage)
 {
     return(from property in properties.Cast <PropertyDescriptor>()
            let name = property.Name
                       where name != Constants.DocumentIdFieldName
                       let value = property.GetValue(val)
                                   from field in CreateFields(name, value, indexDefinition, defaultStorage)
                                   select field);
 }
Example #12
0
        private void Indexer_DocumentWriting(BaseUmbracoIndexer indexer, DocumentWritingEventArgs e, string indexerName)
        {
            e.Fields.TryGetValue(Constants.PropertyAlias.NodeTypeAlias, out string documentAlias);
            var customizers = _IndexCustomizers
                              .Where(c => c.CanIndex(documentAlias ?? "", indexerName));

            foreach (var customizer in customizers)
            {
                var customizedProperties = customizer.CustomizeItems(e.Fields, indexer.DataService) ?? Enumerable.Empty <IContentIndexItem>();

                foreach (var customized in customizedProperties)
                {
                    if (string.IsNullOrWhiteSpace(customized.FieldName))
                    {
                        throw new NullReferenceException($"{customized.GetType().FullName} returned an empty value for {nameof(customized.FieldName)}!");
                    }

                    if (e.Fields.ContainsKey(customized.FieldName))
                    {
                        e.Document.RemoveField(customized.FieldName);
                    }

                    //"Examine.LuceneEngine.SearchCriteria.LuceneBooleanOperation" orderBy imple
                    //https://github.com/Shazwazza/Examine/blob/master/src/Examine/LuceneEngine/Providers/LuceneIndexer.cs#L1283

                    Field.Store      store         = customized.Store ? Field.Store.YES : Field.Store.NO;
                    Field.TermVector termVector    = customized.Store ? Field.TermVector.YES : Field.TermVector.NO;
                    Field.Index      analyzed      = customized.Analyzed ? Field.Index.ANALYZED : Field.Index.NOT_ANALYZED;
                    Field            field         = null;
                    Field            sortableField = null;
                    string           sortableValue = customized.Value;

                    if (customized.ValueType == typeof(string))
                    {
                        field = new Field(customized.FieldName, customized.Value, store, analyzed, termVector);
                    }
                    else if (customized.ValueType == typeof(DateTime))
                    {
                        DateTime custom = DateTime.Parse(customized.Value);
                        sortableValue = DateTools.DateToString(custom, DateTools.Resolution.MILLISECOND);
                        field         = new Field(customized.FieldName, sortableValue, Field.Store.YES, Field.Index.NOT_ANALYZED);
                    }
                    else
                    {
                        throw new ArgumentException($"{customized.FieldName} has type of {customized.ValueType.FullName} which isn't supported for index customizing!");
                    }

                    e.Document.Add(field);

                    if (customized.Sortable)
                    {
                        sortableField = sortableField ??
                                        new Field
                                        (
                            LuceneIndexer.SortedFieldNamePrefix + customized.FieldName,
                            sortableValue,
                            Field.Store.NO,
                            Field.Index.NOT_ANALYZED,
                            Field.TermVector.NO
                                        );

                        e.Document.Add(sortableField);
                    }
                }
            }
        }
Example #13
0
        public static Document AddField(this Document document, string name, string value, Field.Store store, Field.Index index)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(document);
            }

            document.Add(new Field(name, value, store, index));
            return(document);
        }
Example #14
0
        /// <summary>
        /// Adds a new <see cref="TextField"/> with <see cref="string"/> value. </summary>
        /// <param name="document">This <see cref="Document"/>.</param>
        /// <param name="name"> field name </param>
        /// <param name="value"> <see cref="string"/> value </param>
        /// <param name="stored"> <see cref="Field.Store.YES"/> if the content should also be stored </param>
        /// <returns>The field that was added to this <see cref="Document"/>.</returns>
        /// <exception cref="ArgumentNullException"> if this <paramref name="document"/>, the field <paramref name="name"/> or <paramref name="value"/> is <c>null</c>. </exception>
        public static TextField AddTextField(this Document document, string name, string value, Field.Store stored)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            var field = new TextField(name, value, stored);

            document.Add(field);
            return(field);
        }