Last active
August 29, 2015 13:57
-
-
Save gchoueiter/9693540 to your computer and use it in GitHub Desktop.
Elastic Search Example
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 json | |
import argparse | |
import codecs | |
from elastic_search import ElasticIndex | |
def process(args): | |
url = args.url | |
index = args.index | |
es_type = args.type | |
query = args.query | |
output = args.output | |
with codecs.open(query,'r','utf-8') as q: | |
query = json.loads(q.read()) | |
e_index = ElasticIndex(url,index) | |
hits = e_index.get_data(query,es_type) | |
clean_entities = get_entities(hits) | |
with codecs.open(output,'w','utf-8') as o: | |
for e in clean_entities: | |
o.write(u'{0}\n'.format(e)) | |
def get_entities(hits): | |
entities = [] | |
for h in hits["hits"]: | |
entities.append(h["fields"]["relationships.parent.name"]) | |
# entities.extend(h["fields"]["aka"]) | |
entities.append(h["fields"]["name"]) | |
return set(entities) | |
def main(): | |
parser = argparse.ArgumentParser(description= | |
'extract the entities elastic search') | |
parser.add_argument('url', help='endpoint url', type = str) | |
parser.add_argument('index',help='elastic index',type=str) | |
parser.add_argument('type',help='elastic type') | |
parser.add_argument('-q','--query',help='query to run', type=str) | |
parser.add_argument('-o','--output',help='output file', type=str) | |
args = parser.parse_args() | |
process(args) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment