public ExplanationInstance Explain(object query, object docId)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            if (query == null || query == Null.Value || query == Undefined.Value)
            {
                throw new JavaScriptException(Engine, "Error", "A query object must be specified as the first parameter.");
            }

            if (docId == null || docId == Null.Value || docId == Undefined.Value)
            {
                throw new JavaScriptException(Engine, "Error",
                                              "A search result or document id must be specified as the second parameter.");
            }

            var searchQueryType = query.GetType();
            var queryProperty   = searchQueryType.GetProperty("Query", BindingFlags.Instance | BindingFlags.Public);

            if (queryProperty == null || typeof(Query).IsAssignableFrom(queryProperty.PropertyType) == false)
            {
                throw new JavaScriptException(Engine, "Error", "Unsupported query object.");
            }

            var queryValue = queryProperty.GetValue(query, null) as Query;

            //TODO: Change doc ID to also accept searchResults.
            var docIdValue = TypeConverter.ToInteger(docId);

            var explanation = m_baristaSearchServiceProxy.Explain(m_indexDefinition, queryValue, docIdValue);

            return(new ExplanationInstance(Engine.Object.InstancePrototype, explanation));
        }
        public SearchServiceInstance Index(object documentObject)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            if (documentObject == null || documentObject == Null.Value || documentObject == Undefined.Value)
            {
                throw new JavaScriptException(Engine, "Error",
                                              "A document object to be indexed must be supplied as the first parameter.");
            }

            var objects = documentObject as ArrayInstance;

            if (objects != null)
            {
                var documentObjects  = objects;
                var documentsToIndex = documentObjects.ElementValues
                                       .Select(d => ConvertObjectToJsonDocumentDto(d))
                                       .ToList();

                m_baristaSearchServiceProxy.IndexJsonDocuments(m_indexDefinition, documentsToIndex);
            }
            else
            {
                var documentToIndex = ConvertObjectToJsonDocumentDto(documentObject);
                m_baristaSearchServiceProxy.IndexJsonDocument(m_indexDefinition, documentToIndex);
            }

            return(this);
        }
        public void DeleteDocuments(object documentIds)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            IEnumerable <string> documentIdValues;
            var ids = documentIds as ArrayInstance;

            if (ids != null)
            {
                var arrDocumentIds = ids;
                documentIdValues = arrDocumentIds.ElementValues
                                   .Select(documentId => TypeConverter.ConvertTo <string>(Engine, documentId))
                                   .Where(
                    documentIdValue =>
                    documentIdValue.IsNullOrWhiteSpace() == false &&
                    documentIdValue != "undefined")
                                   .ToList();
            }
            else
            {
                documentIdValues = new List <string> {
                    TypeConverter.ToString(documentIds)
                };
            }

            m_baristaSearchServiceProxy.DeleteDocuments(m_indexDefinition, documentIdValues);
        }
        public bool DoesIndexExist()
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            return(m_baristaSearchServiceProxy.DoesIndexExist(m_indexDefinition));
        }
        public void DeleteAllDocuments()
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            m_baristaSearchServiceProxy.DeleteAllDocuments(m_indexDefinition);
        }
        public int SearchResultCount(object query, object maxResults)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            var args = CoerceSearchArguments(query, maxResults, null);

            var searchResultCount = m_baristaSearchServiceProxy.SearchResultCount(m_indexDefinition, args);

            return(searchResultCount);
        }
        public JsonDocumentInstance Retrieve(string documentId)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            var result = m_baristaSearchServiceProxy.Retrieve(m_indexDefinition, documentId);

            return(result == null
              ? null
              : new JsonDocumentInstance(Engine, result));
        }
        public ArrayInstance GetFieldNames()
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            var fieldNames = m_baristaSearchServiceProxy.GetFieldNames(m_indexDefinition);

            // ReSharper disable CoVariantArrayConversion
            return(Engine.Array.Construct(fieldNames.ToArray()));
            // ReSharper enable CoVariantArrayConversion
        }
        public ArrayInstance FacetedSearch(object query, object maxResults, object groupByFields)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            var args = CoerceSearchArguments(query, maxResults, groupByFields);

            var searchResults = m_baristaSearchServiceProxy.FacetedSearch(m_indexDefinition, args);

            // ReSharper disable CoVariantArrayConversion
            return(Engine.Array.Construct(searchResults.Select(sr => new FacetedSearchResultInstance(Engine.Object.InstancePrototype, sr)).ToArray()));
            // ReSharper restore CoVariantArrayConversion
        }
        public string Highlight(object query, object docId, object fieldName, object fragCharSize)
        {
            if (IndexName.IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error", "indexName not set. Please set the indexName property on the Search Instance prior to performing an operation.");
            }

            if (query == null || query == Null.Value || query == Undefined.Value)
            {
                throw new JavaScriptException(Engine, "Error", "A query object must be specified as the first argument.");
            }

            if (docId == null || docId == Null.Value || docId == Undefined.Value)
            {
                throw new JavaScriptException(Engine, "Error",
                                              "A search result or document id must be specified as the second argument.");
            }

            if (fieldName == null || fieldName == Null.Value || fieldName == Undefined.Value || TypeConverter.ToString(fieldName).IsNullOrWhiteSpace())
            {
                throw new JavaScriptException(Engine, "Error",
                                              "A field name must be specified as the third argument.");
            }

            var searchQueryType = query.GetType();
            var queryProperty   = searchQueryType.GetProperty("Query", BindingFlags.Instance | BindingFlags.Public);

            if (queryProperty == null || typeof(Query).IsAssignableFrom(queryProperty.PropertyType) == false)
            {
                throw new JavaScriptException(Engine, "Error", "Unsupported query object.");
            }

            var queryValue = queryProperty.GetValue(query, null) as Query;

            //TODO: Change doc ID to also accept searchResults.
            var docIdValue = TypeConverter.ToInteger(docId);

            var fragCharSizeValue = 100;

            if (fragCharSize != null && fragCharSize != Null.Value && fragCharSize != Undefined.Value)
            {
                fragCharSizeValue = TypeConverter.ToInteger(fragCharSize);
            }

            return(m_baristaSearchServiceProxy.Highlight(m_indexDefinition, queryValue, docIdValue, TypeConverter.ToString(fieldName), fragCharSizeValue));
        }