mirror of
https://github.com/go-gitea/gitea
synced 2024-11-09 11:44:27 +00: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 elasticsearch
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.gitea.io/gitea/modules/indexer/internal"
|
|
|
|
"github.com/olivere/elastic/v7"
|
|
)
|
|
|
|
var _ internal.Indexer = &Indexer{}
|
|
|
|
// Indexer represents a basic elasticsearch indexer implementation
|
|
type Indexer struct {
|
|
Client *elastic.Client
|
|
|
|
url string
|
|
indexName string
|
|
version int
|
|
mapping string
|
|
}
|
|
|
|
func NewIndexer(url, indexName string, version int, mapping string) *Indexer {
|
|
return &Indexer{
|
|
url: url,
|
|
indexName: indexName,
|
|
version: version,
|
|
mapping: mapping,
|
|
}
|
|
}
|
|
|
|
// Init initializes the indexer
|
|
func (i *Indexer) Init(ctx 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")
|
|
}
|
|
|
|
client, err := i.initClient()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
i.Client = client
|
|
|
|
exists, err := i.Client.IndexExists(i.VersionedIndexName()).Do(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if exists {
|
|
return true, nil
|
|
}
|
|
|
|
if err := i.createIndex(ctx); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return exists, nil
|
|
}
|
|
|
|
// 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.ClusterHealth().Do(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp.Status != "green" && resp.Status != "yellow" {
|
|
// It's healthy if the status is green, and it's available if the status is yellow,
|
|
// see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
|
|
return fmt.Errorf("status of elasticsearch cluster is %s", resp.Status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Close closes the indexer
|
|
func (i *Indexer) Close() {
|
|
if i == nil {
|
|
return
|
|
}
|
|
i.Client = nil
|
|
}
|