mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 12:59:11 -05:00
1e76a824bc
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>
94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package meilisearch
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/meilisearch/meilisearch-go"
|
|
)
|
|
|
|
// Indexer represents a basic meilisearch indexer implementation
|
|
type Indexer struct {
|
|
Client *meilisearch.Client
|
|
|
|
url, apiKey string
|
|
indexName string
|
|
version int
|
|
settings *meilisearch.Settings
|
|
}
|
|
|
|
func NewIndexer(url, apiKey, indexName string, version int, settings *meilisearch.Settings) *Indexer {
|
|
return &Indexer{
|
|
url: url,
|
|
apiKey: apiKey,
|
|
indexName: indexName,
|
|
version: version,
|
|
settings: settings,
|
|
}
|
|
}
|
|
|
|
// Init initializes the indexer
|
|
func (i *Indexer) Init(_ context.Context) (bool, error) {
|
|
if i == nil {
|
|
return false, fmt.Errorf("cannot init nil indexer")
|
|
}
|
|
|
|
if i.Client != nil {
|
|
return false, fmt.Errorf("indexer is already initialized")
|
|
}
|
|
|
|
i.Client = meilisearch.NewClient(meilisearch.ClientConfig{
|
|
Host: i.url,
|
|
APIKey: i.apiKey,
|
|
})
|
|
|
|
_, err := i.Client.GetIndex(i.VersionedIndexName())
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
_, err = i.Client.CreateIndex(&meilisearch.IndexConfig{
|
|
Uid: i.VersionedIndexName(),
|
|
PrimaryKey: "id",
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
i.checkOldIndexes()
|
|
|
|
_, err = i.Client.Index(i.VersionedIndexName()).UpdateSettings(i.settings)
|
|
return false, err
|
|
}
|
|
|
|
// Ping checks if the indexer is available
|
|
func (i *Indexer) Ping(ctx context.Context) error {
|
|
if i == nil {
|
|
return fmt.Errorf("cannot ping nil indexer")
|
|
}
|
|
if i.Client == nil {
|
|
return fmt.Errorf("indexer is not initialized")
|
|
}
|
|
resp, err := i.Client.Health()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp.Status != "available" {
|
|
// See https://docs.meilisearch.com/reference/api/health.html#status
|
|
return fmt.Errorf("status of meilisearch is not available: %s", resp.Status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Close closes the indexer
|
|
func (i *Indexer) Close() {
|
|
if i == nil {
|
|
return
|
|
}
|
|
if i.Client == nil {
|
|
return
|
|
}
|
|
i.Client = nil
|
|
}
|