0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Elasticsearch への問い合わせ (python3)

Last updated at Posted at 2018-10-03

こちらで作成したデータに python3 で問い合わせをします。
Elasticsearch へのデータ投入

elastic_query.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	elastic_query.py
#
#					Oct/04/2018
# ------------------------------------------------------------------
import sys
import requests
import json

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
args = {
	"q": "tags:apple,moon"
	}
url = "http://localhost:9200/blog/_search"
#
rr=requests.get(url,args)
#print(rr)
#print(rr.text)
dict_aa = json.loads(rr.text)
print(dict_aa.keys())
llx = len(dict_aa['hits']['hits'])
sys.stderr.write("len(dict_aa['hits']['hits']) = %d\n" % llx)
for it in range(llx):
	print(dict_aa['hits']['hits'][it]['_source'])
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./elastic_query.py 
*** 開始 ***
dict_keys(['took', 'timed_out', '_shards', 'hits'])
len(dict_aa['hits']['hits']) = 2
{'name': '田中康夫', 'title': 'My Name Is Tanaka', 'content': 'I love cat', 'tags': ['Earth', 'Moon', 'Mars']}
{'name': '渡辺五郎', 'title': 'My Name Is Watanabe', 'content': 'I love fish', 'tags': ['apple', 'orange', 'banana']}
*** 終了 ***

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

$ python --version
Python 3.9.7
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?