Skip to content

Commit

Permalink
Implemented add parameters when adding/updating the index (issue 85)
Browse files Browse the repository at this point in the history
  • Loading branch information
richtea committed Jan 4, 2011
1 parent c6d4239 commit 7773114
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 300 deletions.
2 changes: 1 addition & 1 deletion SolrNet.5.1.ReSharper
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<ExceptionName IsNull="False">
</ExceptionName>
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Locals" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateInstanceFields" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="PrivateInstanceFields" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateStaticFields" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Parameters" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="MethodPropertyEvent" />
Expand Down
2 changes: 1 addition & 1 deletion SolrNet.DSL/Solr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void Add<T>(T document) {
/// <param name="documents"></param>
public static void Add<T>(IEnumerable<T> documents) {
var docs = documents.Select(d => new KeyValuePair<T, double?>(d, null));
var cmd = new AddCommand<T>(docs, ServiceLocator.Current.GetInstance<ISolrDocumentSerializer<T>>());
var cmd = new AddCommand<T>(docs, ServiceLocator.Current.GetInstance<ISolrDocumentSerializer<T>>(), null);
cmd.Execute(Connection);
}

Expand Down
77 changes: 69 additions & 8 deletions SolrNet.Tests/AddCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Execute() {
var docs = new[] {
new KeyValuePair<SampleDoc, double?>(new SampleDoc(), null),
};
var cmd = new AddCommand<SampleDoc>(docs, docSerializer);
var cmd = new AddCommand<SampleDoc>(docs, docSerializer, null);
cmd.Execute(conn);
});
}
Expand All @@ -103,7 +103,68 @@ public void DocumentBoost() {
}));
}).Verify(() => {
var docs = new[] { new KeyValuePair<TestDocWithString, double?>(new TestDocWithString(), 2.1) };
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer);
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer, null);
cmd.Execute(conn);
});
}

[Test]
public void DocumentAddParametersCommitWithinSpecified()
{
var mocks = new MockRepository();
var conn = mocks.StrictMock<ISolrConnection>();
var docSerializer = new SolrDocumentSerializer<TestDocWithString>(new AttributesMappingManager(), new DefaultFieldSerializer());
With.Mocks(mocks).Expecting(() => {
conn.Post("/update",
"<add commitWithin=\"1000\"><doc boost=\"2.1\" /></add>");
LastCall.On(conn).Repeat.Once().Do(new Writer(delegate(string ignored, string s) {
Console.WriteLine(s);
return null;
}));
}).Verify(() => {
var docs = new[] { new KeyValuePair<TestDocWithString, double?>(new TestDocWithString(), 2.1) };
var parameters = new AddParameters {CommitWithin = 1000};
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer, parameters);
cmd.Execute(conn);
});
}

[Test]
public void DocumentAddParametersOverwriteSpecifiedTrue() {
var mocks = new MockRepository();
var conn = mocks.StrictMock<ISolrConnection>();
var docSerializer = new SolrDocumentSerializer<TestDocWithString>(new AttributesMappingManager(), new DefaultFieldSerializer());
With.Mocks(mocks).Expecting(() => {
conn.Post("/update",
"<add overwrite=\"true\"><doc boost=\"2.1\" /></add>");
LastCall.On(conn).Repeat.Once().Do(new Writer(delegate(string ignored, string s) {
Console.WriteLine(s);
return null;
}));
}).Verify(() => {
var docs = new[] { new KeyValuePair<TestDocWithString, double?>(new TestDocWithString(), 2.1) };
var parameters = new AddParameters {Overwrite = true};
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer, parameters);
cmd.Execute(conn);
});
}

[Test]
public void DocumentAddParametersOverwriteSpecifiedFalse() {
var mocks = new MockRepository();
var conn = mocks.StrictMock<ISolrConnection>();
var docSerializer = new SolrDocumentSerializer<TestDocWithString>(new AttributesMappingManager(), new DefaultFieldSerializer());
With.Mocks(mocks).Expecting(() => {
conn.Post("/update",
"<add overwrite=\"false\"><doc boost=\"2.1\" /></add>");
LastCall.On(conn).Repeat.Once().Do(new Writer(delegate(string ignored, string s) {
Console.WriteLine(s);
return null;
}));
}).Verify(() => {
var docs = new[] { new KeyValuePair<TestDocWithString, double?>(new TestDocWithString(), 2.1) };
var parameters = new AddParameters {Overwrite = false};
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer, parameters);
cmd.Execute(conn);
});
}
Expand All @@ -128,7 +189,7 @@ public void FieldBoost()
var docs = new[] {
new KeyValuePair<TestDocWithFieldBoost, double?>(new TestDocWithFieldBoost(), null),
};
var cmd = new AddCommand<TestDocWithFieldBoost>(docs, docSerializer);
var cmd = new AddCommand<TestDocWithFieldBoost>(docs, docSerializer, null);
cmd.Execute(conn);
});
}
Expand All @@ -139,8 +200,8 @@ public void ShouldntAlterOriginalServerUrl() {
var conn = mocks.StrictMock<ISolrConnection>();
var docSerializer = new SolrDocumentSerializer<SampleDoc>(new AttributesMappingManager(), new DefaultFieldSerializer());
var cmd = new AddCommand<SampleDoc>(new[] {
new KeyValuePair<SampleDoc, double?>(new SampleDoc(), null),
}, docSerializer);
new KeyValuePair<SampleDoc, double?>(new SampleDoc(), null),
}, docSerializer, null);
cmd.Execute(conn);
}

Expand All @@ -160,7 +221,7 @@ public void SupportsDocumentWithStringCollection() {
var docs = new[] {
new KeyValuePair<TestDocWithCollections, double?>(new TestDocWithCollections(), null),
};
var cmd = new AddCommand<TestDocWithCollections>(docs, docSerializer);
var cmd = new AddCommand<TestDocWithCollections>(docs, docSerializer, null);
cmd.Execute(conn);
});
}
Expand All @@ -170,7 +231,7 @@ public void RemovesControlCharactersFromXML() {
var docSerializer = new SolrDocumentSerializer<TestDocWithString>(new AttributesMappingManager(), new DefaultFieldSerializer());
var doc = new TestDocWithString { Desc = "control" + (char)0x7 + (char)0x1F + (char)0xFFFE + (char)0xFFFF + (char)0xFFF4 };
var docs = new[] {new KeyValuePair<TestDocWithString, double?>(doc, null), };
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer);
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer, null);
var xml = cmd.ConvertToXml();
xml = cmd.RemoveControlCharacters(xml);
Console.WriteLine(xml);
Expand All @@ -187,7 +248,7 @@ public void RemoveControlCharacters() {
var docs = new[] {
new KeyValuePair<TestDocWithString, double?>(new TestDocWithString(), null),
};
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer);
var cmd = new AddCommand<TestDocWithString>(docs, docSerializer, null);
var xml = cmd.RemoveControlCharacters("control &#x7; &#x1; &#x9; &#x1F; &#xFFFE;");
Assert.DoesNotContain(xml, "&#x7;");
Assert.DoesNotContain(xml, "&#x1;");
Expand Down
29 changes: 27 additions & 2 deletions SolrNet.Tests/SolrOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,35 @@ public void Add() {
var ops = new SolrBasicServer<TestDocumentWithoutUniqueKey>(connection, executer, docSerializer, null, headerParser, null);
ops.AddWithBoost(new[] {
new KeyValuePair<TestDocumentWithoutUniqueKey, double?>(new TestDocumentWithoutUniqueKey(), null),
});
}, null);
});
}

[Test]
public void AddWithParameters() {
var mocks = new MockRepository();
var connection = mocks.StrictMock<ISolrConnection>();
var executer = mocks.StrictMock<ISolrQueryExecuter<TestDocumentWithoutUniqueKey>>();
var headerParser = mocks.StrictMock<ISolrHeaderResponseParser>();
var docSerializer = new SolrDocumentSerializer<TestDocumentWithoutUniqueKey>(new AttributesMappingManager(), new DefaultFieldSerializer());
With.Mocks(mocks)
.Expecting(() => {
Expect.On(connection)
.Call(connection.Post("/update", "<add commitWithin=\"4343\" overwrite=\"false\"><doc /></add>"))
.Repeat.Once()
.Return(EmbeddedResource.GetEmbeddedString(GetType(), "Resources.response.xml"));
Expect.On(headerParser)
.Call(headerParser.Parse(null))
.IgnoreArguments()
.Return(new ResponseHeader());
})
.Verify(() => {
var ops = new SolrBasicServer<TestDocumentWithoutUniqueKey>(connection, executer, docSerializer, null, headerParser, null);
var parameters = new AddParameters {CommitWithin = 4343, Overwrite = false};
ops.AddWithBoost(new[] { new KeyValuePair<TestDocumentWithoutUniqueKey, double?>(new TestDocumentWithoutUniqueKey(), null), }, parameters);
});
}

[Test]
public void AddWithBoost() {
var mocks = new MockRepository();
Expand All @@ -93,7 +118,7 @@ public void AddWithBoost() {
})
.Verify(() => {
var ops = new SolrBasicServer<TestDocumentWithoutUniqueKey>(connection, executer, docSerializer, null, headerParser, null);
ops.AddWithBoost(new[] { new KeyValuePair<TestDocumentWithoutUniqueKey, double?>(new TestDocumentWithoutUniqueKey(), 2.1), });
ops.AddWithBoost(new[] { new KeyValuePair<TestDocumentWithoutUniqueKey, double?>(new TestDocumentWithoutUniqueKey(), 2.1), }, null);
});
}

Expand Down
19 changes: 17 additions & 2 deletions SolrNet/Commands/AddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#endregion

using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml;
using SolrNet.Utils;
Expand All @@ -27,10 +28,12 @@ namespace SolrNet.Commands {
public class AddCommand<T> : ISolrCommand {
private readonly IEnumerable<KeyValuePair<T, double?>> documents = new List<KeyValuePair<T, double?>>();
private readonly ISolrDocumentSerializer<T> documentSerializer;
private readonly AddParameters parameters;

public AddCommand(IEnumerable<KeyValuePair<T, double?>> documents, ISolrDocumentSerializer<T> serializer) {
public AddCommand(IEnumerable<KeyValuePair<T, double?>> documents, ISolrDocumentSerializer<T> serializer, AddParameters parameters) {
this.documents = documents;
documentSerializer = serializer;
documentSerializer = serializer;
this.parameters = parameters;
}

/// <summary>
Expand All @@ -46,6 +49,18 @@ public string RemoveControlCharacters(string xml) {
public string ConvertToXml() {
var xml = new XmlDocument();
var addElement = xml.CreateElement("add");
if (parameters != null) {
if (parameters.CommitWithin.HasValue) {
var attr = xml.CreateAttribute("commitWithin");
attr.Value = parameters.CommitWithin.Value.ToString(CultureInfo.InvariantCulture);
addElement.Attributes.Append(attr);
}
if (parameters.Overwrite.HasValue) {
var attr = xml.CreateAttribute("overwrite");
attr.Value = parameters.Overwrite.Value.ToString().ToLowerInvariant();
addElement.Attributes.Append(attr);
}
}
foreach (var docWithBoost in documents) {
var xmlDoc = documentSerializer.Serialize(docWithBoost.Key, docWithBoost.Value);
addElement.AppendChild(xml.ImportNode(xmlDoc.DocumentElement, true));
Expand Down
3 changes: 2 additions & 1 deletion SolrNet/ISolrBasicOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public interface ISolrBasicOperations<T>: ISolrBasicReadOnlyOperations<T> {
/// Adds / updates several documents with index-time boost
/// </summary>
/// <param name="docs"></param>
/// <param name="parameters"></param>
/// <returns></returns>
ResponseHeader AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs);
ResponseHeader AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs, AddParameters parameters);

/// <summary>
/// Deletes all documents that match the given id's or the query
Expand Down
33 changes: 33 additions & 0 deletions SolrNet/ISolrOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public interface ISolrOperations<T> : ISolrReadOnlyOperations<T> {
/// <returns></returns>
ResponseHeader Add(T doc);

/// <summary>
/// Adds / updates a document with parameters
/// </summary>
/// <param name="doc">The document to add/update.</param>
/// <param name="parameters">The add parameters.</param>
/// <returns></returns>
ResponseHeader Add(T doc, AddParameters parameters);

/// <summary>
/// Adds / updates a document with index-time boost
/// </summary>
Expand All @@ -52,20 +60,45 @@ public interface ISolrOperations<T> : ISolrReadOnlyOperations<T> {
/// <returns></returns>
ResponseHeader AddWithBoost(T doc, double boost);

/// <summary>
/// Adds / updates a document with index-time boost and add parameters
/// </summary>
/// <param name="doc">The document to add/update.</param>
/// <param name="boost">The index-time boost to apply.</param>
/// <param name="parameters">The add parameters.</param>
/// <returns></returns>
ResponseHeader AddWithBoost(T doc, double boost, AddParameters parameters);

/// <summary>
/// Adds / updates several documents at once
/// </summary>
/// <param name="docs">documents to add/update</param>
/// <returns></returns>
ResponseHeader Add(IEnumerable<T> docs);

/// <summary>
/// Adds / updates several documents at once
/// </summary>
/// <param name="docs">documents to add/update</param>
/// <param name="parameters">The add parameters.</param>
/// <returns></returns>
ResponseHeader Add(IEnumerable<T> docs, AddParameters parameters);

/// <summary>
/// Adds / updates documents with index-time boost
/// </summary>
/// <param name="docs">List of docs / boost. If boost is null, no boost is applied</param>
/// <returns></returns>
ResponseHeader AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs);

/// <summary>
/// Adds / updates documents with index-time boost and add parameters
/// </summary>
/// <param name="docs">List of docs / boost. If boost is null, no boost is applied</param>
/// <param name="parameters">The add parameters.</param>
/// <returns></returns>
ResponseHeader AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs, AddParameters parameters);

/// <summary>
/// Deletes a document (requires the document to have a unique key defined with non-null value)
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions SolrNet/Impl/SolrBasicServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public ResponseHeader Rollback() {
return SendAndParseHeader(new RollbackCommand());
}

public ResponseHeader AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs) {
var cmd = new AddCommand<T>(docs, documentSerializer);
public ResponseHeader AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs, AddParameters parameters) {
var cmd = new AddCommand<T>(docs, documentSerializer, parameters);
return SendAndParseHeader(cmd);
}

Expand Down
21 changes: 19 additions & 2 deletions SolrNet/Impl/SolrServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,28 @@ public ResponseHeader BuildSpellCheckDictionary() {
}

public ResponseHeader AddWithBoost(T doc, double boost) {
return AddWithBoost(doc, boost, null);
}

public ResponseHeader AddWithBoost(T doc, double boost, AddParameters parameters) {
return ((ISolrOperations<T>)this).AddWithBoost(new[] { new KeyValuePair<T, double?>(doc, boost) });
}

public ResponseHeader Add(IEnumerable<T> docs) {
return basicServer.AddWithBoost(docs.Select(d => new KeyValuePair<T, double?>(d, null)));
return Add(docs, null);
}

public ResponseHeader Add(IEnumerable<T> docs, AddParameters parameters) {
return basicServer.AddWithBoost(docs.Select(d => new KeyValuePair<T, double?>(d, null)), parameters);
}

ResponseHeader ISolrOperations<T>.AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs) {
return basicServer.AddWithBoost(docs);
return ((ISolrOperations<T>)this).AddWithBoost(docs, null);
}

ResponseHeader ISolrOperations<T>.AddWithBoost(IEnumerable<KeyValuePair<T, double?>> docs, AddParameters parameters)
{
return basicServer.AddWithBoost(docs, parameters);
}

public ResponseHeader Delete(IEnumerable<string> ids) {
Expand Down Expand Up @@ -182,6 +195,10 @@ public ResponseHeader Optimize() {
}

public ResponseHeader Add(T doc) {
return Add(doc, null);
}

public ResponseHeader Add(T doc, AddParameters parameters) {
return Add(new[] { doc });
}

Expand Down
Loading

0 comments on commit 7773114

Please sign in to comment.