我需要实现elasticsearch完成建议器.
我有一个像这样映射的索引:
{ "user": { "properties": { "username": { "index": "not_analyzed", "analyzer": "simple", "type": "string" }, "email": { "index": "not_analyzed", "analyzer": "simple", "type": "string" }, "name": { "index": "not_analyzed", "analyzer": "simple", "type": "string" }, "name_suggest": { "payloads": true, "type": "completion" } } } }
我将文档添加到索引中,如下所示:
{ "doc": { "id": 1, "username": "jack", "name": "Jack Nicholson", "email": "nick@myemail.com", "name_suggest": { "input": [ "jack", "Jack Nicholson", "nick@myemail.com" ], "payload": { "id": 1, "name": "Jack Nicholson", "username": "jack", "email": "nick@myemail.com" }, "output": "Jack Nicholson (jack) - nick@myemail.com" } }, "doc_as_upsert": true }
我发送此请求到my_index/_suggest
:
{ "user": { "text": "jack", "completion": { "field": "name_suggest" } } }
我得到的结果看起来像这样:
[ { "text": "John Smith", "score": 1.0, "payload": { "id": 11, "name": "John Smith", "username": "jack", "email": "john@myemail.com" } }, { "text": "Jack Nickolson", "score": 1.0, "payload": { "id": 1, "name": "Jack Nickolson", "username": "jack.n", "email": "nickolson@myemail.com" } }, { "text": "Jackson Jermaine", "score": 1.0, "payload": { "id": 10, "name": "Jackson Jermaine", "username": "jermaine", "email": "jermaine@myemail.com" } }, { "text": "Tito Jackson", "score": 1.0, "payload": { "id": 9, "name": "Tito Jackson", "username": "tito", "email": "jackson@myemail.com" } }, { "text": "Michael Jackson", "score": 1.0, "payload": { "id": 6, "name": "Michael Jackson", "username": "michael_jackson", "email": "jackson_michael@myemail.com" } } ]
这样可以正常工作,但是,我需要将选项排序为那些username
匹配的选项.我可以手动完成,但这会阻止我使用长度和偏移量,并且会更慢.
是否有可能为个别输入添加评分(不是整体建议),这会影响排序?我使用它的方法似乎不是.
另一个相关问题是,是否可以在输入中指定字段数组而不是值数组,这样可以避免重复?如果是,那么当ES生成建议时,是否会考虑在字段上设置分数?