mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 02:44:52 +08:00
Fixing index information retrieval
- Storing doubles instead of floats - Fixing integer values parsing --HG-- branch : dev extra : transplant_source : %EB%7E%27%E7%3B%2B%CEvA%AE%90%3FQ%BE%9Bw%9C%3FXA
This commit is contained in:
@@ -119,14 +119,23 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
[Test]
|
||||
public void PropertiesShouldNotBeLost() {
|
||||
_provider.CreateIndex("default");
|
||||
_provider.Store("default", _provider.New(42).Add("prop1", "value1").Store());
|
||||
_provider.Store("default", _provider.New(42)
|
||||
.Add("prop1", "value1").Store()
|
||||
.Add("prop2", 123).Store()
|
||||
.Add("prop3", 123.456).Store()
|
||||
.Add("prop4", new DateTime(2001,1,1,1,1,1,1)).Store()
|
||||
.Add("prop5", true).Store()
|
||||
);
|
||||
|
||||
var hit = _provider.CreateSearchBuilder("default").Get(42);
|
||||
|
||||
Assert.IsNotNull(hit);
|
||||
Assert.That(hit.ContentItemId, Is.EqualTo(42));
|
||||
Assert.That(hit.GetString("prop1"), Is.EqualTo("value1"));
|
||||
|
||||
Assert.That(hit.GetInt("prop2"), Is.EqualTo(123));
|
||||
Assert.That(hit.GetDouble("prop3"), Is.EqualTo(123.456));
|
||||
Assert.That(hit.GetDateTime("prop4"), Is.EqualTo(new DateTime(2001, 1, 1, 1, 1, 1, 1)));
|
||||
Assert.That(hit.GetBoolean("prop5"), Is.EqualTo(true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@@ -177,6 +177,24 @@ namespace Orchard.Tests.Modules.Indexing {
|
||||
Assert.That(date[1].GetDateTime("date") < date[2].GetDateTime("date"), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldSortByNumber() {
|
||||
_provider.CreateIndex("default");
|
||||
_provider.Store("default", _provider.New(1).Add("downloads", 111).Store());
|
||||
_provider.Store("default", _provider.New(2).Add("downloads", 2222).Store());
|
||||
_provider.Store("default", _provider.New(3).Add("downloads", 3).Store());
|
||||
|
||||
var number = _searchBuilder.SortBy("downloads").Search().ToList();
|
||||
Assert.That(number.Count(), Is.EqualTo(3));
|
||||
Assert.That(number[0].GetInt("downloads") > number[1].GetInt("downloads"), Is.True);
|
||||
Assert.That(number[1].GetInt("downloads") > number[2].GetInt("downloads"), Is.True);
|
||||
|
||||
number = _searchBuilder.SortBy("downloads").Ascending().Search().ToList();
|
||||
Assert.That(number.Count(), Is.EqualTo(3));
|
||||
Assert.That(number[0].GetInt("downloads") < number[1].GetInt("downloads"), Is.True);
|
||||
Assert.That(number[1].GetInt("downloads") < number[2].GetInt("downloads"), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldEscapeSpecialChars() {
|
||||
_provider.CreateIndex("default");
|
||||
|
@@ -15,7 +15,7 @@ namespace Lucene.Models {
|
||||
private string _name;
|
||||
private string _stringValue;
|
||||
private int _intValue;
|
||||
private float _floatValue;
|
||||
private double _doubleValue;
|
||||
private bool _analyze;
|
||||
private bool _store;
|
||||
private bool _removeTags;
|
||||
@@ -46,7 +46,7 @@ namespace Lucene.Models {
|
||||
}
|
||||
|
||||
public IDocumentIndex Add(string name, DateTime value) {
|
||||
return Add(name, DateTools.DateToString(value, DateTools.Resolution.SECOND));
|
||||
return Add(name, DateTools.DateToString(value, DateTools.Resolution.MILLISECOND));
|
||||
}
|
||||
|
||||
public IDocumentIndex Add(string name, int value) {
|
||||
@@ -62,10 +62,10 @@ namespace Lucene.Models {
|
||||
return Add(name, value.ToString());
|
||||
}
|
||||
|
||||
public IDocumentIndex Add(string name, float value) {
|
||||
public IDocumentIndex Add(string name, double value) {
|
||||
PrepareForIndexing();
|
||||
_name = name;
|
||||
_floatValue = value;
|
||||
_doubleValue = value;
|
||||
_typeCode = TypeCode.Single;
|
||||
IsDirty = true;
|
||||
return this;
|
||||
@@ -114,7 +114,7 @@ namespace Lucene.Models {
|
||||
case TypeCode.Single:
|
||||
Fields.Add(new NumericField(_name,
|
||||
_store ? Field.Store.YES : Field.Store.NO,
|
||||
true).SetFloatValue(_floatValue));
|
||||
true).SetDoubleValue(_doubleValue));
|
||||
break;
|
||||
case TypeCode.Empty:
|
||||
break;
|
||||
|
@@ -20,12 +20,12 @@ namespace Lucene.Models {
|
||||
|
||||
public int GetInt(string name) {
|
||||
var field = _doc.GetField(name);
|
||||
return field == null ? 0 : NumericUtils.PrefixCodedToInt(field.StringValue());
|
||||
return field == null ? 0 : Int32.Parse(field.StringValue(), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public float GetFloat(string name) {
|
||||
public double GetDouble(string name) {
|
||||
var field = _doc.GetField(name);
|
||||
return field == null ? 0 : float.Parse(field.StringValue(), CultureInfo.InvariantCulture);
|
||||
return field == null ? 0 : double.Parse(field.StringValue(), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public bool GetBoolean(string name) {
|
||||
|
@@ -10,7 +10,7 @@ namespace Orchard.Indexing {
|
||||
IDocumentIndex Add(string name, DateTime value);
|
||||
IDocumentIndex Add(string name, int value);
|
||||
IDocumentIndex Add(string name, bool value);
|
||||
IDocumentIndex Add(string name, float value);
|
||||
IDocumentIndex Add(string name, double value);
|
||||
|
||||
/// <summary>
|
||||
/// Stores the original value to the index.
|
||||
|
@@ -5,7 +5,7 @@ namespace Orchard.Indexing {
|
||||
float Score { get; }
|
||||
|
||||
int GetInt(string name);
|
||||
float GetFloat(string name);
|
||||
double GetDouble(string name);
|
||||
bool GetBoolean(string name);
|
||||
string GetString(string name);
|
||||
DateTime GetDateTime(string name);
|
||||
|
Reference in New Issue
Block a user