Create and update are the same operation in Solr (and SolrNet). It's mapped to the Add()
method in the ISolrOperations<T>
interface.
There are two overloads of Add()
: one takes a single document as parameter, the other takes a IEnumerable
of documents. Keep in mind that one call to any of these Add()
methods will end up in one HTTP request to Solr. So for example this:
ISolrOperations<MyDocument> solr = ...
for (var i = 0; i < 100; i++)
solr.Add(new MyDocument(i));
is not the same as this:
var documents = Enumerable.Range(0, 100).Select(i => new MyDocument(i));
solr.Add(documents);
If you Add()
a document that already exists in the Solr index, it's replaced (using the unique key as equality)
There's also AddWithBoost()
(also with single-document and IEnumerable
overloads) that you can use to apply an index-time boosting to the added documents.
Add()
supports the commitWithin
and overwrite
parameters. See the Solr wiki for more information about them.
See Querying
For index updates to be visible (searchable), some kind of commit must reopen a searcher to a new point-in-time view of the index.
The realtime get feature allows retrieval (by unique-key) of the latest version of any documents without the associated cost of reopening a searcher. This is primarily useful when using Solr as a NoSQL data store and not just a search index.
For now there is no especific method to use the real time handler, but it can be performed using the /get handler which exists implicitly in Solr
ISolrOperations<Product> solr = ...
solr.Query(SolrQuery.All,
new QueryOptions
{
RequestHandler = new RequestHandlerParameters("/get"),
ExtraParams = new Dictionary<string, string>
{
{"ids", id }
}
});
The ISolrOperations interface has several Delete()
overloads:
Delete(T doc)
: deletes a single document using its unique key to identify it.Delete(string id)
: deletes a single document with a unique keyDelete(IEnumerable<T> docs)
: deletes a batch of documents in a single shot.Delete(IEnumerable<string> ids)
: deletes a batch of documents in a single shot.Delete(ISolrQuery q)
: deletes all documents that match a query.Delete(IEnumerable<string> ids, ISolrQuery q)
: deletes a batch of documents and all documents that match a query in a single shot.
After issuing any number of Add()
or Delete()
operations, be sure to call Commit()
:
solr.Add(myDocument);
solr.Add(anotherDocument);
solr.Delete(oldDocument);
solr.Commit();
... this tells Solr to finalise the changes you have made and to start rebuilding indexes and related data.
Obviously this has a performance penalty on the Solr instance as all query caches are cleared and repopulated due to the new index.
Alternatively, you can let Solr manage commits, enabling the autoCommit options in the Solr configuration.
The Optimize()
method:
solr.Optimize();
... issues a command to tell Solr to begin optimizing its indexes. Again this is an expensive operation in terms of the Solr instance and shouldn't be called too frequently.
Additional information on committing and optimization considerations can be found here
Rollback()
rollbacks all add/deletes made to the index since the last commit. Note that this is nothing like the rollback of relational databases. See the Solr wiki for more information.