@@ -8,7 +8,7 @@ class Result(object):
8
8
Represents the result of a search query, and has an array of Document objects
9
9
"""
10
10
11
- def __init__ (self , res , hascontent , duration = 0 , has_payload = False ):
11
+ def __init__ (self , res , hascontent , duration = 0 , has_payload = False , with_scores = False ):
12
12
"""
13
13
- **snippets**: An optional dictionary of the form {field: snippet_size} for snippet formatting
14
14
"""
@@ -19,15 +19,20 @@ def __init__(self, res, hascontent, duration=0, has_payload = False):
19
19
20
20
step = 1
21
21
if hascontent :
22
- step = 3 if has_payload else 2
23
- else :
24
- # we can't have nocontent and payloads in the same response
25
- has_payload = False
22
+ step = step + 1
23
+ if has_payload :
24
+ step = step + 1
25
+ if with_scores :
26
+ step = step + 1
27
+
28
+ offset = 2 if with_scores else 1
26
29
27
30
for i in xrange (1 , len (res ), step ):
28
31
id = to_string (res [i ])
29
- payload = to_string (res [i + 1 ]) if has_payload else None
30
- fields_offset = 2 if has_payload else 1
32
+ payload = to_string (res [i + offset ]) if has_payload else None
33
+ #fields_offset = 2 if has_payload else 1
34
+ fields_offset = offset + 1 if has_payload else offset
35
+ score = float (res [i + 1 ]) if with_scores else None
31
36
32
37
fields = {}
33
38
if hascontent :
@@ -40,7 +45,7 @@ def __init__(self, res, hascontent, duration=0, has_payload = False):
40
45
except KeyError :
41
46
pass
42
47
43
- doc = Document (id , payload = payload , ** fields )
48
+ doc = Document (id , score = score , payload = payload , ** fields ) if with_scores else Document ( id , payload = payload , ** fields )
44
49
self .docs .append (doc )
45
50
46
51
def __repr__ (self ):
0 commit comments