func main() { ExampleSearchEngine := ferret.New(ExampleWords, ExampleWords, ExampleData, ExampleConverter) fmt.Println(ExampleSearchEngine.Query("ar", 5)) fmt.Println(ExampleSearchEngine.Query("test", 5)) fmt.Println(ExampleSearchEngine.ErrorCorrectingQuery("tsst", 5, ExampleCorrection)) fmt.Println(ExampleSearchEngine.SortedErrorCorrectingQuery("tsst", 5, ExampleCorrection, ExampleSorter)) fmt.Println(ExampleSearchEngine.SortedQuery("a", 5, ExampleSorter)) fmt.Println(ExampleSearchEngine.Query("a", 5)) ExampleSearchEngine.Insert("asdfghjklqwertyuiopzxcvbnm", "asdfghjklqwertyuiopzxcvbnm", []uint64{26}) fmt.Println(ExampleSearchEngine.Query("sdfghjklqwert", 5)) fmt.Println(ExampleSearchEngine.Query("ferret", 5)) ExampleSearchEngine.Insert("ferret", "ferret", []uint64{7}) fmt.Println(ExampleSearchEngine.Query("ferret", 5)) }
func loadSearchItems() { db, err := sql.Open("mysql", *mySQLAddress) if err != nil { fmt.Println("Unable to connect to that DB address :(") fmt.Println("-----------------------------------") panic(err.Error()) } defer db.Close() rows, err := db.Query(config.SQLQuery) if err != nil { fmt.Println("Unable to connect to that DB address") fmt.Println("-----------------------------------") panic(err.Error()) } // populating entries cycle Words := make([]string, 0) Values := make([]interface{}, 0) for rows.Next() { var id, name, category_id, category_name, subcategory_id, subcategory_name, vendor, price, images string if err := rows.Scan(&id, &name, &category_id, &category_name, &subcategory_id, &subcategory_name, &vendor, &price, &images); err != nil { panic(err.Error()) } list_images := strings.Split(images, ",") entry := map[string]interface{}{ "id": id, "name": name, "category_id": category_id, "category": category_name, "subcategory_id": subcategory_id, "subcategory": subcategory_name, "price": price, "vendor": vendor, "images": list_images, } keyWord := vendor + name Words = append(Words, keyWord) Values = append(Values, 10) // to add some fancy prioritizing here jsonString, _ = json.Marshal(entry) ValueIds[keyWord] = entry } SearchEngine = ferret.New(Words, Words, Values, Converter) }
func main() { t := time.Now() Data, err := ioutil.ReadFile("dictionary.dat") if err != nil { panic(err) } Words := make([]string, 0) Values := make([]interface{}, 0) for _, Vals := range bytes.Split(Data, []byte("\r\n")) { WordFreq := bytes.Split(Vals, []byte(" ")) if len(WordFreq) != 2 { continue } Freq, err := strconv.ParseUint(string(WordFreq[1]), 10, 64) if err != nil { continue } Words = append(Words, string(WordFreq[0])) Values = append(Values, Freq) } fmt.Println("Loaded dictionary in:", time.Now().Sub(t)) t = time.Now() SearchEngine := ferret.New(Words, Words, Values, Converter) fmt.Println("Created index in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.Query("ar", 5)) fmt.Println("Performed search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.Query("test", 5)) fmt.Println("Performed search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.ErrorCorrectingQuery("tsst", 5, Correction)) fmt.Println("Performed error correcting search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.SortedErrorCorrectingQuery("tssst", 5, Correction, LengthSorter)) fmt.Println("Performed sorted error correcting search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.SortedErrorCorrectingQuery("tssst", 5, Correction, FreqSorter)) fmt.Println("Performed sorted error correcting search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.SortedQuery("a", 5, LengthSorter)) fmt.Println("Performed sorted search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.SortedQuery("a", 5, FreqSorter)) fmt.Println("Performed sorted search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.Query("a", 5)) fmt.Println("Performed search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.Query("the", 25)) fmt.Println("Performed search in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.SortedQuery("the", 25, FreqSorter)) fmt.Println("Performed sorted search in:", time.Now().Sub(t)) t = time.Now() SearchEngine.Insert("asdfghjklqwertyuiopzxcvbnm", "asdfghjklqwertyuiopzxcvbnm", uint64(0)) fmt.Println("Performed insert in:", time.Now().Sub(t)) t = time.Now() fmt.Println(SearchEngine.Query("sdfghjklqwert", 5)) fmt.Println("Performed search in:", time.Now().Sub(t)) fmt.Println("Running benchmarks...") t = time.Now() n := 0 for _, Query := range SearchEngine.Words { SearchEngine.Query(string(Query), 5) n++ } fmt.Println("Performed", n, "limit-5 searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words { SearchEngine.Query(string(Query), 25) n++ } fmt.Println("Performed", n, "limit-25 searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words { SearchEngine.SortedQuery(string(Query), 5, LengthSorter) n++ } fmt.Println("Performed", n, "limit-5 length sorted searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words { SearchEngine.SortedQuery(string(Query), 25, LengthSorter) n++ } fmt.Println("Performed", n, "limit-25 length sorted searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words { SearchEngine.SortedQuery(string(Query), 5, FreqSorter) n++ } fmt.Println("Performed", n, "limit-5 frequency sorted searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words { SearchEngine.SortedQuery(string(Query), 25, FreqSorter) n++ } fmt.Println("Performed", n, "limit-25 frequency sorted searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words[:2048] { SearchEngine.ErrorCorrectingQuery(string(Query)+"0", 5, Correction) n++ } fmt.Println("Performed", n, "limit-5 error correcting searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words[:2048] { SearchEngine.ErrorCorrectingQuery(string(Query)+"0", 25, Correction) n++ } fmt.Println("Performed", n, "limit-25 error correcting searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words[:2048] { SearchEngine.SortedErrorCorrectingQuery(string(Query)+"0", 5, Correction, LengthSorter) n++ } fmt.Println("Performed", n, "limit-5 length sorted error correcting searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words[:2048] { SearchEngine.SortedErrorCorrectingQuery(string(Query)+"0", 25, Correction, LengthSorter) n++ } fmt.Println("Performed", n, "limit-25 length sorted error correcting searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words[:2048] { SearchEngine.SortedErrorCorrectingQuery(string(Query)+"0", 5, Correction, FreqSorter) n++ } fmt.Println("Performed", n, "limit-5 frequency sorted error correcting searches in:", time.Now().Sub(t)) t = time.Now() n = 0 for _, Query := range SearchEngine.Words[:2048] { SearchEngine.SortedErrorCorrectingQuery(string(Query)+"0", 25, Correction, FreqSorter) n++ } fmt.Println("Performed", n, "limit-25 frequency sorted error correcting searches in:", time.Now().Sub(t)) }