How to count datetime type field on Solr search result? - solr

I want use Solr search and count a result in datetime type field,
If I just use fact_field it will just get 100 rows.
Does anyone have any idea to fix this requirement?
The field like this "created":"2015-09-02T05:57:23Z"
Can I counting this by year or month in Solr?

You can do this with facet.range
If you want to count document by month of current year and previous year :
Use The Below Param :
"facet.range":"created",
"q":"*:*",
"facet.range.gap":"+1MONTH",
"facet":"true",
"facet.range.start":"NOW/YEAR-1YEAR",
"facet.range.end":"NOW"
Example :
http://127.0.0.1:8983/solr/test/select?q=*%3A*&facet=true&facet.range=created&facet.range.start=NOW/YEAR-1YEAR&facet.range.end=NOW&facet.range.gap=%2B1MONTH
Here
facet.range
The facet.range parameter defines the field for which Solr should create range facets. for your case it's created field.
facet.range.start
The facet.range.start parameter specifies the lower bound of the ranges
facet.range.end
The facet.range.end specifies the upper bound of the ranges
facet.range.gap
The span of each range expressed as a value to be added to the lower bound. For date fields, this should be expressed using the DateMathParser syntax (such as, facet.range.gap=%2B1DAY ... '+1DAY'). for your case it's year or month.
Source :
https://cwiki.apache.org/confluence/display/solr/Faceting#Faceting-DateFacetingParameters
https://cwiki.apache.org/confluence/display/solr/Working+with+Dates

Related

Multiple Filter Queries(fq) in SOLR

I'm having my SOLR index as:
{'year':2002
'user_entries':['user1']},
{'year':2003
'user_entries':['user2']},
{'year':2002
'user_entries':['user1']},
Expected result
{facet_fields:{2002:{'user1':2}, 2003:{'user2':1}}}
I can use fq=year:2002 to extract the facets on user_entries to extract the count of entries of each user in year 2002 as
/solr/rss/select?q=:&fq=year:2002&wt=json&indent=true&facet=true&facet.query=year:2002&facet.field=user_entries
But I want to extract the user_entries of each year from: 2002 to 2010 individually without summing them all up.
Current Approach:
1. solr/rss/select?q=*:*&fq=year:2002&wt=json&indent=true&facet=true&facet.query=year:2002&facet.field=user_entries
2. solr/rss/select?q=*:*&fq=year:2003&wt=json&indent=true&facet=true&facet.query=year:2003&facet.field=user_entries...
So can I have a single multiple filter query instead of multiple queries for each year with which I can extract individual year data without summing them up as with default OR operation?
If you are using Solr 4.0 or newer then you can use pivot facets:
?q=*:*
&facet=true
&facet.pivot=year,user_entries
&f.user_entries.facet.limit=3
But the response is not exactly the same as regular field facets, so you may have to change your parsing code. You will get the count for the "2002" value of the year field, and for each combination of "2002" and values of the user_entries fields (limited to 3), followed by the same for the "2003" value.

Solr match any date in given month

In Solr, is it possible to search for all records in a given month regardless of the year or day ? For example, the snippet below would match everything on 01.01.2013 - what I want to do is find everything that appeared on 01.01 for any year.
date:2013-01-01T00:00:00Z
No, not with a date field. Solr can only deal with ranges of dates, just like it only deals with ranges of numbers or ranges of strings. Asking Solr to only query a date field based on the first day of the month is like asking it to query on a numeric field and only give you odd numbers, or querying a string but only those starting with vowels.
What you'll need to do is break up the date into month and day components and then query on those. If your base field is sale_date, you'll also need sale_month and sale_day. Then you can query on month:3 to get everything that happened in any March, or day:1 and get everything for the first day of any month or month:3 AND day:1 to get everything that happened on any March 1st.

Sort result by date difference

We have a solr result queried by a date range +/- 1 Month of the date entered. If i entered 2012-12-01 i get a Result Set from 2012-11-01 to 2013-01-01.
This works fine, but we want to sort the Result after the date difference to the date entered.
For example if we have the Dates:
2012-11-10, 2012-11-30, 2012-12-03, 2012-12-10
we want the Result sorted like this:
2012-11-30, 2012-12-03, 2012-12-10, 2012-11-10
Any Ideas how to accomplish this in solr?
Thanks in advance!
you can just add &sort=sub(date_you_enter,date_fieldname_on_documents) asc parameter to your query. basically sub function will calculate the difference and then you will be able to sort it depending on that result.
for reference you can also check http://wiki.apache.org/solr/FunctionQuery page for more functions that could be useful for you

Sum field by date range

I'm using solr 3.6 and I'm kinda stuck trying to perform a special query.
I'm actually using facets by date range, the face.date.gap is set to +1DAY. Of course, the facet is supposed to return the count of docs at a date range but I also need to get the sum of a special field at the same ranges used in facet. It's like I need to count how many votes I have daily monthly, weekly, whatever... it depends on the gap params.
Any ideas? Should I use the group.query or facet.query?
One suggestion I have is to treat the weeks, days separately, and index them. For ex. Today is part of 24th week. Another suggestion is not to rule out multiple searches to service one request. One to calculate all oth facets and one to return counts for given date range (based on search results from first query).

How to boost a document score in Solr based on a Date field and DIstance field without specifying a distance range?

I am constructing a query that searches on the tile. I would like to boost the doc score based on the distance and a date field. Can this be achieved without restricting the distance and date field to a range?
What would the query be if the query field is title and sfield is latlon?
boost to 'wildcarded' distance and date fields, i.e: [* TO *] ?

Resources