func (c *EntitySearch) Render() web.Result {
if c.EntityType == "" {
return web.Fail(errors.New("entity_type can not be empty"))
}
// Load entity map from yaml or json format.
entity, ok := maps[c.EntityType]
if !ok {
return web.Fail(errors.New("entity_type configuration not found"))
}
// Replace query.
conds := make([]string, len(c.Conditions))
if len(c.Conditions) > 0 {
for k, v := range c.Conditions {
var ok bool
f, ok := entity[k]
if !ok {
return web.Fail(errors.New("field not found"))
}
conds = append(conds, f.Name+":"+v)
}
} else {
conds = append(conds, "*:*")
}
// Build a query object
// Here we are specifying a 'q' param,
// rows, faceting and facet.fields
q := solr.Query{
Params: solr.URLParamMap{
"q": conds,
//"facet.field": []string{"accepts_4x4s", "accepts_bicycles"},
//"facet": []string{"true"},
},
Rows: 30,
}
// perform the query, checking for errors
res, err := solrObj.Select(&q)
if err != nil {
log.Fatal(err)
}
// grab results for ease of use later on
results := res.Results
//log.Printf("%#v\n", q)
//log.Printf("%#v\n", res)
//log.Printf("%#v\n", results)
rows := make([]map[string]string, 0)
for i := 0; i < results.Len(); i++ {
id := fmt.Sprintf("%#v", results.Get(i).Field("id__i"))
name := fmt.Sprintf("%#v", results.Get(i).Field("name"))
url := fmt.Sprintf("%#v", results.Get(i).Field("url"))
address := fmt.Sprintf("%#v", results.Get(i).Field("address"))
description := fmt.Sprintf("%#v", results.Get(i).Field("description"))
menus__json := fmt.Sprintf("%#v", results.Get(i).Field("menus__json"))
features := fmt.Sprintf("%#v", results.Get(i).Field("features"))
class_feature__ss := fmt.Sprintf("%#v", results.Get(i).Field("class_feature__ss"))
halls__json := fmt.Sprintf("%#v", results.Get(i).Field("halls__json"))
service__json := fmt.Sprintf("%#v", results.Get(i).Field("service__json"))
data := make(map[string]string)
data["id"] = id
data["name"] = name
data["url"] = url
data["address"] = address
data["description"] = description
data["menus__json"] = menus__json
data["features"] = features
data["class_feature__ss"] = class_feature__ss
data["halls__json"] = halls__json
data["service__json"] = service__json
rows = append(rows, data)
}
return web.Done(rows)
}
请发表评论