Hi,
I have a fanout index and the resulting documents have an Id property. Now, I assign the inner list property.Id to the Result.Id, but it overwrites the value with the main document Id.
Below is the simulation of my issue:
Do you have any constraints on the fanout index result like the property should not be named Id?
Documents:
public class SimpleTestDocument
{
public string Id { get; set; }
public string Comment { get; set; }
}
public class Level2SimpleTestDocument
{
private readonly List<SimpleTestDocument> _simpleTestDocumentList = new List<SimpleTestDocument>();
public string Id { get; set; }
public IEnumerable<SimpleTestDocument> SimpleTestDocumentList => this._simpleTestDocumentList.AsEnumerable();
public void AddSimpleTestDocument(SimpleTestDocument doc)
{
if (doc != null)
{
this._simpleTestDocumentList.Add(doc);
}
}
}
Index:
public class FanOutIdTestIndex : AbstractIndexCreationTask<Level2SimpleTestDocument>
{
public FanOutIdTestIndex()
{
this.Map = documents => from doc in documents
from innerDoc in doc.SimpleTestDocumentList
select new Result
{
Id = innerDoc.Id,
InnerDocId = innerDoc.Id,
MainDocId = doc.Id
};
this.StoreAllFields(FieldStorage.Yes);
}
public class Result
{
public string Id { get; set; }
public string InnerDocId { get; set; }
public string MainDocId { get; set; }
}
}
Test:
[Fact]
public void GivenAFanOutIndex_WhenIdPropertyInIndexResultIsAssignedAValue_ObjectReturnedByQueryShouldHaveAssignedValue()
{
using (var session = this._dbStore.OpenSession())
{
var testDoc = new Level2SimpleTestDocument
{
Id = session.GetDocumentId(typeof(Level2SimpleTestDocument), 123),
};
testDoc.AddSimpleTestDocument(new SimpleTestDocument { Id = "SimpleTestDocuments/123" });
testDoc.AddSimpleTestDocument(new SimpleTestDocument { Id = "SimpleTestDocuments/124" });
session.Store(testDoc);
session.SaveChanges();
}
using (var session = this._dbStore.OpenSession())
{
var query = session.Query<FanOutIdTestIndex.Result, FanOutIdTestIndex>()
.ProjectInto<FanOutIdTestIndex.Result>().ToList();
query.ForEach(d => Assert.Equal(d.InnerDocId, d.Id));
}
}
Sorry for the trouble, but I have a couple of indexes where I have Id in the index result. Also, I think this was not an issue in RavenDB 3.0.
Thanks,
Lokesh