如何在bool中每个匹配返回一个文档

时间:2017-10-18 09:31:50

标签: elasticsearch

我的文档有(简化)结构,如下所示:

{uuid: 1, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"}
{uuid: 2, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"}
{uuid: 1, timestamp: "2017-10-18T01:25:00.000Z", "key": "value"}
{uuid: 2, timestamp: "2017-10-18T01:25:00.000Z", "key": "value"}
{uuid: 1, timestamp: "2017-10-18T01:20:00.000Z", "key": "value"}
{uuid: 2, timestamp: "2017-10-18T01:20:00.000Z", "key": "value"}

现在我试图按照uuid匹配来获得正好一个文档(使用最新时间戳):

query: {
    bool: {
        should: [{
            match: {
                uuid: 1
            }
        },
        {
            match: {
                uuid: 2
            }
        }]
    }
}

但是,上述查询会为uuid返回多个文档。如何修改我的查询以返回包含uuid 12的两个最新文档,如下所示:

{uuid: 1, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"}
{uuid: 2, timestamp: "2017-10-18T01:30:00.000Z", "key": "value"}

1 个答案:

答案 0 :(得分:3)

我建议使用import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types; public class Sample { public static void main( final String[] args ) { try { Class.forName( "oracle.jdbc.OracleDriver" ); // If you are using the Oracle driver. Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "username", "password" ); CallableStatement cs = con.prepareCall( "BEGIN SELECT HEADCOUNT INTO :1 FROM PROJECT WHERE deptid=1; END;" ); cs.registerOutParameter( 1, Types.NUMERIC ); cs.execute(); System.out.println( cs.getInt( 1 ) ); } catch ( ClassNotFoundException | SQLException ex ) { System.err.println( ex.getMessage() ); } } } 聚合与terms子聚合相结合,如下所示:

top_hits