Solr filter by year on TrieDateField - solr

My Solr schema has a fieldtype tdate of class solr.TrieDateField
<field name="timestamp" type="tdate" indexed="true" stored="true" default="NOW"/>
So when I add documents to the index the timestamp field looks like this:
"timestamp": "2015-11-03T15:52:51.418Z"
Now to filter all documents of the current year I add a range filter to the query:
/select?q=*:*&fq=timestamp:[NOW/YEAR TO NOW/YEAR+1YEAR}
But how would I filter for any year, let's say 2014?
The only thing I could come up with was to create a date range spanning the entire year from the first to the last millisecond:
/select?q=*%3A*&wt=json&indent=true&fq=timestamp:[2014-01-01T0:00:00.000Z TO 2014-12-31T23:59:59.999Z}
Is there a way to get to the same result by just passing in the year as a four digit string?

After some research I believe it is not possible for a TrieDateField to filter on year granularity, other than the proposed solution. You would need a solr.DateRangeField for that.
However it is possible to add a single argument to the query string telling Solr how to conceive NOW, which is an epoch time (milliseconds) chosen in the year we want to filter by.
&fq=timestamp:[NOW/YEAR TO NOW/YEAR+1YEAR}&NOW=1388534400000
1388534400000 represents midnight the 1st of january 2014. But any other epoch timestamp representing a moment in 2014 will be fine.
https://cwiki.apache.org/confluence/display/solr/Working+with+Dates

Related

Solr - facet by day of week

On Solr, I would like to get facets of a field date but according to a day of the week,
for example, if I have 3 records with the following values on the date field:
16/11
22/11
23/11
I would like to get the following facet:
Sunday: 2,
Monday: 1
Is it possible?
Solr does not having anything which could provide you the day of the week based on the date.
You need to index the weeks data in a separate field.
Where your fields would be holding the values like SUNDAY, MONDAY etc..
Once you have this field and indexed the data in solr.
Then you could be able to achieve the faceting based on the weeks.

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.

index column type="timestamp" in solr from cassandra database

Here i am using DSE-3.0.I want to index column type="timestamp" name="DateTime" in solr from cassandra database.What should be the fieldType in schema.xml in solr for type="timestamp".Please help me.I have badly need it.
Thank you
This is my database output: --
cqlsh:mykeyspace> SELECT * FROM mysolr ;
KEY,124 | Date_Time,2013-02-11 10:10:10+0530 | body,A chicken in every pot ... | date,dec 15, 1933 | name,Roosevelt | title,fireside chat
But at query not gives value of Date_Time field.
My query output in solr is :--
id,body,title,name,date
124,A chicken in every pot ...,fireside chat,Roosevelt,"dec 15, 1933"
What am i missing to configure.Please guide me proper way. Thank you.
In versions of Solr prior to 4.2 there was a timestamp field in the example schema.xml that implemented a predefined date fieldType
<field name="timestamp" type="date" indexed="true" stored="true" default="NOW"
multiValued="false"/>
Here is the date fieldType definition
<!-- The format for this date field is of the form 1995-12-31T23:59:59Z, and
is a more restricted form of the canonical representation of dateTime
http://www.w3.org/TR/xmlschema-2/#dateTime
The trailing "Z" designates UTC time and is mandatory.
Optional fractional seconds are allowed: 1995-12-31T23:59:59.999Z
All other components are mandatory.
Expressions can also be used to denote calculations that should be
performed relative to "NOW" to determine the value, ie...
NOW/HOUR
... Round to the start of the current hour
NOW-1DAY
... Exactly 1 day prior to now
NOW/DAY+6MONTHS+3DAYS
... 6 months and 3 days in the future from the start of
the current day
Consult the DateField javadocs for more information.
Note: For faster range queries, consider the tdate type
-->
<fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
So based on this example, I would recommend using the date field type, noting that it will be stored in UTC format.

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.

SOLR travel site: on date queries

I was looking to implement SOLR for a Hotel bookings site. Search based on location, hotel names, facilities works very well and so does the faceting. What I have not been able to figure out is how to search for a hotel given Checkin and Checkout dates.
Eg: User will search for search query - "Hotels in Newyork" and select CheckIn Date: 10th Feb 2012 and CheckOut Date: 12 Feb 2012 from the date selection box.
This is how I have the data -
Hotel_Name 10thFeb2012 11thFEB2012 ........ 31DEC2012
Hotel1 2room 3room 10rooms
Hotel2 1room 4room ........ 12rooms
Now if the query is for Hotel2 for 3rooms from checkin Date 10thFeb2012 to 11thFeb2012 it shdnt match because there is only one room available for 10thFeb.
IF the query for Hotel2 is for 1 room from checkin Date 10thFeb2012 to 11thFeb2012 then it should be part of search result.
Use the ISO 8601 format for your date-times.
Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45Z)
Both your database and Solr will understand date-times from strings that conform to that format.
So,
store the data in DB and Solr with compatible date-time formats. (On the back of my head, Solr must have a Z appended to the date-time, else its invalid).
your search interface must format all dates in that format to query solr.
Solr can do conditional expressions, facets, range bucket faceting etc with dates.
I would go with the following schema:
hotel_name : string (for faceting)
hotel_name_searchable : text (for searching, this is a copy field:look it up)
room_id : string
start_date : date (when the room is availabe)
end_date : date (if not booked, set it to an infinite date, say 2040)
For each room you are ever tracking, store the date-times between which it is free.
You can search for rooms between the start_date and end_date.
Do faceting on hotel_name so your search for rooms "checkin Date 10thFeb2012 to 11thFeb2012" gets you:
Hotel1:[r1,r2,r3]
Hotel2:[r8]
Hotel3:[r2,r3,r4]
Faceting on hotel_name filters to one hotel, facet.mincount on room_id can return hotels having the required number of rooms.
A little warning: I may be a bit rusty on faceting, as I used to do a lot of processing on Solr results itself.

Resources