In [4]:
from elasticsearch import Elasticsearch
from random import randint
In [60]:
es = Elasticsearch([{'host': 'localhost', 'port': 9200}], http_auth=('xxxxxxx', 'xxxxxxxxx'))
In [ ]:
# ~ 6,000,000 companies
# ~ 4,000 colleges
ratio ==> 1500 companies per one college
In [6]:
6000000/4000
Out[6]:
In [61]:
doc = {'email':'name_'+str(i)+'@email.com',
'number': randint(1,100),
'company': 'company_'+str(randint(1,100)),
'school': 'school_'+str(randint(1,10))}
res = es.index(index="test-index", doc_type='tweet', body=doc)
In [122]:
es.count(index='users')['count']
Out[122]:
In [87]:
def add_user():
# Can also implement request.json
print('Please fill out the following information.')
email = input('email: ')
number = input('number: ')
company = input('company: ')
school = input('school: ')
doc = {'email': email,
'number': int(number),
'company': company,
'school': school}
#es.index(index='users',doc_type='people',id=es.count(index='users')['count']+1,body=doc)
add_user()
In [103]:
def delete_user():
# Delete user based off ID in the users index
print('###########################################')
print('################# WARNING #################')
print('###########################################')
print('You are about to delete a user from Remote.')
print(' ')
answer = input('Do you wish to continue? Y/N ')
if answer.upper() == 'Y':
user_id = input('Enter user ID: ')
# es.delete(index='users',doc_type='people',id=int(user_id))
print('You have removed %s from Remote.com. :(' % user_id)
else:
pass
delete_user()
In [121]:
def update_user():
print('You are about to update a user\'s information.')
print(' ')
answer = input('Do you wish to continue? Y/N ')
if answer.upper() == 'Y':
user_id = input('Enter user id: ')
print('Please update the following information.')
email = input('email: ')
number = input('number: ')
company = input('company: ')
school = input('school: ')
doc = {'email': email,
'number': int(number),
'company': company,
'school': school}
# es.index(index, doc_type, body, id=user_id)
# return jsonify({'Update': True})
else:
pass
# return jsonify({'Update': False})
update_user()
In [ ]:
def get_user_info():
user_id = input('Enter user id: ')
return jsonify(es.search(index='users',body={'query': {'match': {'_id':user_id}}})['hits']['hits'][0]['_source'])
In [128]:
es.search(index='users',body={'query': {'match': {'_id':'2600'}}})['hits']['hits'][0]['_source']
Out[128]:
In [117]:
user = es.search(index='users',body={'query': {'match': {'_id':'2600'}}})['hits']['hits'][0]
print(user)
In [131]:
es.search(index='users',body={'query': {'match': {'company':'company_100'}}})
Out[131]:
In [149]:
def user_1st_degree():
user_id = input('Enter user id: ')
user_info = es.search(index='users',body={'query': {'match': {'_id':user_id}}})['hits']['hits'][0]['_source']
coworkers = es.search(index='users',body={'query': {'match': {'company':user_info['company']}}})['hits']['hits']
coworker_ids = [coworker['_id'] for coworker in coworkers]
classmates = es.search(index='users',body={'query': {'match': {'school':user_info['school']}}})['hits']['hits']
classmate_ids = [classmate['_id'] for classmate in classmates]
first_deg_conns = list(set(coworker_ids+classmate_ids))
return first_deg_conns
user_1st_degree()
Out[149]:
In [148]:
coworkers = es.search(index='users',body={'query': {'match': {'company':'company_7092'}}})['hits']['hits']
coworker_ids = [coworker['_id'] for coworker in coworkers]
# print(coworker_ids)
classmates = es.search(index='users',body={'query': {'match': {'school':'school_303'}}})['hits']['hits']
classmate_ids = [classmate['_id'] for classmate in classmates]
# print(classmate_ids)
total = classmate_ids+ coworker_ids
print(list(set(total)))
In [135]:
es.search(index='users',body={'query': {'match': {'company':'company_7092'}}})
Out[135]:
In [162]:
def user_1st_degreex(user_id):
user_info = es.search(index='users',body={'query': {'match': {'_id':user_id}}})['hits']['hits'][0]['_source']
coworkers = es.search(index='users',body={'query': {'match': {'company':user_info['company']}}})['hits']['hits']
coworker_ids = [coworker['_id'] for coworker in coworkers]
classmates = es.search(index='users',body={'query': {'match': {'school':user_info['school']}}})['hits']['hits']
classmate_ids = [classmate['_id'] for classmate in classmates]
first_conns = list(set(coworker_ids+classmate_ids))
return first_conns
def user_2nd_degree():
user_id = input('Enter user id: ')
first_conns = user_1st_degree(user_id)
second_conns = []
for conn in first_conns:
second_conns.extend(user_1st_degree(conn))
unique_second_conns = list(set(second_conns))
return unique_second_conns
user_2nd_degree()
Out[162]:
In [ ]: