如何使用最大匹配文档的休息查询从弹性搜索获得前10个结果?

时间:2018-03-28 06:25:52

标签: c# elasticsearch

我想获得排名前10位的文档\行,这些行应该最近订购这些行,并且最多可以访问多次文档。

“{\”size \“:10,\”query \“:{\”range \“:{\”searchDate \“:{\”gte \“:\”“+ DateTime.Now.AddDays( -30)+“\”},\“aggs \”:{\“top_tags \”:{\“terms \”:{\“field \”:\“searchDate \”},\“aggs \”:{ \“top_otf_hits \”:{\“top_hits \”:{\“sort \”:[{\“saerchDate \”:{\“order \”:\“desc \”}}],\“_ source \”: {\“includes \”:[\“origin \”,\“destination \”]},\“size \”:1}}}}}}}}“

1 个答案:

答案 0 :(得分:1)

{
    "from": 0,
    "size": 0,
    "sort": [{
        "searchDate": "desc"
    }, "_score"],
    "query": {
        "range": {
            "searchDate": {
                "gte": "2018-02-28",
                "lte": "2018-03-05",
                "format": "yyyy-MM-dd"
            }
        }
    },
    "aggs": {
        "frequent": {
            "terms": {
                "field": "tripKey"
            },
            "aggs": {
                "top_otf_hits": {
                    "top_hits": {
                        "sort": [{
                            "searchDate": {
                                "order": "desc"
                            }
                        }],
                        "_source": {
                            "include": ["*"]
                        },
                        "size": 1
                    }
                }
            }
        }
    }
}