diff --git a/common/torrent.go b/common/torrent.go index 757c95a0..930e3147 100644 --- a/common/torrent.go +++ b/common/torrent.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net/http" "strconv" + "strings" "github.com/gorilla/mux" elastic "gopkg.in/olivere/elastic.v5" @@ -38,10 +39,7 @@ type TorrentParam struct { func (p *TorrentParam) FromRequest(r *http.Request) { var err error - nameLike := r.URL.Query().Get("q") - if nameLike == "" { - nameLike = "*" - } + nameLike := strings.TrimSpace(r.URL.Query().Get("q")) page := mux.Vars(r)["page"] pagenum, err := strconv.ParseUint(page, 10, 32) @@ -143,10 +141,15 @@ func (p *TorrentParam) Find(client *elastic.Client) (int64, []model.Torrent, err // TODO Why is it needed, what does it do ? ctx := context.Background() - query := elastic.NewSimpleQueryStringQuery(p.NameLike). - Field("name"). - Analyzer(config.Conf.Search.ElasticsearchAnalyzer). - DefaultOperator("AND") + var query elastic.Query + if p.NameLike == "" { + query = elastic.NewMatchAllQuery() + } else { + query = elastic.NewSimpleQueryStringQuery(p.NameLike). + Field("name"). + Analyzer(config.Conf.Search.ElasticsearchAnalyzer). + DefaultOperator("AND") + } fsc := elastic.NewFetchSourceContext(true). Include("id") diff --git a/config/default_config.yml b/config/default_config.yml index 5d77bd26..5bf06eb3 100644 --- a/config/default_config.yml +++ b/config/default_config.yml @@ -34,8 +34,8 @@ cache: # Size by default for the cache size: 1024 search: -# default analyzer for ES - es_analyze: nyaapantsu_analyzer +# default search analyzer for ES + es_analyze: nyaapantsu_search_analyzer # default search index for ES es_index: nyaapantsu # Name of the type in the es mapping @@ -163,4 +163,4 @@ models: # ReportsTableName = "sukebei_torrent_reports" # CommentsTableName = "sukebei_comments" # UploadsOldTableName = "sukebei_user_uploads_old" -# FilesTableName = "sukebei_files" \ No newline at end of file +# FilesTableName = "sukebei_files" diff --git a/deploy/ansible/group_vars/all b/deploy/ansible/group_vars/all index a72ae917..94d0208f 100644 --- a/deploy/ansible/group_vars/all +++ b/deploy/ansible/group_vars/all @@ -4,7 +4,6 @@ nyaapantsu_password: nyaapantsu nyaapantsu_pgpool_port: 9998 nyaapantsu_directory: /nyaapantsu/ nyaapantsu_gpg_passphrase_file: "{{ nyaapantsu_directory }}/passphrase" -nyaapantsu_elasticsearch_index: nyaapantsu nyaapantsu_build_directory: go_nyaa/ nyaapantsu_elasticsearch_alias: nyaapantsu # nyaapantsu_elasticsearch_alias: sukebei diff --git a/deploy/ansible/roles/elasticsearch/files/elasticsearch_settings.yml b/deploy/ansible/roles/elasticsearch/files/elasticsearch_settings.yml index 8e5be802..c59d47e6 100644 --- a/deploy/ansible/roles/elasticsearch/files/elasticsearch_settings.yml +++ b/deploy/ansible/roles/elasticsearch/files/elasticsearch_settings.yml @@ -2,19 +2,37 @@ settings: analysis: analyzer: - nyaapantsu_analyzer: - tokenizer: nyaapantsu_tokenizer + # Don't use ngram for search otherwise 'horribleexample' would match + # 'horriblesubs' + nyaapantsu_search_analyzer: + tokenizer: standard filter: - standard - lowercase - tokenizer: - nyaapantsu_tokenizer: + char_filter: + - dash_to_underscore + + nyaapantsu_index_analyzer: + tokenizer: standard + filter: + - standard + - lowercase + - e_ngram_filter + char_filter: + - dash_to_underscore + + filter: + e_ngram_filter: type: edge_ngram min_gram: 2 max_gram: 15 - token_chars: - - letter - - digit + + char_filter: + dash_to_underscore: + type: pattern_replace + pattern: "([^\\s]+)-(?=[^\\s]+)" + replacement: "$1_" + index: number_of_shards: 1 number_of_replicas: 0 @@ -28,8 +46,10 @@ mappings: type: long name: type: text - analyzer: nyaapantsu_analyzer - fielddata: true # Use to sort by id because it is currently a text field + analyzer: nyaapantsu_index_analyzer + fields: + raw: + type: keyword category: type: text sub_category: