using System.Linq;
using NUnit.Framework;
using Raven.Abstractions.Indexing;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Client.Linq.Indexing;
namespace Tests
{
[TestFixture]
public class OrenEiniTests
{
[Test]
public void WithingRadiusOf_Should_Not_Break_Relevance()
{
using (var store = new EmbeddableDocumentStore { RunInMemory = true }.Initialize())
using (var session = store.OpenSession())
{
IndexCreation.CreateIndexes(typeof(OrenEiniTests).Assembly, store);
var place1 = new Place("Université du Québec à Montréal")
{
Id = "places/1",
Description = "L'Université du Québec à Montréal (UQAM) est une université francophone, publique et urbaine de Montréal, dans la province du Québec au Canada.",
Latitude = 45.50955,
Longitude = -73.569131
};
var place2 = new Place("UQAM")
{
Id = "places/2",
Description = "L'Université du Québec à Montréal (UQAM) est une université francophone, publique et urbaine de Montréal, dans la province du Québec au Canada.",
Latitude = 45.50955,
Longitude = -73.569131
};
session.Store(place1);
session.Store(place2);
session.SaveChanges();
// places/2: perfect match + boost
var terms = "UQAM";
RavenQueryStatistics stats;
var places = session.Advanced.LuceneQuery<PlacesByTermsAndLocation.PlaceQuery, PlacesByTermsAndLocation>()
.WaitForNonStaleResults()
.Statistics(out stats)
.WithinRadiusOf(500, 45.54545, -73.63908)
.Where("(Name:(" + terms + ") OR Terms:(" + terms + "))")
.Take(10)
.SelectFields<Place>().ToList();
// bug?: false..
//Assert.That(places[0].Id == "places/2");
// places/1: perfect match + boost
terms = "Université Québec Montréal";
places = session.Advanced.LuceneQuery<PlacesByTermsAndLocation.PlaceQuery, PlacesByTermsAndLocation>()
.WaitForNonStaleResults()
.Statistics(out stats)
.WithinRadiusOf(500, 45.54545, -73.63908)
.Where("(Name:(" + terms + ") OR Terms:(" + terms + "))")
.Take(10)
.SelectFields<Place>().ToList();
// true bug?: because places/1 is always first - was inserted first.. no score?
Assert.That(places[0].Id == "places/1");
}
}
public class Place
{
public Place(string name)
{
Name = name;
}
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Address { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class PlacesByTermsAndLocation : AbstractIndexCreationTask<Place, PlacesByTermsAndLocation.PlaceQuery>
{
public class PlaceQuery
{
public string Name { get; set; }
public string Terms { get; set; }
}
public PlacesByTermsAndLocation()
{
Map = boards =>
from b in boards
select new
{
Name = b.Name.Boost(3),
Terms = new
{
b.Description,
b.Address
},
_ = SpatialIndex.Generate(b.Latitude, b.Longitude)
};
Index(p => p.Name, FieldIndexing.Analyzed);
Index(p => p.Terms, FieldIndexing.Analyzed);