Skip to content

Instantly share code, notes, and snippets.

@gchoueiter
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gchoueiter/9693540 to your computer and use it in GitHub Desktop.
Save gchoueiter/9693540 to your computer and use it in GitHub Desktop.
Elastic Search Example
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