public void IndexDocument(BaristaIndexDefinition indexDefinition, string documentId, DocumentDto document) { try { if (documentId.IsNullOrWhiteSpace()) { throw new ArgumentNullException("documentId", @"A document id must be specified."); } if (document == null) { throw new ArgumentNullException("document", @"A document must be specified."); } var index = GetOrAddIndex(indexDefinition, true); try { //Add it to the index. var luceneDocument = DocumentDto.ConvertToLuceneDocument(document); var batch = new IndexingBatch(); batch.Add(new BatchedDocument { DocumentId = documentId, Document = luceneDocument, SkipDeleteFromIndex = false, }); index.IndexDocuments(batch); } catch (OutOfMemoryException) { CloseIndexWriter(indexDefinition, false); } } catch (Exception ex) { throw new FaultException(ex.Message); } }
public void IndexJsonDocuments(BaristaIndexDefinition indexDefinition, IEnumerable <JsonDocumentDto> documents) { try { if (documents == null) { throw new ArgumentNullException("documents", @"A collection of documents must be specified."); } var jsonDocuments = documents as IList <JsonDocumentDto> ?? documents.ToList(); if (jsonDocuments.Any() == false) { throw new ArgumentNullException("documents", @"At least one document must be contained within the collection."); } var index = GetOrAddIndex(indexDefinition, true); try { //Add it to the index. var batch = new IndexingBatch(); //Update the indexDefinition for the index based on the options specified. foreach (var document in jsonDocuments) { UpdateIndexDefinitionFromFieldOptions(index.IndexDefinition, document.FieldOptions); } //Attempt to create a new Search.JsonDocument from the document var searchJsonDocuments = jsonDocuments.Select(document => new Search.JsonDocument { DocumentId = document.DocumentId, Metadata = document.MetadataAsJson.IsNullOrWhiteSpace() == false ? JObject.Parse(document.MetadataAsJson) : new JObject(), DataAsJson = JObject.Parse(document.DataAsJson) }); var luceneDocuments = JsonDocumentToLuceneDocumentConverter.ConvertJsonDocumentToLuceneDocument(index.IndexDefinition, searchJsonDocuments); foreach (var luceneDocument in luceneDocuments) { batch.Add(luceneDocument); } //TODO: Add the batch to a BlockingCollection<IndexingBatch> and run a thread that consumes the batches //See http://www.codethinked.com/blockingcollection-and-iproducerconsumercollection index.IndexDocuments(batch); } catch (OutOfMemoryException) { CloseIndexWriter(indexDefinition, false); } } catch (Exception ex) { throw new FaultException(ex.Message); } }