예제 #1
0
파일: search.go 프로젝트: acgshare/acgsh
func Init() {
	// open a new index
	var err error
	index, err = bleve.Open("acgsh.bleve")
	if err != nil {
		log.Println(err)
		mapping := bleve.NewIndexMapping()
		index, err = bleve.New("acgsh.bleve", mapping)
		if err != nil {
			log.Println(err)
			return
		}
	}

}
예제 #2
0
파일: main.go 프로젝트: acgshare/bleve
func main() {

	flag.Parse()

	if *indexPath == "" {
		log.Fatal("must specify index path")
	}

	// open the index
	index, err := bleve.Open(*indexPath)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		cerr := index.Close()
		if cerr != nil {
			log.Fatalf("error closing index: %v", err)
		}
	}()

	if flag.NArg() < 1 {
		log.Fatal("must specify at least one path to index")
	}

	for file := range handleArgs(flag.Args()) {
		// index the files
		docID := file.filename
		if !*keepDir {
			_, docID = filepath.Split(docID)
		}
		if !*keepExt {
			ext := filepath.Ext(docID)
			docID = docID[0 : len(docID)-len(ext)]
		}
		log.Printf("Indexing: %s", docID)
		err = index.Index(docID, file.contents)
		if err != nil {
			log.Fatal(err)
		}
	}
}
예제 #3
0
파일: main.go 프로젝트: acgshare/bleve
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, strings.TrimSpace(`
bleve_dump prints the properties and binary representations of all rows in the
index specified by -index.
`)+"\n\n")
		flag.PrintDefaults()
	}
	flag.Parse()
	if len(flag.Args()) > 0 {
		log.Fatalf("unexpected argument '%s', use -help to see possible options",
			flag.Args()[0])
	}
	if *indexPath == "" {
		log.Fatal("specify index to dump")
	}

	index, err := bleve.Open(*indexPath)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		cerr := index.Close()
		if cerr != nil {
			log.Fatalf("error closing index: %v", err)
		}
	}()

	if *mappingOnly {
		if *docID != "" || *fieldsOnly {
			log.Fatal("-mapping cannot be used with -docID or -fields")
		}
		mapping := index.Mapping()
		jsonBytes, err := json.MarshalIndent(mapping, "", "  ")
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s\n", jsonBytes)
		return
	}

	var dumpChan chan interface{}
	if *docID != "" {
		if *fieldsOnly {
			log.Fatal("-docID cannot be used with -fields")
		}
		dumpChan = index.DumpDoc(*docID)
	} else if *fieldsOnly {
		dumpChan = index.DumpFields()
	} else {
		dumpChan = index.DumpAll()
	}

	for rowOrErr := range dumpChan {
		switch rowOrErr := rowOrErr.(type) {
		case error:
			log.Printf("error dumping: %v", rowOrErr)
		case upside_down.UpsideDownCouchRow:
			fmt.Printf("%v\n", rowOrErr)
			fmt.Printf("Key:   % -100x\nValue: % -100x\n\n", rowOrErr.Key(), rowOrErr.Value())
		}
	}
}
예제 #4
0
파일: main.go 프로젝트: acgshare/bleve
func main() {

	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		err = pprof.StartCPUProfile(f)
		if err != nil {
			log.Fatal(err)
		}
		defer pprof.StopCPUProfile()
	}

	if *indexPath == "" {
		log.Fatal("Specify index to query")
	}

	if flag.NArg() < 1 {
		log.Fatal("Specify search query")
	}

	// open index
	index, err := bleve.Open(*indexPath)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		cerr := index.Close()
		if cerr != nil {
			log.Fatalf("error closing index: %v", err)
		}
	}()

	for i := 0; i < *repeat; i++ {
		var query bleve.Query

		switch *qtype {
		case "prefix":
			pquery := bleve.NewPrefixQuery(strings.Join(flag.Args(), " "))
			if *qfield != "" {
				pquery.SetField(*qfield)
			}
			query = pquery
		case "term":
			pquery := bleve.NewTermQuery(strings.Join(flag.Args(), " "))
			if *qfield != "" {
				pquery.SetField(*qfield)
			}
			query = pquery
		default:
			// build a search with the provided parameters
			queryString := strings.Join(flag.Args(), " ")
			query = bleve.NewQueryStringQuery(queryString)
		}

		searchRequest := bleve.NewSearchRequestOptions(query, *limit, *skip, *explain)

		// enable highlights if requested
		if *includeHighlights {
			searchRequest.Highlight = bleve.NewHighlightWithStyle("ansi")
		}

		if *includeStoredFields {
			searchRequest.Fields = []string{"*"}
		}

		// execute the search
		searchResult, err := index.Search(searchRequest)
		if err != nil {
			log.Fatalf("search error: %v", err)
		}
		fmt.Println(searchResult)
	}
}
예제 #5
0
파일: main.go 프로젝트: acgshare/bleve
func main() {

	flag.Parse()

	if *indexPath == "" {
		log.Fatal("must specify index path")
	}

	// open the index
	index, err := bleve.Open(*indexPath)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		cerr := index.Close()
		if cerr != nil {
			log.Fatalf("error closing index: %v", err)
		}
	}()

	if flag.NArg() < 1 {
		log.Fatal("must specify at least one path to index")
	}

	i := 0
	batch := index.NewBatch()

	for _, file := range flag.Args() {

		file, err := os.Open(file)
		defer func() {
			cerr := file.Close()
			if cerr != nil {
				log.Fatalf("error closing file: %v", cerr)
			}
		}()
		if err != nil {
			log.Fatal(err)
		}

		log.Printf("Indexing: %s\n", file.Name())
		r := bufio.NewReader(file)

		for {
			if i%*batchSize == 0 {
				log.Printf("Indexing batch (%d docs)...\n", i)
				err := index.Batch(batch)
				if err != nil {
					log.Fatal(err)
				}
				batch = index.NewBatch()
			}

			b, _ := r.ReadBytes('\n')
			if len(b) == 0 {
				break
			}
			docID := randomString(5)
			err := batch.Index(docID, b)
			if err != nil {
				log.Fatal(err)
			}
			i++
		}
		err = index.Batch(batch)
		if err != nil {
			log.Fatal(err)
		}
	}
}