using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Newtonsoft.Json; //Do not remove - Required for JObject using Newtonsoft.Json.Linq; //Do not remove - Required for JObject using Shinydocs.CognitiveToolkit.Scripting; using Shinydocs.CognitiveToolkit.Core.Tools.ScrollTools.RunScript; using Shinydocs.CognitiveToolkit.UI.CommandLine; using Shinydocs.CognitiveToolkit.UI.CommandLine.Progress; using Shinydocs.CognitiveToolkit.Utilities; #pragma warning disable 8601 #pragma warning disable 8618 public class TagQueryResult : IScript { private static string Version = "2.6.0"; private readonly ScriptLogger _log = new ScriptLogger(); private string[] _fieldsToReturn = { "id" }; private string _indexName; private string _fieldName; private string _fieldValues; private string _query; private string _serverUrl; private int _nodesPerRequest; private int _threads; private RunScriptDocumentUpdater _documentUpdater; public void SetUp(string[] arguments) { _log.Information(string.Format("Using version {0}", Version)); Console.WriteLine(string.Format("Using version {0}", Version)); try { Dictionary options; // OptionsParser will parse an array of arguments into a dictionary of flags and values if (OptionsParser.TryParse(arguments, out options)) { OptionsParser.ParseStandardOptions(options, out _serverUrl, out _indexName, out _threads, out _nodesPerRequest); OptionsParser.ParseQueryOption(options, out _query); if (!options.TryGetValue("--field-name", out _fieldName)) { OptionsParser.InputError("The field name parameter (--field-name) is a required"); } if (!options.TryGetValue("--field-values", out _fieldValues)) { OptionsParser.InputError("The field values parameter (--field-values) is a required"); } _fieldsToReturn = _fieldsToReturn.Append(_fieldName).ToArray(); _documentUpdater = new RunScriptDocumentUpdater( _serverUrl, _indexName, _fieldsToReturn, _nodesPerRequest, _threads); } else { throw new ArgumentException("Failed to parse arguments"); } } catch (ArgumentException) { Console.WriteLine("TagQueryResult tool requires the following parameters -q for query, -u for Index server Url, -i for IndexName, " + "--FieldName for FieldName and --FieldValues for FieldValue "); throw; } } public void Run() { using (var progress = new CommandLineProgress()) { _documentUpdater.Progress = progress; var processedTotal = 0; var updatedTotal = 0; _documentUpdater.Update( _query, document => { var id = document["id"]; Interlocked.Increment(ref processedTotal); try { var update = UpdateDocumentWithFieldAndValues(document, _fieldName, _fieldValues.Split(',')); if (update) { Interlocked.Increment(ref updatedTotal); return document.ToObject>(); } } catch (Exception ex) { _log.Error(ex, string.Format("Error processing {0}, {1}", id, ex.Message)); } return new Dictionary(); }); progress.UpdateProgress(string.Format("Completed Processing. Updated : {0}", updatedTotal)); } } public bool UpdateDocumentWithFieldAndValues(JObject document, string fieldName, string[] fieldValues) { var update = false; IEnumerable filteredFieldValues = fieldValues.Where(value => value.Length != 0); //will remove any empty fieldValues if (document.ContainsKey(fieldName)) { var values = document[fieldName].Value(); foreach (var fieldValue in filteredFieldValues) { if (fieldValue == "") { continue; } if (values.All(t => t.Value() != fieldValue)) { values.Add(fieldValue); update = true; } } document[fieldName] = values; } else { update = true; document[fieldName] = new JArray(filteredFieldValues); // filed value will get added to the dictionary } return update; } public void TearDown() { } }