2020-08-31 00:08:01 +08:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-28 02:20:29 +08:00
// SPDX-License-Identifier: MIT
2020-08-31 00:08:01 +08:00
package code
import (
2021-03-04 10:57:01 +08:00
"bufio"
2020-08-31 00:08:01 +08:00
"context"
2022-01-27 16:30:51 +08:00
"errors"
2020-08-31 00:08:01 +08:00
"fmt"
2021-03-04 10:57:01 +08:00
"io"
2022-01-27 16:30:51 +08:00
"net"
2020-08-31 00:08:01 +08:00
"strconv"
"strings"
2022-01-27 16:30:51 +08:00
"sync"
2020-08-31 00:08:01 +08:00
"time"
2021-12-10 09:27:50 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2020-08-31 00:08:01 +08:00
"code.gitea.io/gitea/modules/analyze"
"code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/git"
2022-01-27 16:30:51 +08:00
"code.gitea.io/gitea/modules/graceful"
2021-07-25 00:03:58 +08:00
"code.gitea.io/gitea/modules/json"
2020-08-31 00:08:01 +08:00
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
2021-06-05 20:32:19 +08:00
"code.gitea.io/gitea/modules/typesniffer"
2020-08-31 00:08:01 +08:00
"github.com/go-enry/go-enry/v2"
"github.com/olivere/elastic/v7"
)
const (
esRepoIndexerLatestVersion = 1
2021-01-27 18:00:35 +08:00
// multi-match-types, currently only 2 types are used
// Reference: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-multi-match-query.html#multi-match-types
esMultiMatchTypeBestFields = "best_fields"
esMultiMatchTypePhrasePrefix = "phrase_prefix"
2020-08-31 00:08:01 +08:00
)
2022-01-21 01:46:10 +08:00
var _ Indexer = & ElasticSearchIndexer { }
2020-08-31 00:08:01 +08:00
// ElasticSearchIndexer implements Indexer interface
type ElasticSearchIndexer struct {
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 19:49:59 +08:00
client * elastic . Client
indexerAliasName string
available bool
stopTimer chan struct { }
lock sync . RWMutex
2020-08-31 00:08:01 +08:00
}
// NewElasticSearchIndexer creates a new elasticsearch indexer
func NewElasticSearchIndexer ( url , indexerName string ) ( * ElasticSearchIndexer , bool , error ) {
opts := [ ] elastic . ClientOptionFunc {
elastic . SetURL ( url ) ,
elastic . SetSniff ( false ) ,
elastic . SetHealthcheckInterval ( 10 * time . Second ) ,
elastic . SetGzip ( false ) ,
}
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 06:35:11 +08:00
logger := log . GetLogger ( log . DEFAULT )
2020-08-31 00:08:01 +08:00
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 06:35:11 +08:00
opts = append ( opts , elastic . SetTraceLog ( & log . PrintfLogger { Logf : logger . Trace } ) )
opts = append ( opts , elastic . SetInfoLog ( & log . PrintfLogger { Logf : logger . Info } ) )
opts = append ( opts , elastic . SetErrorLog ( & log . PrintfLogger { Logf : logger . Error } ) )
2020-08-31 00:08:01 +08:00
client , err := elastic . NewClient ( opts ... )
if err != nil {
return nil , false , err
}
indexer := & ElasticSearchIndexer {
client : client ,
indexerAliasName : indexerName ,
2022-01-27 16:30:51 +08:00
available : true ,
stopTimer : make ( chan struct { } ) ,
2020-08-31 00:08:01 +08:00
}
2022-01-27 16:30:51 +08:00
ticker := time . NewTicker ( 10 * time . Second )
go func ( ) {
for {
select {
case <- ticker . C :
indexer . checkAvailability ( )
case <- indexer . stopTimer :
ticker . Stop ( )
return
}
}
} ( )
2020-08-31 00:08:01 +08:00
exists , err := indexer . init ( )
2021-11-15 21:16:11 +08:00
if err != nil {
indexer . Close ( )
return nil , false , err
}
2020-08-31 00:08:01 +08:00
return indexer , ! exists , err
}
const (
defaultMapping = ` {
"mappings" : {
"properties" : {
"repo_id" : {
"type" : "long" ,
"index" : true
} ,
"content" : {
"type" : "text" ,
2020-09-12 20:31:52 +08:00
"term_vector" : "with_positions_offsets" ,
2020-08-31 00:08:01 +08:00
"index" : true
} ,
"commit_id" : {
"type" : "keyword" ,
"index" : true
} ,
"language" : {
"type" : "keyword" ,
"index" : true
} ,
"updated_at" : {
"type" : "long" ,
"index" : true
}
}
}
} `
)
func ( b * ElasticSearchIndexer ) realIndexerName ( ) string {
return fmt . Sprintf ( "%s.v%d" , b . indexerAliasName , esRepoIndexerLatestVersion )
}
// Init will initialize the indexer
func ( b * ElasticSearchIndexer ) init ( ) ( bool , error ) {
2022-01-27 16:30:51 +08:00
ctx := graceful . GetManager ( ) . HammerContext ( )
2020-08-31 00:08:01 +08:00
exists , err := b . client . IndexExists ( b . realIndexerName ( ) ) . Do ( ctx )
if err != nil {
2022-01-27 16:30:51 +08:00
return false , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
if ! exists {
2022-01-21 01:46:10 +08:00
mapping := defaultMapping
2020-08-31 00:08:01 +08:00
createIndex , err := b . client . CreateIndex ( b . realIndexerName ( ) ) . BodyString ( mapping ) . Do ( ctx )
if err != nil {
2022-01-27 16:30:51 +08:00
return false , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
if ! createIndex . Acknowledged {
return false , fmt . Errorf ( "create index %s with %s failed" , b . realIndexerName ( ) , mapping )
}
}
// check version
r , err := b . client . Aliases ( ) . Do ( ctx )
if err != nil {
2022-01-27 16:30:51 +08:00
return false , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
realIndexerNames := r . IndicesByAlias ( b . indexerAliasName )
if len ( realIndexerNames ) < 1 {
res , err := b . client . Alias ( ) .
Add ( b . realIndexerName ( ) , b . indexerAliasName ) .
Do ( ctx )
if err != nil {
2022-01-27 16:30:51 +08:00
return false , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
if ! res . Acknowledged {
2022-01-27 16:30:51 +08:00
return false , fmt . Errorf ( "create alias %s to index %s failed" , b . indexerAliasName , b . realIndexerName ( ) )
2020-08-31 00:08:01 +08:00
}
} else if len ( realIndexerNames ) >= 1 && realIndexerNames [ 0 ] < b . realIndexerName ( ) {
log . Warn ( "Found older gitea indexer named %s, but we will create a new one %s and keep the old NOT DELETED. You can delete the old version after the upgrade succeed." ,
realIndexerNames [ 0 ] , b . realIndexerName ( ) )
res , err := b . client . Alias ( ) .
Remove ( realIndexerNames [ 0 ] , b . indexerAliasName ) .
Add ( b . realIndexerName ( ) , b . indexerAliasName ) .
Do ( ctx )
if err != nil {
2022-01-27 16:30:51 +08:00
return false , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
if ! res . Acknowledged {
2022-01-27 16:30:51 +08:00
return false , fmt . Errorf ( "change alias %s to index %s failed" , b . indexerAliasName , b . realIndexerName ( ) )
2020-08-31 00:08:01 +08:00
}
}
return exists , nil
}
2022-01-27 16:30:51 +08:00
// Ping checks if elastic is available
func ( b * ElasticSearchIndexer ) Ping ( ) bool {
b . lock . RLock ( )
defer b . lock . RUnlock ( )
return b . available
}
2022-01-20 07:26:57 +08:00
func ( b * ElasticSearchIndexer ) addUpdate ( ctx context . Context , batchWriter git . WriteCloserError , batchReader * bufio . Reader , sha string , update fileUpdate , repo * repo_model . Repository ) ( [ ] elastic . BulkableRequest , error ) {
2020-09-07 23:05:08 +08:00
// Ignore vendored files in code search
2021-04-02 01:41:09 +08:00
if setting . Indexer . ExcludeVendored && analyze . IsVendor ( update . Filename ) {
2020-09-07 23:05:08 +08:00
return nil , nil
}
2021-02-18 05:32:25 +08:00
size := update . Size
2022-04-01 10:55:30 +08:00
var err error
2021-02-18 05:32:25 +08:00
if ! update . Sized {
2022-04-01 10:55:30 +08:00
var stdout string
2022-10-23 22:44:45 +08:00
stdout , _ , err = git . NewCommand ( ctx , "cat-file" , "-s" ) . AddDynamicArguments ( update . BlobSha ) . RunStdString ( & git . RunOpts { Dir : repo . RepoPath ( ) } )
2021-02-18 05:32:25 +08:00
if err != nil {
return nil , err
}
if size , err = strconv . ParseInt ( strings . TrimSpace ( stdout ) , 10 , 64 ) ; err != nil {
2022-10-25 03:29:17 +08:00
return nil , fmt . Errorf ( "misformatted git cat-file output: %w" , err )
2021-02-18 05:32:25 +08:00
}
2020-08-31 00:08:01 +08:00
}
2021-02-18 05:32:25 +08:00
if size > setting . Indexer . MaxIndexerFileSize {
2020-08-31 00:08:01 +08:00
return [ ] elastic . BulkableRequest { b . addDelete ( update . Filename , repo ) } , nil
}
2021-03-04 10:57:01 +08:00
if _ , err := batchWriter . Write ( [ ] byte ( update . BlobSha + "\n" ) ) ; err != nil {
return nil , err
}
2022-04-01 10:55:30 +08:00
_ , _ , size , err = git . ReadBatchLine ( batchReader )
2021-03-04 10:57:01 +08:00
if err != nil {
return nil , err
}
2021-09-22 13:38:34 +08:00
fileContents , err := io . ReadAll ( io . LimitReader ( batchReader , size ) )
2020-08-31 00:08:01 +08:00
if err != nil {
return nil , err
2021-06-05 20:32:19 +08:00
} else if ! typesniffer . DetectContentType ( fileContents ) . IsText ( ) {
2020-08-31 00:08:01 +08:00
// FIXME: UTF-16 files will probably fail here
return nil , nil
}
2021-06-21 06:00:46 +08:00
if _ , err = batchReader . Discard ( 1 ) ; err != nil {
return nil , err
}
2020-08-31 00:08:01 +08:00
id := filenameIndexerID ( repo . ID , update . Filename )
return [ ] elastic . BulkableRequest {
elastic . NewBulkIndexRequest ( ) .
Index ( b . indexerAliasName ) .
Id ( id ) .
2023-07-05 11:41:32 +08:00
Doc ( map [ string ] any {
2020-08-31 00:08:01 +08:00
"repo_id" : repo . ID ,
"content" : string ( charset . ToUTF8DropErrors ( fileContents ) ) ,
"commit_id" : sha ,
"language" : analyze . GetCodeLanguage ( update . Filename , fileContents ) ,
"updated_at" : timeutil . TimeStampNow ( ) ,
} ) ,
} , nil
}
2021-12-10 09:27:50 +08:00
func ( b * ElasticSearchIndexer ) addDelete ( filename string , repo * repo_model . Repository ) elastic . BulkableRequest {
2020-08-31 00:08:01 +08:00
id := filenameIndexerID ( repo . ID , filename )
return elastic . NewBulkDeleteRequest ( ) .
Index ( b . indexerAliasName ) .
Id ( id )
}
// Index will save the index data
2022-01-20 07:26:57 +08:00
func ( b * ElasticSearchIndexer ) Index ( ctx context . Context , repo * repo_model . Repository , sha string , changes * repoChanges ) error {
2020-08-31 00:08:01 +08:00
reqs := make ( [ ] elastic . BulkableRequest , 0 )
2021-03-04 10:57:01 +08:00
if len ( changes . Updates ) > 0 {
2021-12-17 03:01:14 +08:00
// 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-07-01 07:48:25 +08:00
if err := git . EnsureValidGitRepository ( ctx , repo . RepoPath ( ) ) ; err != nil {
2021-12-17 03:01:14 +08:00
log . Error ( "Unable to open git repo: %s for %-v: %v" , repo . RepoPath ( ) , repo , err )
return err
}
2021-03-04 10:57:01 +08:00
2022-01-20 07:26:57 +08:00
batchWriter , batchReader , cancel := git . CatFileBatch ( ctx , repo . RepoPath ( ) )
2021-03-04 10:57:01 +08:00
defer cancel ( )
for _ , update := range changes . Updates {
2022-01-20 07:26:57 +08:00
updateReqs , err := b . addUpdate ( ctx , batchWriter , batchReader , sha , update , repo )
2021-03-04 10:57:01 +08:00
if err != nil {
return err
}
if len ( updateReqs ) > 0 {
reqs = append ( reqs , updateReqs ... )
}
2020-08-31 00:08:01 +08:00
}
2021-03-04 10:57:01 +08:00
cancel ( )
2020-08-31 00:08:01 +08:00
}
for _ , filename := range changes . RemovedFilenames {
reqs = append ( reqs , b . addDelete ( filename , repo ) )
}
if len ( reqs ) > 0 {
_ , err := b . client . Bulk ( ) .
Index ( b . indexerAliasName ) .
Add ( reqs ... ) .
2022-01-27 16:30:51 +08:00
Do ( ctx )
return b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
return nil
}
// Delete deletes indexes by ids
func ( b * ElasticSearchIndexer ) Delete ( repoID int64 ) error {
_ , err := b . client . DeleteByQuery ( b . indexerAliasName ) .
Query ( elastic . NewTermsQuery ( "repo_id" , repoID ) ) .
2022-01-27 16:30:51 +08:00
Do ( graceful . GetManager ( ) . HammerContext ( ) )
return b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
2020-09-12 20:31:52 +08:00
// indexPos find words positions for start and the following end on content. It will
2021-07-08 19:38:13 +08:00
// return the beginning position of the first start and the ending position of the
2020-09-12 20:31:52 +08:00
// first end following the start string.
// If not found any of the positions, it will return -1, -1.
func indexPos ( content , start , end string ) ( int , int ) {
startIdx := strings . Index ( content , start )
if startIdx < 0 {
return - 1 , - 1
}
endIdx := strings . Index ( content [ startIdx + len ( start ) : ] , end )
if endIdx < 0 {
return - 1 , - 1
}
return startIdx , startIdx + len ( start ) + endIdx + len ( end )
}
2020-08-31 00:08:01 +08:00
func convertResult ( searchResult * elastic . SearchResult , kw string , pageSize int ) ( int64 , [ ] * SearchResult , [ ] * SearchResultLanguages , error ) {
hits := make ( [ ] * SearchResult , 0 , pageSize )
for _ , hit := range searchResult . Hits . Hits {
// FIXME: There is no way to get the position the keyword on the content currently on the same request.
// So we get it from content, this may made the query slower. See
// https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291
2022-06-20 18:02:49 +08:00
var startIndex , endIndex int
2020-08-31 00:08:01 +08:00
c , ok := hit . Highlight [ "content" ]
if ok && len ( c ) > 0 {
2021-07-08 19:38:13 +08:00
// FIXME: Since the highlighting content will include <em> and </em> for the keywords,
// now we should find the positions. But how to avoid html content which contains the
2020-09-12 20:31:52 +08:00
// <em> and </em> tags? If elastic search has handled that?
startIndex , endIndex = indexPos ( c [ 0 ] , "<em>" , "</em>" )
if startIndex == - 1 {
panic ( fmt . Sprintf ( "1===%s,,,%#v,,,%s" , kw , hit . Highlight , c [ 0 ] ) )
2020-08-31 00:08:01 +08:00
}
} else {
panic ( fmt . Sprintf ( "2===%#v" , hit . Highlight ) )
}
repoID , fileName := parseIndexerID ( hit . Id )
2023-07-05 11:41:32 +08:00
res := make ( map [ string ] any )
2020-08-31 00:08:01 +08:00
if err := json . Unmarshal ( hit . Source , & res ) ; err != nil {
return 0 , nil , nil , err
}
language := res [ "language" ] . ( string )
hits = append ( hits , & SearchResult {
RepoID : repoID ,
Filename : fileName ,
CommitID : res [ "commit_id" ] . ( string ) ,
Content : res [ "content" ] . ( string ) ,
UpdatedUnix : timeutil . TimeStamp ( res [ "updated_at" ] . ( float64 ) ) ,
Language : language ,
StartIndex : startIndex ,
2020-09-12 20:31:52 +08:00
EndIndex : endIndex - 9 , // remove the length <em></em> since we give Content the original data
2020-08-31 00:08:01 +08:00
Color : enry . GetColor ( language ) ,
} )
}
return searchResult . TotalHits ( ) , hits , extractAggs ( searchResult ) , nil
}
func extractAggs ( searchResult * elastic . SearchResult ) [ ] * SearchResultLanguages {
var searchResultLanguages [ ] * SearchResultLanguages
agg , found := searchResult . Aggregations . Terms ( "language" )
if found {
searchResultLanguages = make ( [ ] * SearchResultLanguages , 0 , 10 )
for _ , bucket := range agg . Buckets {
searchResultLanguages = append ( searchResultLanguages , & SearchResultLanguages {
Language : bucket . Key . ( string ) ,
Color : enry . GetColor ( bucket . Key . ( string ) ) ,
Count : int ( bucket . DocCount ) ,
} )
}
}
return searchResultLanguages
}
// Search searches for codes and language stats by given conditions.
2022-01-27 16:30:51 +08:00
func ( b * ElasticSearchIndexer ) Search ( ctx context . Context , repoIDs [ ] int64 , language , keyword string , page , pageSize int , isMatch bool ) ( int64 , [ ] * SearchResult , [ ] * SearchResultLanguages , error ) {
2021-01-27 18:00:35 +08:00
searchType := esMultiMatchTypeBestFields
if isMatch {
searchType = esMultiMatchTypePhrasePrefix
}
kwQuery := elastic . NewMultiMatchQuery ( keyword , "content" ) . Type ( searchType )
2020-08-31 00:08:01 +08:00
query := elastic . NewBoolQuery ( )
query = query . Must ( kwQuery )
if len ( repoIDs ) > 0 {
2023-07-05 11:41:32 +08:00
repoStrs := make ( [ ] any , 0 , len ( repoIDs ) )
2020-08-31 00:08:01 +08:00
for _ , repoID := range repoIDs {
repoStrs = append ( repoStrs , repoID )
}
repoQuery := elastic . NewTermsQuery ( "repo_id" , repoStrs ... )
query = query . Must ( repoQuery )
}
var (
start int
kw = "<em>" + keyword + "</em>"
aggregation = elastic . NewTermsAggregation ( ) . Field ( "language" ) . Size ( 10 ) . OrderByCountDesc ( )
)
if page > 0 {
start = ( page - 1 ) * pageSize
}
if len ( language ) == 0 {
searchResult , err := b . client . Search ( ) .
Index ( b . indexerAliasName ) .
Aggregation ( "language" , aggregation ) .
Query ( query ) .
2020-09-12 20:31:52 +08:00
Highlight (
elastic . NewHighlight ( ) .
Field ( "content" ) .
NumOfFragments ( 0 ) . // return all highting content on fragments
HighlighterType ( "fvh" ) ,
) .
2020-08-31 00:08:01 +08:00
Sort ( "repo_id" , true ) .
From ( start ) . Size ( pageSize ) .
2022-01-27 16:30:51 +08:00
Do ( ctx )
2020-08-31 00:08:01 +08:00
if err != nil {
2022-01-27 16:30:51 +08:00
return 0 , nil , nil , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
return convertResult ( searchResult , kw , pageSize )
}
langQuery := elastic . NewMatchQuery ( "language" , language )
countResult , err := b . client . Search ( ) .
Index ( b . indexerAliasName ) .
Aggregation ( "language" , aggregation ) .
Query ( query ) .
Size ( 0 ) . // We only needs stats information
2022-01-27 16:30:51 +08:00
Do ( ctx )
2020-08-31 00:08:01 +08:00
if err != nil {
2022-01-27 16:30:51 +08:00
return 0 , nil , nil , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
query = query . Must ( langQuery )
searchResult , err := b . client . Search ( ) .
Index ( b . indexerAliasName ) .
Query ( query ) .
2020-09-12 20:31:52 +08:00
Highlight (
elastic . NewHighlight ( ) .
Field ( "content" ) .
NumOfFragments ( 0 ) . // return all highting content on fragments
HighlighterType ( "fvh" ) ,
) .
2020-08-31 00:08:01 +08:00
Sort ( "repo_id" , true ) .
From ( start ) . Size ( pageSize ) .
2022-01-27 16:30:51 +08:00
Do ( ctx )
2020-08-31 00:08:01 +08:00
if err != nil {
2022-01-27 16:30:51 +08:00
return 0 , nil , nil , b . checkError ( err )
2020-08-31 00:08:01 +08:00
}
total , hits , _ , err := convertResult ( searchResult , kw , pageSize )
return total , hits , extractAggs ( countResult ) , err
}
// Close implements indexer
2022-01-27 16:30:51 +08:00
func ( b * ElasticSearchIndexer ) Close ( ) {
select {
case <- b . stopTimer :
default :
close ( b . stopTimer )
}
}
func ( b * ElasticSearchIndexer ) checkError ( err error ) error {
var opErr * net . OpError
if ! ( elastic . IsConnErr ( err ) || ( errors . As ( err , & opErr ) && ( opErr . Op == "dial" || opErr . Op == "read" ) ) ) {
return err
}
b . setAvailability ( false )
return err
}
func ( b * ElasticSearchIndexer ) checkAvailability ( ) {
if b . Ping ( ) {
return
}
// Request cluster state to check if elastic is available again
_ , err := b . client . ClusterState ( ) . Do ( graceful . GetManager ( ) . ShutdownContext ( ) )
if err != nil {
b . setAvailability ( false )
return
}
b . setAvailability ( true )
}
func ( b * ElasticSearchIndexer ) setAvailability ( available bool ) {
b . lock . Lock ( )
defer b . lock . Unlock ( )
if b . available == available {
return
}
b . available = available
}