node search for text added
This commit is contained in:
parent
506107286d
commit
1cb2b042ac
3 changed files with 128 additions and 51 deletions
|
@ -17,6 +17,7 @@ func handleSearchTextMessage(msg Message) {
|
||||||
Safe string `json:"safe"`
|
Safe string `json:"safe"`
|
||||||
Lang string `json:"lang"`
|
Lang string `json:"lang"`
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
|
ResponseAddr string `json:"responseAddr"`
|
||||||
}
|
}
|
||||||
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
err := json.Unmarshal([]byte(msg.Content), &searchParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -24,6 +25,8 @@ func handleSearchTextMessage(msg Message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Received search-text request. ResponseAddr: %s", searchParams.ResponseAddr)
|
||||||
|
|
||||||
results := fetchTextResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
results := fetchTextResults(searchParams.Query, searchParams.Safe, searchParams.Lang, searchParams.Page)
|
||||||
resultsJSON, err := json.Marshal(results)
|
resultsJSON, err := json.Marshal(results)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -33,13 +36,21 @@ func handleSearchTextMessage(msg Message) {
|
||||||
|
|
||||||
responseMsg := Message{
|
responseMsg := Message{
|
||||||
ID: hostID,
|
ID: hostID,
|
||||||
Type: "search-results",
|
Type: "text-results",
|
||||||
Content: string(resultsJSON),
|
Content: string(resultsJSON),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = sendMessage(msg.ID, responseMsg)
|
// Log the address to be used for sending the response
|
||||||
|
log.Printf("Sending text search results to %s", searchParams.ResponseAddr)
|
||||||
|
|
||||||
|
if searchParams.ResponseAddr == "" {
|
||||||
|
log.Printf("Error: Response address is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sendMessage(searchParams.ResponseAddr, responseMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error sending search results to %s: %v", msg.ID, err)
|
log.Printf("Error sending text search results to %s: %v", searchParams.ResponseAddr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
node.go
4
node.go
|
@ -153,8 +153,8 @@ func interpretMessage(msg Message) {
|
||||||
handleSearchForumMessage(msg)
|
handleSearchForumMessage(msg)
|
||||||
case "forum-results":
|
case "forum-results":
|
||||||
handleForumResultsMessage(msg)
|
handleForumResultsMessage(msg)
|
||||||
// case "text-results":
|
case "text-results":
|
||||||
// handleTextResultsMessage(msg) // need to implement
|
handleTextResultsMessage(msg) // need to implement
|
||||||
case "image-results":
|
case "image-results":
|
||||||
handleImageResultsMessage(msg) // need to implement
|
handleImageResultsMessage(msg) // need to implement
|
||||||
// case "video-results":
|
// case "video-results":
|
||||||
|
|
152
text.go
152
text.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var textSearchEngines []SearchEngine
|
var textSearchEngines []SearchEngine
|
||||||
|
var textResultsChan = make(chan []TextSearchResult)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
textSearchEngines = []SearchEngine{
|
textSearchEngines = []SearchEngine{
|
||||||
|
@ -26,16 +28,51 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int)
|
||||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
||||||
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, safe, lang, page)
|
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, safe, lang, page)
|
||||||
|
|
||||||
hasPrevPage := page > 1
|
hasPrevPage := page > 1 // dupe
|
||||||
hasNextPage := len(combinedResults) > 0
|
|
||||||
|
|
||||||
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage)
|
//displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage)
|
||||||
|
|
||||||
// Prefetch next and previous pages
|
// Prefetch next and previous pages
|
||||||
go prefetchPage(query, safe, lang, page+1)
|
go prefetchPage(query, safe, lang, page+1)
|
||||||
if hasPrevPage {
|
if hasPrevPage {
|
||||||
go prefetchPage(query, safe, lang, page-1)
|
go prefetchPage(query, safe, lang, page-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
elapsedTime := time.Since(startTime)
|
||||||
|
tmpl, err := template.New("text.html").Funcs(funcs).ParseFiles("templates/text.html")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error parsing template: %v", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := struct {
|
||||||
|
Results []TextSearchResult
|
||||||
|
Query string
|
||||||
|
Page int
|
||||||
|
Fetched string
|
||||||
|
LanguageOptions []LanguageOption
|
||||||
|
CurrentLang string
|
||||||
|
HasPrevPage bool
|
||||||
|
HasNextPage bool
|
||||||
|
NoResults bool
|
||||||
|
}{
|
||||||
|
Results: combinedResults,
|
||||||
|
Query: query,
|
||||||
|
Page: page,
|
||||||
|
Fetched: fmt.Sprintf("%.2f seconds", elapsedTime.Seconds()),
|
||||||
|
LanguageOptions: languageOptions,
|
||||||
|
CurrentLang: lang,
|
||||||
|
HasPrevPage: page > 1,
|
||||||
|
HasNextPage: len(combinedResults) >= 50,
|
||||||
|
NoResults: len(combinedResults) == 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tmpl.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error executing template: %v", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []TextSearchResult {
|
func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []TextSearchResult {
|
||||||
|
@ -109,6 +146,12 @@ func fetchTextResults(query, safe, lang string, page int) []TextSearchResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no results found after trying all engines
|
||||||
|
if len(results) == 0 {
|
||||||
|
log.Printf("No text results found for query: %s, trying other nodes", query)
|
||||||
|
results = tryOtherNodesForTextSearch(query, safe, lang, page)
|
||||||
|
}
|
||||||
|
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,48 +183,71 @@ func wrapTextSearchFunc(f func(string, string, string, int) ([]TextSearchResult,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string, elapsed float64, page int, hasPrevPage, hasNextPage bool) {
|
func tryOtherNodesForTextSearch(query, safe, lang string, page int) []TextSearchResult {
|
||||||
log.Printf("Displaying results for page %d", page)
|
for _, nodeAddr := range peers {
|
||||||
log.Printf("Total results: %d", len(results))
|
results, err := sendTextSearchRequestToNode(nodeAddr, query, safe, lang, page)
|
||||||
log.Printf("Has previous page: %t, Has next page: %t", hasPrevPage, hasNextPage)
|
|
||||||
|
|
||||||
tmpl, err := template.New("text.html").Funcs(template.FuncMap{
|
|
||||||
"sub": func(a, b int) int {
|
|
||||||
return a - b
|
|
||||||
},
|
|
||||||
"add": func(a, b int) int {
|
|
||||||
return a + b
|
|
||||||
},
|
|
||||||
}).ParseFiles("templates/text.html")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
log.Printf("Error contacting node %s: %v", nodeAddr, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(results) > 0 {
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendTextSearchRequestToNode(nodeAddr, query, safe, lang string, page int) ([]TextSearchResult, error) {
|
||||||
|
searchParams := struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
Safe string `json:"safe"`
|
||||||
|
Lang string `json:"lang"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
ResponseAddr string `json:"responseAddr"`
|
||||||
|
}{
|
||||||
|
Query: query,
|
||||||
|
Safe: safe,
|
||||||
|
Lang: lang,
|
||||||
|
Page: page,
|
||||||
|
ResponseAddr: fmt.Sprintf("http://localhost:%d/node", config.Port),
|
||||||
|
}
|
||||||
|
|
||||||
|
msgBytes, err := json.Marshal(searchParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal search parameters: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := Message{
|
||||||
|
ID: hostID,
|
||||||
|
Type: "search-text",
|
||||||
|
Content: string(msgBytes),
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sendMessage(nodeAddr, msg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to send search request to node %s: %v", nodeAddr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for results
|
||||||
|
select {
|
||||||
|
case res := <-textResultsChan:
|
||||||
|
return res, nil
|
||||||
|
case <-time.After(20 * time.Second): // Increased timeout duration
|
||||||
|
return nil, fmt.Errorf("timeout waiting for results from node %s", nodeAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleTextResultsMessage(msg Message) {
|
||||||
|
var results []TextSearchResult
|
||||||
|
err := json.Unmarshal([]byte(msg.Content), &results)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error unmarshalling text results: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
log.Printf("Received text results: %+v", results)
|
||||||
Results []TextSearchResult
|
// Send results to textResultsChan
|
||||||
Query string
|
go func() {
|
||||||
Fetched string
|
textResultsChan <- results
|
||||||
Page int
|
}()
|
||||||
HasPrevPage bool
|
|
||||||
HasNextPage bool
|
|
||||||
LanguageOptions []LanguageOption
|
|
||||||
CurrentLang string
|
|
||||||
NoResults bool
|
|
||||||
}{
|
|
||||||
Results: results,
|
|
||||||
Query: query,
|
|
||||||
Fetched: fmt.Sprintf("%.2f seconds", elapsed),
|
|
||||||
Page: page,
|
|
||||||
HasPrevPage: hasPrevPage,
|
|
||||||
HasNextPage: hasNextPage,
|
|
||||||
LanguageOptions: languageOptions,
|
|
||||||
CurrentLang: lang,
|
|
||||||
NoResults: len(results) == 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tmpl.Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue