Exemple #1
0
func NewSearchResults(searchTermsString string) SearchResults {
	var articleDirectory string = filepath.Dir("src/static/articles/")

	if articleDirectory == "." {
		log.Panic("Directory not found")
	}

	searchTerms := strings.Split(searchTermsString, " ")

	files, err := ioutil.ReadDir(articleDirectory)
	if err != nil {
		log.Panic(err)
	}
	// 0 length to not get default empty articles that search accidentally indexes
	articlesArray := make([]Article, 0, len(files))

	matcher := search.New(language.English)
	for _, file := range files {
		for _, searchTerm := range searchTerms {
			validArticle := UseMatcher(matcher, searchTerm, file.Name())
			if validArticle {
				article, err := LoadArticleFilePath("src/static/articles/" + file.Name())
				if err != nil {
					log.Panic(err)
				}
				articlesArray = append(articlesArray, article)
			}
			// do not want to add an article twice
			break
		}
	}
	return SearchResults{articlesArray}
}
Exemple #2
0
	"golang.org/x/text/language"
	"golang.org/x/text/search"
)

const (
	SEPARATOR = "\t"
)

var (
	options = []search.Option{
		search.IgnoreCase,
		search.IgnoreWidth,
	}

	matcher = search.New(language.English, options...)
)

type Location struct {
	Code     string
	Name     string
	CityCode string
}

type index struct {
	start int
	end   int
}

// Get cities suggestions
func Locations(pat, l string) []string {