@@ -205,7 +205,21 @@ func runServe(cmd *cobra.Command, args []string) error {
205205 }
206206 defer idx .Close ()
207207
208- if cfg .AutoReindex && idx .ShouldReindex (cfg .ReindexIntervalHours ) {
208+ count , err := idx .GetDocCount ()
209+ if err != nil {
210+ return err
211+ }
212+
213+ if count == 0 {
214+ log .Infof ("index is empty, building initial index..." )
215+ go func () {
216+ if err := idx .ReindexAll (); err != nil {
217+ log .Errorf ("initial index build failed: %v" , err )
218+ } else {
219+ log .Infof ("initial index build complete" )
220+ }
221+ }()
222+ } else if cfg .AutoReindex && idx .ShouldReindex (cfg .ReindexIntervalHours ) {
209223 log .Infof ("auto-reindex triggered (interval: %d hours)" , cfg .ReindexIntervalHours )
210224 go func () {
211225 if err := idx .ReindexAll (); err != nil {
@@ -289,7 +303,7 @@ func runSearch(cmd *cobra.Command, args []string) error {
289303 limit = 10000
290304 }
291305
292- opts := & client.SearchOptions {
306+ clientOpts := & client.SearchOptions {
293307 Query : query ,
294308 Limit : limit ,
295309 Field : searchField ,
@@ -301,7 +315,51 @@ func runSearch(cmd *cobra.Command, args []string) error {
301315 MaxSize : searchMaxSize ,
302316 }
303317
304- result , err := client .SearchWithOptions (opts )
318+ result , err := client .SearchWithOptions (clientOpts )
319+ if err == nil {
320+ if searchJSON {
321+ enc := json .NewEncoder (os .Stdout )
322+ enc .SetIndent ("" , " " )
323+ return enc .Encode (result )
324+ }
325+
326+ log .Infof ("found %d results in %s" , result .Total , result .Took )
327+ for i , hit := range result .Hits {
328+ log .Infof ("%d. %s (score: %.4f)" , i + 1 , hit .ID , hit .Score )
329+ }
330+ return nil
331+ }
332+
333+ cfg := buildConfig ()
334+
335+ idx , err := indexer .New (cfg )
336+ if err != nil {
337+ return fmt .Errorf ("server not running and cannot open index: %v" , err )
338+ }
339+ defer idx .Close ()
340+
341+ count , err := idx .GetDocCount ()
342+ if err != nil {
343+ return err
344+ }
345+
346+ if count == 0 {
347+ return fmt .Errorf ("index is empty - run 'dsearch index generate' or 'dsearch serve' first" )
348+ }
349+
350+ indexerOpts := & indexer.SearchOptions {
351+ Query : query ,
352+ Limit : limit ,
353+ Field : searchField ,
354+ Extension : searchExt ,
355+ Fuzzy : searchFuzzy ,
356+ SortBy : searchSort ,
357+ SortDesc : searchSortDesc ,
358+ MinSize : searchMinSize ,
359+ MaxSize : searchMaxSize ,
360+ }
361+
362+ result , err = idx .SearchWithOptions (indexerOpts )
305363 if err != nil {
306364 return err
307365 }
@@ -322,35 +380,76 @@ func runSearch(cmd *cobra.Command, args []string) error {
322380
323381func runIndexGenerate (cmd * cobra.Command , args []string ) error {
324382 status , err := client .Reindex ()
383+ if err == nil {
384+ log .Infof ("%s" , status )
385+ return nil
386+ }
387+
388+ cfg := buildConfig ()
389+
390+ idx , err := indexer .New (cfg )
325391 if err != nil {
392+ return fmt .Errorf ("server not running and cannot open index: %v" , err )
393+ }
394+ defer idx .Close ()
395+
396+ log .Infof ("starting full reindex..." )
397+ if err := idx .ReindexAll (); err != nil {
326398 return err
327399 }
328400
329- log .Infof ("%s" , status )
330401 return nil
331402}
332403
333404func runIndexSync (cmd * cobra.Command , args []string ) error {
334405 status , err := client .Sync ()
406+ if err == nil {
407+ log .Infof ("%s" , status )
408+ return nil
409+ }
410+
411+ cfg := buildConfig ()
412+
413+ idx , err := indexer .New (cfg )
335414 if err != nil {
415+ return fmt .Errorf ("server not running and cannot open index: %v" , err )
416+ }
417+ defer idx .Close ()
418+
419+ log .Infof ("starting incremental sync..." )
420+ if err := idx .SyncIncremental (); err != nil {
336421 return err
337422 }
338423
339- log .Infof ("%s" , status )
340424 return nil
341425}
342426
343427func runIndexStatus (cmd * cobra.Command , args []string ) error {
344428 stats , err := client .Stats ()
429+ if err == nil {
430+ log .Infof ("Index Statistics:" )
431+ log .Infof (" Total files: %v" , stats ["total_files" ])
432+ log .Infof (" Total bytes: %v" , stats ["total_bytes" ])
433+ log .Infof (" Last index: %v" , stats ["last_index_time" ])
434+ log .Infof (" Duration: %v" , stats ["index_duration" ])
435+ return nil
436+ }
437+
438+ cfg := buildConfig ()
439+
440+ idx , err := indexer .New (cfg )
345441 if err != nil {
346- return err
442+ return fmt . Errorf ( "server not running and cannot open index: %v" , err )
347443 }
444+ defer idx .Close ()
445+
446+ indexStats := idx .Stats ()
348447
349448 log .Infof ("Index Statistics:" )
350- log .Infof (" Total files: %v " , stats [ "total_files" ] )
351- log .Infof (" Total bytes: %v " , stats [ "total_bytes" ] )
352- log .Infof (" Last index: %v" , stats [ "last_index_time" ] )
353- log .Infof (" Duration: %v " , stats [ "index_duration" ] )
449+ log .Infof (" Total files: %d " , indexStats . TotalFiles )
450+ log .Infof (" Total bytes: %d " , indexStats . TotalBytes )
451+ log .Infof (" Last index: %v" , indexStats . LastIndexTime . Format ( "2006-01-02 15:04:05" ) )
452+ log .Infof (" Duration: %s " , indexStats . IndexDuration )
354453
355454 return nil
356455}
0 commit comments