summaryrefslogtreecommitdiffstats
path: root/modules/indexer/code/elastic_search.go
diff options
context:
space:
mode:
authorJui-Nan Lin <jnlinn@gmail.com>2021-01-27 18:00:35 +0800
committerGitHub <noreply@github.com>2021-01-27 12:00:35 +0200
commitc10503afeccd5172ace7613094dd5fe1e0770c55 (patch)
tree5a39cbf125f38a3bc75eb77c689b03ed13efa44c /modules/indexer/code/elastic_search.go
parentb2c20b68a08e23bc952402a553d59a4613188bd0 (diff)
downloadgitea-c10503afeccd5172ace7613094dd5fe1e0770c55.tar.gz
gitea-c10503afeccd5172ace7613094dd5fe1e0770c55.zip
[Feature] add precise search type for Elastic Search (#12869)
* feat: add type query parameters for specifying precise search * feat: add select dropdown in search box Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules/indexer/code/elastic_search.go')
-rw-r--r--modules/indexer/code/elastic_search.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go
index 0f61c4e592..f81dbb34d4 100644
--- a/modules/indexer/code/elastic_search.go
+++ b/modules/indexer/code/elastic_search.go
@@ -27,6 +27,10 @@ import (
const (
esRepoIndexerLatestVersion = 1
+ // 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"
)
var (
@@ -330,8 +334,13 @@ func extractAggs(searchResult *elastic.SearchResult) []*SearchResultLanguages {
}
// Search searches for codes and language stats by given conditions.
-func (b *ElasticSearchIndexer) Search(repoIDs []int64, language, keyword string, page, pageSize int) (int64, []*SearchResult, []*SearchResultLanguages, error) {
- kwQuery := elastic.NewMultiMatchQuery(keyword, "content")
+func (b *ElasticSearchIndexer) Search(repoIDs []int64, language, keyword string, page, pageSize int, isMatch bool) (int64, []*SearchResult, []*SearchResultLanguages, error) {
+ searchType := esMultiMatchTypeBestFields
+ if isMatch {
+ searchType = esMultiMatchTypePhrasePrefix
+ }
+
+ kwQuery := elastic.NewMultiMatchQuery(keyword, "content").Type(searchType)
query := elastic.NewBoolQuery()
query = query.Must(kwQuery)
if len(repoIDs) > 0 {