(w http.ResponseWriter, req *http.Request)
| 31 | } |
| 32 | |
| 33 | func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 34 | |
| 35 | // find the index to operate on |
| 36 | var indexName string |
| 37 | if h.IndexNameLookup != nil { |
| 38 | indexName = h.IndexNameLookup(req) |
| 39 | } |
| 40 | if indexName == "" { |
| 41 | indexName = h.defaultIndexName |
| 42 | } |
| 43 | index := IndexByName(indexName) |
| 44 | if index == nil { |
| 45 | showError(w, req, fmt.Sprintf("no such index '%s'", indexName), 404) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | // read the request body |
| 50 | requestBody, err := ioutil.ReadAll(req.Body) |
| 51 | if err != nil { |
| 52 | showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400) |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | // parse the request |
| 57 | var searchRequest bleve.SearchRequest |
| 58 | err = json.Unmarshal(requestBody, &searchRequest) |
| 59 | if err != nil { |
| 60 | showError(w, req, fmt.Sprintf("error parsing query: %v", err), 400) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | // validate the query |
| 65 | if srqv, ok := searchRequest.Query.(query.ValidatableQuery); ok { |
| 66 | err = srqv.Validate() |
| 67 | if err != nil { |
| 68 | showError(w, req, fmt.Sprintf("error validating query: %v", err), 400) |
| 69 | return |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // execute the query |
| 74 | searchResponse, err := index.Search(&searchRequest) |
| 75 | if err != nil { |
| 76 | showError(w, req, fmt.Sprintf("error executing query: %v", err), 500) |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | // encode the response |
| 81 | mustEncode(w, searchResponse) |
| 82 | } |
nothing calls this directly
no test coverage detected