From c10503afeccd5172ace7613094dd5fe1e0770c55 Mon Sep 17 00:00:00 2001 From: Jui-Nan Lin Date: Wed, 27 Jan 2021 18:00:35 +0800 Subject: [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 Co-authored-by: techknowlogick --- modules/indexer/code/elastic_search.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'modules/indexer/code/elastic_search.go') 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 { -- cgit v1.2.3