~ols/veri-index unlisted

3880d045f1db7aa115c1c12991a59e64fdfa7117 — Oliver Leaver-Smith 4 months ago 70b323f master
sorting of results
3 files changed, 33 insertions(+), 10 deletions(-)

M cache.gohtml
M main.go
M search.gohtml
M cache.gohtml => cache.gohtml +0 -2
@@ 23,9 23,7 @@ body{padding:1em;}
<p class="bold">This is a rudimentary cached copy of <a href="{{ .URL }}">{{ .URL }}</a>, go there to get the latest copy. No ownership of this content is implied.</p>
{{ if not . }}<p>No cache</p>{{ end }}
<h2>{{ .Title }}</h2>
<pre>
{{ .Content }}
</pre>
<p>If your page appears here and you do not want it to, please contact <a href="mailto:~ols/indexing@lists.sr.ht">~ols/indexing@lists.sr.ht</a>. Contact the same email if you want your page considered for indexing. Be aware this is a public mailing list.</p>
<p><a href="https://sr.ht/~ols/veri/">about</a></p>
</body>

M main.go => main.go +33 -7
@@ 8,6 8,7 @@ import (
	"net/http"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"time"


@@ 26,7 27,7 @@ type Entries struct {

type Entry struct {
	URL, Title, Author, Summary, Content, HTMLContent string
	Length, ID                                        int
	Length, ID, Relevance                             int
}

func main() {


@@ 49,7 50,7 @@ func main() {
	http.Handle("/", r)
	srv := &http.Server{
		Handler:      r,
		Addr:         "127.0.0.1:8001",
		Addr:         "127.0.0.1:" + os.Getenv("VERI_PORT"),
		WriteTimeout: 15 * time.Second,
		ReadTimeout:  15 * time.Second,
	}


@@ 71,14 72,39 @@ func ShowIndex(w http.ResponseWriter, r *http.Request) {
func DoSearch(w http.ResponseWriter, r *http.Request, Idx index, entries []string) {
	term := r.URL.Query().Get("q")
	results := Idx.search(term)
	occurences := map[int]*Entry{}
	i := 0
	for _, k := range results {
		contents, _ := ioutil.ReadFile(entries[k-1])
		occurenceCount := strings.Count(string(contents), term)
		occurences[i] = &Entry{
			ID:        k,
			Relevance: occurenceCount,
		}
		i++
	}
	sortedOccurence := make([]Entry, len(occurences))
	ii := 0
	for k := range occurences {
		sortedOccurence[ii] = Entry{
			ID:        occurences[k].ID,
			Relevance: k,
		}
		ii++
	}
	sort.Slice(sortedOccurence, func(i, j int) bool {
		return sortedOccurence[i].Relevance > sortedOccurence[j].Relevance
	})
	var entry Entry
	var retEntries []Entry
	for _, id := range results {
	for _, entryS := range sortedOccurence {
		// TODO figure out wtf this is an off-by-one, for now an id-1 does the trick
		contents, _ := ioutil.ReadFile(entries[id-1])
		json.Unmarshal([]byte(contents), &entry)
		entry.ID = id - 1
		retEntries = append(retEntries, entry)
		if entryS.Relevance > 0 {
			contents, _ := ioutil.ReadFile(entries[entryS.ID-1])
			json.Unmarshal([]byte(contents), &entry)
			entry.ID = entryS.ID - 1
			retEntries = append(retEntries, entry)
		}
	}
	tmpl, err := template.ParseFiles("search.gohtml")
	if err != nil {

M search.gohtml => search.gohtml +0 -1
@@ 23,7 23,6 @@ body{padding:1em;}
<input type="text" id="search" name="q"><br><br>
<input type="submit" value="Search">
</form>
<p class="bold">These results are not ranked, they are just in alphabetical order of URL (this will change)</p>
<ol>
{{ if not . }}<p>No search results</p>{{ end }}
{{ range . }}