Solr 在 TrieDateField 上按年份过滤 [英] Solr filter by year on TrieDateField
问题描述
我的 Solr 架构具有 solr.TrieDateField
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}
但是我将如何过滤任何年,比如说 2014 年?
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?
推荐答案
经过一些研究,我认为 TrieDateField 除了建议的解决方案之外,不可能按年份粒度进行过滤.为此,您需要一个 solr.DateRangeField
.
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.
但是,可以在查询字符串中添加一个参数,告诉 Solr 如何构思 NOW
,这是我们选择的年份中的 epoch
时间(毫秒)想要过滤.
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
表示 2014 年 1 月 1 日午夜.但任何其他表示 2014 年某个时刻的 epoch
时间戳都可以.
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
这篇关于Solr 在 TrieDateField 上按年份过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!