将分数添加到elasticsearch完成建议者输入

时间:2014-03-17 16:21:15

标签: elasticsearch search-suggestion

我需要实施elasticsearch completion suggester

我的索引映射如下:

{
  "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生成建议时会考虑在字段上设置分数吗?

1 个答案:

答案 0 :(得分:1)