func (i *uploadController) ReadMany(c context.Context) error { var results []types.Upload qry := elastigo.Search(iname).Pretty().Query( elastigo.Query().All(), ) out, err := qry.Result(types.ElasticConnection) if err != nil { fmt.Println("err querying elastic connection:%v", err) return goweb.API.RespondWithError(c, http.StatusInternalServerError, err.Error()) } for _, elem := range out.Hits.Hits { bytes, err := elem.Source.MarshalJSON() if err != nil { log.Println("err calling marshalJson:%v", err) return goweb.API.RespondWithError(c, http.StatusInternalServerError, err.Error()) } var t types.Upload json.Unmarshal(bytes, &t) results = append(results, t) } return goweb.API.RespondWithData(c, results) }
func searchHandler(w http.ResponseWriter, r *http.Request) { var index, typ string r.ParseForm() switch r.Form.Get("type") { case "slack": index = slack.Index typ = "message" } if len(index) == 0 { http.Error(w, "unrecognise type", 500) return } out, err := el.Search(index).Type(typ).Search(r.Form.Get("query")).Result(conn) if err != nil { http.Error(w, err.Error(), 500) return } var results []*json.RawMessage for _, hit := range out.Hits.Hits { results = append(results, hit.Source) } b, err := json.Marshal(map[string]interface{}{ "type": index, "results": results, }) if err != nil { http.Error(w, err.Error(), 500) return } w.Write(b) }
// Search ES index func Search(sr *proto.SearchRequest) (string, error) { if len(sr.Query) <= 0 { sr.Query = "*" } size := fmt.Sprintf("%d", sr.Limit) from := fmt.Sprintf("%d", sr.Offset) out, err := lib.Search(sr.Index).Type(sr.Type).Size(size).From(from).Search(sr.Query).Result(conn) if err != nil { return "", err } return string(out.RawJSON), nil }
func (c *Configuration) fetchLastIndexFromES() int64 { var results []Upload qry := elastigo.Search(iname).Pretty().Query( elastigo.Query().All(), ) out, err := qry.Result(ElasticConnection) if err != nil { log.Println("No record found") return 0 } if out.Hits.Total == 0 { log.Println("No indice data for updating fetch information") return 0 } for _, elem := range out.Hits.Hits { bytes, err := elem.Source.MarshalJSON() if err != nil { log.Fatalf("err calling marshalJson:%v", err) } var t Upload json.Unmarshal(bytes, &t) results = append(results, t) } var highestId int64 = 0 for _, p := range results { if p.Id >= highestId { highestId = p.Id } } return highestId }
// Build elasticsearch query from vindalu query func buildElasticsearchBaseQuery(index string, req map[string]interface{}) (query map[string]interface{}, err error) { filterOps := []interface{}{} for k, v := range req { switch v.(type) { case string: val, _ := v.(string) val = strings.TrimSpace(val) if strings.HasPrefix(val, ">") || strings.HasPrefix(val, "<") { // Parse number aVal := "" if strings.HasPrefix(val, ">=") || strings.HasPrefix(val, "<=") { aVal = strings.TrimSpace(val[2:]) } else { aVal = strings.TrimSpace(val[1:]) } // Parse number for comparison var nVal interface{} nVal, ierr := strconv.ParseInt(aVal, 10, 64) if ierr != nil { ierr = nil if nVal, ierr = strconv.ParseFloat(aVal, 64); ierr != nil { err = ierr return } } // Add range filterop if strings.HasPrefix(val, ">") { filterOps = append(filterOps, elastigo.Range().Field(k).Gt(nVal)) } else { filterOps = append(filterOps, elastigo.Range().Field(k).Lt(nVal)) } } else { if isRegexSearch(val) { filterOps = append(filterOps, regexFilter(k, val)) } else { filterOps = append(filterOps, map[string]interface{}{ "term": map[string]string{k: val}, }) } } break case int: break case int64: break case float64: break case []interface{}: break case interface{}: break default: err = fmt.Errorf("invalid type: %#v", v) return } } if len(filterOps) > 0 { query = map[string]interface{}{ "query": map[string]interface{}{ "filtered": elastigo.Search(index).Filter(filterOps...), }, } } else { // Empty query i.e. return everything query = map[string]interface{}{} } return }
/* { "type": ["virtualserver", "physicalserver"], "os": "ubuntu" } */ func (ir *Inventory) parseRequestBody(r *http.Request) (query interface{}, err error) { // check happens earlier var body []byte if body, err = ioutil.ReadAll(r.Body); err != nil { return } defer r.Body.Close() var req map[string]interface{} if err = json.Unmarshal(body, &req); err != nil { return } filterOps := []interface{}{} for k, v := range req { switch v.(type) { case string: val, _ := v.(string) val = strings.TrimSpace(val) if strings.HasPrefix(val, ">") || strings.HasPrefix(val, "<") { // Parse number aVal := "" if strings.HasPrefix(val, ">=") || strings.HasPrefix(val, "<=") { aVal = strings.TrimSpace(val[2:]) } else { aVal = strings.TrimSpace(val[1:]) } // Parse number for comparison var nVal interface{} nVal, ierr := strconv.ParseInt(aVal, 10, 64) if ierr != nil { ierr = nil if nVal, ierr = strconv.ParseFloat(aVal, 64); ierr != nil { err = ierr return } } // Add range filterop if strings.HasPrefix(val, ">") { filterOps = append(filterOps, elastigo.Range().Field(k).Gt(nVal)) } else { filterOps = append(filterOps, elastigo.Range().Field(k).Lt(nVal)) } } else { filterOps = append(filterOps, elastigo.Filter().Terms(k, val)) } break case int: //val, _ := v.(int) break case int64: //val, _ := v.(int64) break case float64: //val, _ := v.(float64) break case []interface{}: vals, _ := v.([]interface{}) filterOps = append(filterOps, elastigo.Filter().Terms(k, vals...)) break case interface{}: //val, _ := v.(interface{}) break default: err = fmt.Errorf("invalid type: %#v", v) return } } query = elastigo.Search(ir.cfg.Datastore.Config.Index).Filter(filterOps...) return }