aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
author6543 <m.huber@kithara.com>2024-03-16 17:01:40 +0100
committerGitHub <noreply@github.com>2024-03-16 17:01:40 +0100
commit0cbbcf20e3f83413a88fe3d436451d707639fe55 (patch)
tree269795ef294d37f43d1bc5c7bbde0c88cded9251
parent47dc4598a305ea1e6017803b95c7c60d0be64784 (diff)
downloadgitea-0cbbcf20e3f83413a88fe3d436451d707639fe55.tar.gz
gitea-0cbbcf20e3f83413a88fe3d436451d707639fe55.zip
Make meilisearch do exact search for issues (#29740 & #29671) (#29846)
Backport https://github.com/go-gitea/gitea/pull/29740 (based on #29671 ...)
-rw-r--r--modules/indexer/issues/meilisearch/meilisearch.go20
-rw-r--r--modules/indexer/issues/meilisearch/meilisearch_test.go10
2 files changed, 29 insertions, 1 deletions
diff --git a/modules/indexer/issues/meilisearch/meilisearch.go b/modules/indexer/issues/meilisearch/meilisearch.go
index ab8dcd0af4..060b5c5279 100644
--- a/modules/indexer/issues/meilisearch/meilisearch.go
+++ b/modules/indexer/issues/meilisearch/meilisearch.go
@@ -5,6 +5,7 @@ package meilisearch
import (
"context"
+ "fmt"
"strconv"
"strings"
@@ -210,7 +211,11 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
skip, limit := indexer_internal.ParsePaginator(options.Paginator, maxTotalHits)
- searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).Search(options.Keyword, &meilisearch.SearchRequest{
+ // to make it non fuzzy ("typo tolerance" in meilisearch terms), we have to quote the keyword(s)
+ // https://www.meilisearch.com/docs/reference/api/search#phrase-search
+ keyword := doubleQuoteKeyword(options.Keyword)
+
+ searchRes, err := b.inner.Client.Index(b.inner.VersionedIndexName()).Search(keyword, &meilisearch.SearchRequest{
Filter: query.Statement(),
Limit: int64(limit),
Offset: int64(skip),
@@ -241,3 +246,16 @@ func parseSortBy(sortBy internal.SortBy) string {
}
return field + ":asc"
}
+
+func doubleQuoteKeyword(k string) string {
+ kp := strings.Split(k, " ")
+ parts := 0
+ for i := range kp {
+ part := strings.Trim(kp[i], "\"")
+ if part != "" {
+ kp[parts] = fmt.Sprintf(`"%s"`, part)
+ parts++
+ }
+ }
+ return strings.Join(kp[:parts], " ")
+}
diff --git a/modules/indexer/issues/meilisearch/meilisearch_test.go b/modules/indexer/issues/meilisearch/meilisearch_test.go
index 3d7237268e..2837c3a0bd 100644
--- a/modules/indexer/issues/meilisearch/meilisearch_test.go
+++ b/modules/indexer/issues/meilisearch/meilisearch_test.go
@@ -11,6 +11,8 @@ import (
"time"
"code.gitea.io/gitea/modules/indexer/issues/internal/tests"
+
+ "github.com/stretchr/testify/assert"
)
func TestMeilisearchIndexer(t *testing.T) {
@@ -48,3 +50,11 @@ func TestMeilisearchIndexer(t *testing.T) {
tests.TestIndexer(t, indexer)
}
+
+func TestDoubleQuoteKeyword(t *testing.T) {
+ assert.EqualValues(t, "", doubleQuoteKeyword(""))
+ assert.EqualValues(t, `"a" "b" "c"`, doubleQuoteKeyword("a b c"))
+ assert.EqualValues(t, `"a" "d" "g"`, doubleQuoteKeyword("a d g"))
+ assert.EqualValues(t, `"a" "d" "g"`, doubleQuoteKeyword("a d g"))
+ assert.EqualValues(t, `"a" "d" "g"`, doubleQuoteKeyword(`a "" "d" """g`))
+}