Created
March 19, 2015 12:12
-
-
Save ZNS/3a3b5c86500dfd5b46b7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.Composition.Hosting; | |
using System.Globalization; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using Lucene.Net.Analysis; | |
using Raven.Client.Indexes; | |
using Raven.Database.Indexing; | |
using Raven.Database.Indexing.Collation; | |
using Raven.Database.Plugins; | |
using Raven.Tests.Helpers; | |
using Xunit; | |
namespace RavenSortingTest.RavenTests | |
{ | |
public class CanCallLastOnArray : RavenTestBase | |
{ | |
private class Student | |
{ | |
public string Name { get; set; } | |
} | |
private class Students_ByName : AbstractIndexCreationTask<Student> | |
{ | |
public override string IndexName | |
{ | |
get | |
{ | |
return "StudentsByName"; | |
} | |
} | |
public Students_ByName() | |
{ | |
Map = students => from student in students | |
select new | |
{ | |
Name = student.Name | |
}; | |
//Remove comment for a successful test | |
//Sort(x => x.Name, Raven.Abstractions.Indexing.SortOptions.String); | |
} | |
} | |
public class StudentAnalyzerGenerator : AbstractAnalyzerGenerator | |
{ | |
public override Analyzer GenerateAnalyzerForIndexing(string indexName, Lucene.Net.Documents.Document document, Analyzer previousAnalyzer) | |
{ | |
var fields = document.GetFields(); | |
RavenPerFieldAnalyzerWrapper perFieldAnalyzer = new RavenPerFieldAnalyzerWrapper(new Raven.Database.Indexing.LowerCaseKeywordAnalyzer()); | |
foreach (var field in fields) | |
{ | |
if (field.Name == "Name") | |
{ | |
perFieldAnalyzer.AddAnalyzer(field.Name, new CollationAnalyzer(CultureInfo.GetCultureInfo("sv"))); | |
} | |
} | |
return perFieldAnalyzer; | |
} | |
public override Analyzer GenerateAnalyzerForQuerying(string indexName, string query, Analyzer previousAnalyzer) | |
{ | |
RavenPerFieldAnalyzerWrapper perFieldAnalyzer = new RavenPerFieldAnalyzerWrapper(new Raven.Database.Indexing.LowerCaseKeywordAnalyzer()); | |
var matches = Regex.Matches(query, @"([^\s\(\)]+)\s*:\s*[^\s]+"); | |
foreach (Match match in matches) | |
{ | |
var field = match.Groups[1].Value; | |
if (field == "Name") | |
{ | |
perFieldAnalyzer.AddAnalyzer(field, new CollationAnalyzer(CultureInfo.GetCultureInfo("sv"))); | |
} | |
} | |
return perFieldAnalyzer; | |
} | |
} | |
[Fact] | |
public void CanOrderByName() | |
{ | |
using (var store = NewDocumentStore(catalog: new TypeCatalog(typeof(StudentAnalyzerGenerator)))) | |
{ | |
using (var session = store.OpenSession()) | |
{ | |
for (var c = 'A'; c != 'Z'; c++) | |
{ | |
session.Store(new Student { Name = c.ToString() + "bcde" }); | |
} | |
session.SaveChanges(); | |
} | |
new Students_ByName().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
var results = session.Advanced.LuceneQuery<Student>("StudentsByName") | |
.WaitForNonStaleResults() | |
.Take(40) | |
.OrderBy("Name") | |
.ToList(); | |
var c = 'A'; | |
foreach (var s in results) | |
{ | |
Assert.True(s.Name.ToCharArray().First() == c); | |
c++; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment