public void IndexJsonDocument(BaristaIndexDefinition indexDefinition, string documentId, object docObject, object metadata, IEnumerable <FieldOptions> fieldOptions)
        {
            try
            {
                using (var searchClient = GetSearchClient())
                {
                    var document = new JsonDocumentDto
                    {
                        DocumentId = documentId,
                        DataAsJson = JsonConvert.SerializeObject(docObject, Formatting.Indented, SerializerSettings),
                    };

                    if (metadata != null)
                    {
                        document.MetadataAsJson = JsonConvert.SerializeObject(metadata, Formatting.Indented, SerializerSettings);
                    }

                    if (fieldOptions != null)
                    {
                        document.FieldOptions = fieldOptions;
                    }

                    searchClient.IndexJsonDocument(indexDefinition, document);
                }
            }
            catch (CommunicationObjectFaultedException ex)
            {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }
                throw;
            }
        }
 public void IndexJsonDocument(BaristaIndexDefinition indexDefinition, JsonDocumentDto document)
 {
     try
     {
         using (var searchClient = GetSearchClient())
         {
             searchClient.IndexJsonDocument(indexDefinition, document);
         }
     }
     catch (CommunicationObjectFaultedException ex)
     {
         if (ex.InnerException != null)
         {
             throw ex.InnerException;
         }
         throw;
     }
 }
        protected virtual JsonDocumentDto ConvertObjectToJsonDocumentDto(object documentObject)
        {
            //TODO: Recognize DocumentInstance, recognize StringInstance, recognize SPListItemInstance.
            //And convert/create a JsonDocumentInstance appropriately.

            JsonDocumentDto documentToIndex;
            var             jsonDocument = documentObject as JsonDocumentInstance;

            if (jsonDocument != null)
            {
                documentToIndex = jsonDocument.JsonDocument;
            }
            else
            {
                var instance = documentObject as ObjectInstance;
                if (instance == null)
                {
                    return(null);
                }

                var obj = instance;
                if (obj.HasProperty("@id") == false)
                {
                    throw new JavaScriptException(Engine, "Error",
                                                  "When adding a POJO to the index, a property named @id must be specified on the object that indicates the document id.");
                }

                var metadata = String.Empty;
                if (obj.HasProperty("@metadata"))
                {
                    metadata = JSONObject.Stringify(Engine, obj.GetPropertyValue("@metadata"), null, null);
                }

                //Clone the object and remove the @id and @metadata
                var json    = JSONObject.Stringify(Engine, obj, null, null);
                var jObject = JObject.Parse(json);
                jObject.Remove("@id");
                jObject.Remove("@metadata");

                //Obtain any field options, add them to the field options collection and remove them from the cloned object.
                var fieldOptions = new Dictionary <string, FieldOptions>();
                foreach (var property in jObject.Properties().ToList())
                {
                    var fieldOption = new FieldOptions();

                    var foundMatch = false;

                    if (FieldIndexDeclarative.IsMatch(property.Name))
                    {
                        fieldOption.FieldName = FieldIndexDeclarative.Match(property.Name).Groups["FieldName"].Value;
                        FieldIndexType indexType;
                        if (property.Value.ToString().TryParseEnum(true, FieldIndexType.Analyzed, out indexType))
                        {
                            fieldOption.Index = indexType;
                            foundMatch        = true;
                        }
                    }
                    else if (FieldStoreDeclarative.IsMatch(property.Name))
                    {
                        fieldOption.FieldName = FieldStoreDeclarative.Match(property.Name).Groups["FieldName"].Value;
                        FieldStorageType storageType;
                        if (property.Value.ToString().TryParseEnum(true, FieldStorageType.Stored, out storageType))
                        {
                            fieldOption.Storage = storageType;
                            foundMatch          = true;
                        }
                    }
                    else if (FieldTermVectorDeclarative.IsMatch(property.Name))
                    {
                        fieldOption.FieldName = FieldTermVectorDeclarative.Match(property.Name).Groups["FieldName"].Value;
                        FieldTermVectorType termVectorType;
                        if (property.Value.ToString().TryParseEnum(true, FieldTermVectorType.Yes, out termVectorType))
                        {
                            fieldOption.TermVectorType = termVectorType;
                            foundMatch = true;
                        }
                    }

                    if (foundMatch)
                    {
                        if (fieldOptions.ContainsKey(fieldOption.FieldName))
                        {
                            var efo = fieldOptions[fieldOption.FieldName];
                            efo.Index          = fieldOption.Index ?? efo.Index;
                            efo.Storage        = fieldOption.Storage ?? efo.Storage;
                            efo.TermVectorType = fieldOption.TermVectorType ?? efo.TermVectorType;
                        }
                        else
                        {
                            fieldOptions.Add(fieldOption.FieldName, fieldOption);
                        }

                        jObject.Remove(property.Name);
                    }
                }

                documentToIndex = new JsonDocumentDto
                {
                    DocumentId     = obj.GetPropertyValue("@id").ToString(),
                    FieldOptions   = fieldOptions.Values,
                    MetadataAsJson = metadata,
                    DataAsJson     = jObject.ToString(Formatting.None)
                };
            }

            return(documentToIndex);
        }
 public JsonDocumentInstance(ScriptEngine engine, JsonDocumentDto jsonDocument)
     : base(engine)
 {
     m_jsonDocument = jsonDocument;
     this.PopulateFunctions();
 }