@@ -103,6 +103,20 @@ def prepare(self, obj):
103
103
return data
104
104
105
105
106
+ class ElasticsearchFacetingMockSearchIndex (indexes .SearchIndex , indexes .Indexable ):
107
+ text = indexes .CharField (document = True )
108
+ author = indexes .CharField (model_attr = 'author' , faceted = True )
109
+ editor = indexes .CharField (model_attr = 'editor' , faceted = True )
110
+ pub_date = indexes .DateField (model_attr = 'pub_date' , faceted = True )
111
+ facet_field = indexes .FacetCharField (model_attr = 'author' )
112
+
113
+ def prepare_text (self , obj ):
114
+ return '%s %s' % (obj .author , obj .editor )
115
+
116
+ def get_model (self ):
117
+ return AFourthMockModel
118
+
119
+
106
120
class ElasticsearchRoundTripSearchIndex (indexes .SearchIndex , indexes .Indexable ):
107
121
text = indexes .CharField (document = True , default = '' )
108
122
name = indexes .CharField ()
@@ -1227,3 +1241,89 @@ def test_recreate_index(self):
1227
1241
self .fail ("There is no mapping after recreating the index" )
1228
1242
self .assertEqual (original_mapping , updated_mapping ,
1229
1243
"Mapping after recreating the index differs from the original one" )
1244
+
1245
+
1246
+ class ElasticsearchFacetingTestCase (TestCase ):
1247
+ def setUp (self ):
1248
+ super (ElasticsearchFacetingTestCase , self ).setUp ()
1249
+
1250
+ # Wipe it clean.
1251
+ clear_elasticsearch_index ()
1252
+
1253
+ # Stow.
1254
+ self .old_ui = connections ['default' ].get_unified_index ()
1255
+ self .ui = UnifiedIndex ()
1256
+ self .smmi = ElasticsearchFacetingMockSearchIndex ()
1257
+ self .ui .build (indexes = [self .smmi ])
1258
+ connections ['default' ]._index = self .ui
1259
+ self .sb = connections ['default' ].get_backend ()
1260
+
1261
+ # Force the backend to rebuild the mapping each time.
1262
+ self .sb .existing_mapping = {}
1263
+ self .sb .setup ()
1264
+
1265
+ self .sample_objs = []
1266
+
1267
+ for i in range (1 , 10 ):
1268
+ mock = AFourthMockModel ()
1269
+ mock .id = i
1270
+ if i > 5 :
1271
+ mock .editor = 'George Taylor'
1272
+ else :
1273
+ mock .editor = 'Perry White'
1274
+ if i % 2 :
1275
+ mock .author = 'Daniel Lindsley'
1276
+ else :
1277
+ mock .author = 'Dan Watson'
1278
+ mock .pub_date = datetime .date (2013 , 9 , (i % 4 ) + 1 )
1279
+ self .sample_objs .append (mock )
1280
+
1281
+ def tearDown (self ):
1282
+ connections ['default' ]._index = self .old_ui
1283
+ super (ElasticsearchFacetingTestCase , self ).tearDown ()
1284
+
1285
+ def test_facet (self ):
1286
+ self .sb .update (self .smmi , self .sample_objs )
1287
+ counts = SearchQuerySet ().facet ('author' ).facet ('editor' ).facet_counts ()
1288
+ self .assertEqual (counts ['fields' ]['author' ], [
1289
+ ('Daniel Lindsley' , 5 ),
1290
+ ('Dan Watson' , 4 ),
1291
+ ])
1292
+ self .assertEqual (counts ['fields' ]['editor' ], [
1293
+ ('Perry White' , 5 ),
1294
+ ('George Taylor' , 4 ),
1295
+ ])
1296
+ counts = SearchQuerySet ().filter (content = 'white' ).facet ('facet_field' , order = 'reverse_count' ).facet_counts ()
1297
+ self .assertEqual (counts ['fields' ]['facet_field' ], [
1298
+ ('Dan Watson' , 2 ),
1299
+ ('Daniel Lindsley' , 3 ),
1300
+ ])
1301
+
1302
+ def test_narrow (self ):
1303
+ self .sb .update (self .smmi , self .sample_objs )
1304
+ counts = SearchQuerySet ().facet ('author' ).facet ('editor' ).narrow ('editor_exact:"Perry White"' ).facet_counts ()
1305
+ self .assertEqual (counts ['fields' ]['author' ], [
1306
+ ('Daniel Lindsley' , 3 ),
1307
+ ('Dan Watson' , 2 ),
1308
+ ])
1309
+ self .assertEqual (counts ['fields' ]['editor' ], [
1310
+ ('Perry White' , 5 ),
1311
+ ])
1312
+
1313
+ def test_date_facet (self ):
1314
+ self .sb .update (self .smmi , self .sample_objs )
1315
+ start = datetime .date (2013 , 9 , 1 )
1316
+ end = datetime .date (2013 , 9 , 30 )
1317
+ # Facet by day
1318
+ counts = SearchQuerySet ().date_facet ('pub_date' , start_date = start , end_date = end , gap_by = 'day' ).facet_counts ()
1319
+ self .assertEqual (counts ['dates' ]['pub_date' ], [
1320
+ (datetime .datetime (2013 , 9 , 1 ), 2 ),
1321
+ (datetime .datetime (2013 , 9 , 2 ), 3 ),
1322
+ (datetime .datetime (2013 , 9 , 3 ), 2 ),
1323
+ (datetime .datetime (2013 , 9 , 4 ), 2 ),
1324
+ ])
1325
+ # By month
1326
+ counts = SearchQuerySet ().date_facet ('pub_date' , start_date = start , end_date = end , gap_by = 'month' ).facet_counts ()
1327
+ self .assertEqual (counts ['dates' ]['pub_date' ], [
1328
+ (datetime .datetime (2013 , 9 , 1 ), 9 ),
1329
+ ])
0 commit comments