Пример #1
0
        public object Post(TypeImportRequest request)
        {
            OpenSearchEngine ose = new OpenSearchEngine();

            ose.LoadPlugins();
            ose.DefaultTimeOut = 60000;

            OpenSearchUrl url = new OpenSearchUrl(request.url);

            IOpenSearchable entity = new GenericOpenSearchable(url, ose);

            IOpenSearchableElasticType type = ecf.GetOpenSearchableElasticTypeByNameOrDefault(request.IndexName, request.TypeName);

            IOpenSearchResultCollection osres = ose.Query(entity, new NameValueCollection());

            OpenSearchFactory.RemoveLinksByRel(ref osres, "alternate");
            OpenSearchFactory.RemoveLinksByRel(ref osres, "via");
            OpenSearchFactory.RemoveLinksByRel(ref osres, "self");
            OpenSearchFactory.RemoveLinksByRel(ref osres, "search");

            IElasticCollection documents = type.FromOpenSearchResultCollection(osres);

            BulkRequest bulkRequest = new BulkRequest()
            {
                Refresh     = true,
                Consistency = Consistency.One,
                Index       = request.IndexName
            };

            foreach (var doc in documents.Items)
            {
                bulkRequest.Operations.Add(new BulkIndexOperation <IElasticItem>((IElasticItem)doc)
                {
                    Id = doc.Id
                });
            }

            var response = client.Bulk(bulkRequest);

            return(response);
        }
Пример #2
0
        public BulkOperationsResponse Ingest(IOpenSearchableElasticType type, IOpenSearchResultCollection results)
        {
            OpenSearchFactory.RemoveLinksByRel(ref results, "self");
            OpenSearchFactory.RemoveLinksByRel(ref results, "search");

            IElasticCollection docs = type.FromOpenSearchResultCollection(results);

            BulkRequest bulkRequest = new BulkRequest() {
                Refresh = true,
                Consistency = Consistency.One,
                Index = type.Index,
                Type = type.Type,
                Operations = new List<IBulkOperation>()
            };

            RootObjectMapping currentMapping = null;

            try {
                var mappingResponse = client.GetMapping<IElasticType>(g => g.Index(type.Index.Name).Type(type.Type.Name));
                currentMapping = mappingResponse.Mapping;
            } catch (Exception) {
            }

            var rootObjectMapping = type.GetRootMapping();

            if (!rootObjectMapping.Equals(currentMapping)) {
                client.Map<IElasticType>(m => m.Index(type.Index.Name).Type(type.Type.Name));
            }

            foreach (var doc in docs.ElasticItems) {
                var bulkIndexOperation = new BulkIndexOperation<IElasticItem>(doc);
                bulkIndexOperation.Id = ((IOpenSearchResultItem)doc).Identifier;
                bulkIndexOperation.Type = type.Type.Name;
                var bulkOp = bulkIndexOperation;
                bulkRequest.Operations.Add(bulkOp);
            }

            var response = client.Bulk(bulkRequest);

            BulkOperationsResponse ingestionResponse = new BulkOperationsResponse();
            foreach (var item in response.Items) {
                if (!item.IsValid) {
                    ingestionResponse.Errors++;
                    continue;
                }
                if (item.Version == "1")
                    ingestionResponse.Added++;
                else
                    ingestionResponse.Updated++;

            }

            return ingestionResponse;
        }
Пример #3
0
        public BulkOperationsResponse Ingest(IOpenSearchableElasticType type, IOpenSearchResultCollection results)
        {
            OpenSearchFactory.RemoveLinksByRel(ref results, "self");
            OpenSearchFactory.RemoveLinksByRel(ref results, "search");

            IElasticCollection docs = type.FromOpenSearchResultCollection(results);

            BulkRequest bulkRequest = new BulkRequest()
            {
                Refresh     = true,
                Consistency = Consistency.One,
                Index       = type.Index,
                Type        = type.Type,
                Operations  = new List <IBulkOperation>()
            };

            RootObjectMapping currentMapping = null;

            try {
                var mappingResponse = client.GetMapping <IElasticType>(g => g.Index(type.Index.Name).Type(type.Type.Name));
                currentMapping = mappingResponse.Mapping;
            } catch (Exception) {
            }

            var rootObjectMapping = type.GetRootMapping();

            if (!rootObjectMapping.Equals(currentMapping))
            {
                client.Map <IElasticType>(m => m.Index(type.Index.Name).Type(type.Type.Name));
            }

            foreach (var doc in docs.ElasticItems)
            {
                var bulkIndexOperation = new BulkIndexOperation <IElasticItem>(doc);
                bulkIndexOperation.Id   = ((IOpenSearchResultItem)doc).Identifier;
                bulkIndexOperation.Type = type.Type.Name;
                var bulkOp = bulkIndexOperation;
                bulkRequest.Operations.Add(bulkOp);
            }

            var response = client.Bulk(bulkRequest);

            BulkOperationsResponse ingestionResponse = new BulkOperationsResponse();

            foreach (var item in response.Items)
            {
                if (!item.IsValid)
                {
                    ingestionResponse.Errors++;
                    continue;
                }
                if (item.Version == "1")
                {
                    ingestionResponse.Added++;
                }
                else
                {
                    ingestionResponse.Updated++;
                }
            }

            return(ingestionResponse);
        }