我正在尝试实现对存储在elasticsearch中的记录的搜索查询。记录结构如下所示。
{
"_index" : "box_info_store",
"_type" : "boxes",
"_id" : "pWjQLWkBIJk0ORjd0X2P",
"_score" : null,
"_source" : {
"transactionID" : "60ab66cf24c9924f562bf1a2b5d92305d0a6",
"boxNumber" : "Box3",
"createDate" : "2013-09-17T00:00:00",
"itemNumber" : "Item1",
"address" : "Sample Address"
}
}一个框可以包含多个项目。例如,Box3可以有Item1、Item2和Item3。所以在elasticsearch中,我会有3个不同的文档。同时,相同的盒子和相同的项目也可以存在,但地址不同。这些文档的transactionID可能不同,也可能不同。
我的要求是获取最近的n个不同的transactionID以及它们的记录。
我尝试了以下查询来获取最后7个不同的transactionID
GET /box_info_store/boxes/_search?size=7
{
"query": {
"bool": {
"must": [
{"match":{"boxNumber":"Box3"}},
{"match":{"itemNumber":"Item1"}}
]
}
},
"sort": [
{
"createDate": {
"order": "desc"
}
}
],
"aggs": {
"distinct_transactions": {
"terms": { "field": "transactionID"}
}
}
}这获取了最后7个文档,其中boxNumber是Box3,itemNumber是Item1,但不是7个不同的transactionID,这7个文档中有2个有相同的transactionID(虽然都有不同的地址)。但我的要求是获得7个不同的transactionIds,无论它返回多少文档。
希望我能解释清楚。感谢这里的任何帮助
谢谢
-编辑@gaurav9620,我运行第一个查询,结果计数为32,然后运行第二个查询,重复计数为3。我得到了以下结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 32,
"max_score" : null,
"hits" : [
{
"_index" : "box_info_store",
"_type" : "boxes",
"_id" : "RWjRLWkBIJk0ORjdEX-L",
"_score" : null,
"_source" : {
"transactionID" : "3087e106244f6247a5290fb21ce64254529c",
"boxNumber" : "Box3",
"createDate" : "2017-11-15T00:00:00",
"itemNumber" : "Item1",
"address" : "sampleAddress12",
},
"sort" : [
1510704000000
]
},
{
"_index" : "box_info_store",
"_type" : "boxes",
"_id" : "MGjQLWkBIJk0ORjdwX0M",
"_score" : null,
"_source" : {
"transactionID" : "60ab66cf24c9924f562bf1a2b5d92305d0a6",
"boxNumber" : "Box3",
"createDate" : "2016-04-03T00:00:00",
"itemNumber" : "Item1",
"address" : "sampleAddress321",
},
"sort" : [
1459641600000
]
},
..........
..........
..........
{
"_index" : "box_info_store",
"_type" : "boxes",
"_id" : "AGjRLWkBIJk0ORjdK4CJ",
"_score" : null,
"_source" : {
"transactionID" : "3087e106244f6247a5290fb21ce64254529c",
"boxNumber" : "Box3",
"createDate" : "1996-02-16T00:00:00",
"itemNumber" : "Item1",
"address" : "sampleAddress4324",
},
"sort" : [
824428800000
]
}
]
},
"aggregations" : {
"unique_transactions" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 16,
"buckets" : [
{
"key" : "3087e106244f6247a5290fb21ce64254529c",
"doc_count" : 6
},
{
"key" : "27c5f3422f4482495d29e7b2c15c0e311743",
"doc_count" : 5
},
{
"key" : "c40e53212e74e24bf02a5bd2b134cf92bffb",
"doc_count" : 5
}
]
}
}
}发布于 2019-02-27 17:32:10
您使用的大小:表示检索到的原始文档的数量。
如果你的案例是:
聚合中提到的大小为0Size,这将不会返回任何原始documents
GET /box_info_store/Box3/_search?size=7{“查询”:{“布尔”:{“必须”:{“匹配”:{ "boxNumber":"Box3“} },{ "match":{ "itemNumber":"Item1“},"sort":{ "createDate":{ "order":"desc”}},"aggs":{ "distinct_transactions":{ "terms":{ "field":"transactionID","size":7 }
EDIT-------------------------------------
首先触发此查询
GET /box_info_store/boxes/_search?size=0
{
"query": {
"bool": {
"must": [
{
"match": {
"boxNumber": "Box3"
}
},
{
"match": {
"itemNumber": "Item1"
}
}
]
}
}
}在这里,您将找到与您的查询匹配的文档总数,您可以在触发查询后将其设置为n,如下所示
GET /box_info_store/boxes/_search?size=**n**
{
"query": {
"bool": {
"must": [
{
"match": {
"boxNumber": "Box3"
}
},
{
"match": {
"itemNumber": "Item1"
}
}
]
}
},
"sort": [
{
"createDate": {
"order": "desc"
}
}
],
"aggs": {
"distinct_transactions": {
"terms": {
"field": "transactionID",
"size": NUMBER_OF_UNIQUE_TRANSACTION_IDS_TO_BE_FETCHED
}
}
}
}https://stackoverflow.com/questions/54900525
复制相似问题