예제 #1
0
 public static Index WithField(this Index index, string name, string type, Action<IndexField> options = null)
 {
     var field = new IndexField(name, type);
     if (options != null)
         options(field);
     index.Fields.Add(field);
     return index;
 }
예제 #2
0
        public static Index WithField(this Index index, string name, string type, Action <IndexField> options = null)
        {
            var field = new IndexField(name, type);

            if (options != null)
            {
                options(field);
            }
            index.Fields.Add(field);
            return(index);
        }
예제 #3
0
        public virtual void Index(string scope, string documentType, IDocument document)
        {
            var core = GetCoreName(scope, documentType);
            if (!_pendingDocuments.ContainsKey(core))
            {
                _pendingDocuments.Add(core, new List<AzureDocument>());
            }

            Index mapping = null;
            var indexAlreadyCreated = false;
            if (!_mappings.ContainsKey(core))
            {
                Thread.Sleep(3000);

                // Get mapping info
                mapping = Client.GetIndex(scope).Result;
                if (mapping != null)
                {
                    indexAlreadyCreated = true;
                    _mappings.Add(core, mapping);
                }
            }
            else
            {
                indexAlreadyCreated = true;
                mapping = _mappings[core];
            }

            var submitMapping = false;

            var localDocument = new AzureDocument();

            for (var index = 0; index < document.FieldCount; index++)
            {
                var field = document[index];

                var key = ConvertToAzureName(field.Name.ToLower());

                if (localDocument.ContainsKey(key))
                {
                    var objTemp = localDocument[key];
                    string[] objListTemp;
                    var temp = objTemp as string[];
                    if (temp != null)
                    {
                        var objList = new List<string>(temp) { ConvertToOffset(field.Value).ToString() };
                        objListTemp = objList.ToArray();
                    }
                    else
                    {
                        objListTemp = new string[] { objTemp.ToString(), ConvertToOffset(field.Value).ToString() };
                    }

                    localDocument[key] = objListTemp;
                }
                else
                {
                    if (mapping == null || !mapping.Fields.Any(x => x.Name.Equals(key)))
                    {
                        if (mapping == null)
                        {
                            mapping = new Index(scope);
                        }

                        var indexField = new IndexField(key, AzureTypeMapper.GetAzureSearchType(field));

                        indexField.IsFilterable();

                        if (key == ConvertToAzureName("__key"))
                        {
                            indexField.IsKey();
                        }

                        if (field.ContainsAttribute(IndexStore.Yes))
                        {
                            indexField.IsRetrievable();
                        }

                        if (field.ContainsAttribute(IndexType.Analyzed))
                        {
                            indexField.IsSearchable();
                        }


                        if (indexField.Type != FieldType.StringCollection)
                        {
                            indexField.IsSortable();
                        }

                        if (indexField.Type == FieldType.StringCollection || indexField.Type == FieldType.String)
                        {

                            if (field.ContainsAttribute(IndexType.Analyzed))
                            {
                                indexField.IsSearchable();
                            }
                        }

                        mapping.Fields.Add(indexField);
                        submitMapping = true;
                    }

                    if (field.ContainsAttribute(IndexDataType.StringCollection))
                        localDocument.Add(key, ConvertToOffset(field.Values.OfType<string>().ToArray()));
                    else
                        localDocument.Add(key, ConvertToOffset(field.Value));
                }
            }

            // submit mapping
            if (submitMapping)
            {
                IApiResponse<Index> result = null;
                if (indexAlreadyCreated)
                {
                    result = Client.UpdateIndex(mapping).Result;
                }
                else
                {
                    result = Client.CreateIndex(mapping).Result;
                }

                if (!result.IsSuccess)
                {
                    throw new IndexBuildException(AzureSearchHelper.FormatSearchException(result));
                }
            }

            _pendingDocuments[core].Add(localDocument);

            // Auto commit changes when limit is reached
            if (AutoCommit && _pendingDocuments[core].Count > AutoCommitCount)
            {
                Commit(scope);
            }
        }
예제 #4
0
 public static IndexField IsSearchable(this IndexField field, bool value = true)
 {
     field.Searchable = value;
     return(field);
 }
예제 #5
0
 public static IndexField Analyzer(this IndexField field, string value)
 {
     field.Analyzer = value;
     return(field);
 }
예제 #6
0
 public static IndexField SupportSuggestions(this IndexField field, bool value = true)
 {
     field.Suggestions = value;
     return(field);
 }
예제 #7
0
 public static IndexField IsRetrievable(this IndexField field, bool value = true)
 {
     field.Retrievable = value;
     return(field);
 }
예제 #8
0
 public static IndexField IsKey(this IndexField field, bool value = true)
 {
     field.Key = value;
     return(field);
 }
예제 #9
0
 public static IndexField IsFacetable(this IndexField field, bool value = true)
 {
     field.Facetable = value;
     return(field);
 }