anchor.diff:              return! Choice1Of2(searchResults)
           }
       
  -    [<Sealed>]
  -    type Service(state : State.T, queryFactory : IFlexFactory<IFlexQuery>) = 
  -        
  -        // Generate query types from query factory. This is necessary as a single query can support multiple
  -        // query names
  -        let queryTypes = 
  -            let result = new Dictionary<string, IFlexQuery>(StringComparer.OrdinalIgnoreCase)
  -            for pair in queryFactory.GetAllModules() do
  -                for queryName in pair.Value.QueryName() do
  -                    result.Add(queryName, pair.Value)
  -            result
  -        
  -        interface ISearchService with
  -            member __.Search(query) = state |> search (query, queryTypes)
  -            member __.SearchAsDocmentSeq(searchQuery) = 
  -                maybe { let! writers = state.IndexFactory |> LazyFactory.getInstance searchQuery.IndexName
  -                        let! query = generateSearchQuery (writers, searchQuery, None, queryTypes)
  -                        return! SearchDsl.searchDocumentSeq (writers, query, searchQuery) }
  -            member __.SearchAsDictionarySeq(searchQuery : SearchQuery.Dto) = 
  -                maybe { let! writers = state.IndexFactory |> LazyFactory.getInstance searchQuery.IndexName
  -                        let! query = generateSearchQuery (writers, searchQuery, None, queryTypes)
  -                        return! SearchDsl.searchDictionarySeq (writers, query, searchQuery) }
  -            member __.SearchUsingProfile(searchQuery : SearchQuery.Dto, inputFields : Dictionary<string, string>) = 
  -                maybe { 
  -                    let! writers = state.IndexFactory |> LazyFactory.getInstance searchQuery.IndexName
  -                    let! query = generateSearchQuery (writers, searchQuery, Some(inputFields), queryTypes)
  -                    let! (results, recordsReturned, totalAvailable) = SearchDsl.searchDocumentSeq 
  -                                                                          (writers, query, searchQuery)
  -                    let searchResults = new SearchResults()
  -                    searchResults.Documents <- results.ToList()
  -                    searchResults.TotalAvailable <- totalAvailable
  -                    searchResults.RecordsReturned <- recordsReturned
  -                    return! Choice1Of2(searchResults)
  -                }
  -
  -module DocumentService = 
  -    /// Get a document by Id
  -    let getDocument (indexName) (id) (searchService : ISearchService) = 
  -        maybe { 
  -            let q = new SearchQuery.Dto(indexName, (sprintf "%s = '%s'" Constants.IdField id))
  -            q.ReturnScore <- false
  -            q.ReturnFlatResult <- false
  -            q.Columns <- [| "*" |]
  -            match searchService.Search(q) with
  -            | Choice1Of2(v') -> 
  -                if v'.Documents.Count <> 0 then return! Choice1Of2(v'.Documents.First())
  -                else return! fail <| DocumentIdNotFound(indexName, id)
  -            | Choice2Of2(e) -> return! Choice2Of2(e)
  -        }
  -    
  -    /// Get top 10 document from the index
  -    let getDocuments (indexName) (count) (searchService : ISearchService) = 
  -        maybe { 
  -            let q = new SearchQuery.Dto(indexName, (sprintf "%s matchall 'x'" Constants.IdField))
  -            q.ReturnScore <- false
  -            q.ReturnFlatResult <- false
  -            q.Columns <- [| "*" |]
  -            q.Count <- count
  -            q.MissingValueConfiguration.Add(Constants.IdField, MissingValueOption.Ignore)
  -            return! searchService.Search(q)
  -        }
  -    
  -    /// Add or update an existing document
  -    let addOrUpdateDocument (document : Document.Dto) (state : State.T) = 
  -        maybe { 
  -            do! document.Validate()
  -            do! state.IndexFactory |> LazyFactory.exists document.IndexName IndexService.indexNotFound
  -            let! indexWriter = state.IndexFactory |> LazyFactory.getInstance document.IndexName
  -            indexWriter |> IndexWriter.updateDocument document
  -        }
  -    
  -    /// Add a new document to the index
  -    let addDocument (document : Document.Dto) (state : State.T) = 
  -        maybe { 
  -            do! document.Validate()
  -            if document.TimeStamp > 0L then 
  -                return! fail <| IndexingVersionConflict(document.IndexName, document.Id, document.TimeStamp.ToString())
  -            let! writer = state.IndexFactory |> LazyFactory.getInstance document.IndexName
  -            writer |> IndexWriter.addDocument document
  -            return new CreateResponse(document.Id)
  -        }
  -    
  -    /// Delete a document by Id
  -    let deleteDocument (indexName) (id) (state : State.T) = maybe { let! writer = state.IndexFactory 
  -                                                                                  |> LazyFactory.getInstance indexName
  -                                                                    writer |> IndexWriter.deleteDocument id }
  -    
  -    /// Delete all documents of an index
  -    let deleteAllDocuments (indexName) (state : State.T) = maybe { let! writer = state.IndexFactory 
  -                                                                                 |> LazyFactory.getInstance indexName
  -                                                                   writer |> IndexWriter.deleteAllDocuments }
  -    
  -    type Service(searchService : ISearchService, state : State.T) = 
  -        interface IDocumentService with
  -            member __.TotalDocumentCount(indexName : string) = maybe { let! writer = state.IndexFactory 
  -                                                                                     |> LazyFactory.getInstance 
  -                                                                                            indexName
  -                                                                       return writer |> IndexWriter.getDocumentCount }
  -            member __.GetDocument(indexName, documentId) = searchService |> getDocument indexName documentId
  -            member __.GetDocuments(indexName, count) = searchService |> getDocuments indexName count
  -            member __.AddOrUpdateDocument(document) = state |> addOrUpdateDocument document
  -            member __.AddDocument(document) = state |> addDocument document
  -            member __.DeleteDocument(indexName, documentId) = state |> deleteDocument indexName documentId
  -            member __.DeleteAllDocuments indexName = state |> deleteAllDocuments indexName
  +    // Generate query types from query factory. This is necessary as a single query can support multiple
  +    // query names
  +    let queryTypes = 
  +        let result = new Dictionary<string, IFlexQuery>(StringComparer.OrdinalIgnoreCase)
  +        for pair in queryFactory.GetAllModules() do
  +            for queryName in pair.Value.QueryName() do
  +                result.Add(queryName, pair.Value)
  +        result
  +    
  +    interface ISearchService with
  +        member __.Search(query) = search (query, queryTypes)
  +        member __.SearchAsDocmentSeq(searchQuery) = 
  +            maybe { let! writers = indexService.IsIndexOnline <| searchQuery.IndexName
  +                    let! query = generateSearchQuery (writers, searchQuery, None, queryTypes)
  +                    return! SearchDsl.searchDocumentSeq (writers, query, searchQuery) }
  +        member __.SearchAsDictionarySeq(searchQuery : SearchQuery.Dto) = 
  +            maybe { let! writers = indexService.IsIndexOnline <| searchQuery.IndexName
  +                    let! query = generateSearchQuery (writers, searchQuery, None, queryTypes)
  +                    return! SearchDsl.searchDictionarySeq (writers, query, searchQuery) }
  +        member __.SearchUsingProfile(searchQuery : SearchQuery.Dto, inputFields : Dictionary<string, string>) = 
  +            maybe { 
  +                let! writers = indexService.IsIndexOnline <| searchQuery.IndexName
  +                let! query = generateSearchQuery (writers, searchQuery, Some(inputFields), queryTypes)
  +                let! (results, recordsReturned, totalAvailable) = SearchDsl.searchDocumentSeq 
  +                                                                      (writers, query, searchQuery)
  +                let searchResults = new SearchResults()
  +                searchResults.Documents <- results.ToList()
  +                searchResults.TotalAvailable <- totalAvailable
  +                searchResults.RecordsReturned <- recordsReturned
  +                return! Choice1Of2(searchResults)
  +            }
  +
  +[<Sealed>]
  +type DocumentService(searchService : ISearchService, indexService : IIndexService) = 
  +    interface IDocumentService with
  +        
  +        /// Returns the total number of documents present in the index
  +        member __.TotalDocumentCount(indexName : string) = maybe { let! writer = indexService.IsIndexOnline <| indexName
  +                                                                   return writer |> IndexWriter.getDocumentCount }
  +        
  +        /// Get a document by Id        
  +        member __.GetDocument(indexName, documentId) = 
  +            maybe { 
  +                let q = new SearchQuery.Dto(indexName, (sprintf "%s = '%s'" Constants.IdField documentId))
  +                q.ReturnScore <- false
  +                q.ReturnFlatResult <- false
  +                q.Columns <- [| "*" |]
  +                match searchService.Search(q) with
  +                | Choice1Of2(v') -> 
  +                    if v'.Documents.Count <> 0 then return! Choice1Of2(v'.Documents.First())
  +                    else return! fail <| DocumentIdNotFound(indexName, documentId)
  +                | Choice2Of2(e) -> return! Choice2Of2(e)
  +            }
  +        
  +        /// Get top 10 document from the index
  +        member __.GetDocuments(indexName, count) = 
  +            maybe { 
  +                let q = new SearchQuery.Dto(indexName, (sprintf "%s matchall 'x'" Constants.IdField))
  +                q.ReturnScore <- false
  +                q.ReturnFlatResult <- false
  +                q.Columns <- [| "*" |]
  +                q.Count <- count
  +                q.MissingValueConfiguration.Add(Constants.IdField, MissingValueOption.Ignore)
  +                return! searchService.Search(q)
  +            }
  +        
  +        /// Add or update an existing document
  +        member __.AddOrUpdateDocument(document) = 
  +            maybe { 
  +                do! document.Validate()
  +                let! indexWriter = indexService.IsIndexOnline <| document.IndexName
  +                indexWriter |> IndexWriter.updateDocument document
  +            }
  +        
  +        /// Add a new document to the index
  +        member __.AddDocument(document) = 
  +            maybe { 
  +                do! document.Validate()
  +                if document.TimeStamp > 0L then 
  +                    return! fail 
  +                            <| IndexingVersionConflict(document.IndexName, document.Id, document.TimeStamp.ToString())
  +                let! writer = indexService.IsIndexOnline <| document.IndexName
  +                writer |> IndexWriter.addDocument document
  +                return new CreateResponse(document.Id)
  +            }
  +        
  +        /// Delete a document by Id
  +        member __.DeleteDocument(indexName, documentId) = maybe { let! writer = indexService.IsIndexOnline <| indexName
  +                                                                  writer |> IndexWriter.deleteDocument documentId }
  +        
  +        /// Delete all the documents present in an index
  +        member __.DeleteAllDocuments indexName = maybe { let! writer = indexService.IsIndexOnline <| indexName
  +                                                         writer |> IndexWriter.deleteAllDocuments }
  
positive.code:                             (writers.Settings.FieldsLookup, predicate, searchQuery, searchProfile, queryTypes)
          }
      
      let search (searchQuery : SearchQuery.Dto, queryTypes) (state : State.T) = 
          maybe { 
              let! (index, state, writers) = state.IndexFactory 
                                             |> LazyFactory.getAsTuple searchQuery.IndexName IndexService.indexNotFound
              let! query = generateSearchQuery (writers, searchQuery, None, queryTypes)
              let! (results, recordsReturned, totalAvailable) = SearchDsl.searchDocumentSeq (writers, query, searchQuery)
              let searchResults = new SearchResults()
  
negative.code: 
  /// Index related operations
  type IIndexService = 
      abstract GetIndex : indexName:string -> Choice<Index.Dto, Error>
      abstract UpdateIndexFields : fields:Field.Dto [] -> Choice<unit, Error>
      abstract DeleteIndex : indexName:string -> Choice<unit, Error>
      abstract AddIndex : index:Index.Dto -> Choice<CreateResponse, Error>
      abstract GetAllIndex : unit -> Index.Dto array
      abstract IndexExists : indexName:string -> bool
      abstract IndexOnline : indexName:string -> bool
      abstract IsIndexOnline : indexName:string -> Choice<IndexWriter.T, Error>
      abstract GetIndexState : indexName:string -> Choice<IndexState, Error>
      abstract OpenIndex : indexName:string -> Choice<unit, Error>
      abstract CloseIndex : indexName:string -> Choice<unit, Error>
      abstract Commit : indexName:string -> Choice<unit, Error>
      abstract Refresh : indexName:string -> Choice<unit, Error>
      abstract GetRealtimeSearchers : indexName:string -> Choice<array<RealTimeSearcher>, Error>
      abstract GetRealtimeSearcher : indexName:string * int -> Choice<RealTimeSearcher, Error>
  
  /// Document related operations
  type IDocumentService = 
  