5
5
6
6
7
7
def schema ():
8
- return SchemaBuilder ().add_text_field ("title" , stored = True ).add_text_field ("body" ).build ()
8
+ return (
9
+ SchemaBuilder ()
10
+ .add_text_field ("title" , stored = True )
11
+ .add_text_field ("body" )
12
+ .build ()
13
+ )
14
+
9
15
10
16
def create_index (dir = None ):
11
17
# assume all tests will use the same documents for now
@@ -99,7 +105,9 @@ def test_simple_search_in_ram(self, ram_index):
99
105
100
106
def test_and_query (self , ram_index ):
101
107
index = ram_index
102
- query = index .parse_query ("title:men AND body:summer" , default_field_names = ["title" , "body" ])
108
+ query = index .parse_query (
109
+ "title:men AND body:summer" , default_field_names = ["title" , "body" ]
110
+ )
103
111
# look for an intersection of documents
104
112
searcher = index .searcher ()
105
113
result = searcher .search (query , 10 )
@@ -119,7 +127,8 @@ def test_and_query_parser_default_fields(self, ram_index):
119
127
def test_and_query_parser_default_fields_undefined (self , ram_index ):
120
128
query = ram_index .parse_query ("winter" )
121
129
assert (
122
- repr (query ) == """Query(BooleanQuery { subqueries: [(Should, TermQuery(Term(type=Str, field=0, "winter"))), (Should, TermQuery(Term(type=Str, field=1, "winter")))] })"""
130
+ repr (query )
131
+ == """Query(BooleanQuery { subqueries: [(Should, TermQuery(Term(type=Str, field=0, "winter"))), (Should, TermQuery(Term(type=Str, field=1, "winter")))] })"""
123
132
)
124
133
125
134
def test_query_errors (self , ram_index ):
@@ -129,9 +138,11 @@ def test_query_errors(self, ram_index):
129
138
index .parse_query ("bod:men" , ["title" , "body" ])
130
139
131
140
def test_order_by_search (self ):
132
- schema = (SchemaBuilder ()
141
+ schema = (
142
+ SchemaBuilder ()
133
143
.add_unsigned_field ("order" , fast = "single" )
134
- .add_text_field ("title" , stored = True ).build ()
144
+ .add_text_field ("title" , stored = True )
145
+ .build ()
135
146
)
136
147
137
148
index = Index (schema )
@@ -152,15 +163,13 @@ def test_order_by_search(self):
152
163
doc .add_unsigned ("order" , 1 )
153
164
doc .add_text ("title" , "Another test title" )
154
165
155
-
156
166
writer .add_document (doc )
157
167
158
168
writer .commit ()
159
169
index .reload ()
160
170
161
171
query = index .parse_query ("test" )
162
172
163
-
164
173
searcher = index .searcher ()
165
174
166
175
result = searcher .search (query , 10 , offset = 2 , order_by_field = "order" )
@@ -184,9 +193,11 @@ def test_order_by_search(self):
184
193
assert searched_doc ["title" ] == ["Test title" ]
185
194
186
195
def test_order_by_search_without_fast_field (self ):
187
- schema = (SchemaBuilder ()
196
+ schema = (
197
+ SchemaBuilder ()
188
198
.add_unsigned_field ("order" )
189
- .add_text_field ("title" , stored = True ).build ()
199
+ .add_text_field ("title" , stored = True )
200
+ .build ()
190
201
)
191
202
192
203
index = Index (schema )
@@ -319,3 +330,66 @@ def test_document_with_facet(self):
319
330
def test_document_error (self ):
320
331
with pytest .raises (ValueError ):
321
332
tantivy .Document (name = {})
333
+
334
+
335
+ class TestJsonField :
336
+ def test_query_from_json_field (self ):
337
+ schema = (
338
+ SchemaBuilder ()
339
+ .add_json_field (
340
+ "attributes" ,
341
+ stored = True ,
342
+ tokenizer_name = "default" ,
343
+ index_option = "position" ,
344
+ )
345
+ .build ()
346
+ )
347
+
348
+ index = Index (schema )
349
+
350
+ writer = index .writer ()
351
+
352
+ doc = Document ()
353
+ doc .add_json (
354
+ "attributes" ,
355
+ """{
356
+ "target": "submit-button",
357
+ "cart": {"product_id": 103},
358
+ "description": "the best vacuum cleaner ever"
359
+ }""" ,
360
+ )
361
+
362
+ writer .add_document (doc )
363
+
364
+ doc = Document ()
365
+ doc .add_json (
366
+ "attributes" ,
367
+ """{
368
+ "target": "submit-button",
369
+ "cart": {"product_id": 133},
370
+ "description": "das keyboard"
371
+ }""" ,
372
+ )
373
+
374
+ writer .add_document (doc )
375
+
376
+ writer .commit ()
377
+ index .reload ()
378
+
379
+ query = index .parse_query ("target:submit-button" , ["attributes" ])
380
+ result = index .searcher ().search (query , 2 )
381
+ assert len (result .hits ) == 2
382
+
383
+ query = index .parse_query ("target:submit" , ["attributes" ])
384
+ result = index .searcher ().search (query , 2 )
385
+ assert len (result .hits ) == 2
386
+
387
+ # query = index.parse_query_for_attributes("cart.product_id:103")
388
+ # result = index.searcher().search(query, 1)
389
+ # assert len(result.hits) == 1
390
+
391
+ # query = index.parse_query_for_attributes(
392
+ # "target:submit-button AND cart.product_id:133"
393
+ # )
394
+ # result = index.searcher().search(query, 2)
395
+ # assert len(result.hits) == 1
0 commit comments