Example #1
1
// Search searches the index for the given query, and returns documents,
// the total number of results, or an error if something went wrong
func (i *Index) Search(q query.Query) (docs []index.Document, total int, err error) {
	query := solr.NewQuery()
	query.Q(q.Term)
	query.AddParam("cache", "false")
	//query.Start(int(q.Paging.Offset))
	//query.Rows(int(q.Paging.Num))
	s := i.si.Search(query)
	r, err := s.Result(nil)
	if err != nil {
		return nil, 0, err
	}

	ret := make([]index.Document, 0, len(r.Results.Docs))
	for _, d := range r.Results.Docs {

		doc := index.NewDocument(d.Get("id").(string), 1.0)
		for k, v := range d {
			if reflect.TypeOf(v).Kind() == reflect.Slice {
				v = v.([]interface{})[0]
			}
			if k != "id" {
				doc.Set(k, v)
			}
		}
		ret = append(ret, doc)
	}

	return ret, r.Results.NumFound, nil
}
Example #2
0
func main() {
	si, _ := solr.NewSolrInterface("http://localhost:8983/solr", "collection1")

	query := solr.NewQuery()
	query.Q("title:add sucess 1")
	query.Start(0)
	query.Rows(15)
	s := si.Search(query)

	parser := &TestResultParser{}
	r, err := s.Result(parser)
	if err != nil {
		fmt.Println("Error when querying solr:", err.Error())
		return
	}

	fmt.Println(r.Results.Docs)
	fmt.Println(parser.original_response)

	parser2 := &InheritResultParser{}
	r2, err := s.Result(parser2)

	fmt.Println(r2.Results.Docs)

	fmt.Println(parser2.original_response)
}
Example #3
0
// Suggest gets completion suggestions from solr
func (i *Index) Suggest(prefix string, num int, fuzzy bool) ([]index.Suggestion, error) {
	s := i.si.Search(solr.NewQuery())

	parms := url.Values{}
	parms.Set("suggest.q", prefix)
	parms.Set("suggest.num", fmt.Sprintf("%d", num))
	parms.Set("suggest", "true")
	b, err := s.Resource("suggest", &parms)
	if err != nil || b == nil {
		return nil, err
	}

	var res SuggestResponse

	if err := json.Unmarshal(*b, &res); err != nil {
		return nil, err
	}

	for _, s := range res.Suggest.Autocomplete {
		ret := make([]index.Suggestion, 0, num)
		for _, sugg := range s.Suggestions {
			ret = append(ret, index.Suggestion{Term: sugg.Term, Score: sugg.Weight})
		}

		return ret, nil
	}
	return nil, nil

}