Skip to content

Commit a42637a

Browse files
committed
Meilisearch double quote on "match" query (go-gitea#29740)
make `nonFuzzyWorkaround` unessesary cc @Kerollmops
1 parent 47dc459 commit a42637a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

modules/indexer/issues/meilisearch/meilisearch.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package meilisearch
55

66
import (
77
"context"
8+
"fmt"
89
"strconv"
910
"strings"
1011

@@ -210,7 +211,14 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
210211

211212
skip, limit := indexer_internal.ParsePaginator(options.Paginator, maxTotalHits)
212213

213-
searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).Search(options.Keyword, &meilisearch.SearchRequest{
214+
keyword := options.Keyword
215+
if !options.IsFuzzyKeyword {
216+
// to make it non fuzzy ("typo tolerance" in meilisearch terms), we have to quote the keyword(s)
217+
// https://www.meilisearch.com/docs/reference/api/search#phrase-search
218+
keyword = doubleQuoteKeyword(keyword)
219+
}
220+
221+
searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).Search(keyword, &meilisearch.SearchRequest{
214222
Filter: query.Statement(),
215223
Limit: int64(limit),
216224
Offset: int64(skip),
@@ -241,3 +249,16 @@ func parseSortBy(sortBy internal.SortBy) string {
241249
}
242250
return field + ":asc"
243251
}
252+
253+
func doubleQuoteKeyword(k string) string {
254+
kp := strings.Split(k, " ")
255+
parts := 0
256+
for i := range kp {
257+
part := strings.Trim(kp[i], "\"")
258+
if part != "" {
259+
kp[parts] = fmt.Sprintf(`"%s"`, part)
260+
parts++
261+
}
262+
}
263+
return strings.Join(kp[:parts], " ")
264+
}

modules/indexer/issues/meilisearch/meilisearch_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/modules/indexer/issues/internal/tests"
14+
"github.com/stretchr/testify/assert"
1415
)
1516

1617
func TestMeilisearchIndexer(t *testing.T) {
@@ -48,3 +49,11 @@ func TestMeilisearchIndexer(t *testing.T) {
4849

4950
tests.TestIndexer(t, indexer)
5051
}
52+
53+
func TestDoubleQuoteKeyword(t *testing.T) {
54+
assert.EqualValues(t, "", doubleQuoteKeyword(""))
55+
assert.EqualValues(t, `"a" "b" "c"`, doubleQuoteKeyword("a b c"))
56+
assert.EqualValues(t, `"a" "d" "g"`, doubleQuoteKeyword("a d g"))
57+
assert.EqualValues(t, `"a" "d" "g"`, doubleQuoteKeyword("a d g"))
58+
assert.EqualValues(t, `"a" "d" "g"`, doubleQuoteKeyword(`a "" "d" """g`))
59+
}

0 commit comments

Comments
 (0)