RavenDb faceted search by applying predicate against nested values on document - mapreduce

In RavenDB I understand when you require to count properties matching a certain criteria, it can be achieved
by creating facets against those property names you wish to group,
and then creating an index over the above properties and the properties required in the where clause.
and finally issuing a query by using the above index. and materializing the query using the ToFacets extension.
But what happens when your where clause happens to contain a predicate against a property that is a collection of values on the document? Because if I add the nested property from the collection to the index against the parent document, my facet counts on properties on the parent document will not be accurate?
for e.g.
public class Camera {
string Make { get;set; }
string Model { get;set; }
double Price { get;set; }
IEnumerable<string> Showrooms { get;set; }
}
My query would look like
(from camera in session.Query<Camera, Camera_Facets>()
where camera.Price < 100 && camera.ShowRooms.Any(s => s.In("VIC", "ACT", "QLD"))
select new {
camera.Make,
camera.Model,
camera.Price}
).ToFacets("facets/CameraFacets");
Update:
Here is the failing test
[TestFixture]
public class FacetedSearchWhenQueryingNestedCollections
{
public class Car
{
public Car()
{
Locations = new List<string>();
}
public string Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public IEnumerable<string> Locations { get; set; }
}
public class Car_Facets : AbstractIndexCreationTask<Car>
{
public Car_Facets()
{
Map = cars => from car in cars
select new {car.Make, car.Model, car.Price};
}
}
private static IDocumentSession OpenSession
{
get { return new EmbeddableDocumentStore {RunInMemory = true}.Initialize().OpenSession(); }
}
[Test]
public void CanGetFacetsWhenQueryingNesetedCollectionValues()
{
var cars = Builder<Car>.CreateListOfSize(50)
.Section(0, 10)
.With(x => x.Model = "Camry")
.With(x => x.Make = "Toyota")
.With(x => x.Price = 2000)
.With(x => x.Locations = new[] {"VIC", "ACT"})
.Section(11, 20)
.With(x => x.Model = "Corolla")
.With(x => x.Make = "Toyota")
.With(x => x.Price = 1000)
.With(x => x.Locations = new[] { "NSW", "ACT" })
.Section(21, 30)
.With(x => x.Model = "Rx8")
.With(x => x.Make = "Mazda")
.With(x => x.Price = 5000)
.With(x => x.Locations = new[] { "ACT", "SA", "TAS" })
.Section(31, 49)
.With(x => x.Model = "Civic")
.With(x => x.Make = "Honda")
.With(x => x.Price = 1500)
.With(x => x.Locations = new[] { "QLD", "SA", "TAS" })
.Build();
IDictionary<string, IEnumerable<FacetValue>> facets;
using(var s = OpenSession)
{
s.Store(new FacetSetup { Id = "facets/CarFacets", Facets = new List<Facet> { new Facet { Name = "Model" }, new Facet { Name = "Make" }, new Facet { Name = "Price" } } });
s.SaveChanges();
IndexCreation.CreateIndexes(typeof(Car_Facets).Assembly, s.Advanced.DocumentStore);
foreach (var car in cars)
s.Store(car);
s.SaveChanges();
s.Query<Car, Car_Facets>().Customize(x => x.WaitForNonStaleResults()).ToList();
facets = s.Query<Car, Car_Facets>()
.Where(x => x.Price < 3000)
.Where(x => x.Locations.Any(l => l.In("QLD", "VIC")))
.ToFacets("facets/CarFacets");
}
Assert.IsNotNull(facets);
Assert.IsTrue(facets.All(f => f.Value.Count() > 0));
Assert.IsTrue(facets.All(f => f.Value.All(x => x.Count > 0)));
}
[TearDown]
public void ClearData()
{
using(var s = OpenSession)
{
foreach (var car in s.Query<Car>().ToList())
s.Delete(car);
s.SaveChanges();
}
}
}
But if I change my query to be. (not querying the nested collection any more)
facets = s.Query<Car, Car_Facets>()
.Where(x => x.Price < 3000)
.ToFacets("facets/CarFacets");
Now I get 3 Enumerations in the dictionary, all with values.

if I change my index to be
public class Car_Facets : AbstractIndexCreationTask<Car>
{
public Car_Facets()
{
Map = cars => from car in cars
from location in car.Locations
select new {car.Make, car.Model, car.Price, Location = location};
}
}
Create a class for the projection
public class CarOnLocation
{
public string Make { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public string Location { get; set; }
}
And then query as
facets = s.Query<Car, Car_Facets>().AsProjection<CarOnLocation>()
.Where(x => x.Price < 3000)
.Where(x => x.Location.In("QLD", "VIC"))
.ToFacets("facets/CarFacets");
it works.

Related

ef core unit test echec

I have a method in my repository that I’m trying to test
public User UpdateUserManyToMany(User user, List<Guid> manyToManyds)
{
var dbContext = _databaseContext as DbContext;
dbContext?.TryUpdateManyToMany(user.ManyToMany, manyToManyds
.Select(x => new ManyToMany{
OtherEntityId = x,
UserId = user.Id,
}), x => x.OtherEntityId);
return user;
}
My ManyToMany Entity :
public class ManyToMany
{
public Guid OtherEntityId { get; set; }
public OtherEntity OtherEntityId { get; set; }
public Guid UserId { get; set; }
public User User { get; set; }
}
My TryUpdateManyToMany :
public static class ManyToManyExtensions
{
public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class
{
db.Set<T>().RemoveRange(currentItems.Except(newItems, getKey));
db.Set<T>().AddRange(newItems.Except(currentItems, getKey));
}
public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc)
{
return items
.GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems })
.SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp })
.Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T)))
.Select(t => t.t.item);
}
}
Here’s my unit test :
using (var context = new InMemoryDataBaseContext())
{
// Arrange
var repository = new UserRepository(context);
await context.Users.AddRangeAsync(GetUser());
await context.SaveChangesAsync();
// Act
var manyIds = new List<Guid>();
manyIds.Add(Guid.Parse("855d1a64-a707-40d5-ab93-34591a923abf"));
manyIds.Add(Guid.Parse("855d1a64-a787-40d9-ac93-34591a923abf"));
manyIds.Add(Guid.Parse("855d1a64-a707-41d9-ab93-39591a923abf"));
var user = new User();
var expected = repository.UpdateUserManyToMany(GetUser(), manyIds);
// Assert
}
But I get the following error in my test :
Message:
System.InvalidOperationException : The instance of entity type 'ManyToMany' cannot be tracked because another instance with the same key value for {'UserId', 'OtherEntityId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Arborescence des appels de procédure:
IdentityMap`1.ThrowIdentityConflict(InternalEntityEntry entry)
IdentityMap`1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
IdentityMap`1.Add(TKey key, InternalEntityEntry entry)
NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
StateManager.StartTracking(InternalEntityEntry entry)
InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
DbContext.RemoveRange(IEnumerable`1 entities)
InternalDbSet`1.RemoveRange(IEnumerable`1 entities)
ManyToManyExtensions.TryUpdateManyToMany[T,TKey](DbContext db, IEnumerable`1 currentItems, IEnumerable`1 newItems, Func`2 getKey) ligne 24
UserRepository.UpdateUserManyToMany(User user, List`1 manyToManyds) ligne 59
MyRepoUnitTest.MyTestMethod() ligne 102
--- End of stack trace from previous location where exception was thrown ```
The following sample program, that is based on the code you provided, works without issues:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace IssueConsoleTemplate
{
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<ManyToMany> ManyToMany { get; set; } = new HashSet<ManyToMany>();
}
public class OtherEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<ManyToMany> ManyToMany { get; set; } = new HashSet<ManyToMany>();
}
public class ManyToMany
{
public Guid OtherEntityId { get; set; }
public Guid UserId { get; set; }
public OtherEntity OtherEntity { get; set; }
public User User { get; set; }
}
public class Context : DbContext
{
public DbSet<OtherEntity> OtherEntities { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<ManyToMany> ManyToMany { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(#"Data Source=.\MSSQL14;Integrated Security=SSPI;Initial Catalog=So63077461")
.UseLoggerFactory(
LoggerFactory.Create(
b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<OtherEntity>(
entity =>
{
entity.HasKey(e => e.Id);
entity.HasMany(e => e.ManyToMany)
.WithOne(e => e.OtherEntity)
.HasForeignKey(e => e.OtherEntityId);
entity.HasData(
new OtherEntity
{
Id = new Guid("855d1a64-a707-40d5-ab93-34591a923abf"),
Name = "Bicycle"
},
new OtherEntity
{
Id = new Guid("855d1a64-a787-40d9-ac93-34591a923abf"),
Name = "Bus"
},
new OtherEntity
{
Id = new Guid("855d1a64-a707-41d9-ab93-39591a923abf"),
Name = "Plane"
});
});
modelBuilder.Entity<User>(
entity =>
{
entity.HasKey(e => e.Id);
entity.HasMany(e => e.ManyToMany)
.WithOne(e => e.User)
.HasForeignKey(e => e.UserId);
});
modelBuilder.Entity<ManyToMany>(
entity =>
{
entity.HasKey(e => new {e.OtherEntityId, e.UserId});
});
}
}
public static class ManyToManyExtensions
{
public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class
{
db.Set<T>().RemoveRange(currentItems.Except(newItems, getKey));
db.Set<T>().AddRange(newItems.Except(currentItems, getKey));
}
public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc)
{
return items
.GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems })
.SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp })
.Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T)))
.Select(t => t.t.item);
}
}
internal class UserRepository
{
private readonly Context _databaseContext;
public UserRepository(Context context)
{
_databaseContext = context;
}
public User UpdateUserManyToMany(User user, List<Guid> manyToManyds)
{
var dbContext = _databaseContext as DbContext;
dbContext?.TryUpdateManyToMany(user.ManyToMany, manyToManyds
.Select(x => new ManyToMany{
OtherEntityId = x,
UserId = user.Id,
}), x => x.OtherEntityId);
return user;
}
}
internal static class Program
{
private static async Task Main()
{
//
// Operations with referential integrity intact:
//
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
// Arrange
var repository = new UserRepository(context);
await context.Users.AddRangeAsync(GetUser());
await context.SaveChangesAsync();
// Act
var manyIds = new List<Guid>
{
new Guid("855d1a64-a707-40d5-ab93-34591a923abf"),
new Guid("855d1a64-a787-40d9-ac93-34591a923abf"),
new Guid("855d1a64-a707-41d9-ab93-39591a923abf")
};
var expected = repository.UpdateUserManyToMany(GetUser(), manyIds);
}
private static User GetUser()
=> User;
private static readonly User User = new User
{
Id = new Guid("30c35d2e-77fd-480b-9974-6ebf037a8f86"),
Name = "John"
};
}
}
System.InvalidOperationException : The instance of entity type 'ManyToMany' cannot be tracked because another instance with the same key value for {'UserId', 'OtherEntityId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
The error message says, that at least one of your ManyToMany entries already exists in the database (there is an entry with the same UserId and OtherEntityId combination).
You can verify this, by running the following code directly after your filled the manyIds variable with the 3 IDs:
var user = GetUser();
var alreadyExistingManyToMany = context.ManyToMany
.Where(m => m.UserId == user.Id &&
manyIds.Contains(m.OtherEntityId))
.ToList();
Debug.Assert(alreadyExistingManyToMany.Count == 0);

Fluent nHibernate Unit Test HasOne mapping

I have situation where I DO require one-to-one relationship thus the usage of HasOne. Yes I do want to have separate table for Patrons and Members. Following are my classes and their corresponding mapping classes.
public class Member
{
public virtual string ID { get; set; }
public virtual bool IsRegistered { get; set; }
public virtual Patron Patron { get; set; }
public virtual string Reference1 { get; set; }
public virtual string Reference2 { get; set; }
}
public class Patron
{
public virtual string ID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Address { get; set; }
public virtual int Telephone1 { get; set; }
public virtual int Telephone2 { get; set; }
public virtual int Age { get; set; }
public virtual string Occupation { get; set; }
public virtual string Gender { get; set; }
public virtual string Room { get; set; }
}
public class PatronMap : ClassMap<Patron>
{
public PatronMap()
{
Id(x => x.ID);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Gender);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Occupation);
Map(x => x.Telephone1);
Map(x => x.Telephone2);
Map(x => x.Room);
Table("Patrons");
}
}
public class MemberMap : ClassMap<Member>
{
public MemberMap()
{
Id(x => x.ID);
HasOne(x => x.Patron).PropertyRef(x => x.ID).Constrained();
Map(x => x.Reference1);
Map(x => x.Reference2);
Map(x => x.IsRegistered);
Table("Members");
}
}
I also have a little test, as below, to see if the things are mapped correctly or not.
[TestMethod]
public void MemberMap_Create_Success()
{
new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
.CheckProperty(x => x.ID, "1")
//I'm not quite sure about the following mapping check. How do I do HasOne check here? :-(
.CheckReference(x => x.Patron, new Patron()
{
ID = "2",
FirstName = "Foo",
LastName = "Bar",
Gender = "M",
Age = 59,
Address = "City, Coutnry",
Telephone1 = 0123456789,
Telephone2 = 0987654321,
Occupation = "Learning Fluent nHibernate",
Room = "Somewhere"
})
.CheckProperty(x => x.Reference1, "Ref1")
.CheckProperty(x => x.Reference2, "Ref2")
.CheckProperty(x => x.IsRegistered, true)
.VerifyTheMappings();
}
I'm not sure how to test HasOne mapping property in this test. Any help is appreciated. Thanks in advance.
Following is what I did to implement HasOne mapping and unit test. Maps are as followed:
public class PatronMap : ClassMap<Patron>
{
public PatronMap()
{
Id(x => x.ID);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Gender);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Occupation);
Map(x => x.Telephone1);
Map(x => x.Telephone2);
Map(x => x.AshramRoom);
HasOne(x => x.Member).ForeignKey();
Table("Patrons");
}
}
public class MemberMap : ClassMap<Member>
{
public MemberMap()
{
Id(x => x.ID);
Map(x => x.IsRegistered);
Map(x => x.Reference1);
Map(x => x.Reference2);
References(x => x.Patron)
.Column("PatronID")
.Unique()
.UniqueKey("IDX_UniquePatronID");
Table("Members");
}
}
Unit test is as thus:
public void MemberMap_Create_Success()
{
new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
.CheckProperty(x => x.ID, "1")
.CheckReference(x => x.Patron, new Patron()
{
ID = "2",
FirstName = "Abc",
LastName = "Xyz",
Gender = "M",
Age = 99,
Address = "Address",
Telephone1 = 0000000001,
Telephone2 = 1000000000,
Occupation = "Occupation",
AshramRoom = "Room"
})
.CheckProperty(x => x.Reference1, "Ref1")
.CheckProperty(x => x.Reference2, "Ref2")
.CheckProperty(x => x.IsRegistered, true)
.VerifyTheMappings();
}
Hope this helps someone. :-)
I know it's been a while but if it helps.
You can use:
var patrol = new Patron()
{
ID = "2",
FirstName = "Foo",
LastName = "Bar",
Gender = "M",
Age = 59,
Address = "City, Coutnry",
Telephone1 = 0123456789,
Telephone2 = 0987654321,
Occupation = "Learning Fluent nHibernate",
Room = "Somewhere"
};
...
.CheckReference(x => x.Patron, patrol)
.CheckProperty(x => x.Reference1, patrol)
.CheckProperty(x => x.Reference2, patrol)
See: https://github.com/jagregory/fluent-nhibernate/wiki/Persistence-specification-testing

Failure in Multi Map Index with Spatial Index in RavenDB

I want to search for things that belong to users (near me). I have the following index defined, but I am not getting a merged resultset:
public class Things_ByLocation : AbstractMultiMapIndexCreationTask<Things_ByLocation.ThingsByLocationResult>
{
public class ThingsByLocationResult
{
public string ThingId { get; set; }
public string UserId { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public Things_ByLocation()
{
AddMap<Thing>(things => from t in things
select new
{
ThingId = t.Id,
UserId = (string)null,
Latitude = 0,
Longitude = 0,
_ = (object)null,
});
AddMap<User>(users => from u in users
select new
{
ThingId = (string)null,
UserId = u.Id,
Latitude = u.Latitude,
Longitude = u.Longitude,
_ = SpatialIndex.Generate(u.Latitude, u.Longitude)
});
Reduce = results => from result in results
group result by result.ThingId into g
let userId = g.Select(x => x.UserId).Where(t => !string.IsNullOrWhiteSpace(t)).FirstOrDefault()
let lat = g.Select(x => x.Latitude).Where(t => t != 0).FirstOrDefault()
let lng = g.Select(x => x.Longitude).Where(t => t != 0).FirstOrDefault()
select new
{
ThingId = g.Key,
UserId = userId,
Latitude = lat,
Longitude = lng,
_ = SpatialIndex.Generate(lat, lng)
};
Store(x => x.ThingId, FieldStorage.Yes);
Store(x => x.UserId, FieldStorage.Yes);
}
}
Result looks like this:
{
"ThingId": "Thing/Id26",
"UserId": null,
"Longitude": "0",
"__spatialShape": "0.000000 0.000000"
}
My models:
public class User
{
public string Id { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class Thing
{
public string Id { get; set; }
public double Name { get; set; }
public double Description { get; set; }
public string UserId { get; set; }
}
Any ideas as to what I am doing wrong? If I switch the group to the user, the user section gets populated and the ThingId is null. Hence, it appears that the merging process is failing. I'm just not sure why.
It is also very odd, as to why the result show the Longitude, but not the Latitude property.
Working with RavenDB Build 960 in RAM.
I realise I could denormalize the location into the Thing, but that would mean that if the user location changed I would have to update potentially hundreds of Things. Is that the preferred NoSql way to do this?
UPDATE
Based on Ayende's suggestion I now have the following:
public class Things_ByLocation : AbstractMultiMapIndexCreationTask<Things_ByLocation.ThingsByLocationResult>
{
public class ThingsByLocationResult
{
public string ThingId { get; set; }
public string UserId { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public Things_ByLocation()
{
AddMap<Thing>(things => from t in things
select new
{
ThingId = t.Id,
UserId = (string)null,
Latitude = 0,
Longitude = 0
});
AddMap<User>(users => from u in users
select new
{
ThingId = (string)null,
UserId = u.Id,
Latitude = u.Latitude,
Longitude = u.Longitude
});
Reduce = results => from result in results
group result by result.ThingId into g
select new
{
ThingId = g.Key,
UserId = g.Select(x => x.UserId).Where(x => x != null).FirstOrDefault(),
Latitude = g.Select(x => x.Latitude).Where(t => t != 0).FirstOrDefault(),
Longitude = g.Select(x => x.Longitude).Where(t => t != 0).FirstOrDefault()
};
Store(x => x.ThingId, FieldStorage.Yes);
Store(x => x.UserId, FieldStorage.Yes);
}
}
The index itself:
Map:
docs.Things
.Select(t => new {ThingId = t.__document_id, UserId = (String)null, Latitude = 0, Longitude = 0})
Map:
docs.Users
.Select(u => new {ThingId = (String)null, UserId = u.__document_id, Latitude = ((double)u.Latitude), Longitude = ((double)u.Longitude)})
Reduce:
results
.GroupBy(result => result.ThingId)
.Select(g => new {ThingId = g.Key, UserId = g
.Select(x => x.UserId)
.Where(x => x != null).FirstOrDefault(), Latitude = g
.Select(x => ((double)x.Latitude))
.Where(t => t != 0).FirstOrDefault(), Longitude = g
.Select(x => ((double)x.Longitude))
.Where(t => t != 0).FirstOrDefault()})
The resulting projection looks like this:
{
"ThingId": "Thing/Id26",
"UserId": null,
"Latitude": null,
"Longitude": null
}
I appear to be doing something academically wrong here.
Try:
Removing SpatialIndex.Generate from the map, it should only be on the reduce.
Removing SpatialIndex.Generate from the reduce and just seeing why you don't the Latitude & Longitude as you should.
Through a process of trial and error (and Ayende's suggestion), I ended up with the following. It works works, but I have no idea why!
I have a suspicion that I would be best changing the data model and denormalize the geo location data into the Thing.
public class Things_ByLocation : AbstractMultiMapIndexCreationTask<Things_ByLocation.ThingsByLocationResult>
{
public class ThingsByLocationResult
{
public string Id { get; set; }
public string UserId { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string SKU { get; set; }
public string Name { get; set; }
}
public Things_ByLocation()
{
AddMap<Thing>(things => from t in things
select new
{
Id = t.Id,
UserId = t.UserId,
Latitude = 0,
Longitude = 0,
Name = t.Name,
SKU = t.SKU,
_ = (object)null,
});
AddMap<User>(users => from u in users
select new
{
Id = (string)null,
UserId = u.Id,
Latitude = u.Latitude,
Longitude = u.Longitude,
Name = (string)null,
SKU = (string)null,
_ = (object)null,
});
Reduce = results => from result in results
group result by result.Id into g
let lat = g.Select(x => x.Latitude).Where(x => x != 0).FirstOrDefault()
let lng = g.Select(x => x.Longitude).Where(x => x != 0).FirstOrDefault()
let userId = g.Select(x => x.UserId).Where(x => x != null).FirstOrDefault()
let name = g.Select(x => x.Name).Where(x => x != null).FirstOrDefault()
let sku = g.Select(x => x.SKU).Where(x => x != null).FirstOrDefault()
select new
{
Id = g.Key,
UserId = userId,
Latitude = lat,
Longitude = lng,
Name = name,
SKU = sku,
_ = SpatialIndex.Generate(lat, lng)
};
Store(x => x.Id, FieldStorage.Yes);
Store(x => x.UserId, FieldStorage.Yes);
TransformResults = (database, results) => from result in results
let user = database.Load<User>(result.UserId)
select new
{
Id = result.Id,
UserId = result.UserId,
Latitude = user.Latitude,
Longitude = user.Longitude,
Name = result.Name,
SKU = result.SKU,
};
}
}

RavenDB MultiMapReduce Sum not returning the correct value

Sorry for this lengthy query, I decided to add the whole test so that it will be easier for even newbies to help me with this total brain-melt.
The using directives are:
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
Please leave feedback if I'm too lengthy, but what could possibly go wrong if I add a complete test?
[TestFixture]
public class ClicksByScoreAndCardTest
{
private IDocumentStore _documentStore;
[SetUp]
public void SetUp()
{
_documentStore = new EmbeddableDocumentStore {RunInMemory = true}.Initialize();
_documentStore.DatabaseCommands.DisableAllCaching();
IndexCreation.CreateIndexes(typeof (ClicksBySearchAndProductCode).Assembly, _documentStore);
}
[TearDown]
public void TearDown()
{
_documentStore.Dispose();
}
[Test]
public void ShouldCountTotalLeadsMatchingPreference()
{
var userFirst = new User {Id = "users/134"};
var userSecond = new User {Id = "users/135"};
var searchFirst = new Search(userFirst)
{
Id = "searches/24",
VisitId = "visits/63"
};
searchFirst.Result = new Result();
searchFirst.Result.Rows = new List<Row>(
new[]
{
new Row {ProductCode = "CreditCards/123", Score = 6},
new Row {ProductCode = "CreditCards/124", Score = 4}
});
var searchSecond = new Search(userSecond)
{
Id = "searches/25",
VisitId = "visits/64"
};
searchSecond.Result = new Result();
searchSecond.Result.Rows = new List<Row>(
new[]
{
new Row {ProductCode = "CreditCards/122", Score = 9},
new Row {ProductCode = "CreditCards/124", Score = 4}
});
var searches = new List<Search>
{
searchFirst,
searchSecond
};
var click = new Click
{
VisitId = "visits/64",
ProductCode = "CreditCards/122",
SearchId = "searches/25"
};
using (var session = _documentStore.OpenSession())
{
foreach (var search in searches)
{
session.Store(search);
}
session.Store(click);
session.SaveChanges();
}
IList<ClicksBySearchAndProductCode.MapReduceResult> clicksBySearchAndProductCode = null;
using (var session = _documentStore.OpenSession())
{
clicksBySearchAndProductCode = session.Query<ClicksBySearchAndProductCode.MapReduceResult>(ClicksBySearchAndProductCode.INDEX_NAME)
.Customize(x => x.WaitForNonStaleResults()).ToArray();
}
Assert.That(clicksBySearchAndProductCode.Count, Is.EqualTo(4));
var mapReduce = clicksBySearchAndProductCode
.First(x => x.SearchId.Equals("searches/25")
&& x.ProductCode.Equals("CreditCards/122"));
Assert.That(mapReduce.Clicks,
Is.EqualTo(1));
}
}
public class ClicksBySearchAndProductCode :
AbstractMultiMapIndexCreationTask
<ClicksBySearchAndProductCode.MapReduceResult>
{
public const string INDEX_NAME = "ClicksBySearchAndProductCode";
public override string IndexName
{
get { return INDEX_NAME; }
}
public class MapReduceResult
{
public string SearchId { get; set; }
public string ProductCode { get; set; }
public string Score { get; set; }
public int Clicks { get; set; }
}
public ClicksBySearchAndProductCode()
{
AddMap<Search>(
searches =>
from search in searches
from row in search.Result.Rows
select new
{
SearchId = search.Id,
ProductCode = row.ProductCode,
Score = row.Score.ToString(),
Clicks = 0
});
AddMap<Click>(
clicks =>
from click in clicks
select new
{
SearchId = click.SearchId,
ProductCode = click.ProductCode,
Score = (string)null,
Clicks = 1
});
Reduce =
results =>
from result in results
group result by
new { SearchId = result.SearchId, ProductCode = result.ProductCode }
into g
select
new
{
SearchId = g.Key.SearchId,
ProductCode = g.Key.ProductCode,
Score = g.First(x => x.Score != null).Score,
Clicks = g.Sum(x => x.Clicks)
};
}
}
public class User
{
public string Id { get; set; }
}
public class Search
{
public string Id { get; set; }
public string VisitId { get; set; }
public User User { get; set; }
private Result _result = new Result();
public Result Result
{
get { return _result; }
set { _result = value; }
}
public Search(User user)
{
User = user;
}
}
public class Result
{
private IList<Row> _rows = new List<Row>();
public IList<Row> Rows
{
get { return _rows; }
set { _rows = value; }
}
}
public class Row
{
public string ProductCode { get; set; }
public int Score { get; set; }
}
public class Click
{
public string VisitId { get; set; }
public string SearchId { get; set; }
public string ProductCode { get; set; }
}
My problem here is that I expect Count to be one in that specific test, but it just doesn't seem to add the Clicks in the Click map and the result is 0 clicks. I'm totally confused, and I'm sure that there is a really simple solution to my problem, but I just can't find it..
..hope there is a week-end warrior out there who can take me under his wings.
Yes, it was a brain-melt, for me non-trivial, but still. The proper reduce should look like this:
Reduce =
results =>
from result in results
group result by
new { SearchId = result.SearchId, ProductCode = result.ProductCode }
into g
select
new
{
SearchId = g.Key.SearchId,
ProductCode = g.Key.ProductCode,
Score = g.Select(x=>x.Score).FirstOrDefault(),
Clicks = g.Sum(x => x.Clicks)
};
Not all Maps had the Score set to a non-null-value, and therefore my original version had a problem with:
Score = g.First(x => x.Score != null).Score
Mental note, use:
Score = g.Select(x=>x.Score).FirstOrDefault()
Don't use:
Score = g.First(x => x.Score != null).Score

MultiMap / Reduce - Counts = 0?

I want to create an index for a query, I want to return to my view a list of Audio items along with statistics for these items, which are TotalDownloads & TotalPlays.
Here are my relevant docs:
Audio
- Id
- ArtistName
- Name
AudioCounter
- AudioId
- Type
- DateTime
Here is my current Index:
public class AudioWithCounters : AbstractMultiMapIndexCreationTask<AudioWithCounters.AudioViewModel>
{
public class AudioViewModel
{
public string Id { get; set; }
public string ArtistName { get; set; }
public string Name { get; set; }
public int TotalDownloads { get; set; }
public int TotalPlays { get; set; }
}
public AudioWithCounters()
{
AddMap<Audio>(audios => from audio in audios
select new
{
Id = audio.Id,
ArtistName = audio.ArtistName,
Name = audio.Name,
TotalDownloads = 0,
TotalPlays = 0
});
AddMap<AudioCounter>(counters => from counter in counters
where counter.Type == Core.Enums.Audio.AudioCounterType.Download
select new
{
Id = counter.AudioId,
ArtistName = (string)null,
Name = (string)null,
TotalDownloads = 1,
TotalPlays = 0
});
AddMap<AudioCounter>(counters => from counter in counters
where counter.Type == Core.Enums.Audio.AudioCounterType.Download
select new
{
Id = counter.AudioId,
ArtistName = (string)null,
Name = (string)null,
TotalDownloads = 0,
TotalPlays = 1
});
Reduce = results => from result in results
group result by result.Id
into g
select new
{
Id = g.Key,
ArtistName = g.Select(x => x.ArtistName).Where(x => x != null).First(),
Name = g.Select(x => x.Name).Where(x => x != null).First(),
TotalDownloads = g.Sum(x => x.TotalDownloads),
TotalPlays = g.Sum(x => x.TotalPlays)
};
}
}
However, my TotalDownloads & TotalPlays are always 0 even though there should be data in there. What am I doing wrong?
In the reduce function, replace .First() with .FirstOrDefault(), then it works.
Besides that, there is a typo in the second map-function, because you are filtering on the same AudioCounterType.Download.