-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
52 lines (45 loc) · 1.26 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 entro <[email protected]>
#
# Distributed under terms of the Psycho license.
import pymongo
try:
client = pymongo.MongoClient()
print "Connected successfully!!!"
except pymongo.errors.ConnectionFailure, e:
print "Could not clientect to MongoDB: %s" % e
db = client.indexes
res = db.indexes1
def count_keywords(doc, keywords):
count = 0
keywords = keywords.split()
keywords = map(str.lower, keywords)
for j in keywords:
for i in ['title', 'stars', 'director']:
k = map(unicode.lower, doc[i])
for m in k:
if (j == m):
count += 1
return count
keywords = raw_input("Enter the keywords:")
res1 = db.test_collection
for i in res.find():
m = {}
m['count'] = count_keywords(i, keywords)
m['doc_id'] = i['_id']
res1.insert_one(m)
m = filter(lambda s : s['count'] > 0, res1.find())
if (len(m) > 0):
m = sorted(m, key = lambda k : k['count'])
for i in m:
r = res.find_one({"_id":i['doc_id']})
for j in r:
if (j != "_id"):
print ("%s : %s") % (j, r[j])
print("\n")
else:
('No results were found!')
db.test_collection.drop()