Esempio n. 1
0
        public void RemoveAll(string scope, string documentType)
        {
            var result = Client.DeleteIndex(scope).Result;

            if (!result.IsSuccess)
            {
                if (result.StatusCode == HttpStatusCode.NotFound)
                {
                    // ignore index not found exception as it might simply not exist and thus no deletion is necessary
                }
                else
                {
                    throw new IndexBuildException(AzureSearchHelper.FormatSearchException(result));
                }
            }
        }
Esempio n. 2
0
        public void Commit(string scope)
        {
            var coreList = _pendingDocuments.Keys.ToList();

            foreach (var core in coreList)
            {
                var documents = _pendingDocuments[core];
                if (documents == null || documents.Count == 0)
                {
                    continue;
                }

                var coreArray = core.Split('.');
                var indexName = coreArray[0];
                var indexType = coreArray[1];

                var response = Client.IndexBulk(indexName, documents);
                var result   = response.Result;

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

                foreach (var op in result.Body)
                {
                    if (!op.Status)
                    {
                        throw new IndexBuildException(op.ErrorMessage);
                    }
                }

                // Remove documents
                _pendingDocuments[core].Clear();
            }
        }
Esempio n. 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);
            }
        }