Repository navigation
elasticsearch #151
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
Merged
Merged
elasticsearch #151
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6fafe59
example user + track indexer and search
stereosteve 763ab88
playlist indexer
stereosteve 923bac2
autocomplete endpoint
stereosteve 8a17b7b
wip basic tests
stereosteve 5f54627
wip: add elastic dsl builder
stereosteve 028b3c6
es dsl builders for users, tracks, playlists
stereosteve a7275fd
Use dsl builders, add searchtest.html
stereosteve 051156a
socials index to boost items you have interacted with.
stereosteve e73d1fa
improve reindex cli. Do all the data.
stereosteve a59f911
Merge remote-tracking branch 'origin/main' into sp-es
stereosteve 4215dc8
Merge remote-tracking branch 'origin/main' into sp-es
stereosteve 39ea3b9
Basic search test
stereosteve 9631f08
test various query string filters
stereosteve ac48ed9
Test that reposted tracks rank higher for current user.
stereosteve 315e98a
more explicit skipAuthCheck flag
stereosteve 85bc659
multi match track search + index tags.
stereosteve 3e5c192
Don't index deleted / private / removed content.
stereosteve 2d05f55
wip: incremental indexing
stereosteve 5778b79
honor kind + only_verified params. Playlist has track stubs
stereosteve 8dc50d6
tag search endpoint
stereosteve 5c3ee61
example of LISTEN for NOTIFY events.
stereosteve fc40d41
reorganize code... dedicated users / tracks / playlists search endpoints
stereosteve abb1535
single indexer with collectionConfigs.
stereosteve e14a474
es-indexer listens to NOTIFY and re-indexes documents.
stereosteve b57cc64
scoring tweaks
stereosteve 801f3f0
Fix + improve explore pages
stereosteve a4ab5a9
es-indexer listener batches changes in 10s interval.
stereosteve e8f95ea
rename v1SearchFull handler func
stereosteve f772fb8
savings: only personalize results for "full" endpoint
stereosteve 16db5c9
Merge remote-tracking branch 'origin/main' into sp-es
stereosteve 0f0662b
start all containers in github tests
stereosteve 155d665
PR cleanup
stereosteve dc5ecd5
Merge remote-tracking branch 'origin/main' into sp-es
stereosteve 739a357
rename files: esindexer
stereosteve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package searchv1 | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/elastic/go-elasticsearch/v8" | ||
| "github.com/elastic/go-elasticsearch/v8/esapi" | ||
| "github.com/tidwall/gjson" | ||
|
stereosteve marked this conversation as resolved.
Resolved
|
||
| "github.com/tidwall/pretty" | ||
| ) | ||
|
|
||
| func toAnySlice[T any](slice []T) []any { | ||
| result := make([]any, len(slice)) | ||
| for i, v := range slice { | ||
| result[i] = v | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func BuildFunctionScoreDSL(scoreField string, innerQuery map[string]any) string { | ||
| innerJson, err := json.Marshal(innerQuery) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| dsl := fmt.Sprintf(` | ||
| { | ||
| "query": { | ||
| "function_score": { | ||
| "query": %s, | ||
| "boost_mode": "multiply", | ||
| "score_mode": "multiply", | ||
| "functions": [ | ||
| { | ||
| "field_value_factor": { | ||
| "field": %q, | ||
| "factor": 100, | ||
| "modifier": "ln2p", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the ln2p?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly because the python codebase used it. It adds 2 to the value to smooths out the lower end of the range a bit... |
||
| "missing": 0 | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }`, innerJson, scoreField) | ||
|
|
||
| return dsl | ||
| } | ||
|
|
||
| func SearchAndPluck(esClient *elasticsearch.Client, index, dsl string, limit, offset int) ([]int32, error) { | ||
| req := esapi.SearchRequest{ | ||
| Index: []string{index}, | ||
| Body: strings.NewReader(dsl), | ||
| Source: []string{"false"}, | ||
| Size: &limit, | ||
| From: &offset, | ||
| } | ||
|
|
||
| res, err := req.Do(context.Background(), esClient) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer res.Body.Close() | ||
|
|
||
| if res.IsError() { | ||
| return nil, fmt.Errorf("Search %s failed: %s", index, res.String()) | ||
| } | ||
|
|
||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| result := []int32{} | ||
| for _, hit := range gjson.GetBytes(body, "hits.hits").Array() { | ||
| id := hit.Get("_id").Int() | ||
| result = append(result, int32(id)) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func pprintJson(j string) { | ||
| fmt.Println(string(pretty.Pretty([]byte(j)))) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package searchv1 | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/aquasecurity/esquery" | ||
| ) | ||
|
|
||
| type PlaylistSearchQuery struct { | ||
| Query string | ||
| Genres []string | ||
| Moods []string | ||
| IsTagSearch bool | ||
| IsAlbum bool | ||
| OnlyVerified bool | ||
| MyID int32 | ||
| } | ||
|
|
||
| func (q *PlaylistSearchQuery) Map() map[string]any { | ||
| builder := esquery.Bool() | ||
|
|
||
| if q.IsTagSearch { | ||
| builder.Must(esquery.MultiMatch().Query(q.Query).Fields("tracks.tags").Type(esquery.MatchTypeBoolPrefix)) | ||
| } else if q.Query != "" { | ||
| builder.Must(esquery.MultiMatch().Query(q.Query).Fields("title^10", "user.handle", "user.name", "tracks.tags").Type(esquery.MatchTypeBoolPrefix)) | ||
| } else { | ||
| builder.Must(esquery.MatchAll()) | ||
| } | ||
|
|
||
| builder.Filter(esquery.Term("is_album", q.IsAlbum)) | ||
|
|
||
| if len(q.Genres) > 0 { | ||
| builder.Filter(esquery.Terms("tracks.genre.keyword", toAnySlice(q.Genres)...)) | ||
| // by using a match query... the TF/IDF will apply to tracks. Which will rank playlists higher if they have a larger proportion of genre | ||
| for _, value := range q.Genres { | ||
| builder.Should(esquery.Match("tracks.genre", value)).Boost(10) | ||
| } | ||
| } | ||
|
|
||
| if len(q.Moods) > 0 { | ||
| builder.Filter(esquery.Terms("tracks.mood.keyword", toAnySlice(q.Moods)...)) | ||
| // by using a match query... the TF/IDF will apply to tracks. Which will rank playlists higher if they have a larger proportion of mood | ||
| for _, value := range q.Genres { | ||
| builder.Should(esquery.Match("tracks.mood", value)).Boost(10) | ||
| } | ||
| } | ||
|
|
||
| if q.OnlyVerified { | ||
| builder.Must(esquery.Term("tracks.user.is_verified", true)) | ||
| } else { | ||
| builder.Should(esquery.Term("user.is_verified", true)) | ||
| } | ||
|
|
||
| // personalize results | ||
| if q.MyID > 0 { | ||
| builder.Should(esquery.CustomQuery(map[string]any{ | ||
| "terms": map[string]any{ | ||
| "_id": map[string]any{ | ||
| "index": "socials", | ||
| "id": fmt.Sprintf("%d", q.MyID), | ||
| "path": "reposted_playlist_ids", | ||
| }, | ||
| "boost": 10, | ||
| }, | ||
| })) | ||
| } | ||
|
|
||
| return builder.Map() | ||
| } | ||
|
|
||
| func (q *PlaylistSearchQuery) DSL() string { | ||
| return BuildFunctionScoreDSL("repost_count", q.Map()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package searchv1 | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/aquasecurity/esquery" | ||
| ) | ||
|
|
||
| type TrackSearchQuery struct { | ||
| Query string | ||
| MinBPM int | ||
| MaxBPM int | ||
| IsDownloadable bool | ||
| IsPurchaseable bool | ||
| IsTagSearch bool | ||
| OnlyVerified bool | ||
| Genres []string | ||
| Moods []string | ||
| MusicalKeys []string | ||
| MyID int32 | ||
| } | ||
|
|
||
| func (q *TrackSearchQuery) Map() map[string]any { | ||
| builder := esquery.Bool() | ||
|
|
||
| if q.IsTagSearch { | ||
| builder.Must(esquery.MultiMatch().Query(q.Query).Fields("tags").Type(esquery.MatchTypeBoolPrefix)) | ||
| } else if q.Query != "" { | ||
| builder.Must( | ||
| esquery.MultiMatch(). | ||
| Query(q.Query). | ||
| Fields("title^10", "user.handle", "user.name", "tags"). | ||
| // Operator(esquery.OperatorAnd). | ||
| Type(esquery.MatchTypeBoolPrefix), | ||
| ) | ||
|
|
||
| // for exact title / handle / artist name match | ||
| builder.Should( | ||
| esquery.MultiMatch().Query(q.Query).Fields("title", "user.name", "user.handle").Operator(esquery.OperatorAnd).Type(esquery.MatchTypePhrasePrefix), | ||
| ) | ||
| } else { | ||
| builder.Must(esquery.MatchAll()) | ||
| } | ||
|
|
||
| if q.MinBPM > 0 || q.MaxBPM > 0 { | ||
| bpmRange := esquery.Range("bpm") | ||
| if q.MinBPM > 0 { | ||
| bpmRange.Gte(q.MinBPM) | ||
| } | ||
| if q.MaxBPM > 0 { | ||
| bpmRange.Lte(q.MaxBPM) | ||
| } | ||
| builder.Filter(bpmRange) | ||
| } | ||
|
|
||
| if len(q.Genres) > 0 { | ||
| builder.Filter(esquery.Terms("genre.keyword", toAnySlice(q.Genres)...)) | ||
| } | ||
|
|
||
| if len(q.Moods) > 0 { | ||
| builder.Filter(esquery.Terms("mood.keyword", toAnySlice(q.Moods)...)) | ||
| } | ||
|
|
||
| if len(q.MusicalKeys) > 0 { | ||
| builder.Filter(esquery.Terms("musical_key.keyword", toAnySlice(q.MusicalKeys)...)) | ||
| } | ||
|
|
||
| if q.IsDownloadable { | ||
| builder.Filter(esquery.Term("is_downloadable", true)) | ||
| } | ||
|
|
||
| // todo: only_with_downloads | ||
| // => downloadable + has stems | ||
|
|
||
| if q.IsPurchaseable { | ||
| // stream or download | ||
| builder.Filter( | ||
| esquery.Bool(). | ||
| Should(esquery.Exists("stream_conditions.usdc_purchase")). | ||
| Should(esquery.Exists("download_conditions.usdc_purchase")), | ||
| ) | ||
| } | ||
|
|
||
| if q.OnlyVerified { | ||
| builder.Must(esquery.Term("user.is_verified", true)) | ||
| } else { | ||
| builder.Should(esquery.Term("user.is_verified", true)) | ||
| } | ||
|
|
||
| // boost tracks that are saved / reposted | ||
| if q.MyID > 0 { | ||
| builder.Should(esquery.CustomQuery(map[string]any{ | ||
| "terms": map[string]any{ | ||
| "_id": map[string]any{ | ||
| "index": "socials", | ||
| "id": fmt.Sprintf("%d", q.MyID), | ||
| "path": "reposted_track_ids", | ||
| }, | ||
| "boost": 10, | ||
| }, | ||
| })) | ||
| } | ||
|
|
||
| return builder.Map() | ||
| } | ||
|
|
||
| func (q *TrackSearchQuery) DSL() string { | ||
| return BuildFunctionScoreDSL("repost_count", q.Map()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.