Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1873: Added timeout option in the Search Handler #1898

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion http/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package http

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/search/query"
Expand Down Expand Up @@ -80,8 +82,22 @@ func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}

// check for timeout and create context
var ctx context.Context
timeoutStr := req.FormValue("timeout")
if timeoutStr == "" {
ctx = context.Background()
} else {
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
showError(w, req, fmt.Sprintf("error parsing timeout value: %v", err), 400)
return
}
ctx, _ = context.WithTimeout(context.Background(), timeout)
}

// execute the query
searchResponse, err := index.Search(&searchRequest)
searchResponse, err := index.SearchInContext(ctx, &searchRequest)
Likith101 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
showError(w, req, fmt.Sprintf("error executing query: %v", err), 500)
return
Expand Down