Refactor and enhance issue indexer to support both searching, filtering and paging (#26012)
Fix #24662.
Replace #24822 and #25708 (although it has been merged)
## Background
In the past, Gitea supported issue searching with a keyword and
conditions in a less efficient way. It worked by searching for issues
with the keyword and obtaining limited IDs (as it is heavy to get all)
on the indexer (bleve/elasticsearch/meilisearch), and then querying with
conditions on the database to find a subset of the found IDs. This is
why the results could be incomplete.
To solve this issue, we need to store all fields that could be used as
conditions in the indexer and support both keyword and additional
conditions when searching with the indexer.
## Major changes
- Redefine `IndexerData` to include all fields that could be used as
filter conditions.
- Refactor `Search(ctx context.Context, kw string, repoIDs []int64,
limit, start int, state string)` to `Search(ctx context.Context, options
*SearchOptions)`, so it supports more conditions now.
- Change the data type stored in `issueIndexerQueue`. Use
`IndexerMetadata` instead of `IndexerData` in case the data has been
updated while it is in the queue. This also reduces the storage size of
the queue.
- Enhance searching with Bleve/Elasticsearch/Meilisearch, make them
fully support `SearchOptions`. Also, update the data versions.
- Keep most logic of database indexer, but remove
`issues.SearchIssueIDsByKeyword` in `models` to avoid confusion where is
the entry point to search issues.
- Start a Meilisearch instance to test it in unit tests.
- Add unit tests with almost full coverage to test
Bleve/Elasticsearch/Meilisearch indexer.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-07-31 08:28:53 +02:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package meilisearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-03-09 02:39:27 +01:00
|
|
|
"code.gitea.io/gitea/modules/indexer/issues/internal"
|
Refactor and enhance issue indexer to support both searching, filtering and paging (#26012)
Fix #24662.
Replace #24822 and #25708 (although it has been merged)
## Background
In the past, Gitea supported issue searching with a keyword and
conditions in a less efficient way. It worked by searching for issues
with the keyword and obtaining limited IDs (as it is heavy to get all)
on the indexer (bleve/elasticsearch/meilisearch), and then querying with
conditions on the database to find a subset of the found IDs. This is
why the results could be incomplete.
To solve this issue, we need to store all fields that could be used as
conditions in the indexer and support both keyword and additional
conditions when searching with the indexer.
## Major changes
- Redefine `IndexerData` to include all fields that could be used as
filter conditions.
- Refactor `Search(ctx context.Context, kw string, repoIDs []int64,
limit, start int, state string)` to `Search(ctx context.Context, options
*SearchOptions)`, so it supports more conditions now.
- Change the data type stored in `issueIndexerQueue`. Use
`IndexerMetadata` instead of `IndexerData` in case the data has been
updated while it is in the queue. This also reduces the storage size of
the queue.
- Enhance searching with Bleve/Elasticsearch/Meilisearch, make them
fully support `SearchOptions`. Also, update the data versions.
- Keep most logic of database indexer, but remove
`issues.SearchIssueIDsByKeyword` in `models` to avoid confusion where is
the entry point to search issues.
- Start a Meilisearch instance to test it in unit tests.
- Add unit tests with almost full coverage to test
Bleve/Elasticsearch/Meilisearch indexer.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-07-31 08:28:53 +02:00
|
|
|
"code.gitea.io/gitea/modules/indexer/issues/internal/tests"
|
2024-03-09 02:39:27 +01:00
|
|
|
|
|
|
|
"github.com/meilisearch/meilisearch-go"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:42:06 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
Refactor and enhance issue indexer to support both searching, filtering and paging (#26012)
Fix #24662.
Replace #24822 and #25708 (although it has been merged)
## Background
In the past, Gitea supported issue searching with a keyword and
conditions in a less efficient way. It worked by searching for issues
with the keyword and obtaining limited IDs (as it is heavy to get all)
on the indexer (bleve/elasticsearch/meilisearch), and then querying with
conditions on the database to find a subset of the found IDs. This is
why the results could be incomplete.
To solve this issue, we need to store all fields that could be used as
conditions in the indexer and support both keyword and additional
conditions when searching with the indexer.
## Major changes
- Redefine `IndexerData` to include all fields that could be used as
filter conditions.
- Refactor `Search(ctx context.Context, kw string, repoIDs []int64,
limit, start int, state string)` to `Search(ctx context.Context, options
*SearchOptions)`, so it supports more conditions now.
- Change the data type stored in `issueIndexerQueue`. Use
`IndexerMetadata` instead of `IndexerData` in case the data has been
updated while it is in the queue. This also reduces the storage size of
the queue.
- Enhance searching with Bleve/Elasticsearch/Meilisearch, make them
fully support `SearchOptions`. Also, update the data versions.
- Keep most logic of database indexer, but remove
`issues.SearchIssueIDsByKeyword` in `models` to avoid confusion where is
the entry point to search issues.
- Start a Meilisearch instance to test it in unit tests.
- Add unit tests with almost full coverage to test
Bleve/Elasticsearch/Meilisearch indexer.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-07-31 08:28:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMeilisearchIndexer(t *testing.T) {
|
2023-08-01 08:44:46 +02:00
|
|
|
t.Skip("meilisearch not found in Forgejo test yet")
|
Refactor and enhance issue indexer to support both searching, filtering and paging (#26012)
Fix #24662.
Replace #24822 and #25708 (although it has been merged)
## Background
In the past, Gitea supported issue searching with a keyword and
conditions in a less efficient way. It worked by searching for issues
with the keyword and obtaining limited IDs (as it is heavy to get all)
on the indexer (bleve/elasticsearch/meilisearch), and then querying with
conditions on the database to find a subset of the found IDs. This is
why the results could be incomplete.
To solve this issue, we need to store all fields that could be used as
conditions in the indexer and support both keyword and additional
conditions when searching with the indexer.
## Major changes
- Redefine `IndexerData` to include all fields that could be used as
filter conditions.
- Refactor `Search(ctx context.Context, kw string, repoIDs []int64,
limit, start int, state string)` to `Search(ctx context.Context, options
*SearchOptions)`, so it supports more conditions now.
- Change the data type stored in `issueIndexerQueue`. Use
`IndexerMetadata` instead of `IndexerData` in case the data has been
updated while it is in the queue. This also reduces the storage size of
the queue.
- Enhance searching with Bleve/Elasticsearch/Meilisearch, make them
fully support `SearchOptions`. Also, update the data versions.
- Keep most logic of database indexer, but remove
`issues.SearchIssueIDsByKeyword` in `models` to avoid confusion where is
the entry point to search issues.
- Start a Meilisearch instance to test it in unit tests.
- Add unit tests with almost full coverage to test
Bleve/Elasticsearch/Meilisearch indexer.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-07-31 08:28:53 +02:00
|
|
|
// The meilisearch instance started by pull-db-tests.yml > test-unit > services > meilisearch
|
|
|
|
url := "http://meilisearch:7700"
|
|
|
|
key := "" // auth has been disabled in test environment
|
|
|
|
|
|
|
|
if os.Getenv("CI") == "" {
|
|
|
|
// Make it possible to run tests against a local meilisearch instance
|
|
|
|
url = os.Getenv("TEST_MEILISEARCH_URL")
|
|
|
|
if url == "" {
|
|
|
|
t.Skip("TEST_MEILISEARCH_URL not set and not running in CI")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
key = os.Getenv("TEST_MEILISEARCH_KEY")
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := false
|
|
|
|
for i := 0; i < 60; i++ {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err == nil && resp.StatusCode == http.StatusOK {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Logf("Waiting for meilisearch to be up: %v", err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Failed to wait for meilisearch to be up")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
indexer := NewIndexer(url, key, fmt.Sprintf("test_meilisearch_indexer_%d", time.Now().Unix()))
|
|
|
|
defer indexer.Close()
|
|
|
|
|
|
|
|
tests.TestIndexer(t, indexer)
|
|
|
|
}
|
2024-03-09 02:39:27 +01:00
|
|
|
|
2024-03-16 14:19:41 +01:00
|
|
|
func TestConvertHits(t *testing.T) {
|
|
|
|
_, err := convertHits(&meilisearch.SearchResponse{
|
2024-03-09 02:39:27 +01:00
|
|
|
Hits: []any{"aa", "bb", "cc", "dd"},
|
2024-03-16 14:19:41 +01:00
|
|
|
})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.ErrorIs(t, err, ErrMalformedResponse)
|
2024-03-09 02:39:27 +01:00
|
|
|
|
|
|
|
validResponse := &meilisearch.SearchResponse{
|
|
|
|
Hits: []any{
|
|
|
|
map[string]any{
|
|
|
|
"id": float64(11),
|
|
|
|
"title": "a title",
|
|
|
|
"content": "issue body with no match",
|
|
|
|
"comments": []any{"hey whats up?", "I'm currently bowling", "nice"},
|
|
|
|
},
|
|
|
|
map[string]any{
|
|
|
|
"id": float64(22),
|
|
|
|
"title": "Bowling as title",
|
|
|
|
"content": "",
|
|
|
|
"comments": []any{},
|
|
|
|
},
|
|
|
|
map[string]any{
|
|
|
|
"id": float64(33),
|
|
|
|
"title": "Bowl-ing as fuzzy match",
|
|
|
|
"content": "",
|
|
|
|
"comments": []any{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-03-16 14:19:41 +01:00
|
|
|
hits, err := convertHits(validResponse)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-09 02:39:27 +01:00
|
|
|
assert.EqualValues(t, []internal.Match{{ID: 11}, {ID: 22}, {ID: 33}}, hits)
|
|
|
|
}
|
2024-03-16 14:19:41 +01:00
|
|
|
|
|
|
|
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`))
|
|
|
|
}
|