public override async Task <IEnumerable <SearchResult> > Search(CancellationToken ct, string query) { var results = new List <SearchResult>(); //try { //var entries = await db.Table<CedictEntry>().Where(p => p.Traditional.StartsWith(query)).ToListAsync(); var entries = await db.QueryAsync <CedictEntry>(ct, "SELECT * FROM CedictEntry WHERE Traditional LIKE ? ESCAPE '\\'", query + "%"); foreach (var q in Pinyin.ToQueryForms(query)) { var newEntries = await db.QueryAsync <CedictEntry>(ct, "SELECT * FROM (SELECT * FROM CedictEntry WHERE PinyinNoNumbers LIKE ? ESCAPE '\\') " + "WHERE Pinyin LIKE ? ESCAPE '\\'", Pinyin.RemoveNumbersAndUnderscore(q) + "%", q + "%"); entries.AddRange(newEntries); } foreach (var s in entries) { var definitions = await db.QueryAsync <CedictDefinition>(ct, "SELECT * FROM CedictDefinition WHERE EntryId = ?", s.Id); //var definitions = await db.Table<CedictDefinition>().Where(d => d.EntryId == s.Id).ToListAsync(); results.Add(new SearchResult() { Traditional = s.Traditional, Simplified = s.Simplified, Pinyin = Pinyin.ConvertToAccents(s.Pinyin), PinyinNumbered = s.Pinyin, Definitions = new List <List <string> >() { new List <string>(from d in definitions select d.Definition) } }); } } return(results); }
public async Task <IEnumerable <Term> > Search(CancellationToken ct, string query) { var results = new List <TermWithFreq>(); results.AddRange(await db.QueryAsync <TermWithFreq>(ct, "SELECT * FROM Term LEFT JOIN Frequency ON Simplified = Hanzi OR Traditional = Hanzi WHERE Traditional LIKE ? OR Simplified LIKE ? ESCAPE '\\'", query + "%", query + "%")); var queryForms = Pinyin.ToQueryForms(query); int numberOfForms = queryForms.Count(); bool allowMultiCharacter = numberOfForms > 1 || (numberOfForms > 0 && queryForms.First().IndexOf(' ') != -1); string limitLength = allowMultiCharacter ? "" : "Length = 1 AND "; foreach (var q in queryForms) { if (!string.IsNullOrWhiteSpace(q)) { results.AddRange(await db.QueryAsync <TermWithFreq>(ct, "SELECT * FROM (SELECT * FROM (SELECT * FROM Term WHERE " + limitLength + "PinyinNoNumbers LIKE ? ESCAPE '\\') " + " WHERE PinyinNumbered LIKE ?) LEFT JOIN Frequency ON Simplified = Hanzi OR Traditional = Hanzi", Pinyin.RemoveNumbersAndUnderscore(q) + "%", q + "%")); } } return(results.OrderBy(r => r.Length) .ThenBy(r => r.PinyinNumbered != null ? r.PinyinNumbered.Length : int.MaxValue) .ThenByDescending(r => r.Score, freqComparer) .ThenBy(r => r.PinyinNumbered)); }