private string GenerateDeleteItemsCommandText(string indexName, string idField, List <ElasticSearchItem> elasticSearchItems)
        {
            var idsToDelete = new List <string>();

            foreach (var item in elasticSearchItems)
            {
                idsToDelete.Add(ElasticSearchEtl.LowerCaseDocumentIdProperty(item.DocumentId));
            }

            using (var context = JsonOperationContext.ShortTermSingleUse())
            {
                var result = new DynamicJsonValue()
                {
                    ["query"] = new DynamicJsonValue()
                    {
                        ["terms"] = new DynamicJsonValue()
                        {
                            [idField] = new DynamicJsonArray(idsToDelete)
                        }
                    }
                };

                var resultJson = context.ReadObject(result, "").ToString();

                var sb = new StringBuilder("POST ")
                         .Append(indexName)
                         .AppendLine("/_delete_by_query?refresh=true")
                         .AppendLine(resultJson);

                return(sb.ToString());
            }
        }
        protected override void LoadToFunction(string indexName, ScriptRunnerResult document)
        {
            if (indexName == null)
            {
                ThrowLoadParameterIsMandatory(nameof(indexName));
            }

            var result = document.TranslateToObject(Context);


            var index = GetOrAdd(indexName);

            if (result.TryGet(index.DocumentIdProperty, out object _) == false)
            {
                result.Modifications = new DynamicJsonValue
                {
                    [index.DocumentIdProperty] = ElasticSearchEtl.LowerCaseDocumentIdProperty(Current.Document.Id)
                };
            }

            index.Inserts.Add(new ElasticSearchItem(Current)
            {
                TransformationResult = result
            });
        }
        private IEnumerable <string> GenerateInsertItemsCommandText(string indexName, ElasticSearchIndexWithRecords index, DocumentsOperationContext context)
        {
            var result = new List <string>();

            if (index.Inserts.Count > 0)
            {
                var sb = new StringBuilder("POST ")
                         .Append(indexName)
                         .AppendLine("/_bulk?refresh=wait_for");

                foreach (var item in index.Inserts)
                {
                    using (var json = ElasticSearchEtl.EnsureLowerCasedIndexIdProperty(context, item.TransformationResult, index))
                    {
                        sb.AppendLine(ElasticSearchEtl.IndexBulkAction);
                        sb.AppendLine(json.ToString());
                    }
                }

                result.Add(sb.ToString());
            }

            return(result);
        }