Node.js: Elastic Cloud の filters の使い方


こちらと同じことを Node.js で記述しました。
Elastic Cloud の filters の使い方

elastic_filter.js
// ---------------------------------------------------------------
//	elastic_filter.js
//
//					Apr/01/2022
//
// ---------------------------------------------------------------
'use strict'

import * as https from "https"
import * as fs from 'fs'

const host="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
const search_key="search-******"
const name_engine="national-parks-demo"
const json_query="query01.json"

console.error ("*** 開始 ***")

const json_str = fs.readFileSync (json_query,'utf8')

const url=host + "/api/as/v1/engines/" + name_engine + "/search"

const options = {
	method: "POST",
	headers: {
		"Content-Type": "application/json",
		"Content-Length": json_str.length,
		"Authorization": "Bearer " + search_key
		}
	}


const request = https.request(url, options, response => {
	console.error(`statusCode: ${response.statusCode}`)
	response.on('data', (dd) => {
		process.stdout.write(dd)
		})
	console.error ("*** 終了 ***")
	})

request.write(json_str)
request.end()

// ---------------------------------------------------------------
query01.json
{
  "query": "",
  "filters" : { "states": [ "Texas", "Minnesota" ] }
}
package.json
{
  "type": "module"
}

実行コマンド

node ./elastic_filter.js | jq .results[].states.raw

実行結果

$ node ./elastic_filter.js | jq .results[].states.raw
*** 開始 ***
statusCode: 200
*** 終了 ***
[
  "Texas"
]
[
  "Minnesota"
]
[
  "Texas"
]

次のバージョンで確認しました。

$ node --version
v17.8.0