@@ -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
@pytest .fixture ()
71
119
def dir_index (tmpdir ):
@@ -77,6 +125,11 @@ def ram_index():
77
125
return create_index ()
78
126
79
127
128
+ @pytest .fixture (scope = "class" )
129
+ def ram_index_numeric_fields ():
130
+ return create_index_with_numeric_fields ()
131
+
132
+
80
133
class TestClass (object ):
81
134
def test_simple_search_in_dir (self , dir_index ):
82
135
_ , index = dir_index
@@ -120,6 +173,25 @@ def test_and_query(self, ram_index):
120
173
121
174
assert len (result .hits ) == 1
122
175
176
+ def test_and_query_numeric_fields (self , ram_index_numeric_fields ):
177
+ index = ram_index_numeric_fields
178
+ searcher = index .searcher ()
179
+
180
+ # 1 result
181
+ float_query = index .parse_query ("3.5" , ["rating" ])
182
+ result = searcher .search (float_query )
183
+ assert len (result .hits ) == 1
184
+ assert searcher .doc (result .hits [0 ][1 ])['rating' ][0 ] == 3.5
185
+
186
+ integer_query = index .parse_query ("1" , ["id" ])
187
+ result = searcher .search (integer_query )
188
+ assert len (result .hits ) == 1
189
+
190
+ # 0 result
191
+ integer_query = index .parse_query ("10" , ["id" ])
192
+ result = searcher .search (integer_query )
193
+ assert len (result .hits ) == 0
194
+
123
195
def test_and_query_parser_default_fields (self , ram_index ):
124
196
query = ram_index .parse_query ("winter" , default_field_names = ["title" ])
125
197
assert repr (query ) == """Query(TermQuery(Term(type=Str, field=0, "winter")))"""
@@ -279,8 +351,9 @@ def test_create_readers(self):
279
351
280
352
281
353
class TestSearcher (object ):
282
- def test_searcher_repr (self , ram_index ):
354
+ def test_searcher_repr (self , ram_index , ram_index_numeric_fields ):
283
355
assert repr (ram_index .searcher ()) == "Searcher(num_docs=3, num_segments=1)"
356
+ assert repr (ram_index_numeric_fields .searcher ()) == "Searcher(num_docs=2, num_segments=1)"
284
357
285
358
286
359
class TestDocument (object ):
0 commit comments