2019-12-09 00:45:35 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-09 00:45:35 +05:30
|
|
|
|
|
|
|
package code
|
|
|
|
|
|
|
|
import (
|
2021-03-04 08:27:01 +05:30
|
|
|
"bufio"
|
2022-01-20 04:56:57 +05:30
|
|
|
"context"
|
2019-12-09 00:45:35 +05:30
|
|
|
"fmt"
|
2021-03-04 08:27:01 +05:30
|
|
|
"io"
|
2019-12-15 15:21:28 +05:30
|
|
|
"os"
|
2019-12-09 00:45:35 +05:30
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-02-21 01:23:55 +05:30
|
|
|
"time"
|
2019-12-09 00:45:35 +05:30
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-02-21 01:23:55 +05:30
|
|
|
"code.gitea.io/gitea/modules/analyze"
|
2019-12-09 00:45:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/charset"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-07-19 01:29:10 +05:30
|
|
|
gitea_bleve "code.gitea.io/gitea/modules/indexer/bleve"
|
2019-12-09 00:45:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-02-21 01:23:55 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-06-05 18:02:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/typesniffer"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-12-23 18:01:16 +05:30
|
|
|
|
2021-01-18 06:51:14 +05:30
|
|
|
"github.com/blevesearch/bleve/v2"
|
|
|
|
analyzer_custom "github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
|
|
|
|
analyzer_keyword "github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
|
|
|
|
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
|
|
|
|
"github.com/blevesearch/bleve/v2/analysis/token/unicodenorm"
|
|
|
|
"github.com/blevesearch/bleve/v2/analysis/tokenizer/unicode"
|
|
|
|
"github.com/blevesearch/bleve/v2/index/upsidedown"
|
|
|
|
"github.com/blevesearch/bleve/v2/mapping"
|
|
|
|
"github.com/blevesearch/bleve/v2/search/query"
|
2019-12-09 00:45:35 +05:30
|
|
|
"github.com/ethantkoenig/rupture"
|
2020-04-15 23:10:39 +05:30
|
|
|
"github.com/go-enry/go-enry/v2"
|
2019-12-09 00:45:35 +05:30
|
|
|
)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
const (
|
|
|
|
unicodeNormalizeName = "unicodeNormalize"
|
|
|
|
maxBatchSize = 16
|
|
|
|
)
|
2019-12-09 00:45:35 +05:30
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// numericEqualityQuery a numeric equality query for the given value and field
|
|
|
|
func numericEqualityQuery(value int64, field string) *query.NumericRangeQuery {
|
|
|
|
f := float64(value)
|
|
|
|
tru := true
|
|
|
|
q := bleve.NewNumericRangeInclusiveQuery(&f, &f, &tru, &tru)
|
|
|
|
q.SetField(field)
|
|
|
|
return q
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
func addUnicodeNormalizeTokenFilter(m *mapping.IndexMappingImpl) error {
|
|
|
|
return m.AddCustomTokenFilter(unicodeNormalizeName, map[string]interface{}{
|
|
|
|
"type": unicodenorm.Name,
|
|
|
|
"form": unicodenorm.NFC,
|
|
|
|
})
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2020-08-30 21:38:01 +05:30
|
|
|
// openBleveIndexer open the index at the specified path, checking for metadata
|
2019-12-23 18:01:16 +05:30
|
|
|
// updates and bleve version updates. If index needs to be created (or
|
|
|
|
// re-created), returns (nil, nil)
|
2020-08-30 21:38:01 +05:30
|
|
|
func openBleveIndexer(path string, latestVersion int) (bleve.Index, error) {
|
2019-12-23 18:01:16 +05:30
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
metadata, err := rupture.ReadIndexMetadata(path)
|
2019-12-09 00:45:35 +05:30
|
|
|
if err != nil {
|
2019-12-23 18:01:16 +05:30
|
|
|
return nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
if metadata.Version < latestVersion {
|
|
|
|
// the indexer is using a previous version, so we should delete it and
|
|
|
|
// re-populate
|
2020-08-12 01:35:34 +05:30
|
|
|
return nil, util.RemoveAll(path)
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
index, err := bleve.Open(path)
|
|
|
|
if err != nil && err == upsidedown.IncompatibleVersion {
|
|
|
|
// the indexer was built with a previous version of bleve, so we should
|
|
|
|
// delete it and re-populate
|
2020-08-12 01:35:34 +05:30
|
|
|
return nil, util.RemoveAll(path)
|
2019-12-23 18:01:16 +05:30
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
return index, nil
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// RepoIndexerData data stored in the repo indexer
|
|
|
|
type RepoIndexerData struct {
|
2020-02-21 01:23:55 +05:30
|
|
|
RepoID int64
|
|
|
|
CommitID string
|
|
|
|
Content string
|
|
|
|
Language string
|
|
|
|
UpdatedAt time.Time
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// Type returns the document type, for bleve's mapping.Classifier interface.
|
|
|
|
func (d *RepoIndexerData) Type() string {
|
|
|
|
return repoIndexerDocType
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
const (
|
|
|
|
repoIndexerAnalyzer = "repoIndexerAnalyzer"
|
|
|
|
repoIndexerDocType = "repoIndexerDocType"
|
2020-02-21 01:23:55 +05:30
|
|
|
repoIndexerLatestVersion = 5
|
2019-12-23 18:01:16 +05:30
|
|
|
)
|
|
|
|
|
2020-08-30 21:38:01 +05:30
|
|
|
// createBleveIndexer create a bleve repo indexer if one does not already exist
|
|
|
|
func createBleveIndexer(path string, latestVersion int) (bleve.Index, error) {
|
2019-12-23 18:01:16 +05:30
|
|
|
docMapping := bleve.NewDocumentMapping()
|
|
|
|
numericFieldMapping := bleve.NewNumericFieldMapping()
|
|
|
|
numericFieldMapping.IncludeInAll = false
|
|
|
|
docMapping.AddFieldMappingsAt("RepoID", numericFieldMapping)
|
|
|
|
|
|
|
|
textFieldMapping := bleve.NewTextFieldMapping()
|
|
|
|
textFieldMapping.IncludeInAll = false
|
|
|
|
docMapping.AddFieldMappingsAt("Content", textFieldMapping)
|
|
|
|
|
2020-02-21 01:23:55 +05:30
|
|
|
termFieldMapping := bleve.NewTextFieldMapping()
|
|
|
|
termFieldMapping.IncludeInAll = false
|
|
|
|
termFieldMapping.Analyzer = analyzer_keyword.Name
|
|
|
|
docMapping.AddFieldMappingsAt("Language", termFieldMapping)
|
|
|
|
docMapping.AddFieldMappingsAt("CommitID", termFieldMapping)
|
|
|
|
|
|
|
|
timeFieldMapping := bleve.NewDateTimeFieldMapping()
|
|
|
|
timeFieldMapping.IncludeInAll = false
|
|
|
|
docMapping.AddFieldMappingsAt("UpdatedAt", timeFieldMapping)
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
mapping := bleve.NewIndexMapping()
|
|
|
|
if err := addUnicodeNormalizeTokenFilter(mapping); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if err := mapping.AddCustomAnalyzer(repoIndexerAnalyzer, map[string]interface{}{
|
2020-02-21 01:23:55 +05:30
|
|
|
"type": analyzer_custom.Name,
|
2019-12-23 18:01:16 +05:30
|
|
|
"char_filters": []string{},
|
|
|
|
"tokenizer": unicode.Name,
|
|
|
|
"token_filters": []string{unicodeNormalizeName, lowercase.Name},
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
mapping.DefaultAnalyzer = repoIndexerAnalyzer
|
|
|
|
mapping.AddDocumentMapping(repoIndexerDocType, docMapping)
|
|
|
|
mapping.AddDocumentMapping("_all", bleve.NewDocumentDisabledMapping())
|
|
|
|
|
|
|
|
indexer, err := bleve.New(path, mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
|
|
|
|
if err = rupture.WriteIndexMetadata(path, &rupture.IndexMetadata{
|
|
|
|
Version: latestVersion,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
return indexer, nil
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var _ Indexer = &BleveIndexer{}
|
2019-12-23 18:01:16 +05:30
|
|
|
|
|
|
|
// BleveIndexer represents a bleve indexer implementation
|
|
|
|
type BleveIndexer struct {
|
|
|
|
indexDir string
|
|
|
|
indexer bleve.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBleveIndexer creates a new bleve local indexer
|
|
|
|
func NewBleveIndexer(indexDir string) (*BleveIndexer, bool, error) {
|
|
|
|
indexer := &BleveIndexer{
|
|
|
|
indexDir: indexDir,
|
|
|
|
}
|
|
|
|
created, err := indexer.init()
|
2021-11-15 18:46:11 +05:30
|
|
|
if err != nil {
|
|
|
|
indexer.Close()
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
return indexer, created, err
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
func (b *BleveIndexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserError, batchReader *bufio.Reader, commitSha string,
|
2022-02-24 01:46:07 +05:30
|
|
|
update fileUpdate, repo *repo_model.Repository, batch *gitea_bleve.FlushingBatch,
|
|
|
|
) error {
|
2020-08-30 21:38:01 +05:30
|
|
|
// Ignore vendored files in code search
|
2021-04-01 23:11:09 +05:30
|
|
|
if setting.Indexer.ExcludeVendored && analyze.IsVendor(update.Filename) {
|
2020-08-30 21:38:01 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:02:25 +05:30
|
|
|
size := update.Size
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
var err error
|
2021-02-18 03:02:25 +05:30
|
|
|
if !update.Sized {
|
2022-04-01 08:25:30 +05:30
|
|
|
var stdout string
|
2022-10-23 20:14:45 +05:30
|
|
|
stdout, _, err = git.NewCommand(ctx, "cat-file", "-s").AddDynamicArguments(update.BlobSha).RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
|
2021-02-18 03:02:25 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if size, err = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Misformatted git cat-file output: %w", err)
|
2021-02-18 03:02:25 +05:30
|
|
|
}
|
2020-08-30 21:38:01 +05:30
|
|
|
}
|
2021-02-18 03:02:25 +05:30
|
|
|
|
|
|
|
if size > setting.Indexer.MaxIndexerFileSize {
|
2020-08-30 21:38:01 +05:30
|
|
|
return b.addDelete(update.Filename, repo, batch)
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:27:01 +05:30
|
|
|
if _, err := batchWriter.Write([]byte(update.BlobSha + "\n")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-01 08:25:30 +05:30
|
|
|
_, _, size, err = git.ReadBatchLine(batchReader)
|
2021-03-04 08:27:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-22 11:08:34 +05:30
|
|
|
fileContents, err := io.ReadAll(io.LimitReader(batchReader, size))
|
2020-08-30 21:38:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-06-05 18:02:19 +05:30
|
|
|
} else if !typesniffer.DetectContentType(fileContents).IsText() {
|
2020-08-30 21:38:01 +05:30
|
|
|
// FIXME: UTF-16 files will probably fail here
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 03:30:46 +05:30
|
|
|
if _, err = batchReader.Discard(1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-30 21:38:01 +05:30
|
|
|
id := filenameIndexerID(repo.ID, update.Filename)
|
|
|
|
return batch.Index(id, &RepoIndexerData{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
CommitID: commitSha,
|
|
|
|
Content: string(charset.ToUTF8DropErrors(fileContents)),
|
|
|
|
Language: analyze.GetCodeLanguage(update.Filename, fileContents),
|
|
|
|
UpdatedAt: time.Now().UTC(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
func (b *BleveIndexer) addDelete(filename string, repo *repo_model.Repository, batch *gitea_bleve.FlushingBatch) error {
|
2020-08-30 21:38:01 +05:30
|
|
|
id := filenameIndexerID(repo.ID, filename)
|
|
|
|
return batch.Delete(id)
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// init init the indexer
|
|
|
|
func (b *BleveIndexer) init() (bool, error) {
|
|
|
|
var err error
|
2020-08-30 21:38:01 +05:30
|
|
|
b.indexer, err = openBleveIndexer(b.indexDir, repoIndexerLatestVersion)
|
2019-12-09 00:45:35 +05:30
|
|
|
if err != nil {
|
2019-12-23 18:01:16 +05:30
|
|
|
return false, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
if b.indexer != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-08-30 21:38:01 +05:30
|
|
|
b.indexer, err = createBleveIndexer(b.indexDir, repoIndexerLatestVersion)
|
2019-12-23 18:01:16 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close close the indexer
|
|
|
|
func (b *BleveIndexer) Close() {
|
|
|
|
log.Debug("Closing repo indexer")
|
|
|
|
if b.indexer != nil {
|
|
|
|
err := b.indexer.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error whilst closing the repository indexer: %v", err)
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
log.Info("PID: %d Repository Indexer closed", os.Getpid())
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
// SetAvailabilityChangeCallback does nothing
|
|
|
|
func (b *BleveIndexer) SetAvailabilityChangeCallback(callback func(bool)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping does nothing
|
|
|
|
func (b *BleveIndexer) Ping() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// Index indexes the data
|
2022-01-20 04:56:57 +05:30
|
|
|
func (b *BleveIndexer) Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *repoChanges) error {
|
2021-07-19 01:29:10 +05:30
|
|
|
batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize)
|
2021-03-04 08:27:01 +05:30
|
|
|
if len(changes.Updates) > 0 {
|
|
|
|
|
2021-12-17 00:31:14 +05:30
|
|
|
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := git.EnsureValidGitRepository(ctx, repo.RepoPath()); err != nil {
|
2021-12-17 00:31:14 +05:30
|
|
|
log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
batchWriter, batchReader, cancel := git.CatFileBatch(ctx, repo.RepoPath())
|
2021-03-04 08:27:01 +05:30
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for _, update := range changes.Updates {
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := b.addUpdate(ctx, batchWriter, batchReader, sha, update, repo, batch); err != nil {
|
2021-03-04 08:27:01 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2021-03-04 08:27:01 +05:30
|
|
|
cancel()
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
for _, filename := range changes.RemovedFilenames {
|
2020-08-30 21:38:01 +05:30
|
|
|
if err := b.addDelete(filename, repo, batch); err != nil {
|
2019-12-23 18:01:16 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 21:38:01 +05:30
|
|
|
return batch.Flush()
|
2019-12-23 18:01:16 +05:30
|
|
|
}
|
2019-12-09 00:45:35 +05:30
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// Delete deletes indexes by ids
|
|
|
|
func (b *BleveIndexer) Delete(repoID int64) error {
|
|
|
|
query := numericEqualityQuery(repoID, "RepoID")
|
|
|
|
searchRequest := bleve.NewSearchRequestOptions(query, 2147483647, 0, false)
|
|
|
|
result, err := b.indexer.Search(searchRequest)
|
2019-12-09 00:45:35 +05:30
|
|
|
if err != nil {
|
2019-12-23 18:01:16 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-07-19 01:29:10 +05:30
|
|
|
batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize)
|
2019-12-23 18:01:16 +05:30
|
|
|
for _, hit := range result.Hits {
|
|
|
|
if err = batch.Delete(hit.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
return batch.Flush()
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
// Search searches for files in the specified repo.
|
|
|
|
// Returns the matching file-paths
|
2022-01-27 14:00:51 +05:30
|
|
|
func (b *BleveIndexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error) {
|
2021-01-27 15:30:35 +05:30
|
|
|
var (
|
|
|
|
indexerQuery query.Query
|
|
|
|
keywordQuery query.Query
|
|
|
|
)
|
|
|
|
|
|
|
|
if isMatch {
|
|
|
|
prefixQuery := bleve.NewPrefixQuery(keyword)
|
|
|
|
prefixQuery.FieldVal = "Content"
|
|
|
|
keywordQuery = prefixQuery
|
|
|
|
} else {
|
|
|
|
phraseQuery := bleve.NewMatchPhraseQuery(keyword)
|
|
|
|
phraseQuery.FieldVal = "Content"
|
|
|
|
phraseQuery.Analyzer = repoIndexerAnalyzer
|
|
|
|
keywordQuery = phraseQuery
|
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
|
|
|
|
if len(repoIDs) > 0 {
|
2022-01-20 23:16:10 +05:30
|
|
|
repoQueries := make([]query.Query, 0, len(repoIDs))
|
2019-12-23 18:01:16 +05:30
|
|
|
for _, repoID := range repoIDs {
|
|
|
|
repoQueries = append(repoQueries, numericEqualityQuery(repoID, "RepoID"))
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-15 15:21:28 +05:30
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
indexerQuery = bleve.NewConjunctionQuery(
|
|
|
|
bleve.NewDisjunctionQuery(repoQueries...),
|
2021-01-27 15:30:35 +05:30
|
|
|
keywordQuery,
|
2019-12-23 18:01:16 +05:30
|
|
|
)
|
|
|
|
} else {
|
2021-01-27 15:30:35 +05:30
|
|
|
indexerQuery = keywordQuery
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
|
|
|
|
2020-02-21 01:23:55 +05:30
|
|
|
// Save for reuse without language filter
|
|
|
|
facetQuery := indexerQuery
|
|
|
|
if len(language) > 0 {
|
|
|
|
languageQuery := bleve.NewMatchQuery(language)
|
|
|
|
languageQuery.FieldVal = "Language"
|
|
|
|
languageQuery.Analyzer = analyzer_keyword.Name
|
|
|
|
|
|
|
|
indexerQuery = bleve.NewConjunctionQuery(
|
|
|
|
indexerQuery,
|
|
|
|
languageQuery,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
from := (page - 1) * pageSize
|
|
|
|
searchRequest := bleve.NewSearchRequestOptions(indexerQuery, pageSize, from, false)
|
2020-02-21 01:23:55 +05:30
|
|
|
searchRequest.Fields = []string{"Content", "RepoID", "Language", "CommitID", "UpdatedAt"}
|
2019-12-23 18:01:16 +05:30
|
|
|
searchRequest.IncludeLocations = true
|
2019-12-09 00:45:35 +05:30
|
|
|
|
2020-02-21 01:23:55 +05:30
|
|
|
if len(language) == 0 {
|
|
|
|
searchRequest.AddFacet("languages", bleve.NewFacetRequest("Language", 10))
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:00:51 +05:30
|
|
|
result, err := b.indexer.SearchInContext(ctx, searchRequest)
|
2019-12-23 18:01:16 +05:30
|
|
|
if err != nil {
|
2020-02-21 01:23:55 +05:30
|
|
|
return 0, nil, nil, err
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
|
2020-02-21 01:23:55 +05:30
|
|
|
total := int64(result.Total)
|
|
|
|
|
2019-12-23 18:01:16 +05:30
|
|
|
searchResults := make([]*SearchResult, len(result.Hits))
|
|
|
|
for i, hit := range result.Hits {
|
2022-06-20 15:32:49 +05:30
|
|
|
startIndex, endIndex := -1, -1
|
2019-12-23 18:01:16 +05:30
|
|
|
for _, locations := range hit.Locations["Content"] {
|
|
|
|
location := locations[0]
|
|
|
|
locationStart := int(location.Start)
|
|
|
|
locationEnd := int(location.End)
|
|
|
|
if startIndex < 0 || locationStart < startIndex {
|
|
|
|
startIndex = locationStart
|
|
|
|
}
|
|
|
|
if endIndex < 0 || locationEnd > endIndex {
|
|
|
|
endIndex = locationEnd
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 01:23:55 +05:30
|
|
|
language := hit.Fields["Language"].(string)
|
|
|
|
var updatedUnix timeutil.TimeStamp
|
|
|
|
if t, err := time.Parse(time.RFC3339, hit.Fields["UpdatedAt"].(string)); err == nil {
|
|
|
|
updatedUnix = timeutil.TimeStamp(t.Unix())
|
|
|
|
}
|
2019-12-23 18:01:16 +05:30
|
|
|
searchResults[i] = &SearchResult{
|
2020-02-21 01:23:55 +05:30
|
|
|
RepoID: int64(hit.Fields["RepoID"].(float64)),
|
|
|
|
StartIndex: startIndex,
|
|
|
|
EndIndex: endIndex,
|
|
|
|
Filename: filenameOfIndexerID(hit.ID),
|
|
|
|
Content: hit.Fields["Content"].(string),
|
|
|
|
CommitID: hit.Fields["CommitID"].(string),
|
|
|
|
UpdatedUnix: updatedUnix,
|
|
|
|
Language: language,
|
|
|
|
Color: enry.GetColor(language),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
searchResultLanguages := make([]*SearchResultLanguages, 0, 10)
|
|
|
|
if len(language) > 0 {
|
|
|
|
// Use separate query to go get all language counts
|
|
|
|
facetRequest := bleve.NewSearchRequestOptions(facetQuery, 1, 0, false)
|
|
|
|
facetRequest.Fields = []string{"Content", "RepoID", "Language", "CommitID", "UpdatedAt"}
|
|
|
|
facetRequest.IncludeLocations = true
|
|
|
|
facetRequest.AddFacet("languages", bleve.NewFacetRequest("Language", 10))
|
|
|
|
|
|
|
|
if result, err = b.indexer.Search(facetRequest); err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
languagesFacet := result.Facets["languages"]
|
2022-01-01 13:56:27 +05:30
|
|
|
for _, term := range languagesFacet.Terms.Terms() {
|
2020-02-21 01:23:55 +05:30
|
|
|
if len(term.Term) == 0 {
|
|
|
|
continue
|
2019-12-23 18:01:16 +05:30
|
|
|
}
|
2020-02-21 01:23:55 +05:30
|
|
|
searchResultLanguages = append(searchResultLanguages, &SearchResultLanguages{
|
|
|
|
Language: term.Term,
|
|
|
|
Color: enry.GetColor(term.Term),
|
|
|
|
Count: term.Count,
|
|
|
|
})
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|
2020-02-21 01:23:55 +05:30
|
|
|
return total, searchResults, searchResultLanguages, nil
|
2019-12-09 00:45:35 +05:30
|
|
|
}
|