Last active
June 20, 2020 13:34
-
-
Save dakeshi19/5b5a14c739067d7c43c03d0e24695126 to your computer and use it in GitHub Desktop.
pandasのDataFrameに格納
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import elasticsearch | |
import pandas as pd | |
es = elasticsearch.Elasticsearch("localhost:9200") | |
INDEX = 'ldgourmet' | |
AGGS = "aggregations" | |
BKTS = "buckets" | |
aNames = ['a1','a2'] | |
def qBuilder(qField,qValue,field1,field2): | |
qBody = {} | |
qBody["query"] = { | |
"term":{ | |
qField: { | |
"value": qValue | |
} | |
} | |
} | |
qBody[AGGS] = { | |
aNames[0]:{ | |
"terms": { | |
"field": field1, | |
"size": 47 | |
}, | |
AGGS:{ | |
aNames[1]:{ | |
"significant_terms":{ | |
"field": field2, | |
"size":50, | |
"min_doc_count": 5, | |
"chi_square": {} | |
} | |
} | |
} | |
} | |
} | |
import json | |
print(json.dumps(qBody,ensure_ascii=False)) | |
return qBody | |
def searchEs(qBody): | |
_r = es.search( | |
index=INDEX, | |
body=qBody | |
) | |
def myAggs4DF(result): | |
_bs = result[AGGS][aNames[0]][BKTS] | |
aggs = [] | |
for _b in _bs: | |
k = _b['key'] | |
dc = _b['doc_count'] | |
innerBs = [( | |
ib['key'], | |
ib['doc_count'] | |
) for ib in _b[aNames[1]][BKTS]] | |
aggs.append([k,dc,innerBs]) | |
return (aggs,['k','dc','bkts']) | |
aggs = myAggs4DF(_r) | |
h = [i['_source'] | |
for i in _r['hits']['hits']] | |
return h,aggs | |
h,a = searchEs(qBuilder("pref.raw","13__東京都","area_name.raw","cates.raw")) | |
#hDF = pd.json_normalize(h) | |
hDF = pd.DataFrame(h) | |
aDF = pd.DataFrame(a[0],columns=a[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment