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]:
1500.0

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]:
2000000

POST (adding new user)


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()


Please fill out the following information.
email: fds
number: 129394
company: dklfdj
school: dfsd
{'number': 129394, 'company': 'dklfdj', 'school': 'dfsd', 'email': 'fds'}

DELETE (delete existing 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()


###########################################
################# WARNING #################
###########################################
You are about to delete a user from Remote.
 
Do you wish to continue? Y/N y
Enter user ID: 24039
You have removed 24039 from Remote.com. :(

PUT (update 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()


You are about to update a user's information.
 
Do you wish to continue? Y/N n

GET (view user info)


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]:
{'company': 'company_5836',
 'email': 'name_2600@email.com',
 'number': 986,
 'school': 'school_287'}

In [117]:
user = es.search(index='users',body={'query': {'match': {'_id':'2600'}}})['hits']['hits'][0]
print(user)


{'_score': 1.0, '_source': {'number': 986, 'email': 'name_2600@email.com', 'school': 'school_287', 'company': 'company_5836'}, '_id': '2600', '_type': 'people', '_index': 'users'}

In [131]:
es.search(index='users',body={'query': {'match': {'company':'company_100'}}})


Out[131]:
{'_shards': {'failed': 0, 'successful': 5, 'total': 5},
 'hits': {'hits': [{'_id': '16070',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_16070@email.com',
     'number': 4555,
     'school': 'school_896'},
    '_type': 'people'},
   {'_id': '25640',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_25640@email.com',
     'number': 4083,
     'school': 'school_55'},
    '_type': 'people'},
   {'_id': '33656',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_33656@email.com',
     'number': 6642,
     'school': 'school_370'},
    '_type': 'people'},
   {'_id': '41594',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_41594@email.com',
     'number': 7192,
     'school': 'school_654'},
    '_type': 'people'},
   {'_id': '180279',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_180279@email.com',
     'number': 1803,
     'school': 'school_158'},
    '_type': 'people'},
   {'_id': '189027',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_189027@email.com',
     'number': 3393,
     'school': 'school_748'},
    '_type': 'people'},
   {'_id': '202606',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_202606@email.com',
     'number': 9737,
     'school': 'school_886'},
    '_type': 'people'},
   {'_id': '237701',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_237701@email.com',
     'number': 4325,
     'school': 'school_919'},
    '_type': 'people'},
   {'_id': '317737',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_317737@email.com',
     'number': 9259,
     'school': 'school_513'},
    '_type': 'people'},
   {'_id': '315431',
    '_index': 'users',
    '_score': 9.302847,
    '_source': {'company': 'company_100',
     'email': 'name_315431@email.com',
     'number': 9838,
     'school': 'school_111'},
    '_type': 'people'}],
  'max_score': 9.302847,
  'total': 195},
 'timed_out': False,
 'took': 9}

GET (user's 1st connection)


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()


Enter user id: 100
Out[149]:
['50640',
 '46958',
 '54872',
 '920211',
 '61922',
 '649661',
 '37042',
 '46507',
 '354142',
 '70260',
 '834550',
 '475258',
 '40005',
 '10577',
 '6257',
 '1108020',
 '663234',
 '332767',
 '40318',
 '942818']

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)))


['46958', '54872', '61922', '920211', '37042', '46507', '70260', '354142', '649661', '663234', '834550', '10577', '40005', '332767', '475258', '1108020', '6257', '50640', '40318', '942818']

In [135]:
es.search(index='users',body={'query': {'match': {'company':'company_7092'}}})


Out[135]:
{'_shards': {'failed': 0, 'successful': 5, 'total': 5},
 'hits': {'hits': [{'_id': '40005',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_40005@email.com',
     'number': 6637,
     'school': 'school_882'},
    '_type': 'people'},
   {'_id': '332767',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_332767@email.com',
     'number': 1653,
     'school': 'school_911'},
    '_type': 'people'},
   {'_id': '354142',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_354142@email.com',
     'number': 3405,
     'school': 'school_446'},
    '_type': 'people'},
   {'_id': '475258',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_475258@email.com',
     'number': 9578,
     'school': 'school_849'},
    '_type': 'people'},
   {'_id': '649661',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_649661@email.com',
     'number': 9300,
     'school': 'school_132'},
    '_type': 'people'},
   {'_id': '663234',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_663234@email.com',
     'number': 821,
     'school': 'school_118'},
    '_type': 'people'},
   {'_id': '834550',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_834550@email.com',
     'number': 8074,
     'school': 'school_231'},
    '_type': 'people'},
   {'_id': '920211',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_920211@email.com',
     'number': 8536,
     'school': 'school_532'},
    '_type': 'people'},
   {'_id': '942818',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_942818@email.com',
     'number': 4451,
     'school': 'school_370'},
    '_type': 'people'},
   {'_id': '1108020',
    '_index': 'users',
    '_score': 9.584238,
    '_source': {'company': 'company_7092',
     'email': 'name_1108020@email.com',
     'number': 5391,
     'school': 'school_824'},
    '_type': 'people'}],
  'max_score': 9.584238,
  'total': 206},
 'timed_out': False,
 'took': 16}

GET (user's 2nd degree connections)

From your 1st degree connections, get their 1st degree connections...this will yield your 2nd degree connections


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()


Enter user id: 100
Out[162]:
219

In [ ]: