LoginSignup
0
0

More than 1 year has passed since last update.

Elasticsearch への問い合わせ (Node.js)

Last updated at Posted at 2018-10-03

こちらで作成したデータに 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0