Skip to content

Commit

Permalink
Merge branch 'regex-field' of git://github.com/stephenpope/SolrNet.git
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Jan 11, 2013
2 parents af345ce + 966cc73 commit 7e145ba
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions SolrNet.Tests/SolrNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<Compile Include="SolrFacetPivotQueryTest.cs" />
<Compile Include="SolrMoreLikeThisHandlerStreamUrlQueryTests.cs" />
<Compile Include="SolrQueryByDistanceTests.cs" />
<Compile Include="SolrQueryByFieldRegexTests.cs" />
<Compile Include="SolrQuerySerializerStub.cs" />
<Compile Include="SolrSchemaParserTests.cs" />
<Compile Include="SolrStatusResponseParserTests.cs" />
Expand Down
59 changes: 59 additions & 0 deletions SolrNet.Tests/SolrQueryByFieldRegexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//

using MbUnit.Framework;
using SolrNet.Impl.FieldSerializers;
using SolrNet.Impl.QuerySerializers;

namespace SolrNet.Tests {
[TestFixture]
public class SolrQueryByFieldRegexTests {

public string Serialize(object q)
{
var serializer = new DefaultQuerySerializer(new DefaultFieldSerializer());
return serializer.Serialize(q);
}

[Test]
public void NullField_yields_null_query()
{
var q = new SolrQueryByFieldRegex(null, "11(.*?)");
Assert.IsNull(Serialize(q));
}

[Test]
public void NullValue_yields_null_query()
{
var q = new SolrQueryByFieldRegex("id", null);
Assert.IsNull(Serialize(q));
}

[Test]
public void Basic_test_creates_correctly()
{
var q = new SolrQueryByFieldRegex("id","11(.*?)");
Assert.AreEqual("id:/11(.*?)/", Serialize(q));
}

[Test]
public void Basic_test_brackets_creates_correctly()
{
var q = new SolrQueryByFieldRegex("id", "[0-9]{5}");
Assert.AreEqual("id:/[0-9]{5}/", Serialize(q));
}

[Test]
public void Strip_slashes_creates_correctly()
{
var q = new SolrQueryByFieldRegex("id", "/11(.*?)/");
Assert.AreEqual("id:/11(.*?)/", Serialize(q));
}

[Test]
public void FieldNameWithSpaces()
{
var q = new SolrQueryByFieldRegex("field with spaces", "11(.*?)");
Assert.AreEqual(@"field\ with\ spaces:/11(.*?)/", Serialize(q));
}
}
}
1 change: 1 addition & 0 deletions SolrNet/Impl/QuerySerializers/DefaultQuerySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DefaultQuerySerializer : ISolrQuerySerializer {
public DefaultQuerySerializer(ISolrFieldSerializer fieldSerializer) {
serializer = new AggregateQuerySerializer(new ISolrQuerySerializer[] {
new QueryByFieldSerializer(),
new QueryByFieldRegexSerializer(),
new LocalParamsSerializer(this),
new BoostQuerySerializer(this),
new HasValueQuerySerializer(this),
Expand Down
31 changes: 31 additions & 0 deletions SolrNet/Impl/QuerySerializers/QueryByFieldRegexSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
namespace SolrNet.Impl.QuerySerializers
{
/// <summary>
/// Serializes a SolrQueryByFieldRegex query.
/// </summary>
public class QueryByFieldRegexSerializer : SingleTypeQuerySerializer<SolrQueryByFieldRegex>
{
/// <summary>
/// Serializes a SolrQueryByFieldRegex query.
/// </summary>
/// <param name="q">The query.</param>
/// <returns></returns>
public override string Serialize(SolrQueryByFieldRegex q)
{
if (q == null || q.FieldName == null || q.Expression == null)
{
return null;
}

var expression = q.Expression;

if (expression.StartsWith("/") && expression.EndsWith("/"))
{
expression = expression.Substring(1,expression.Length-2);
}

return string.Format("{0}:/{1}/", QueryByFieldSerializer.EscapeSpaces(q.FieldName), expression);
}
}
}
2 changes: 2 additions & 0 deletions SolrNet/SolrNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="Commands\Parameters\TermVectorParameters.cs" />
<Compile Include="Impl\FieldParsers\LocationFieldParser.cs" />
<Compile Include="Impl\FieldParsers\MoneyFieldParser.cs" />
<Compile Include="Impl\QuerySerializers\QueryByFieldRegexSerializer.cs" />
<Compile Include="Location.cs" />
<Compile Include="Money.cs" />
<Compile Include="ExtractField.cs" />
Expand Down Expand Up @@ -303,6 +304,7 @@
<Compile Include="SolrHasValueQuery.cs" />
<Compile Include="SolrMultipleCriteriaQuery.cs" />
<Compile Include="SolrNotQuery.cs" />
<Compile Include="SolrQueryByFieldRegex.cs" />
<Compile Include="SolrRequiredQuery.cs" />
<Compile Include="SolrQuery.cs" />
<Compile Include="SolrQueryBoost.cs" />
Expand Down
44 changes: 44 additions & 0 deletions SolrNet/SolrQueryByFieldRegex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
namespace SolrNet {
/// <summary>
/// Queries a field for a value
/// </summary>
public class SolrQueryByFieldRegex : AbstractSolrQuery
{
private readonly string fieldName;
private readonly string expression;

/// <summary>
/// Queries a field based on a regular expression
/// </summary>
/// <param name="fieldName">Field name</param>
/// <param name="regularExpression">The regular expression.</param>
public SolrQueryByFieldRegex(string fieldName, string regularExpression)
{
this.fieldName = fieldName;
this.expression = regularExpression;
}

/// <summary>
/// Gets the name of the field.
/// </summary>
/// <value>
/// The name of the field.
/// </value>
public string FieldName
{
get { return fieldName; }
}

/// <summary>
/// Gets the regular expression.
/// </summary>
/// <value>
/// The regular expression to be used.
/// </value>
public string Expression
{
get { return expression; }
}
}
}

0 comments on commit 7e145ba

Please sign in to comment.