From a42637ad56210d50de4ba861f3f0e84a8490aecd Mon Sep 17 00:00:00 2001 From: 6543 Date: Sat, 16 Mar 2024 14:19:41 +0100 Subject: [PATCH 1/3] Meilisearch double quote on "match" query (#29740) make `nonFuzzyWorkaround` unessesary cc @Kerollmops --- .../indexer/issues/meilisearch/meilisearch.go | 23 ++++++++++++++++++- .../issues/meilisearch/meilisearch_test.go | 9 ++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/modules/indexer/issues/meilisearch/meilisearch.go b/modules/indexer/issues/meilisearch/meilisearch.go index ab8dcd0af45a8..1ebf06defd2e9 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,14 @@ 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{ + keyword := options.Keyword + if !options.IsFuzzyKeyword { + // 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(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 +249,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 3d7237268e1bd..e16f0c9812722 100644 --- a/modules/indexer/issues/meilisearch/meilisearch_test.go +++ b/modules/indexer/issues/meilisearch/meilisearch_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/modules/indexer/issues/internal/tests" + "github.com/stretchr/testify/assert" ) func TestMeilisearchIndexer(t *testing.T) { @@ -48,3 +49,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`)) +} From 3b1f1dd388a8215575a7bc424287545ba2ecfe8e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 16 Mar 2024 15:27:49 +0100 Subject: [PATCH 2/3] adjust backport --- modules/indexer/issues/meilisearch/meilisearch.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/indexer/issues/meilisearch/meilisearch.go b/modules/indexer/issues/meilisearch/meilisearch.go index 1ebf06defd2e9..060b5c5279370 100644 --- a/modules/indexer/issues/meilisearch/meilisearch.go +++ b/modules/indexer/issues/meilisearch/meilisearch.go @@ -211,12 +211,9 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) ( skip, limit := indexer_internal.ParsePaginator(options.Paginator, maxTotalHits) - keyword := options.Keyword - if !options.IsFuzzyKeyword { - // 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(keyword) - } + // 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(), From 3027764f0af260f8f5dc34e7e388840fb50b1d33 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 16 Mar 2024 16:04:09 +0100 Subject: [PATCH 3/3] Update modules/indexer/issues/meilisearch/meilisearch_test.go --- modules/indexer/issues/meilisearch/meilisearch_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/indexer/issues/meilisearch/meilisearch_test.go b/modules/indexer/issues/meilisearch/meilisearch_test.go index e16f0c9812722..2837c3a0bd921 100644 --- a/modules/indexer/issues/meilisearch/meilisearch_test.go +++ b/modules/indexer/issues/meilisearch/meilisearch_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/modules/indexer/issues/internal/tests" + "github.com/stretchr/testify/assert" )