func (b *Index) Search(queryStr string, offset, limit int, highlights bool) (*bleve.SearchResult, error) { query := bleve.NewQueryStringQuery(queryStr) req := bleve.NewSearchRequestOptions(query, limit, offset, false) if highlights { req.Highlight = bleve.NewHighlightWithStyle("ansi") } return b.index.Search(req) }
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++ { // 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") } // execute the search searchResult, err := index.Search(searchRequest) if err != nil { log.Fatalf("search error: %v", err) } fmt.Println(searchResult) } }
// START OMIT func main() { index, err := bleve.Open("gophercon.bleve") if err != nil { log.Fatal(err) } qString := `+content:debugger ` // HLQUERY qString += `title:"delve" ` // HLQUERY qString += `title:go~2 ` // HLQUERY qString += `-content:rust ` // HLQUERY qString += `word_count:>30` // HLQUERY q := bleve.NewQueryStringQuery(qString) req := bleve.NewSearchRequest(q) req.Highlight = bleve.NewHighlightWithStyle("html") req.Fields = []string{"title", "author", "content", "word_count"} res, err := index.Search(req) if err != nil { log.Fatal(err) } fmt.Println(res) }
func main() { flag.Parse() 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 index.Close() // 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") } // execute the search searchResult, err := index.Search(searchRequest) if err != nil { log.Fatalf("search error: %v", err) } fmt.Println(searchResult) }
func query(term, highlight string, index bleve.Index, u content.User, feedIds []data.FeedId, paging ...int) (ua []content.UserArticle, err error) { var query bleve.Query query = bleve.NewQueryStringQuery(term) if len(feedIds) > 0 { queries := make([]bleve.Query, len(feedIds)) conjunct := make([]bleve.Query, 2) for i, id := range feedIds { q := bleve.NewTermQuery(strconv.FormatInt(int64(id), 10)) q.SetField("FeedId") queries[i] = q } disjunct := bleve.NewDisjunctionQuery(queries) conjunct[0] = query conjunct[1] = disjunct query = bleve.NewConjunctionQuery(conjunct) } searchRequest := bleve.NewSearchRequest(query) if highlight != "" { searchRequest.Highlight = bleve.NewHighlightWithStyle(highlight) } limit, offset := pagingLimit(paging) searchRequest.Size = limit searchRequest.From = offset searchResult, err := index.Search(searchRequest) if err != nil { return } if len(searchResult.Hits) == 0 { return } articleIds := []data.ArticleId{} hitMap := map[data.ArticleId]*search.DocumentMatch{} for _, hit := range searchResult.Hits { if articleId, err := strconv.ParseInt(hit.ID, 10, 64); err == nil { id := data.ArticleId(articleId) articleIds = append(articleIds, id) hitMap[id] = hit } } ua = u.ArticlesById(articleIds) if u.HasErr() { return ua, u.Err() } for i := range ua { data := ua[i].Data() hit := hitMap[data.Id] if len(hit.Fragments) > 0 { data.Hit.Fragments = hit.Fragments ua[i].Data(data) } } return }
func Search(q string) (*bleve.SearchResult, error) { query := bleve.NewQueryStringQuery(q) req := bleve.NewSearchRequest(query) req.Highlight = bleve.NewHighlightWithStyle("html_ex") return index.Search(req) }
// queryCmd represents the query command var queryCmd = &cobra.Command{ Use: "query [index path] [query]", Short: "queries the index", Long: `The query command will execute a query against the index.`, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 2 { return fmt.Errorf("must specify query") } query := buildQuery(args) for i := 0; i < repeat; i++ { req := bleve.NewSearchRequestOptions(query, limit, skip, explain) if highlight { req.Highlight = bleve.NewHighlightWithStyle("ansi") } if fields { req.Fields = []string{"*"} } res, err := idx.Search(req) if err != nil { return fmt.Errorf("error running query: %v", err) } fmt.Println(res) } return nil }, } func buildQuery(args []string) query.Query {
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) } }