about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-08 17:11:28 +0200
committerAlan Pearce2024-05-08 17:11:28 +0200
commit4c06f763f471f51a655b8437f9529149512e5507 (patch)
tree98cf200da087d81321851121c9440d014e7f7d0c
parent8537192b854cb4abde7c7ca2d58c201df060504a (diff)
downloadsearchix-4c06f763f471f51a655b8437f9529149512e5507.tar.lz
searchix-4c06f763f471f51a655b8437f9529149512e5507.tar.zst
searchix-4c06f763f471f51a655b8437f9529149512e5507.zip
refactor: enable access to bleve DocumentMatch structs
-rw-r--r--frontend/templates/blocks/options.gotmpl98
-rw-r--r--internal/search/search.go16
2 files changed, 61 insertions, 53 deletions
diff --git a/frontend/templates/blocks/options.gotmpl b/frontend/templates/blocks/options.gotmpl
index 88553b2..a9c2a1b 100644
--- a/frontend/templates/blocks/options.gotmpl
+++ b/frontend/templates/blocks/options.gotmpl
@@ -2,57 +2,59 @@
 {{- define "results" }}
   {{- if gt .Results.Total 0 }}
     <section id="results">
-      {{- range .Results.Results }}
-        <details id="{{ .Option }}">
-          <summary>
-            {{ .Option }}
-          </summary>
-          <p>
-            {{ markdown .Description }}
-          </p>
-          <dl>
-            {{- with .Type }}
-              <dt>Type</dt>
-              <dd><code>{{ . }}</code></dd>
-            {{- end }}
-            {{- with .Default }}
-              {{- if or .Text .Markdown }}
-                <dt>Default</dt>
-                <dd>
-                  {{- if .Markdown }}
-                    {{ markdown .Markdown }}
-                  {{- else }}
-                    <pre><code>{{ .Text }}</code></pre>
-                  {{- end }}
-                </dd>
+      {{- range .Results.Hits }}
+        {{- with .Data }}
+          <details id="{{ .Option }}">
+            <summary>
+              {{ .Option }}
+            </summary>
+            <p>
+              {{ markdown .Description }}
+            </p>
+            <dl>
+              {{- with .Type }}
+                <dt>Type</dt>
+                <dd><code>{{ . }}</code></dd>
               {{- end }}
-            {{- end }}
-            {{- with .Example }}
-              {{- if or .Text .Markdown }}
-                <dt>Example</dt>
-                <dd>
-                  {{- if .Markdown }}
-                    {{ markdown .Markdown }}
-                  {{- else }}
-                    <pre><code>{{ .Text }}</code></pre>
-                  {{- end }}
-                </dd>
+              {{- with .Default }}
+                {{- if or .Text .Markdown }}
+                  <dt>Default</dt>
+                  <dd>
+                    {{- if .Markdown }}
+                      {{ markdown .Markdown }}
+                    {{- else }}
+                      <pre><code>{{ .Text }}</code></pre>
+                    {{- end }}
+                  </dd>
+                {{- end }}
               {{- end }}
-            {{- end }}
-            {{- with .RelatedPackages }}
-              <dt>Related Packages</dt>
-              <dd>{{ . }}</dd>
-            {{- end }}
-            {{- with .Declarations }}
-              <dt>Declared</dt>
-              {{- range . }}
-                <dd>
-                  <a href="{{ .URL }}">{{ .Name }}</a>
-                </dd>
+              {{- with .Example }}
+                {{- if or .Text .Markdown }}
+                  <dt>Example</dt>
+                  <dd>
+                    {{- if .Markdown }}
+                      {{ markdown .Markdown }}
+                    {{- else }}
+                      <pre><code>{{ .Text }}</code></pre>
+                    {{- end }}
+                  </dd>
+                {{- end }}
               {{- end }}
-            {{- end }}
-          </dl>
-        </details>
+              {{- with .RelatedPackages }}
+                <dt>Related Packages</dt>
+                <dd>{{ . }}</dd>
+              {{- end }}
+              {{- with .Declarations }}
+                <dt>Declared</dt>
+                {{- range . }}
+                  <dd>
+                    <a href="{{ .URL }}">{{ .Name }}</a>
+                  </dd>
+                {{- end }}
+              {{- end }}
+            </dl>
+          </details>
+        {{- end }}
       {{- end }}
       <footer>
         <nav id="pagination">
diff --git a/internal/search/search.go b/internal/search/search.go
index da5865c..97d8404 100644
--- a/internal/search/search.go
+++ b/internal/search/search.go
@@ -15,6 +15,7 @@ import (
 	"github.com/blevesearch/bleve/v2/analysis/token/camelcase"
 	"github.com/blevesearch/bleve/v2/analysis/tokenizer/letter"
 	"github.com/blevesearch/bleve/v2/document"
+	"github.com/blevesearch/bleve/v2/search"
 	index "github.com/blevesearch/bleve_index_api"
 	"github.com/mitchellh/mapstructure"
 	"github.com/pkg/errors"
@@ -22,9 +23,14 @@ import (
 
 const ResultsPerPage = 20
 
+type DocumentMatch[T options.NixOption] struct {
+	search.DocumentMatch
+	Data T
+}
+
 type Result[T options.NixOption] struct {
 	*bleve.SearchResult
-	Results []T
+	Hits []DocumentMatch[T]
 }
 
 type Index[T options.NixOption] struct {
@@ -158,14 +164,14 @@ func (index *Index[T]) Search(ctx context.Context, keyword string, from uint64)
 			return nil, errors.WithMessage(err, "failed to execute search query")
 		}
 
-		results := make([]T, min(ResultsPerPage, bleveResult.Total))
+		results := make([]DocumentMatch[T], min(ResultsPerPage, bleveResult.Total))
 		var buf bytes.Buffer
 		for i, result := range bleveResult.Hits {
 			_, err = buf.WriteString(result.Fields["data"].(string))
 			if err != nil {
 				return nil, errors.WithMessage(err, "error fetching result data")
 			}
-			err = gob.NewDecoder(&buf).Decode(&results[i])
+			err = gob.NewDecoder(&buf).Decode(&results[i].Data)
 			if err != nil {
 				return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String())
 			}
@@ -173,8 +179,8 @@ func (index *Index[T]) Search(ctx context.Context, keyword string, from uint64)
 		}
 
 		return &Result[T]{
-			bleveResult,
-			results,
+			SearchResult: bleveResult,
+			Hits:         results,
 		}, nil
 	}
 }