how to get date and compare it while indexing - solr

I want to make a bust for latest data while indexing my content. For example
Content from now to 6 months past should have boost = 10
and Content from 6 month past to 12 month past should have boost = 5
Older content should have boost = 0
my date is saved as timestamp so the only problem is to get current date while indexing
I can get date of content from row that is a param in my function but I don't know how to get current date and compare it. should it be something like this ?
and one more question
is there some way to check the boost ? I mean can i monitor what is boosted how ? Cause using result list with couple thousands articles is hard to mesure
// EDIT ANSWER
GOT IT
The script should look like this (this is for one 1 year past and older
<script>
<![CDATA[
function s1(row) {
var curTime = parseInt(new Date().getTime()/1000);
var itemDate = row.get('publication_date');
if(itemDate >= (curTime - 31104000)) {
row.put('$docBoost', 40);
} else {
row.put('$docBoost', 20);
}
return row;
}
]]>
</script>

I would recommend using query/search time boosting instead for your use case. The main advantage is that you don't need to reindex your documents periodically to adjust boosts. See Date Boosting and "How can I boost the score of newer documents" wiki tips

Related

How should I dynamically query the data per month?

I'll be using reactjs and chartjs to create this. Though I do not have the codes yet, I would only like to ask for advice or to have a better understanding before coding it. In firestore, I already have this data:
I wanted to dynamically query it per vaccine and per month. What I somewhat thought of to do was to have a selection for the user to choose what year and what type of vaccine. I kind of thought of these codes however, dynamically querying it per month was quite difficult:
the selectedVaccine - is the variabale for the select where users can choose what type of vaccine.
db.collection("users") .where("doses.selectedVaccine","==", selectedVaccine) .where("doses.firstDose","==", true)
Any advice would be appreciated about querying it per month. Thank you.
db.collection("users")
.where("doses.selectedVaccine","==", selectedVaccine)
.where("doses.firstDose","==", true)
.where("doses.firstDose", ">=" start)
.where("doses.firstDose", "<" end);
// change doses.firstDose to whichever date you want to use.
For any start and end date (e.g 1st Jan 2020 and 31st Dec 2020), the above query should return all first doses within that time frame.
Then you can loop through the results and place them in arrays depending on their months. Array of all Januaries first doses in position 0, Array of all Februaries first doses in position 1 etc.
This will involve downloading the full data of all doses for a year. If your intention is just to plot a chart, then consider this solution

Find the end of current month with Velocity

I've just started to use Marketo's mail scripting,
and I need to find out the last of the current month.
I could find the current date as below.How can I do further?
#set($date = $date.calendar)
#set($current_date = $date.format('yyyy-mm-dd', $date.getTime()))
$current_date
Refer to here,
Subtract months from date in velocity
I tried to subtract 1 day from the beginning of next month, but it doesn't work.
Knowing the number of days in this month is also meets the requirement.
The method you tried should work. I think that your problem is that you are overwriting $date - which initially contains the DateTool - with your working variable. When Velocity Tools org.apache.velocity.tools.userCanOverwriteTools configuration value is true (which is the default) Velocity will let you overwrite $date but the DateTool will be unavailable thereafter.
So try changing your working variable to $cal for instance. Then, you have several methods:
#set($cal = $date.calendar)
$cal.add(2, 1)
$cal.set(5, 1)
$cal.add(5, -1)
$date.format('yyyy-MM-dd', $cal)
or
#set($cal = $date.calendar)
$cal.set(5, $cal.getActualMaximum(5))
$date.format('yyyy-MM-dd', $cal)
In all cases, you resort to using the Calendar.MONTH and Calendar.DATE constants (respectively 2 and 5). You may want to put such utility operations in a Java tool of your own to have more readable templates.

How to display data in area chart starting from the year chosen on a slicer and get all following years in Power BI

When choose a single Year on slicer I want area chart display all data from that chosen year and till the end (all years I have in my datasource).
But instead it just displays me the data for single year choosing on a slicer.
So I have this:
But I want it look like this: whatever Year I choose in slicer - chart will show all data starting from 2014 and goes till 2017.
I am simply following a PowerBI template example and it seems like it's possible to do that:
https://app.powerbi.com/view?r=eyJrIjoiMjc2NzExODItMjNhYy00ZWMxLWI2NGItYjFiNWMzYzUzMzhlIiwidCI6IjU3NGMzZTU2LTQ5MjQtNDAwNC1hZDFhLWQ4NDI3ZTdkYjI0MSIsImMiOjZ9
This is doable but it requires some tricks and extra measures.
TL;DR: The slicer you see is actually served as a value picker, not as a filter. An extra measure based on the value is created and used as visual level filter for the visual to do the trick.
If you want to follow along, you can download the .pbix file from this Microsoft edX course about Power BI.
First, create a new table based on the existing Date table, with only distinct years:
Year = DISTINCT('Date'[Year])
Then, create a slicer with the Year column from the newly created Year table (NOT the Date table).
A measure (used as flag) is created as follows:
Flag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) >= YearSelected, 1, 0)
So basically it gets the year selected from the year slicer and compare it with the year value in the date table to see if it's greater than or equal to it.
The chart is created with Year column from the Date table (NOT the Year table), and other needed measures. Flag is added to the Visual level filters and set to 1.
So the Flag value will change according to the value picked in the Year slicer, and served as the actual filter to the chart displayed.
Results:
EDIT: on more use cases
#Oleg Try to think of how you can apply the Flag concept further. For example, if you want another chart displaying data of the same year as the slicer, you can set up another flag called SameYearFlag and only change the part of value comparison to =. Add it to the chart Visual level filter and it'll show only data in the same year. Yes, by extension, that means you can have another flags like LastYearFlag, NextYearFlag, etc, as long as it makes sense to you. The use case is up to you.
LastYearFlag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(YearSelected - VALUES('Date'[Year]) = 1, 1, 0)
NextYearFlag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) - YearSelected = 1, 1, 0)
SameYearFlag =
VAR YearSelected = FIRSTNONBLANK(VALUES('Year'[Year]), 0)
RETURN
IF(VALUES('Date'[Year]) = YearSelected, 1, 0)
Examples:
By having only one year slicer, I can have charts with data in the same year, last year, next year and all years following, by applying different flags to them.
As said, it's up to you to come up with more interesting use cases!
I would propose to consider the new numeric range slicer.
You can just set it as "Greater than or equal to". Users can select then the initial year in the range by entering the number or dragging the slicer.
You would need to enable this feature in Power Bi Desktop, Options under "Preview features".
Is well presented in the documentation https://powerbi.microsoft.com/en-us/documentation/powerbi-desktop-slicer-numeric-range/
This is how it could look like:

ValueFilter for DateTime Attributes

I'm working with the Blog app and I see how to filter the Blog posts by year using the Visual Query Designer. I use the querystring value that has the year and in the ValueFilter and my properties are as follows:
Attribute: PublicationMoment
Value: [QueryString:year]-01-01 and [QueryString:year]-12-31
Operation: between
How would I get the posts from a specific month and year, if those values are passed via query string parameters. Because the months of the year have a varying number of days, I'm not sure how you would accomplish this in the Value field of the ValueFilter. Currently I'm passing the 2 digit month as the parameter.
I tried something like: [QueryString:year]-[Querystring:month]
Operation: contains
but the above operation doesn't really work because the datatype is a DateTime object.
I could do it in the razor view but I'm afraid that the paging datasource would have too many pages in it since it would be based on the larger subset of posts for the given year that was passed in the querystring parameter.
Is there any way to do this with the filter?
Basically dates are not perfectly handled yet, but there are a few ways to do it using the visual query:
Use the correct date in the query like between [QueryString:Start] and [QueryString:End] and calculate the correct dates there where you generate the links
Since your main problem with the "between" filter is actually that it would include the last day too, you could also use a two filters a >= first date and another < second date, so the first-date would be the year/month and day 1; the second one is year-month and day 1 as well
Last but not least: if you do it with razor and LINQ you shouldn't run into any performance issues - it's technically the same thing the pipeline does and it's been tested to perform well with tens of thousands of records.

Add efficiently 1 month to current date in CakePHP 2.x

What is the best way to add an expiry date in CakePHP?
I've got an "expiry date" column in my database, I want to add 1 month to the current date, and store this. At the moment, I'm just using strings and plain PHP date functions to create a new date string to save:
$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +31 days");
$this->data['Access']['expires'] = $date;
Is there a "more CakePHP" way or efficient/performance wise?
Performance wise we're most probably talking about micro optimizations at best, so that probably shouldn't be your main concern. However, all the back and forth that you're doing there doesn't make much sense, a single call to strtotime() is all you need, ie
$this->data['Access']['expires'] = strtotime('+31 days');
This will result in a timestamp of "now + 31 days".
The Cake-ish way would be to use the CakeTime class, for example CakeTime::fromString(), like
App::uses('CakeTime', 'Utility');
$this->data['Access']['expires'] = CakeTime::fromString('+31 days');
When passing a string (it also accepts integers and \DateTime instances), this is basically just a wrapper for strtotime(), and an additional mechanism that incorporates the timezone set for your application, so while this will be a little bit slower of course, it might still be wise to use it.
See also
Cookbook > Core Libraries > Utilities > CakeTime
Better that adding 31 days is better to use 1 months this will add 30 or 31 days depending on which month will be the current date
$dateAfterOneMonth = date('Y-m-d H:i:s', CakeTime::fromString('+1 months'))

Resources