forked from SolrNet/SolrNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'regex-field' of git://github.com/stephenpope/SolrNet.git
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
SolrNet/Impl/QuerySerializers/QueryByFieldRegexSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
} |