private Field CreateBinaryFieldWithCaching(string name, byte[] value, Field.Store store, Field.Index index, Field.TermVector termVector)
        {
            if (value.Length > 1024)
            {
                throw new ArgumentException("Binary values must be smaller than 1Kb");
            }

            var   cacheKey = new FieldCacheKey(name, null, store, termVector, multipleItemsSameFieldCount.ToArray());
            Field field;
            var   stringWriter = new StringWriter();

            JsonExtensions.CreateDefaultJsonSerializer().Serialize(stringWriter, value);
            var sb = stringWriter.GetStringBuilder();

            sb.Remove(0, 1);             // remove prefix "
            sb.Remove(sb.Length - 1, 1); // remove postfix "
            var val = sb.ToString();

            if (fieldsCache.TryGetValue(cacheKey, out field) == false)
            {
                fieldsCache[cacheKey] = field = new Field(name, val, store, index, termVector);
            }
            field.SetValue(val);
            field.Boost     = 1;
            field.OmitNorms = true;
            return(field);
        }
        private Field CreateFieldWithCaching(string name, string value, Field.Store store, Field.Index index, Field.TermVector termVector)
        {
            var   cacheKey = new FieldCacheKey(name, index, store, termVector, multipleItemsSameFieldCount.ToArray());
            Field field;

            if (fieldsCache.TryGetValue(cacheKey, out field) == false)
            {
                fieldsCache[cacheKey] = field = new Field(name, value, store, index, termVector);
            }

            field.SetValue(value);
            field.Boost     = 1;
            field.OmitNorms = true;
            return(field);
        }
        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));
        }
        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);
        }
Exemple #5
0
 private void AddParameterToDocument(string name, dynamic value, Field.Store store, Field.Index index)
 {
     document.Add(new Field(name, value.ToString(), store, index));
 }
        public void Set(string name, object value, Document document, Field.Store store, Field.Index index, float?boost)
        {
            // In this particular class the name of the new field was passed
            // from the name field of the ClassBridge Annotation. This is not
            // a requirement. It just works that way in this instance. The
            // actual name could be supplied by hard coding it below.
            Departments deps        = (Departments)value;
            Field       field       = null;
            string      fieldValue1 = deps.Manufacturer;

            if (fieldValue1 == null)
            {
                fieldValue1 = string.Empty;
            }
            else
            {
                string fieldValue = (string)equips[fieldValue1];
                field = new Field(name, fieldValue, store, index);
                if (boost != null)
                {
                    field.Boost = (float)boost;
                }
            }

            document.Add(field);
        }
Exemple #7
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));
                    }
                }
            }
        }
Exemple #8
0
        public void Set(String name, Object value, Document document, Field.Store store, Field.Index index, float?boost)
        {
            string indexedString = stringBridge.ObjectToString(value);

            // Do not add fields on empty strings, seems a sensible default in most situations
            // TODO if Store, probably also save empty ones
            if (StringHelper.IsNotEmpty(indexedString))
            {
                Field field = new Field(name, indexedString, store, index);
                if (boost != null)
                {
                    field.SetBoost(boost.Value);
                }

                document.Add(field);
            }
        }
Exemple #9
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);
                    }
                }
            }
        }
Exemple #10
0
 private static void AddFieldIfNotNullOrEmpty(Document doc, string fieldName, string value, Field.Index index = Field.Index.NO)
 {
     if (!string.IsNullOrEmpty(value))
     {
         doc.Add(new Field(fieldName, value, Field.Store.YES, index, Field.TermVector.NO));
     }
 }
Exemple #11
0
        public static Field.Index GetLuceneValue(this FieldIndexing value, string analyzer, Field.Index @default)
        {
            switch (value)
            {
            case FieldIndexing.No:
                return(Field.Index.NO);

            case FieldIndexing.Analyzed:
                return(string.IsNullOrWhiteSpace(analyzer) == false ? Field.Index.ANALYZED : Field.Index.ANALYZED_NO_NORMS);

            case FieldIndexing.NotAnalyzed:
                return(Field.Index.NOT_ANALYZED_NO_NORMS);

            case FieldIndexing.Default:
                return(@default);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public void Set(string name, object value, Document document, Field.Store store, Field.Index index, float?boost)
        {
            DateTime date = (DateTime)value;

            int year  = date.Year;
            int month = date.Month;
            int day   = date.Day;

            // set year
            Field field = new Field(name + ".year", year.ToString(), store, index);

            if (boost != null)
            {
                field.SetBoost(boost.Value);
            }
            document.Add(field);

            // set month and pad it if necessary
            field = new Field(name + ".month", month.ToString("D2"), store, index);
            if (boost != null)
            {
                field.SetBoost(boost.Value);
            }
            document.Add(field);

            // set day and pad it if necessary
            field = new Field(name + ".day", day.ToString("D2"), store, index);
            if (boost != null)
            {
                field.SetBoost(boost.Value);
            }
            document.Add(field);

            throw new NotImplementedException();
        }
Exemple #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);
        }
        public void Set(string name, object value, Document document, Field.Store store, Field.Index index, float?boost)
        {
            var service  = (Service)value;
            var location = service.Location;
            var lat      = new Field("Location_Latitude", NumericUtils.DoubleToPrefixCoded(location.Y), Field.Store.YES, Field.Index.NOT_ANALYZED);
            var lng      = new Field("Location_Longitude", NumericUtils.DoubleToPrefixCoded(location.X), Field.Store.YES, Field.Index.NOT_ANALYZED);

            document.Add(lat);
            document.Add(lng);

            document.Add(new Field("metafile", "doc", Field.Store.YES, Field.Index.ANALYZED));

            int ctpsize = Ctps.Count;

            for (int i = 0; i < ctpsize; i++)
            {
                CartesianTierPlotter ctp = Ctps[i];
                var boxId = ctp.GetTierBoxId(location.Y, location.X);
                document.Add(new Field(ctp.GetTierFieldName(), NumericUtils.DoubleToPrefixCoded(boxId), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            }
        }
        public void Set(string name, object value, Document document, Field.Store store, Field.Index index, float?boost)
        {
            // In this particular class the name of the new field was passed
            // from the name field of the ClassBridge Annotation. This is not
            // a requirement. It just works that way in this instance. The
            // actual name could be supplied by hard coding it below.
            Department dep         = (Department)value;
            String     fieldValue1 = dep.Branch ?? string.Empty;
            String     fieldValue2 = dep.Network ?? string.Empty;
            String     fieldValue  = fieldValue1 + sepChar + fieldValue2;
            Field      field       = new Field(name, fieldValue, store, index);

            if (boost != null)
            {
                field.Boost = (float)boost;
            }

            document.Add(field);
        }