Пример #1
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);
            }
        }