func jobTags(w http.ResponseWriter, r *http.Request) { query := bleve.NewMatchAllQuery() searchRequest := bleve.NewSearchRequest(query) searchRequest.Fields = []string{"tags"} tagsFacet := bleve.NewFacetRequest("tags", 99999) searchRequest.AddFacet("tags", tagsFacet) jobsSearchResults, err := doRequest("jobs", searchRequest) if err != nil { respond.With(w, r, http.StatusInternalServerError, err) return } tags := []string{} for _, t := range jobsSearchResults.Facets["tags"].Terms { tags = append(tags, fmt.Sprintf("%s", t.Term)) } respond.With(w, r, http.StatusOK, struct { Total int `json:"total"` Tags []string `json:"tags"` }{ Total: len(tags), Tags: tags, }) }
// START OMIT func main() { index, err := bleve.Open("gophercon.bleve") if err != nil { log.Fatal(err) } q := bleve.NewMatchAllQuery() req := bleve.NewSearchRequest(q) req.Size = 0 req.AddFacet("sections", // HL bleve.NewFacetRequest("section", 50)) // HL res, err := index.Search(req) if err != nil { log.Fatal(err) } fmt.Println(res) }
func (brs *BleveRecipeStore) All() ([]model.Recipe, error) { recipes := []model.Recipe{} q := bleve.NewMatchAllQuery() src := bleve.NewSearchRequest(q) res, err := brs.idx.Search(src) for _, hit := range res.Hits { data, err := brs.idx.GetInternal([]byte(hit.ID)) if err != nil { panic(err) } recipe := model.Recipe{} json.Unmarshal(data, &recipe) recipes = append(recipes, recipe) } return recipes, err }
func freelancers(w http.ResponseWriter, r *http.Request) { searchRequest := bleve.NewSearchRequest(bleve.NewMatchAllQuery()) searchRequest.Fields = []string{"*"} freelnacersSearchResults, err := doRequest("freelancers", searchRequest) if err != nil { respond.With(w, r, http.StatusInternalServerError, err) return } freelancers := []interface{}{} for _, hit := range freelnacersSearchResults.Hits { freelancers = append(freelancers, hit.Fields) } respond.With(w, r, http.StatusOK, struct { Total int `json:"total"` Items []interface{} `json:"items"` }{ Total: len(freelancers), Items: freelancers, }) }
func makeSearchQuery(queryString string, ids []string) (query.Query, error) { nodes, err := blevext.Parse(queryString) if err != nil { return nil, err } addIdsFilter := func(q query.Query) query.Query { if len(ids) == 0 { return q } return bleve.NewConjunctionQuery( query.NewDocIDQuery(ids), q, ) } var makeQuery func(*blevext.Node) (query.Query, error) makeQuery = func(n *blevext.Node) (query.Query, error) { if n == nil { return bleve.NewMatchAllQuery(), nil } switch n.Kind { case blevext.NodeAnd, blevext.NodeOr: left, err := makeQuery(n.Children[0]) if err != nil { return nil, err } right, err := makeQuery(n.Children[1]) if err != nil { return nil, err } if n.Kind == blevext.NodeOr { q := query.NewDisjunctionQuery([]query.Query{left, right}) q.Min = 1 return q, nil } return query.NewConjunctionQuery([]query.Query{left, right}), nil case blevext.NodeString, blevext.NodePhrase: fn := func(s string) query.FieldableQuery { return bleve.NewMatchQuery(s) } if n.Kind == blevext.NodePhrase { fn = func(s string) query.FieldableQuery { return blevext.NewAllMatchQuery(s) } } htmlQuery := fn(n.Value) htmlQuery.SetField("html") titleQuery := fn(n.Value) titleQuery.SetField("title") q := query.NewDisjunctionQuery([]query.Query{ addIdsFilter(htmlQuery), addIdsFilter(titleQuery), }) q.Min = 1 return q, nil } return nil, fmt.Errorf("unknown query node type: %d", n.Kind) } return makeQuery(nodes) }