@@ -12,6 +12,14 @@ def schema():
12
12
.build ()
13
13
)
14
14
15
+ def schema_numeric_fields ():
16
+ return (
17
+ SchemaBuilder ()
18
+ .add_integer_field ("id" , stored = True , indexed = True )
19
+ .add_float_field ("rating" , stored = True , indexed = True )
20
+ .add_text_field ("body" , stored = True )
21
+ .build ()
22
+ )
15
23
16
24
def create_index (dir = None ):
17
25
# assume all tests will use the same documents for now
@@ -66,6 +74,46 @@ def create_index(dir=None):
66
74
index .reload ()
67
75
return index
68
76
77
+ def create_index_with_numeric_fields (dir = None ):
78
+ index = Index (schema_numeric_fields (), dir )
79
+ writer = index .writer ()
80
+
81
+ doc = Document ()
82
+ doc .add_integer ("id" , 1 )
83
+ doc .add_float ("rating" , 3.5 )
84
+ doc .add_text (
85
+ "body" ,
86
+ (
87
+ "He was an old man who fished alone in a skiff in"
88
+ "the Gulf Stream and he had gone eighty-four days "
89
+ "now without taking a fish."
90
+ ),
91
+ )
92
+ writer .add_document (doc )
93
+ doc = Document .from_dict (
94
+ {
95
+ "id" : 2 ,
96
+ "rating" : 4.5 ,
97
+ "body" : (
98
+ "A few miles south of Soledad, the Salinas River drops "
99
+ "in close to the hillside bank and runs deep and "
100
+ "green. The water is warm too, for it has slipped "
101
+ "twinkling over the yellow sands in the sunlight "
102
+ "before reaching the narrow pool. On one side of the "
103
+ "river the golden foothill slopes curve up to the "
104
+ "strong and rocky Gabilan Mountains, but on the valley "
105
+ "side the water is lined with trees—willows fresh and "
106
+ "green with every spring, carrying in their lower leaf "
107
+ "junctures the debris of the winter’s flooding; and "
108
+ "sycamores with mottled, white, recumbent limbs and "
109
+ "branches that arch over the pool"
110
+ ),
111
+ }
112
+ )
113
+ writer .add_document (doc )
114
+ writer .commit ()
115
+ index .reload ()
116
+ return index
69
117
70
118
def spanish_schema ():
71
119
return (
@@ -127,6 +175,11 @@ def ram_index():
127
175
return create_index ()
128
176
129
177
178
+ @pytest .fixture (scope = "class" )
179
+ def ram_index_numeric_fields ():
180
+ return create_index_with_numeric_fields ()
181
+
182
+
130
183
@pytest .fixture (scope = "class" )
131
184
def spanish_index ():
132
185
return create_spanish_index ()
@@ -185,6 +238,25 @@ def test_and_query(self, ram_index):
185
238
186
239
assert len (result .hits ) == 1
187
240
241
+ def test_and_query_numeric_fields (self , ram_index_numeric_fields ):
242
+ index = ram_index_numeric_fields
243
+ searcher = index .searcher ()
244
+
245
+ # 1 result
246
+ float_query = index .parse_query ("3.5" , ["rating" ])
247
+ result = searcher .search (float_query )
248
+ assert len (result .hits ) == 1
249
+ assert searcher .doc (result .hits [0 ][1 ])['rating' ][0 ] == 3.5
250
+
251
+ integer_query = index .parse_query ("1" , ["id" ])
252
+ result = searcher .search (integer_query )
253
+ assert len (result .hits ) == 1
254
+
255
+ # 0 result
256
+ integer_query = index .parse_query ("10" , ["id" ])
257
+ result = searcher .search (integer_query )
258
+ assert len (result .hits ) == 0
259
+
188
260
def test_and_query_parser_default_fields (self , ram_index ):
189
261
query = ram_index .parse_query ("winter" , default_field_names = ["title" ])
190
262
assert repr (query ) == """Query(TermQuery(Term(type=Str, field=0, "winter")))"""
@@ -344,8 +416,9 @@ def test_create_readers(self):
344
416
345
417
346
418
class TestSearcher (object ):
347
- def test_searcher_repr (self , ram_index ):
419
+ def test_searcher_repr (self , ram_index , ram_index_numeric_fields ):
348
420
assert repr (ram_index .searcher ()) == "Searcher(num_docs=3, num_segments=1)"
421
+ assert repr (ram_index_numeric_fields .searcher ()) == "Searcher(num_docs=2, num_segments=1)"
349
422
350
423
351
424
class TestDocument (object ):
0 commit comments