internal/index/search.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | package index import ( "bytes" "context" "encoding/gob" "iter" "time" "go.alanpearce.eu/searchix/internal/config" "go.alanpearce.eu/searchix/internal/nix" "go.alanpearce.eu/x/log" "github.com/blevesearch/bleve/v2" "github.com/blevesearch/bleve/v2/search" "github.com/blevesearch/bleve/v2/search/query" "gitlab.com/tozd/go/errors" ) const DefaultPageSize = 100 type DocumentMatch struct { *search.DocumentMatch Data nix.Importable } type Result struct { *bleve.SearchResult Hits iter.Seq[DocumentMatch] } type ReadIndex struct { index bleve.Index log *log.Logger meta *Meta } func (index *ReadIndex) LastUpdated() time.Time { return index.meta.LastUpdated() } func (index *ReadIndex) GetEnabledSources() ([]string, errors.E) { facet := bleve.NewFacetRequest("Source", 100) query := bleve.NewMatchAllQuery() search := bleve.NewSearchRequest(query) search.AddFacet("Source", facet) results, err := index.index.Search(search) if err != nil { return nil, errors.WithMessage(err, "could not get list of enabled sources from index") } enabledSources := make([]string, results.Facets["Source"].Terms.Len()) for i, term := range results.Facets["Source"].Terms.Terms() { enabledSources[i] = term.Term } return enabledSources, nil } func setField[T query.FieldableQuery]( q T, field string, ) T { q.SetField(field) return q } func setBoost[T query.BoostableQuery](q T, boost float64) T { q.SetBoost(boost) return q } func (index *ReadIndex) search( ctx context.Context, request *bleve.SearchRequest, ) (*Result, errors.E) { request.Fields = []string{"_data", "Source"} bleveResult, err := index.index.SearchInContext(ctx, request) select { case <-ctx.Done(): return nil, errors.WithStack(ctx.Err()) default: if err != nil { return nil, errors.WithMessage(err, "failed to execute search query") } hits := func(yield func(DocumentMatch) bool) { var buf bytes.Buffer for _, match := range bleveResult.Hits { hit := DocumentMatch{ DocumentMatch: match, Data: nil, } _, err := buf.WriteString(match.Fields["_data"].(string)) if err != nil { index.log.Warn("error fetching result data", "error", err) } err = gob.NewDecoder(&buf).Decode(&hit.Data) if err != nil { index.log.Warn("error decoding gob data", "error", err, "data", buf.String()) } buf.Reset() if !yield(hit) { return } } } return &Result{ SearchResult: bleveResult, Hits: hits, }, nil } } func (index *ReadIndex) Search( ctx context.Context, source *config.Source, keyword string, from int, pageSize int, ) (*Result, errors.E) { query := bleve.NewBooleanQuery() // match the user's query in any field ... query.AddMust(bleve.NewDisjunctionQuery( bleve.NewTermQuery(keyword), bleve.NewPrefixQuery(keyword), bleve.NewMatchPhraseQuery(keyword), bleve.NewMatchQuery(keyword), )) if source != nil { query.AddMust( setField(bleve.NewTermQuery(source.Key), "Source"), ) } else { q := bleve.NewDisjunctionQuery( setBoost(setField(bleve.NewTermQuery("nixpkgs"), "Source"), -150), setBoost(setField(bleve.NewTermQuery("nur"), "Source"), -200), ) query.AddShould(q) } programsQuery := bleve.NewMatchQuery(keyword) programsQuery.SetField("Programs") programsQuery.SetBoost(2) query.AddShould(programsQuery) programsLiteralQuery := bleve.NewTermQuery(keyword) programsLiteralQuery.SetField("Programs") programsLiteralQuery.SetBoost(50) query.AddShould(programsLiteralQuery) attrQuery := bleve.NewMatchQuery(keyword) attrQuery.SetField("Attribute") attrQuery.SetBoost(2) query.AddShould(attrQuery) attrPrefixQuery := bleve.NewPrefixQuery(keyword) attrPrefixQuery.SetField("Attribute") attrPrefixQuery.SetBoost(10) query.AddShould(attrPrefixQuery) nameQuery := bleve.NewMatchQuery(keyword) nameQuery.SetField("Name") nameQuery.SetBoost(2) query.AddShould(nameQuery) namePrefixQuery := bleve.NewPrefixQuery(keyword) namePrefixQuery.SetField("Name") namePrefixQuery.SetBoost(10) query.AddShould(namePrefixQuery) search := bleve.NewSearchRequest(query) search.Explain = config.DevMode search.Size = pageSize if from != 0 { search.From = from } return index.search(ctx, search) } func (index *ReadIndex) GetDocument( ctx context.Context, source *config.Source, id string, ) (*nix.Importable, errors.E) { key := nix.MakeKey(source, id) query := bleve.NewDocIDQuery([]string{key}) search := bleve.NewSearchRequest(query) search.Size = 1 result, err := index.search(ctx, search) if err != nil { return nil, err } if result.Total == 0 { return nil, nil } for hit := range result.Hits { if hit.ID == key { return &hit.Data, err } } return nil, err } |