首页 文章

获得最高分的doc . Elasticsearch

提问于
浏览
0

我是Elasticsearch的新手 . 其实我今天开始了 .

我找了他们所拥有的“银行”示例并且两次添加了相同的帐户 .

然后我想知道我是否可以使用该号码获得帐户,并且它有三个帐号具有相同的号码 . 没关系 .

但后来,我写了自己的“得分解释”,看到它有效 .

现在,我想获得最高分的文档(帐户),但我不知道该怎么做 .

这就是我写的JSON:

{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "account_number": "998"                                                 }
                        }
                    ]
                }
            },
            "functions": [
                {
                    "script_score": {
                        "script": { 
                            "lang": "groovy",
                            "file": "test-score",                           
                            "params": {
                                "boostBy": 2
                            }                       
                        }
                    }
                }
            ]           
        }
    }
}

而这就是:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 33738,
        "hits": [
            {
                "_index": "bank",
                "_type": "account",
                "_id": "998",
                "_score": 33738,
                "_source": {
                    "account_number": 998,
                    "balance": 16869,
                    "firstname": "Letha",
                    "lastname": "Baker",
                    "age": 40,
                    "gender": "F",
                    "address": "206 Llama Court",
                    "employer": "Dognosis",
                    "email": "lethabaker@dognosis.com",
                    "city": "Dunlo",
                    "state": "WV"
                }
            },
            {
                "_index": "bank",
                "_type": "account",
                "_id": "AVn6HUT-4vooOra1Vb9f",
                "_score": 33246,
                "_source": {
                    "account_number": 998,
                    "balance": 16623,
                    "firstname": "Bradshaw",
                    "lastname": "Mckenzie",
                    "age": 29,
                    "gender": "F",
                    "address": "244 Columbus Place",
                    "employer": "Euron",
                    "email": "bradshawmckenzie@euron.com",
                    "city": "Hobucken",
                    "state": "CO",
                    "aggregations": {
                        "testing_n_grams": {
                            "avg": {
                                "script": {
                                    "inline": "doc['grade'].value",
                                    "lang": "painless"
                                }
                            }
                        }
                    }
                }
            },
            {
                "_index": "bank",
                "_type": "account",
                "_id": "AVn6G6hA4vooOra1Vb9e",
                "_score": 33246,
                "_source": {
                    "account_number": 998,
                    "balance": 16623,
                    "firstname": "Bradshaw",
                    "lastname": "Mckenzie",
                    "age": 29,
                    "gender": "F",
                    "address": "244 Columbus Place",
                    "employer": "Euron",
                    "email": "bradshawmckenzie@euron.com",
                    "city": "Hobucken",
                    "state": "CO",
                    "aggregations": {
                        "testing_n_grams": {
                            "script": {
                                "inline": "doc['firstname'].value",
                                "lang": "painless"
                            }
                        }
                    }
                }
            }
        ]
    }
}

我想只使用“max_score”帐户 . 可能吗?或者我必须采取一切,其他地方决定什么是最高分?

我还想要,如果max_score有多个帐户,它会返回每个帐户的“max_score” . 这就是为什么如果我只写“size = 1”它就不起作用 .

对不起,如果这是一个愚蠢的问题,正如我所说,我今天开始使用Elasticsearch并且不知道该怎么做 .

也许有人曾尝试过类似的东西 .

2 回答

  • 0

    我没有找到一个好的答案 . 所以我使用“尺寸”:1当Shadocko建议我只需要第一个时 . 当我需要每个匹配max_score的数据时,我只需要整个日期和其他地方我决定哪个数据是正确的 .

  • 0

    您可以使用 size 参数限制ES将返回的文档数 .

    {
        "query": {
            [...snip...]
        },
        "size": 1
    }
    

    reference

相关问题