建议编辑

建议功能使用建议器根据提供的文本建议类似的术语。建议功能的部分内容仍在开发中。

建议请求部分与查询部分一起定义在 search 请求中。如果省略查询部分,则只返回建议。

'use strict'

const { Client } = require('@elastic/elasticsearch')
const client = new Client({
  cloud: { id: '<cloud-id>' },
  auth: { apiKey: 'base64EncodedKey' }
})

async function run () {
  const bulkResponse = await client.bulk({
    refresh: true,
    operations: [
      { index: { _index: 'game-of-thrones' } },
      {
        character: 'Ned Stark',
        quote: 'Winter is coming.'
      },

      { index: { _index: 'game-of-thrones' } },
      {
        character: 'Daenerys Targaryen',
        quote: 'I am the blood of the dragon.'
      },

      { index: { _index: 'game-of-thrones' } },
      {
        character: 'Tyrion Lannister',
        quote: 'A mind needs books like a sword needs a whetstone.'
      }
    ]
  })

  if (bulkResponse.errors) {
    console.log(bulkResponse)
    process.exit(1)
  }

  const result = await client.search({
    index: 'game-of-thrones',
    query: {
      match: { quote: 'winter' }
    },
    suggest: {
      gotsuggest: {
        text: 'winter',
        term: { field: 'quote' }
      }
    }
  })

  console.log(result)
}

run().catch(console.log)