Skip to content

Commit 95cb294

Browse files
authored
Merge pull request #152 from Quaver/es-mapsets-search-tag-improvements
implement tag search terms priority
2 parents b901195 + 853c5ec commit 95cb294

File tree

1 file changed

+79
-20
lines changed

1 file changed

+79
-20
lines changed

db/elasticsearch_mapsets.go

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"github.com/Quaver/api2/enums"
10-
"github.com/Quaver/api2/sliceutil"
11-
"github.com/elastic/go-elasticsearch/v8/esutil"
12-
"github.com/sirupsen/logrus"
139
"io"
1410
"math"
1511
"strconv"
1612
"strings"
1713
"time"
14+
15+
"github.com/Quaver/api2/enums"
16+
"github.com/Quaver/api2/sliceutil"
17+
"github.com/elastic/go-elasticsearch/v8/esutil"
18+
"github.com/sirupsen/logrus"
1819
)
1920

2021
type ElasticMapsetSearchOptions struct {
@@ -97,6 +98,37 @@ type ElasticMap struct {
9798
DateLastUpdated int64 `json:"date_last_updated"`
9899
}
99100

101+
var tagSearchTerms = []string{
102+
"scroll velocity",
103+
"sv",
104+
"long notes",
105+
"ln",
106+
"mixed ln",
107+
"inverse",
108+
"hybrid",
109+
"hb",
110+
"full flavor",
111+
"rice",
112+
"rc",
113+
"mixed rice",
114+
"speed",
115+
"dump",
116+
"stream",
117+
"streams",
118+
"jumpstream",
119+
"js",
120+
"handstream",
121+
"hs",
122+
"chordjack",
123+
"cj",
124+
"speedjack",
125+
"brackets",
126+
"delay",
127+
"chordstream",
128+
"tb",
129+
"tiebreaker",
130+
}
131+
100132
type ElasticHits struct {
101133
Hits struct {
102134
Hits []struct {
@@ -286,22 +318,44 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, int,
286318
boolQuery := BoolQuery{}
287319

288320
if options.Search != "" {
289-
boolQuerySearch := BoolQuery{}
321+
searchTerm := strings.ToLower(strings.TrimSpace(options.Search))
322+
323+
if searchTerm == "" {
324+
options.Search = ""
325+
} else {
326+
options.Search = searchTerm
327+
328+
boost := 0.2
329+
useTagSearchOnly := false
330+
331+
for _, term := range tagSearchTerms {
332+
if strings.Contains(searchTerm, term) {
333+
useTagSearchOnly = true
334+
boost = 1
335+
break
336+
}
337+
}
290338

291-
qs := NewQueryString(options.Search, []string{"title", "artist"}, "OR", 1.0)
292-
qs2 := NewQueryString(options.Search, []string{"source", "creator_username", "difficulty_name"}, "OR", 0.8)
339+
boolQuerySearch := BoolQuery{}
293340

294-
m := map[string]interface{}{
295-
"match": map[string]interface{}{
296-
"tags": map[string]interface{}{
297-
"query": options.Search,
298-
"boost": 0.2,
341+
m := map[string]interface{}{
342+
"match": map[string]interface{}{
343+
"tags": map[string]interface{}{
344+
"query": options.Search,
345+
"boost": boost,
346+
},
299347
},
300-
},
301-
}
348+
}
302349

303-
boolQuerySearch.BoolQuery.Should = append(boolQuerySearch.BoolQuery.Should, qs, qs2, m)
304-
boolQuery.BoolQuery.Must = append(boolQuery.BoolQuery.Must, boolQuerySearch)
350+
if useTagSearchOnly {
351+
boolQuerySearch.BoolQuery.Should = append(boolQuerySearch.BoolQuery.Should, m)
352+
} else {
353+
qs := NewQueryString(options.Search, []string{"title", "artist"}, "OR", 1.0)
354+
qs2 := NewQueryString(options.Search, []string{"source", "creator_username", "difficulty_name"}, "OR", 0.8)
355+
boolQuerySearch.BoolQuery.Should = append(boolQuerySearch.BoolQuery.Should, qs, qs2, m)
356+
}
357+
boolQuery.BoolQuery.Must = append(boolQuery.BoolQuery.Must, boolQuerySearch)
358+
}
305359
}
306360

307361
if options.Mode != nil {
@@ -385,6 +439,14 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, int,
385439
sort = "date_clan_ranked"
386440
}
387441

442+
sortFields := []map[string]SortOrder{
443+
{sort: {Order: sortOrder}},
444+
}
445+
446+
if options.Search != "" {
447+
sortFields = append(sortFields, map[string]SortOrder{"_score": {Order: "desc"}})
448+
}
449+
388450
query := Query{
389451
Size: options.Limit,
390452
From: options.Page * options.Limit,
@@ -399,10 +461,7 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, int,
399461
},
400462
},
401463
Query: boolQuery,
402-
Sort: []map[string]SortOrder{
403-
{"_score": {Order: "desc"}},
404-
{sort: {Order: sortOrder}},
405-
},
464+
Sort: sortFields,
406465
Aggs: map[string]interface{}{
407466
"distinct_mapset_ids": map[string]interface{}{
408467
"cardinality": map[string]interface{}{

0 commit comments

Comments
 (0)