예제 #1
0
파일: corpus.go 프로젝트: piger/corpus
func runSearch(index *corpus.Index, args []string) {
	// Execute query and display results.
	results, err := index.Search(strings.Join(args, " "), 0, *limit, *highlights)
	if err != nil {
		log.Fatalf("Search error: %s\n", err)
	} else if results.Total == 0 {
		log.Printf("No results")
		return
	}
	for _, hit := range results.Hits {
		if *noScores {
			fmt.Printf("%s\n", hit.ID)
		} else {
			fmt.Printf(" %-6.4f  %s\n", hit.Score, hit.ID)
		}
		if *highlights {
			hl := ""
			for _, fragments := range hit.Fragments {
				for _, fragment := range fragments {
					hl += fmt.Sprintf("%s", fragment)
				}
			}
			fmt.Printf("%s\n", hl)
		}
	}
}
예제 #2
0
파일: corpus.go 프로젝트: piger/corpus
func runIndex(index *corpus.Index, args []string) {
	docs := make([]corpus.Document, 0)

	w := &corpus.Walker{
		Exclude: excludes,
		Include: includes,
		MinSize: 1024,
	}

	// For each argument, process it or recurse if it's a directory.
	for _, root := range args {
		log.Printf("scanning %s ...", root)
		err := w.Walk(root, func(path string, info os.FileInfo, fileErr error) error {
			if fileErr == nil {
				doc, err := file.New(path)
				if err != nil {
					log.Printf("%s: %v", path, err)
				} else {
					docs = append(docs, doc)
				}
			}
			return nil
		})
		if err != nil {
			log.Printf("Cannot scan %s: %s", root, err)
		}
	}

	if len(docs) == 0 {
		log.Fatal("No documents found!")
	}

	// Now add all documents to the index.
	if err := index.Insert(docs); err != nil {
		log.Fatal(err)
	}

	log.Printf("added %d documents", len(docs))
}