@@ -25,7 +25,6 @@ func getTestConnectionDetails() (string, string) {
2525 return host , password
2626}
2727
28-
2928func createClient (indexName string ) * Client {
3029 host , password := getTestConnectionDetails ()
3130 if password != "" {
@@ -39,12 +38,11 @@ func createClient(indexName string) *Client {
3938 return err
4039 }
4140 return NewClientFromPool (pool , indexName )
42- }else {
41+ } else {
4342 return NewClient (host , indexName )
4443 }
4544}
4645
47-
4846func createAutocompleter (dictName string ) * Autocompleter {
4947 host , password := getTestConnectionDetails ()
5048 if password != "" {
@@ -398,9 +396,9 @@ func TestSpellCheck(t *testing.T) {
398396 }
399397
400398 assert .Nil (t , c .Index (docs ... ))
401- query := NewQuery ("Anla Portuga" )
402- opts := NewSpellCheckOptions (2 )
403- sugs , total , err := c .SpellCheck (query ,opts )
399+ query := NewQuery ("Anla Portuga" )
400+ opts := NewSpellCheckOptions (2 )
401+ sugs , total , err := c .SpellCheck (query , opts )
404402 assert .Nil (t , err )
405403 assert .Equal (t , 2 , len (sugs ))
406404 assert .Equal (t , 2 , total )
@@ -409,21 +407,68 @@ func TestSpellCheck(t *testing.T) {
409407 // 1) 1) "TERM"
410408 // 2) "an"
411409 // 3) (empty list or set)
412- queryEmpty := NewQuery ("An" )
413- sugs , total , err = c .SpellCheck (queryEmpty ,opts )
410+ queryEmpty := NewQuery ("An" )
411+ sugs , total , err = c .SpellCheck (queryEmpty , opts )
414412 assert .Nil (t , err )
415413 assert .Equal (t , 1 , len (sugs ))
416414 assert .Equal (t , 0 , total )
417415
418416 // same query but now with a distance of 4
419417 opts .SetDistance (4 )
420- sugs , total , err = c .SpellCheck (queryEmpty ,opts )
418+ sugs , total , err = c .SpellCheck (queryEmpty , opts )
421419 assert .Nil (t , err )
422420 assert .Equal (t , 1 , len (sugs ))
423421 assert .Equal (t , 1 , total )
424422
425423}
426424
425+ func TestFilter (t * testing.T ) {
426+ c := createClient ("testFilter" )
427+ // Create a schema
428+ sc := NewSchema (DefaultOptions ).
429+ AddField (NewTextField ("body" )).
430+ AddField (NewTextFieldOptions ("title" , TextFieldOptions {Weight : 5.0 , Sortable : true })).
431+ AddField (NewNumericField ("age" )).
432+ AddField (NewGeoFieldOptions ("location" , GeoFieldOptions {}))
433+
434+ // Drop an existing index. If the index does not exist an error is returned
435+ c .Drop ()
436+ assert .Nil (t , c .CreateIndex (sc ))
437+
438+ // Create a document with an id and given score
439+ doc := NewDocument ("doc1" , 1.0 )
440+ doc .Set ("title" , "Hello world" ).
441+ Set ("body" , "foo bar" ).
442+ Set ("age" , 18 ).
443+ Set ("location" , "13.361389,38.115556" )
444+
445+ assert .Nil (t , c .IndexOptions (DefaultIndexingOptions , doc ))
446+ // Searching with NumericFilter
447+ docs , total , err := c .Search (NewQuery ("hello world" ).
448+ AddFilter (Filter {Field : "age" , Options : NumericFilterOptions {Min : 1 , Max : 20 }}).
449+ SetSortBy ("age" , true ).
450+ SetReturnFields ("body" ))
451+ assert .Nil (t , err )
452+ assert .Equal (t , 1 , total )
453+ assert .Equal (t , "foo bar" , docs [0 ].Properties ["body" ])
454+
455+ // Searching with GeoFilter
456+ docs , total , err = c .Search (NewQuery ("hello world" ).
457+ AddFilter (Filter {Field : "location" , Options : GeoFilterOptions {Lon : 15 , Lat : 37 , Radius : 200 , Unit : KILOMETERS }}).
458+ SetSortBy ("age" , true ).
459+ SetReturnFields ("age" ))
460+ assert .Nil (t , err )
461+ assert .Equal (t , 1 , total )
462+ assert .Equal (t , "18" , docs [0 ].Properties ["age" ])
463+
464+ docs , total , err = c .Search (NewQuery ("hello world" ).
465+ AddFilter (Filter {Field : "location" , Options : GeoFilterOptions {Lon : 10 , Lat : 13 , Radius : 1 , Unit : KILOMETERS }}).
466+ SetSortBy ("age" , true ).
467+ SetReturnFields ("body" ))
468+ assert .Nil (t , err )
469+ assert .Equal (t , 0 , total )
470+ }
471+
427472func ExampleClient () {
428473
429474 // Create a client. By default a client is schemaless
@@ -434,7 +479,8 @@ func ExampleClient() {
434479 sc := NewSchema (DefaultOptions ).
435480 AddField (NewTextField ("body" )).
436481 AddField (NewTextFieldOptions ("title" , TextFieldOptions {Weight : 5.0 , Sortable : true })).
437- AddField (NewNumericField ("date" ))
482+ AddField (NewNumericField ("date" )).
483+ AddField (NewGeoFieldOptions ("location" , GeoFieldOptions {}))
438484
439485 // Drop an existing index. If the index does not exist an error is returned
440486 c .Drop ()
@@ -448,7 +494,8 @@ func ExampleClient() {
448494 doc := NewDocument ("doc1" , 1.0 )
449495 doc .Set ("title" , "Hello world" ).
450496 Set ("body" , "foo bar" ).
451- Set ("date" , time .Now ().Unix ())
497+ Set ("date" , time .Now ().Unix ()).
498+ Set ("location" , "13.361389,38.115556" )
452499
453500 // Index the document. The API accepts multiple documents at a time
454501 if err := c .IndexOptions (DefaultIndexingOptions , doc ); err != nil {
@@ -463,5 +510,3 @@ func ExampleClient() {
463510 fmt .Println (docs [0 ].Id , docs [0 ].Properties ["title" ], total , err )
464511 // Output: doc1 Hello world 1 <nil>
465512}
466-
467-
0 commit comments