我得到了以下0个结果,我认为情况不应该如此。
查询代码:
var sourceResults = session.Advanced.LuceneQuery<Route>("Routes/BySource")
.WithinRadiusOf(5, toMatch.Source.Location.Latitude, toMatch.Source.Location.Longitude)
.WhereBetween(r => r.DateTime, minDateTime, maxDateTime).ToArray();
索引代码:
/// <summary>
/// Index for spatial search by route source.
/// </summary>
public class Routes_BySource : AbstractIndexCreationTask<Route>
{
public Routes_BySource()
{
Map = routes => from r in routes
select new { _ = SpatialIndex.Generate(r.Source.Location.Latitude, r.Source.Location.Longitude) };
}
}
型号:
public class Route
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>
/// The id.
/// </value>
public string Id { get; set; }
/// <summary>
/// Gets or sets the source.
/// </summary>
/// <value>
/// From.
/// </value>
public Address Source { get; set; }
/// <summary>
/// Gets or sets destination.
/// </summary>
/// <value>
/// To.
/// </value>
public Address Destination { get; set; }
/// <summary>
/// Gets or sets the id of the user who requested the route.
/// </summary>
/// <value>
/// The user id.
/// </value>
public string UserId { get; set; }
/// <summary>
/// Gets or sets the date time that the request is for.
/// </summary>
/// <value>
/// The date time.
/// </value>
public DateTime DateTime { get; set; }
}
public class Address
{
/// <summary>
/// Gets or sets the name. This is the formatted full name of the address.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the location.
/// </summary>
/// <value>
/// The location.
/// </value>
public Location Location { get; set; }
}
public class Location
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
单元测试(调用查询代码)
using (var session = Common.DocumentStore.OpenSession())
{
var routeService = new RouteService(session);
var newRoute = new Route
{
Id = Guid.NewGuid().ToString(),
DateTime = DateTime.Now,
UserId = "user",
Source = new Address { Name = "101 W. 79th St. NY, NY", Location = new Location { Latitude = 32, Longitude = 72 } },
Destination = new Address { Name = "101 W. 79th St. NY, NY", Location = new Location { Latitude = 32, Longitude = 72 } }
};
routeService.Save(newRoute);
var routes = routeService.Search(newRoute);
Assert.IsTrue(routes.Any());
}
知道我在这里做错了什么吗?看起来应该很简单。。。
感谢
在您的代码中,您有routeService.Save(newRoute);,我看惯了会话。商店(newRoute);然后会话。SaveChanges();
在调用会话之前,不会将内容写入数据库。SaveChanges();
但您需要给ravenDB时间来索引更改。我建议使用"等待非过时结果",我现在不记得它的代码了,但如果你在谷歌上搜索ravendb非过时结果。