Skip to content

Commit

Permalink
Meilisearch double quote on "match" query (go-gitea#29740)
Browse files Browse the repository at this point in the history
make `nonFuzzyWorkaround` unessesary

cc @Kerollmops
  • Loading branch information
6543 committed Mar 16, 2024
1 parent 47dc459 commit a42637a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
23 changes: 22 additions & 1 deletion modules/indexer/issues/meilisearch/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package meilisearch

import (
"context"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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], " ")
}
9 changes: 9 additions & 0 deletions modules/indexer/issues/meilisearch/meilisearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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`))
}

0 comments on commit a42637a

Please sign in to comment.