Skip to content

Commit 0cbbcf2

Browse files
authored
Make meilisearch do exact search for issues (#29740 & #29671) (#29846)
Backport #29740 (based on #29671 ...)
1 parent 47dc459 commit 0cbbcf2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

modules/indexer/issues/meilisearch/meilisearch.go

Lines changed: 19 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,11 @@ 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+
// to make it non fuzzy ("typo tolerance" in meilisearch terms), we have to quote the keyword(s)
215+
// https://www.meilisearch.com/docs/reference/api/search#phrase-search
216+
keyword := doubleQuoteKeyword(options.Keyword)
217+
218+
searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).Search(keyword, &meilisearch.SearchRequest{
214219
Filter: query.Statement(),
215220
Limit: int64(limit),
216221
Offset: int64(skip),
@@ -241,3 +246,16 @@ func parseSortBy(sortBy internal.SortBy) string {
241246
}
242247
return field + ":asc"
243248
}
249+
250+
func doubleQuoteKeyword(k string) string {
251+
kp := strings.Split(k, " ")
252+
parts := 0
253+
for i := range kp {
254+
part := strings.Trim(kp[i], "\"")
255+
if part != "" {
256+
kp[parts] = fmt.Sprintf(`"%s"`, part)
257+
parts++
258+
}
259+
}
260+
return strings.Join(kp[:parts], " ")
261+
}

modules/indexer/issues/meilisearch/meilisearch_test.go

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

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

1618
func TestMeilisearchIndexer(t *testing.T) {
@@ -48,3 +50,11 @@ func TestMeilisearchIndexer(t *testing.T) {
4850

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

0 commit comments

Comments
 (0)