我在elasticsearch中的文档映射中有一个日期字段,名为conversationSMPostDate,
"conversationSMPostDate": {
"format": "yyyy-MM-dd HH:mm:ss",
"type": "date"
}
我希望在午夜至晚上9点的时间范围内获取有关数据的统计数据。
我尝试使用过滤器组合多个通配符,但没有成功。我搜索过,找不到办法做到这一点。我想做类似的事情 elasticsearch: getMinuteOfDay() applied to time() in date range filter
答案 0 :(得分:0)
您好我不确定这将如何在弹性搜索中发挥作用,但使用lucene-core
您可以使用它如下
public class TestLuceneDate {
private static final String DETAILS = "details";
private static Path path = Paths.get("/luceneindex");
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final String DATE = "date";
public static void main(String[] args) throws Exception {
index();
search("1988-04-08 03:00:00", "1988-04-08 16:00:00");
}
private static void index() throws Exception {
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
Directory directory = FSDirectory.open(path);
IndexWriter writer = new IndexWriter(directory, config);
writer.addDocument(newDocument("document with date 1", "1988-04-08 00:05:12"));
writer.addDocument(newDocument("document with date 2", "1988-04-08 04:20:00"));
writer.addDocument(newDocument("document with date 3", "1988-04-08 11:13:50"));
writer.addDocument(newDocument("document with date 4", "1988-04-08 15:07:05"));
writer.addDocument(newDocument("document with date 5", "1988-04-08 18:14:33"));
writer.addDocument(newDocument("document with date 6", "1988-04-08 19:20:47"));
writer.addDocument(newDocument("document with date 7", "1988-04-08 22:16:07"));
writer.addDocument(newDocument("document with date 8", "1988-04-08 23:44:98"));
writer.close();
}
private static Document newDocument(String details, String date) throws Exception {
Document document = new Document();
document.add(new TextField(DETAILS, details, Store.YES));
document.add(new LongField(DATE, DATE_FORMAT.parse(date).getTime(), Store.YES));
return document;
}
private static void search(String fromDateString, String toDateString) throws Exception {
Directory directory = FSDirectory.open(path);
IndexReader inxRead = DirectoryReader.open(directory);
IndexSearcher is = new IndexSearcher(inxRead);
Long fromDate = DATE_FORMAT.parse(fromDateString).getTime();
Long toDate = DATE_FORMAT.parse(toDateString).getTime();
NumericRangeQuery<Long> dateRangeQuery = NumericRangeQuery.newLongRange(DATE, fromDate, toDate, true, true);
TopDocs hits = is.search(dateRangeQuery, Integer.MAX_VALUE);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println("Parser > " + scoreDoc.score + " :: " + doc.get(DETAILS) + " - " + DATE_FORMAT.format(new Date(Long.parseLong(doc.get(DATE)))));
}
inxRead.close();
}
}
我使用Lucene 5.1.0来做到这一点。