如何解决ElasticSearch中的Top Hit ERROR

pbwdgjma  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(2)|浏览(159)

我的文档是像下面的格式,我没有做任何Map,但它出来不同,当我使用两个字段来执行顶部命中聚合

"max_score": 1.0,
    "hits": [
        {
            "_index": "data",
            "_type": "test",
            "_id": "UzzwY3cBOTCkvi7mN2VG",
            "_score": 1.0,
            "_source": {
                "hdId": 20210120,
                "hrId": 5348,
                "timestamp": 1612289417551,
                "timestampstring": "2021-02-03 02:10:17.551",
                "ooId": "672848824",
                "okCount": 17,
                "totalVol": 4220842,
                "type": "test"
            }
        }
    ]

我想使用“Top Hit”聚合来找出我查询的每个ID的最新记录,

{
"query": {
    "bool": {
    "must": {
        "terms": {
        "ooId": [672848824,672848823]
        }
    },
    
    "filter": {
        "match": {
        "type": "test"
        }
    }
    }
},
"size": 1,
"aggs": {
    "unique_id": {
    "terms": {
        "field": "ooId",
        "size": 1
    },
    "aggs": {
        "top_result": {
        "top_hits": {
            "size": 1,
            "_source": ["ooId","totalVol","okCount"],
            "sort": {
            "timestamp" : "desc"
            }
        }
        }
    }
    }
}
}

when i do the about query, it shows the ERROR
{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [ooId] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "test",
                "node": "CzM0ERKGTVyQc3mSiRBY2A",
                "reason": {
                    "type": "illegal_argument_exception",
                    "reason": "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [ooId] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
                }
            }
        ],
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [ooId] in order to load field data by uninverting the inverted index. Note that this can use significant memory.",
            "caused_by": {
                "type": "illegal_argument_exception",
                "reason": "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [ooId] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
            }
        }
    },
    "status": 400
}

但当我使用另一个字段“hdId”取代“ooId“做查询它去没有错误
像`

"aggs": {
    "unique_id": {
    "terms": {
        "field": "hdId",
        "size": 1
    },

出来的时候没问题

{
"took": 7,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
        "value": 104,
        "relation": "eq"
    },
    "max_score": null,
    "hits": []
},
"aggregations": {
    "unique_id": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": 20210119,
                "doc_count": 54,
                "top_result": {
                    "hits": {
                        "total": {
                            "value": 54,
                            "relation": "eq"
                        },
                        "max_score": null,
                        "hits": [
                            {
                                "_index": "data",
                                "_type": "test",
                                "_id": "jk4bXHgBsdomP0OQIkT1",
                                "_score": null,
                                "_source": {
                                    "totalVol": 29613406,
                                    "ooId": "672848823",
                                    "okCount": 64
                                },
                                "sort": [
                                    1616452982797
                                ]
                            }
                        ]
                    }
                }
            },
            {
                "key": 20210120,
                "doc_count": 50,
                "top_result": {
                    "hits": {
                        "total": {
                            "value": 50,
                            "relation": "eq"
                        },
                        "max_score": null,
                        "hits": [
                            {
                                "_index": "data",
                                "_type": "test",
                                "_id": "FU4dW3gBsdomP0OQwCdo",
                                "_score": null,
                                "_source": {
                                    "totalVol": 29296696,
                                    "ooId": "672848824",
                                    "okCount": 66
                                },
                                "sort": [
                                    1616436374297
                                ]
                            }
                        ]
                    }
                }
            }
        ]
    }
}

}
我怎么能修复错误,因为我不想使用hdId作为查询参数,
谢谢
杰夫

vsikbqxv

vsikbqxv1#

claim_status_aggs = A("terms", field="status_es.keyword", size=100000)
s.aggs.bucket("claim_status_aggs", claim_status_aggs)

上面的代码是为我工作。

Map:

"status_es" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
bjp0bcyl

bjp0bcyl2#

答案在这里how to set fielddata=true in kibana
通过使用

"field": "ooId.keyword",

相关问题