@@ -403,3 +403,91 @@ func ExampleClient_HybridSearch() {
403403 log .Println ("Scores: " , resultSet .Scores )
404404 }
405405}
406+
407+ func ExampleClient_Search_textMatch () {
408+ ctx , cancel := context .WithCancel (context .Background ())
409+ defer cancel ()
410+
411+ collectionName := "text_min_match"
412+ titleField := "title"
413+ textField := "document_text"
414+ titleSparse := "title_sparse_vector"
415+ textSparse := "text_sparse_vector"
416+ milvusAddr := "127.0.0.1:19530"
417+
418+ cli , err := milvusclient .New (ctx , & milvusclient.ClientConfig {
419+ Address : milvusAddr ,
420+ })
421+ if err != nil {
422+ log .Fatal ("failed to connect to milvus server: " , err .Error ())
423+ }
424+ defer cli .Close (ctx )
425+
426+ _ = cli .DropCollection (ctx , milvusclient .NewDropCollectionOption (collectionName ))
427+
428+ schema := entity .NewSchema ().
429+ WithField (entity .NewField ().WithName ("id" ).WithDataType (entity .FieldTypeInt64 ).WithIsPrimaryKey (true ).WithIsAutoID (true )).
430+ WithField (entity .NewField ().WithName (titleField ).WithDataType (entity .FieldTypeVarChar ).WithMaxLength (512 ).WithEnableAnalyzer (true ).WithEnableMatch (true )).
431+ WithField (entity .NewField ().WithName (textField ).WithDataType (entity .FieldTypeVarChar ).WithMaxLength (2048 ).WithEnableAnalyzer (true ).WithEnableMatch (true )).
432+ WithField (entity .NewField ().WithName (titleSparse ).WithDataType (entity .FieldTypeSparseVector )).
433+ WithField (entity .NewField ().WithName (textSparse ).WithDataType (entity .FieldTypeSparseVector )).
434+ WithFunction (entity .NewFunction ().WithName ("title_bm25_func" ).WithType (entity .FunctionTypeBM25 ).WithInputFields (titleField ).WithOutputFields (titleSparse )).
435+ WithFunction (entity .NewFunction ().WithName ("text_bm25_func" ).WithType (entity .FunctionTypeBM25 ).WithInputFields (textField ).WithOutputFields (textSparse ))
436+
437+ idxOpts := []milvusclient.CreateIndexOption {
438+ milvusclient .NewCreateIndexOption (collectionName , titleField , index .NewInvertedIndex ()),
439+ milvusclient .NewCreateIndexOption (collectionName , textField , index .NewInvertedIndex ()),
440+ milvusclient .NewCreateIndexOption (collectionName , titleSparse , index .NewSparseInvertedIndex (entity .BM25 , 0.2 )),
441+ milvusclient .NewCreateIndexOption (collectionName , textSparse , index .NewSparseInvertedIndex (entity .BM25 , 0.2 )),
442+ }
443+
444+ err = cli .CreateCollection (ctx , milvusclient .NewCreateCollectionOption (collectionName , schema ).WithIndexOptions (idxOpts ... ))
445+ if err != nil {
446+ log .Fatal ("failed to create collection: " , err .Error ())
447+ }
448+
449+ _ , err = cli .Insert (ctx , milvusclient .NewColumnBasedInsertOption (collectionName ).
450+ WithVarcharColumn (titleField , []string {
451+ "History of AI" ,
452+ "Alan Turing Biography" ,
453+ "Machine Learning Overview" ,
454+ }).
455+ WithVarcharColumn (textField , []string {
456+ "Artificial intelligence was founded in 1956 by computer scientists." ,
457+ "Alan Turing proposed early concepts of AI and machine learning." ,
458+ "Machine learning is a subset of artificial intelligence." ,
459+ }))
460+ if err != nil {
461+ log .Fatal ("failed to insert data: " , err .Error ())
462+ }
463+
464+ task , err := cli .LoadCollection (ctx , milvusclient .NewLoadCollectionOption (collectionName ))
465+ if err != nil {
466+ log .Fatal ("failed to load collection: " , err .Error ())
467+ }
468+ _ = task .Await (ctx )
469+
470+ q := "artificial intelligence"
471+ expr := "text_match(" + titleField + ", \" " + q + "\" , minimum_should_match=2) OR text_match(" + textField + ", \" " + q + "\" , minimum_should_match=2)"
472+
473+ boost := entity .NewFunction ().
474+ WithName ("title_boost" ).
475+ WithType (entity .FunctionTypeRerank ).
476+ WithParam ("reranker" , "boost" ).
477+ WithParam ("filter" , "text_match(" + titleField + ", \" " + q + "\" , minimum_should_match=2)" ).
478+ WithParam ("weight" , "2.0" )
479+
480+ vectors := []entity.Vector {entity .Text (q )}
481+ rs , err := cli .Search (ctx , milvusclient .NewSearchOption (collectionName , 5 , vectors ).
482+ WithANNSField (textSparse ).
483+ WithFilter (expr ).
484+ WithOutputFields ("id" , titleField , textField ).
485+ WithFunctionReranker (boost ))
486+ if err != nil {
487+ log .Fatal ("failed to search: " , err .Error ())
488+ }
489+
490+ for _ , r := range rs {
491+ _ = r .ResultCount
492+ }
493+ }
0 commit comments