Does RavenDb's `Include` support constructing a document id, not just selecting one? - ravendb

This code here is not working as expected. Specifically, it seems the calls to Include are not causing any extra documents to be added to the session. Thus, the Loads are each costing a full db hit. This is RavenDb 3.5.
I couldn't find examples that actually use Include this way. They all seem to emit a "selector" that is used to find a member on the document, and that member holds the literal document id to include. Whereas I am constructing the document id and returning it as a string.
session.Query<Coil>()
.Include(c => nameof(CoilState) + "/" + id)
.Include(c => nameof(CoilExt) + "/" + id)
.Include(c => nameof(Material) + "/" + c.MaterialCode)
.Where(c => c.CoilId == id)
.ToList()
.Select(c =>
Coil_Dto.ToCoilDto(
c
, session.Load<CoilState>(nameof(CoilState) + "/" + c.CoilId)
, session.Load<CoilExt>(nameof(CoilExt) + "/" + c.CoilId)
, session.Load<Material>(nameof(Material) + "/" + c.MaterialCode)
)
)
.SingleOrDefault()
My question is: can Include be used in this way? If not, is there some way to achieve that "include" functionality using my constructed ids?

It sounds like you want to use Lazy, rather than Include.
Include
The .Include method should be passed the name of a property on your object(s) which contains an ID.
// Good:
// Note that StateId, ExtId, and MaterialId are all properties on our Coil class.
session.Query<Coil>()
.Include(c => c.StateId)
.Include(c => c.ExtId)
.Inclide(c => c.MaterialId);
// Bad:
session.Query<Coil>
.Include(c => "CoilStates/123")
.Include(c => "CoilExts/456")
.Include(c => "Materials/789")
In short, make sure your .Include calls are passed the name of properties on the Coil class. Those properties should contain string values which are IDs of other documents. Then they'll all be loaded in a single trip to the database.
Lazy
If Include doesn't make sense for your scenario, but you still want to load disparate objects in a single DB call, use the lazy API
// Lazily load the coil. No DB trip yet.
var lazyCoil = session.Query<Coil>()
.Where(...)
.Lazily();
// Lazily load a CoilState. No DB trip yet.
var lazyCoilState = session.Advanced.Lazily.Load<CoilState>("CoilStates/123");
// Lazily load a Material. Still no DB trip.
var lazyMaterial = session.Advanced.Lazily.Load<Material>("Materials/456");
// Grab one of the values. This will fetch all lazy loaded items in 1 trip.
var coil = lazyCoil.Value;
// Grab the other values. No DB trip needed; they're already loaded!
var coilState = lazyCoilState.Value;
var material = lazyMaterial.Value;

Related

Latest modified row for each item

I have a sql table containing multiple rows with a memberid and lastmodified date. I need to get latest modified row for each member id. This is what I have tried in EFCore 3.1.1:
var a = context.Members
.Include(m => m.Histories.OrderByDescending(h => h.LastModifiedDate)
.FirstOrDefault());
and it gives error: Lambda expression used inside Include is not valid
What am I missing?
UPDATE:
I tried this as well that didn't work either:
var a = context.Histories
.GroupBy(h => h.MemberId)
.Select(g => g.OrderByDescending(p => p.LastModifiedDate).FirstOrDefault()).ToList();
The LINQ expression '(GroupByShaperExpression:
KeySelector: (o.MemberId),
ElementSelector:(EntityShaperExpression:
EntityType: History
ValueBufferExpression:
(ProjectionBindingExpression: EmptyProjectionMember)
IsNullable: False
)
)
.OrderByDescending(p => p.LastModifiedDate)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
While newer versions of EF Core do support some filtering via adding Where clauses, it's best to think of entities as a "complete" or "complete-able" representation of your data state. You either use the complete state of data, or you project to get the details you want.
For example, if you just want the last modified date for each member:
var lastModifiedDetails = context.Members
.Select(m => new
{
m.MemberId,
LastModifiedDate = m.Histories.OrderByDescending(h => h.LastModifiedDate)
.Select(h => h.LastModifiedDate)
.FirstOrDefault()
}).ToList();
This would give you a collection containing the MemberId and it's LastModifiedDate.
Alternatively, if you want the complete member and a quick reference to the last modified date:
var memberDetails = context.Members
.Select(m => new
{
Member = m,
LastModifiedHistory = m.Histories.OrderByDescending(h => h.LastModifiedDate)
.FirstOrDefault()
}).ToList();
Here instead of trying to get the last modified date or the latest history through a Member entity, you get a set of anonymous types that project down that detail. You iterate through that collection and can get the applicable history and/or modified date for the associated Member.

Changes b/w ElasticSearch 1.x and 2.x

Does documentation exist on how to change code written in NEST 1.x to 2.x?
I've looked at these sites and they're incomplete:
https://github.com/elastic/elasticsearch-net/blob/master/docs/2.0-breaking-changes/nest-breaking-changes.md
https://github.com/elastic/elasticsearch-net
https://www.elastic.co/blog/ga-release-of-nest-2-0-our-dot-net-client-for-elasticsearch
For example I'd like to know how to replace the following:
1)
given ISearchResponse<T> searchResults = ...
How to do:
searchResults.ConnectionStatus
searchResults.RequestInformation.Request
2)
client.Get<T>(s => s.Id(id));
3)
Given QueryContainer query
new SearchDescriptor<T>()
.From(from)
.Size(pageSize)
.Query(query); //this dosen't work anymore
4)
MatchQuery doesn't accept fuziness as double and type parameters as string as it used to
5) QueryDescriptor seems gone gasp
6) client.Update is busted
var result = client.Update<CustomerProfile>(request => request
.Id(customer.CustomerId)
.Doc(customer)
.Refresh()
);
7) client.Get is busted in a similar way to client.Update
8) In Mappings the following setup doesn't work anymore
CreateIndexDescriptor cid = ...
cid.NumberOfReplicas(numReplicas)
.NumberOfShards(numShards)
.Settings(s => s
.Add("merge.policy.merge_factor", "10")
.Add("search.slowlog.threshold.fetch.warn", "1s")
)
.Analysis(a => a.TokenFilters etc etc
EDIT
9) Date Ranges:
startDate and endDate are DateTime type
var qd = new QueryContainerDescriptor<EsActivity>();
QueryContainer qc = qd.Range(r =>
r.Field("esactivity.timestamp")
.GreaterThanOrEquals(DateMath.Anchored(startDate))
.LessThanOrEquals(DateMath.Anchored(endDate))
);
.GreaterThanOrEquals expects a double parameter but on the documentation page it takes DateMath.Anchored(startDate)
10) Highlighting:
highlightFields: List<string>
Action<HighlightFieldDescriptor<T>> [] tmp = highlightFields.Select(field =>
new Action<HighlightFieldDescriptor<T>>(
highlighter => highlighter.Field(field)
)
).ToArray();
sd:SearchDescriptor<..>..
sd.Highlight(h => h
.PreTags(preTag)
.PostTags(postTag)
.OnFields(tmp)
);
I see I can replace OnFields(tmp) with .Fields(f=>f.OnAll()) but I'd still like to specify the fields myself in some way.
And how come there is a HighlightQuery option available since we already apply highlighting on a query object.. now there are 2 query calls.
I've converted the highlighting above to
var tmp = highlightFields.Select(field =>
Tuple.Create<Field, IHighlightField>(
Field.Create(field),
new HighlightField()
)
).ToDictionary(x => x.Item1, x => x.Item2);
sd.Highlight(h => new Highlight
{
PreTags = new[] { preTag },
PostTags = new[] { postTag },
Fields = tmp
}
);
1) searchResults.ApiCall replaces searchResults .ConnectionStatus.
You can get the request bytes with searchResults.ApiCall.RequestBodyInBytes and you will also need to set .DisableDirectStreaming() on ConnectionSettings in order to capture the bytes as the request is written to the request stream directly by default.
2) Use client.Get<T>(id) - The first parameter is a DocumentPath<T> type.
3) To pass a QueryContainer to a Fluent API descriptor, just return it from the Func<QueryContainerDescriptor<T>, QueryContainer>
new SearchDescriptor<T>()
.From(from)
.Size(pageSize)
.Query(_ => query);
4) match query fuzziness as a double mapped to a formula to calculate edit distance in Elasticsearch 1.x. Since this was removed in Elasticsearch 2.x, it is also gone from NEST. You can set fuzziness edit distance with
client.Search<Document>(s => s
.Query(q => q
.Match(m => m
.Query("this is my query")
.Fuzziness(Fuzziness.EditDistance(3))
)
)
);
Not sure what you're referring to with type, but I think you're referring to document type? If that's the case, document type takes a Types type which string implicitly converts to
client.Search<Document>(s => s
.Type("other-type")
.MatchAll()
);
5) QueryDescriptor<T> was renamed to QueryContainerDescriptor<T> to better reflect the fact that it's a descriptor for building a QueryContainer
6) Update API works
// specifying id
client.Update<Document>("document-id", u => u
.Doc(document)
.Refresh()
);
Since the first parameter is a DocumentPath<T>, the document instance (if you have it) can be passed as the first parameter
client.Update<Document>(document, u => u
.Doc(document)
.Refresh()
);
where index, type and id will be inferred from the document instance
7) See above
8) Create index settings have been revised to reflect the level at which the settings appear in the REST API json call
client.CreateIndex("index-name", c => c
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(2)
.SlowLog(sl => sl
.Search(sls => sls
.Fetch(slsf => slsf
.ThresholdWarn("1s")
)
)
)
.Analysis(a => a) // etc...
)
);
You can also use strings for settings if you prefer, although the fluent API will ensure the correct setting values are sent e.g. "search.slowlog.threshold.fetch.warn" is now "index.search.slowlog.threshold.fetch.warn"
client.CreateIndex("index-name", c => c
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(2)
.Setting("index.search.slowlog.threshold.fetch.warn", "1s")
.Analysis(a => a) // etc...
)
);
merge.policy.merge_factor is removed in Elasticsearch 2.0

QueryOver OrderBy child property using strings

I'm struggling with using QueryOver.OrderBy with strings for property names on child entities. e.g. the following works but I am hardcoding the OrderBy field.
Customer custAlias = null;
session.QueryOver<Campaign>()
.JoinAlias(x => x.Customer, () => custAlias)
.OrderBy(() => custAlias.Name).Desc() // want to use string property name
.List();
I can specify the OrderBy using a string with something like:
.OrderBy(Projections.Property("DOB")).Desc();
But this is looking for "DOB" on the Campaign entity, not the child Customer entity. Is it possible to retrieve the alias used by NH and then set the path to the property e.g.
.OrderBy(Projections.Property("cust.DOB")).Desc(); // where "cust" is the alias
Any ideas?
The alias used is the name of the variable. So
Projections.Property("custAlias.DOB")
(can't test now, but if I remember corretly it works)
Interestingly, it isn't the variable, in itself, that is used as the alias, but its name. What does it means?
QueryOver<Campaign> query;
{
Customer custAlias = null;
query = session.QueryOver<Campaign>()
.JoinAlias(x => x.Customer, () => custAlias)
}
{
Customer custAlias = null;
var result = query.OrderBy(() => custAlias.Name).Desc() // want to use string property name
.List()
}
Two different custAlias, but it still works :-)
(useful if you want to split pieces of a query in multiple methods... The only important thing is that they use the same naming for the aliases)

NHibernate Multiquery for eager loading without joins

Is it possible to use a multiquery and have two hql queries returning two different sets of entities where one of the sets are used in the other and that the session "fixes" this via the first level cache?
E.g. scenario (a dumb one and it could be solved with joins)
public class Room
{
...
public virtual ISet<Bookings> Bookings {get;set;}
public virtual bool IsAvailible {get;set;}
...
}
public class Booking
{
...
}
After executing a multicriteria with two hql's:
returning all rooms where
IsAvailible = true
returning all bookings having a room that has a room that IsAvailible
when accessing a room from the result and its bookings I want them to be resolved from the second resultset via the firstlevel cache of the session and there by avoiding n+1.
Generally speaking, NHibernate can use the cache to "combine" the results from queries executed through Multiquery. However, it should be noted that this usually only applies to cases where lazy collections are loaded with no restrictions whatsoever.
Examples:
Invoice iAlias = null;
InvoiceDetails idAlias = null;
// Base-Query: get Invoices with certain condition
var invoices = session.QueryOver<Invoice>()
.Where(i => i.Number == "001")
.Future<Invoice>();
// Option 1: this will still cause N+1 if we iterate through invoices,
// because it doesn't know better
var invoicedetails = session.QueryOver<InvoiceDetails>()
.JoinAlias(a => a.Invoice, () => iAlias)
.Where(() => iAlias.Number == "001")
.Future<InvoiceDetails>();
// Option 2: this will still cause N+1 if we iterate through invoices,
// because we limited the possible results using a where-condition
var invoices2 = session.QueryOver<Invoice>()
.Left.JoinAlias(i => i.Details, () => idAlias)
.Where(i => i.Number == "001")
.And(() => idAlias.Quantity > 5)
.Future<Invoice>();
// Option 3: this will work without N+1, because we don't use a filter
// -> NHibernate will use the collection in cache
var invoices3 = session.QueryOver<Invoice>()
.Left.JoinAlias(i => i.Details, () => idAlias)
.Where(i => i.Number == "001")
.Future<Invoice>();
foreach (Invoice i in invoices)
{
int count = i.Details.Count;
}
If we comment out two of the three options and execute the code, we will see that only option 3 will prevent a N+1, the other two will still load the InvoiceDetails for each Invoice in the loop.
Of course this is a very simple example and it is obvious that Option 3 could also be executed without the Base-query and still return the same result, but I hope you get the idea.
In the case where we load two different sets of entities, i.e. the root class is different as in Option 1, this "combining" will most likely not work.
Sorry, if I used QueryOver instead of HQL, but the same rules apply.
Gyus, keep in mind that sometimes you can have similar problems because of
LeftOuterJoin is not set.
.JoinAlias(x => x.Prop, () => propAlias, JoinType.LeftOuterJoin)

zend retriving tag list

I have some problem with zend. Here it is. I'm going to make some kind of articles db, which containt some info. Every article is marked with 1 or more tags (like WordPress).
I have a controller (let it be index) and action (also index).
All I need is to get articles and tags, associated with it, when user goes to site/index/index.
I have 3 tables:
articles(idarticles, title..)
tags(idtags, title)
tagList(idarticles, idtags).
How can I read tags, associated with article?
Zend's MVC doesn't actually include a model, however, the quickstart guide outlines creating a model.
The simplest way (not necessarily the best way), is to setup the connection in your application.ini, or setup the adapter like this (see the Zend_Db_Adapter docs):
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
Then use SQL to select your data.
//all articles
$articles = $db->query('SELECT * FROM articles');
//a article's tags
$tags = $db->query('SELECT * FROM tagList JOIN tags ON
(tagList.idtag = tags.idtags) WHERE idarticles = ?', $idarticles);
This is also taged for Zend_Db_Table, to use that to access the data, first setup a default adapter (or again, use application.ini):
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Then get objects for you tables like this:
$ariclesTable = new Zend_Db_Table('articles');
To get all the articles:
$articles = $articlesTable->fetchAll();
To get an article's tags (little more complex here, using a Zend_Db_Table_Select as recommended):
$select = $tagsTable->select();
//3rd argument must be empty array, so no joined columns are selected
$select->join('tagList', 'tagList.idtag = tags.idtags', array());
$select->where('tagList.idarticles = ?', $idarticles);
$tags = tagsTable->fetchAll($select);