Lucene 4.0.0时间范围搜索

时间:2013-09-01 16:42:22

标签: lucene date-range

我们在Lucene 4.0.0中遇到与时间范围搜索/过滤器相关的问题。我们已经为一些推文编制了索引,现在我们想要收集特定用户在特定时间范围内发送的推文。当我们使用创建的过滤器运行相关查询时,我们会在指定的时间范围内获取推文。例如。在下面的例子中,由于timeStamp小于lowerBound,我们不期望有“exp tweet”。

您能否就如何执行此任务向我们提出任何建议,或者我们的代码中存在哪些问题?

此致

相关代码

// time range, format "yyyyMMddHHmmss"
String upperBoundStr = 20110126024422;
String lowerBoundStr = 20110126021422;
String tweetTimeStr = 20110126022922;

//create filter
Filter lowerFilter = new QueryWrapperFilter( TermRangeQuery.newStringRange("creationTime",lowerBoundStr,tweetTimeStr,true,false));      
Filter upperFilter = new QueryWrapperFilter( TermRangeQuery.newStringRange("creationTime",tweetTimeStr,upperBoundStr,false,true));
Filter[] filters = new Filter[2];
filters[0] = lowerFilter;
filters[1] = upperFilter;
Filter chainFilter = new ChainedFilter(filters, ChainedFilter.OR);

// search
Query luceneQuery = new TermQuery(new Term("username", "userName1"));
SimpleFSDirectory index = new SimpleFSDirectory(new File("lucene_index"));
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
ScoreDoc[] hits = searchFilteredQuery(luceneQuery, searcher,chainFilter,maxNumberOfNewTweets);
List<RankResult> filteredtweets = convertHitsToRankResults(hits, searcher);

示例输出(格式:date dateIn(“yyyyMMddHHmmss”)userName)

base tweet: Wed Jan 26 02:29:22 VET 2011 20110126022922 userName1
exp tweet: Tue Jan 25 20:05:02 VET 2011 20110125200502 userName1

1 个答案:

答案 0 :(得分:0)

您只是想让推文在upperBoundStrlowerBoundStr之间有时间戳?如果是,则应将Filter chainFilter = new ChainedFilter(filters, ChainedFilter.OR);更改为Filter chainFilter = new ChainedFilter(filters, ChainedFilter.AND);。因为OR意味着大于lowerBoundStr的timeStamp和小于upperBoundStr的timeStamp将被放入搜索结果中。