Commit 3e949794 authored by Nick Craver's avatar Nick Craver

Cleanup: extensionsCleanup: NRediSearch

parent 4196a130
......@@ -7,11 +7,12 @@ namespace NRediSearch
/// </summary>
internal static class Literals
{
private static Hashtable _boxed = new Hashtable();
private static object _null = RedisValue.Null;
private static readonly Hashtable _boxed = new Hashtable();
private static readonly object _null = RedisValue.Null;
/// <summary>
/// Obtain a lazily-cached pre-encoded and boxed representation of a string
/// </summary>
/// <param name="value">The value to get a literal representation for.</param>
/// <remarks>This shoul donly be used for fixed values, not user data (the cache is never reclaimed, so it will be a memory leak)</remarks>
public static object Literal(this string value)
{
......
// .NET port of https://github.com/RedisLabs/JRediSearch/
using StackExchange.Redis;
using System;
using System.Collections.Generic;
......@@ -17,7 +16,6 @@ public class Query
/// </summary>
public abstract class Filter
{
public string Property { get; }
internal abstract void SerializeRedisArgs(List<object> args);
......@@ -26,7 +24,6 @@ internal Filter(string property)
{
Property = property;
}
}
/// <summary>
......@@ -34,7 +31,6 @@ internal Filter(string property)
/// </summary>
public class NumericFilter : Filter
{
private readonly double min, max;
private readonly bool exclusiveMin, exclusiveMax;
......@@ -48,7 +44,6 @@ public NumericFilter(string property, double min, bool exclusiveMin, double max,
public NumericFilter(string property, double min, double max) : this(property, min, false, max, false) { }
internal override void SerializeRedisArgs(List<object> args)
{
RedisValue FormatNum(double num, bool exclude)
......@@ -72,9 +67,8 @@ RedisValue FormatNum(double num, bool exclude)
/// </summary>
public class GeoFilter : Filter
{
private readonly double lon, lat, radius;
private GeoUnit unit;
private readonly GeoUnit unit;
public GeoFilter(string property, double lon, double lat, double radius, GeoUnit unit) : base(property)
{
......@@ -118,17 +112,17 @@ public Paging(int offset, int count)
/// <summary>
/// The query's filter list. We only support AND operation on all those filters
/// </summary>
List<Filter> _filters = new List<Filter>();
private readonly List<Filter> _filters = new List<Filter>();
/// <summary>
/// The textual part of the query
/// </summary>
public string QueryString { get; }
/// <summary>
/// The sorting parameters
/// </summary>
Paging _paging = new Paging(0, 10);
private Paging _paging = new Paging(0, 10);
/// <summary>
/// Set the query to verbatim mode, disabling stemming and query expansion
......@@ -169,12 +163,13 @@ public Paging(int offset, int count)
/// <summary>
/// Set the query parameter to sort by ASC by default
/// </summary>
public bool SortAscending {get; set;} = true;
public bool SortAscending { get; set; } = true;
/// <summary>
/// Create a new index
/// </summary>
public Query(String queryString)
/// <param name="queryString">The query string to use for this query.</param>
public Query(string queryString)
{
QueryString = queryString;
}
......@@ -208,14 +203,14 @@ internal void SerializeRedisArgs(List<object> args)
args.Add("LANGUAGE".Literal());
args.Add(Language);
}
if (_fields != null && _fields.Length > 0)
if (_fields?.Length > 0)
{
args.Add("INFIELDS".Literal());
args.Add(_fields.Length);
args.AddRange(_fields);
}
if(SortBy != null)
if (SortBy != null)
{
args.Add("SORTBY".Literal());
args.Add(SortBy);
......@@ -235,7 +230,7 @@ internal void SerializeRedisArgs(List<object> args)
args.Add(_paging.Count);
}
if (_filters != null && _filters.Count > 0)
if (_filters?.Count > 0)
{
foreach (var f in _filters)
{
......@@ -243,12 +238,12 @@ internal void SerializeRedisArgs(List<object> args)
}
}
}
/// <summary>
/// Limit the results to a certain offset and limit
/// </summary>
/// <param name="offset">the first result to show, zero based indexing</param>
/// <param name="limit">how many results we want to show</param>
/// <param name="count">how many results we want to show</param>
/// <returns>the query itself, for builder-style syntax</returns>
public Query Limit(int offset, int count)
{
......@@ -274,7 +269,7 @@ public Query AddFilter(Filter f)
/// <returns>the query object itself</returns>
public Query LimitFields(params string[] fields)
{
this._fields = fields;
_fields = fields;
return this;
}
......@@ -291,4 +286,4 @@ public Query SetSortBy(string field, bool ascending = true)
return this;
}
}
}
}
\ No newline at end of file
......@@ -50,6 +50,7 @@ object GetForRedis(FieldType type)
if(Sortable){args.Add("SORTABLE");}
}
}
public class TextField : Field
{
public double Weight { get; }
......@@ -57,10 +58,12 @@ internal TextField(string name, double weight = 1.0) : base(name, FieldType.Full
{
Weight = weight;
}
internal TextField(string name, bool sortable, double weight = 1.0) : base(name, FieldType.FullText, sortable)
{
Weight = weight;
}
internal override void SerializeRedisArgs(List<object> args)
{
base.SerializeRedisArgs(args);
......@@ -138,6 +141,7 @@ internal TagField(string name, string separator = ",") : base(name, FieldType.Ta
{
Separator = separator;
}
internal override void SerializeRedisArgs(List<object> args)
{
base.SerializeRedisArgs(args);
......@@ -160,7 +164,5 @@ public Schema AddTagField(string name, string separator = ",")
Fields.Add(new TagField(name, separator));
return this;
}
}
}
// .NET port of https://github.com/RedisLabs/JRediSearch/
using StackExchange.Redis;
using System.Collections.Generic;
......@@ -15,7 +14,6 @@ public class SearchResult
public long TotalResults { get; }
public List<Document> Documents { get; }
internal SearchResult(RedisResult[] resp, bool hasContent, bool hasScores, bool hasPayloads)
{
// Calculate the step distance to walk over the results.
......@@ -26,18 +24,18 @@ internal SearchResult(RedisResult[] resp, bool hasContent, bool hasScores, bool
int payloadOffset = 0;
if (hasScores)
{
step += 1;
step++;
scoreOffset = 1;
contentOffset += 1;
contentOffset++;
}
if (hasContent)
{
step += 1;
step++;
if (hasPayloads)
{
payloadOffset = scoreOffset + 1;
step += 1;
contentOffset += 1;
step++;
contentOffset++;
}
}
......@@ -47,7 +45,6 @@ internal SearchResult(RedisResult[] resp, bool hasContent, bool hasScores, bool
Documents = docs;
for (int i = 1; i < resp.Length; i += step)
{
var id = (string)resp[i];
double score = 1.0;
byte[] payload = null;
......@@ -68,8 +65,6 @@ internal SearchResult(RedisResult[] resp, bool hasContent, bool hasScores, bool
docs.Add(Document.Load(id, score, payload, fields));
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment