我需要实现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 生成建议时是否会考虑在字段上设置分数?