// Copyright (C) 2019 Marius Schellenberger

package index

import (
	"github.com/blevesearch/bleve"
	"github.com/blevesearch/bleve/analysis/analyzer/custom"
	"github.com/blevesearch/bleve/analysis/token/edgengram"
	"github.com/blevesearch/bleve/analysis/token/lowercase"
	"github.com/blevesearch/bleve/analysis/tokenizer/unicode"
	"github.com/blevesearch/bleve/mapping"
)

const (
	bigram = "bigram_tokenfilter"
	ngram  = "fulltext_ngram"
)

func createMapping() (m *mapping.IndexMappingImpl, err error) {
	m = bleve.NewIndexMapping()

	err = m.AddCustomTokenFilter(bigram, map[string]interface{}{
		"type": edgengram.Name,
		"side": edgengram.FRONT,
		"min":  3.0,
		"max":  25.0,
	})
	if err != nil {
		return
	}
	err = m.AddCustomAnalyzer(ngram, map[string]interface{}{
		"type":      custom.Name,
		"tokenizer": unicode.Name,
		"token_filters": []string{
			lowercase.Name,
			bigram,
		},
	})
	if err != nil {
		return
	}

	doc := bleve.NewDocumentMapping()
	ti := bleve.NewTextFieldMapping()
	ti.Analyzer = ngram
	doc.AddFieldMappingsAt(titleField, ti)
	st := bleve.NewTextFieldMapping()
	st.Analyzer = ngram
	doc.AddFieldMappingsAt(storeTitleField, st)
	s := bleve.NewTextFieldMapping()
	s.Analyzer = ngram
	doc.AddFieldMappingsAt(searchField, s)

	m.AddDocumentMapping(docType, doc)
	return
}
