こちらで作成したデータに Node.js で問い合わせをします。
Elasticsearch へのデータ投入
elastic_query.js
#! /usr/bin/node
// ---------------------------------------------------------------
// elastic_query.js
//
// Mar/21/2022
//
// ---------------------------------------------------------------
'use strict'
import * as http from "http"
console.error ("*** 開始 ***")
const url = "http://localhost:9200/blog/_search?q=tags:apple,moon"
const options = {
method: 'GET'
}
const req = http.request(url,options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', dd => {
// process.stdout.write(dd)
const dict_aa = JSON.parse (dd)
const llx = dict_aa['hits']['hits'].length
console.error ("llx = " + llx)
// console.log(dict_aa['hits']['hits'])
for (var it =0; it< llx; it+=1)
{
console.log(dict_aa['hits']['hits'][it]['_id'])
console.log(dict_aa['hits']['hits'][it]['_source'])
console.log()
}
console.error ("*** 終了 ***")
})
})
req.on('error', error => {
console.error(error)
})
req.end()
// ---------------------------------------------------------------
package.json
{
"type": "module"
}
実行結果
$ ./elastic_query.js
*** 開始 ***
statusCode: 200
llx = 2
t002
{
name: '田中康夫',
title: 'My Name Is Tanaka',
content: 'I love cat',
tags: [ 'Earth', 'Moon', 'Mars' ]
}
t003
{
name: '渡辺五郎',
title: 'My Name Is Watanabe',
content: 'I love fish',
tags: [ 'apple', 'orange', 'banana' ]
}
*** 終了 ***
次のバージョンで確認しました。
$ node --version
v17.7.2