I have a requirement to get given month data from the datestart field.
public DateTime? datestart { get; set; }
I tried following code. But it did not work. Return no result.
string givenMonth = "5"; //May
thisMonthQuery = Query<ProjectModel>.Match(
q => q.Field(f => f.datestart.Value.Month.ToString()).Query(givenMonth));
You cannot use Match query to search month in a date field. Date field is not split into different tokens like a text field so you cannot search on month.
You need to use a script query for this
.Script(sn => sn
.Inline("doc['datestart'].value.monthOfYear==param1")
.Params(p => p.Add("param1", 5))
)
You can also create a sub field of type text and use match query, but then your input can also match date part!.
Related
I have situation like this: in LearningStatuses table I have a composite primary key, made up of CourseCode and UserID.
CourseCode and UserID are also the PK of Course and ApplicationUser tables respectively.
The LearningStatuses table is basically working as a enroll table.
So if a user enroll in a course, his user ID and Course Code will be stored in this table.
Now I want to fetch all the enrolled courses of a particular user in his dashboard.
Firstly I'm looking for the courseCode that is there in the learningSatatuses table with his user ID, and storing it in a list of int.
Now I want the list of courses from the database, of those IDs as an IEnumerable to iterate over them in the view.
public ActionResult Dashboard()
{
string currentUserID = User.Identity.GetUserId();
List<int> CoursesEnrolled = db.LearningStatuses
.Where(l => l.UserID == currentUserID)
.Select(l => l.CourseCode)
.ToList();
List<Course> CoursesEnrolledCourse = new List<Course>();
foreach (int item in CoursesEnrolled)
{
Course c = (Course)db.Courses.Where(c => c.CourseCode == item);
CoursesEnrolledCourse.Add(c);
}
return View();
}
During the 2nd step, I find myself unable to cast this IQueryable to a list anymore.
I've tried few other approaches too. but at the end of it I'm not able to get the desired IEnumerable of course objects for a particular user.
N.B: I have an Identity Framework user table. So my UserID is an alias of ApplicationUserID, that's a string.
What is the right approach to get the expected result.
Assuming a link is created and is pointing to a URL with the following pattern:
/00O70000001SOsa?pv0={!Account.Id}&pv1={!Case.IsClosed}
How can this be written in an Apex controller if I have to invoke the same thing using pagereference or any other similar API.
// You will have to apply your own logic on how you pick data for the link
Account acc = [SELECT Id FROM Account LIMIT 1];
Case c = [SELECT IsClosed FROM Case LIMIT 1];
Pagereference pr = new PageReference('/00O70000001SOsa');
pr.getParameters().putAll(new Map<String, String>{
'pv0' => acc.Id,
'pv1' => c.isClosed ? 'true' : 'false'
});
System.debug(pr.getUrl());
// outputs something like /00O70000001SOsa?pv0=0017000001TSmKmAAL&pv1=false
Parameters you pass to the URL have to be Strings (or something that easily casts to String). So it's your job to convert booleans, display dates in format that the other page will "like" (reports need dates in the same format the user would use so according to his/her locale, look at format method in Date and DateTime class).
But that's it. You don't have to worry about "?" sign or any fancy escaping of special characters, it'll be done for you:
Pagereference pr = new PageReference('/home/home.jsp');
pr.getParameters().putAll(new Map<String, String>{
'spaces' => 'spa ce',
'html-entities' => '& < >',
'percents' => '1 / 4 = 25%'
});
System.debug(pr.getUrl());
// /home/home.jsp?html-entities=%26+%3C+%3E&percents=1+%2F+4+%3D+25%25&spaces=spa+ce
Let's say I have Post and a Collection of Comments,
public class Post {
String title;
List<Comment> comments;
}
public class Comment {
Date date;
String author;
String comment;
}
I would like to be able to know for a certain post title what is the most recent comment in a specific date range. The result show be presented as a projection with the following structure:
public class Result {
String postTitle;
Date commentDate
String commentAuthor;
String comment;
}
I'm struggling to get it work, I tried a few approaches, but could not get it right. I have an Index for this but I'm not quite sure how I can get only the last entry of the child element.
I'm getting all the records in the date range and not only the last record.
This is my index:
public Posts_LastCommentDateRange() {
map = "docs.Posts.SelectMany(post => post.comments, (post, comment) => new {" +
" post.title," +
" commentDate = comment.date," +
" commentAuthor = comment.author," +
" comment.comment" +
"})";
}
This is my query:
List<Result> res = session.query( Result.class, Posts_LastCommentDateRange.class )
.whereEquals( "title", "RavenDB Date Range" )
.whereBetween( "commentDate", "2019-01-02T10:27:18.7970000Z", "2019-01-25T15:01:23.8750000Z" )
.selectFields( Result.class )
.toList();
Any help or direction would be much appreciated.
Thanks
Rather than storing one result for every post + comment, you could use the index to only output the posts latest comment with the linq Max method.
map = docs.Posts.Select(post =>
{
var latestComment = post.comments.Max(a => a.date);
return new {
title = post.title,
commentDate = latestComment.date,
commentAuthor = latestComment.author,
comment = latestComment.comment
};
});
So your index is basically iterating over your posts and outputting a record that holds only the latest comment. Then your query won't have to check over the comments that are definitely not the latest.
Hi I am working on ELK stack. I have a date in the form of a string like below:
"23/Nov/2017:02:35:02 +0000"
Now I want to use scripted fields in kibana to convert the string date time to a date field.
Anyone can help me with what to put in the script? or How can I go about it?
So for your case lets say the date is in the field { logdate:"23/Nov/2017:02:35:02 +0000" }
In order to convert the logdate(string) to a timestamp value, we can use a Logstash Date filter
In our case, the date filter should look something like below, this filter will parse the date time and save it to the #timestamp filed which is default if you want to save it to a particular field used the target setting
filter {
date {
match => [ "logdate", "dd/MMM/yyyy:HH:mm:ss Z" ]
}
}
Let me know if the filter works for you
In Plone 4, I am using the CalendarWidget. Is it possible to display only the year in a custom view for a datetime field? Some records that I need to input do not have the month and date but the year has been provided.
Is there a way to limit the widget to allow only the year but ignore the date and month fields unless they are provided?
Currently my code is as follows:
atapi.DateTimeField('item_publication_date',
required=True,
searchable = True,
validators = ('isValidDate'),
widget = atapi.CalendarWidget(
show_hm=False,
label=_(u'Publication Date'),
starting_year=1970,
format='%Y',
description=_(u"Item Publication Date"),
),
),
You can't. In Zope, DateTime objects must have a month and day to work at all, and what would they be set to if you only supply a year?
You can, however, use a simple StringField with a dynamic vocabulary to let users pick a year from a drop-down list, then use a custom method on your content class to return a DateTime object for getItem_publication_date() where you take the year for the return value from that field.