elasticsearch 按日期字段中的时间范围获取对象elasticsearch,elasticsearch,lucene,elasticsearch,Lucene" /> elasticsearch 按日期字段中的时间范围获取对象elasticsearch,elasticsearch,lucene,elasticsearch,Lucene" />

elasticsearch 按日期字段中的时间范围获取对象elasticsearch

elasticsearch 按日期字段中的时间范围获取对象elasticsearch,elasticsearch,lucene,elasticsearch,Lucene,elasticsearch中的文档映射中有一个日期字段,名为ConversationsPostDate "conversationSMPostDate": { "format": "yyyy-MM-dd HH:mm:ss", "type": "date" } 我想在从午夜到上午9点的时间范围内获得一些数据统计数据 我尝试使用过滤器组合多个通配符,但没有成功。我搜索了一下,找不到一个方法来做这个。我想做一些类似的事情 您好,我不确定这在elasticsearch中如何实现,但使用lucene

elasticsearch中的文档映射中有一个日期字段,名为ConversationsPostDate


"conversationSMPostDate": {
"format": "yyyy-MM-dd HH:mm:ss",
"type": "date"
}
我想在从午夜到上午9点的时间范围内获得一些数据统计数据

我尝试使用过滤器组合多个通配符,但没有成功。我搜索了一下,找不到一个方法来做这个。我想做一些类似的事情

您好,我不确定这在elasticsearch中如何实现,但使用
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();
    }
}
公共类TestLuceneDate{
私有静态最终字符串DETAILS=“DETAILS”;
私有静态路径路径=Path.get(“/luceneindex”);
私有静态最终简化格式日期\格式=新简化格式(“yyyy-MM-dd HH:MM:ss”);
私有静态最终字符串DATE=“DATE”;
公共静态void main(字符串[]args)引发异常{
索引();
搜索(“1988-04-08 03:00:00”、“1988-04-08 16:00:00”);
}
私有静态void index()引发异常{
Analyzer Analyzer=新的StandardAnalyzer();
IndexWriterConfig配置=新的IndexWriterConfig(分析器);
Directory Directory=FSDirectory.open(路径);
IndexWriter=newindexwriter(目录,配置);
作者:addDocument(新文档(“日期为1的文档”,“1988-04-08 00:05:12”);
作者:addDocument(新文档(“日期为2的文档”,“1988-04-08 04:20:00”);
作者:addDocument(新文档(“日期为3的文档”,“1988-04-08 11:13:50”);
作者:addDocument(新文档(“日期为4的文档”,“1988-04-08 15:07:05”);
作者:addDocument(新文档(“日期为5的文档”,“1988-04-08 18:14:33”);
作者:addDocument(新文档(“日期为6的文档”,“1988-04-08 19:20:47”);
作者:addDocument(新文档(“日期为7的文档”,“1988-04-08 22:16:07”);
作者:addDocument(新文档(“日期为8的文档”,“1988-04-08 23:44:98”);
writer.close();
}
私有静态文档newDocument(字符串详细信息、字符串日期)引发异常{
文档=新文档();
document.add(新文本字段(详细信息,详细信息,Store.YES));
add(新的LongField(DATE,DATE_FORMAT.parse(DATE).getTime(),Store.YES));
归还文件;
}
私有静态无效搜索(String fromDateString,String toDateString)引发异常{
Directory Directory=FSDirectory.open(路径);
IndexReader inxRead=DirectoryReader.open(目录);
IndexSearcher is=新的IndexSearcher(inxRead);
Long fromDate=DATE_FORMAT.parse(fromDateString.getTime();
Long toDate=DATE_FORMAT.parse(toDateString.getTime();
NumericRangeQuery dateRangeQuery=NumericRangeQuery.newLongRange(日期、fromDate、toDate、true、true);
TopDocs hits=is.search(dateRangeQuery,Integer.MAX_值);
for(ScoreDoc ScoreDoc:hits.scoreDocs){
单据单据=is.doc(scoreDoc.doc);
System.out.println(“解析器>”+scoreDoc.score+”:“+doc.get(详细信息)+”-“+DATE\u FORMAT.FORMAT(新日期(Long.parseLong(doc.get(DATE)!))));
}
inxRead.close();
}
}

我已经使用Lucene 5.1.0来实现这一点。

嗨,我不确定在elasticsearch中如何实现这一点,但使用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();
    }
}
公共类TestLuceneDate{
私有静态最终字符串DETAILS=“DETAILS”;
私有静态路径路径=Path.get(“/luceneindex”);
私有静态最终简化格式日期\格式=新简化格式(“yyyy-MM-dd HH:MM:ss”);
私有静态最终字符串DATE=“DATE”;
公共静态void main(字符串[]args)引发异常{
索引();
搜索(“1988-04-08 03:00:00”、“1988-04-08 16:00:00”);
}
私有静态void index()引发异常{
Analyzer Analyzer=新的StandardAnalyzer();
IndexWriterConfig配置=新的IndexWriterConfig(分析器);
Directory Directory=FSDirectory.open(路径);
IndexWriter=newindexwriter(目录,配置);
作者:addDocument(新文档(“日期为1的文档”,“1988-04-08 00:05:12”);
作者:addDocument(新文档(“日期为2的文档”,“1988-04-08 04:20:00”);
作者:addDocument(新文档(“日期为3的文档”,“1988-04-08 11:13:50”);
作者:addDocument(新文档(“日期为4的文档”,“1988-04-08 15:07:05”);
作者:addDocument(新文档(“日期为5的文档”,“1988-04-08 18:14:33”);
作者:addDocument(新文档(“日期为6的文档”,“1988-04-08 19:20:47”);
作者:addDocument(新文档(“日期为7的文档”,“1988-04-08 22:16:07”);
作者:addDocument(新文档(“日期为8的文档”,“1988-04-08 23:44:98”);
writer.close();
}
私有静态文档newDocument(字符串详细信息、字符串日期)引发异常{
文档=新文档();
document.add(新文本字段(详细信息,详细信息,Store.YES));
add(新的LongField(DATE,DATE_FORMAT.parse(DATE).getTime(),Store.YES));
归还文件;
}
私有静态无效搜索(String fromDateString,String toDateString)引发异常{
Directory Directory=FSDirectory.open(路径);
IndexReader inxRead=DirectoryReader.open(目录);
IndexSearcher is=新的IndexSearcher(inxRead);
Long fromDate=DATE_FORMAT.parse(fromDateString.getTime();
Long toDate=DATE_FORMAT.parse(toDateString.getTime();
NumericRangeQuery dateRangeQuery=NumericRangeQuery.newLongRange(日期、fromDate、toDate、true、true);
TopDocs hits=is.search(dateRangeQuery,Integer.MAX_值);
for(ScoreDoc ScoreDoc:hits.scoreDocs){
单据单据=is.doc(scoreDoc.doc);
System.out.println(“解析器>”+scoreDoc.score+”:“+doc.get(详细信息)+”-“+DATE\u格式(新日期长解析长解析长文档获取(DA