This commit is contained in:
2021-10-11 19:56:14 +00:00
parent ceb655e761
commit 0f8773bde4

View File

@@ -126,67 +126,129 @@ order by atype, objectid
//============================== START SEARCH ==========================================================
#region old search
//List holder for matching dictionary ID's // //List holder for matching dictionary ID's
List<long> DictionaryMatches = new List<long>(); // List<long> DictionaryMatches = new List<long>();
//GET LIST OF DICTIONARY ID'S THAT MATCH REGULAR SEARCH TERMS // //GET LIST OF DICTIONARY ID'S THAT MATCH REGULAR SEARCH TERMS
if (SearchTerms.Count > 0) // if (SearchTerms.Count > 0)
// foreach (string Term in SearchTerms)
// {
// DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word==Term).Select(z => z.Id).ToListAsync());
// }
// //GET LIST OF DICTIONARY ID'S THAT MATCH WILDCARD SEARCH TERMS
// if (PreWildCardedSearchTerms.Count > 0)
// {
// foreach (string WildCardSearchTerm in PreWildCardedSearchTerms)
// {
// //Contains?
// if (WildCardSearchTerm.StartsWith("%") && WildCardSearchTerm.EndsWith("%"))
// {
// DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word.Contains(WildCardSearchTerm.Replace("%", ""))).Select(z => z.Id).ToListAsync());
// }
// else if (WildCardSearchTerm.EndsWith("%")) //STARTS WITH?
// {
// DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word.StartsWith(WildCardSearchTerm.Replace("%", ""))).Select(z => z.Id).ToListAsync());
// }
// else if (WildCardSearchTerm.StartsWith("%"))//ENDS WITH?
// {
// DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word.EndsWith(WildCardSearchTerm.Replace("%", ""))).Select(z => z.Id).ToListAsync());
// }
// }
// }
// //SEARCH SEARCHKEY FOR MATCHING WORDS AND OPTIONALLY TYPE
// var TotalSearchTermsToMatch = PreWildCardedSearchTerms.Count + SearchTerms.Count;
// //Build search query based on searchParameters
// var q = ct.SearchKey.Distinct().Where(z => DictionaryMatches.Contains(z.WordId));
// //Of type?
// if (searchParameters.TypeOnly != AyaType.NoType)
// q = q.Where(z => z.AType == searchParameters.TypeOnly);
// //Find the records that have the search terms in searchkey
// var SearchMatches = q.GroupBy(z => new { z.AType, z.ObjectId }).Select(z => new { ObjectId = z.Key.ObjectId, AType = z.Key.AType, ObjectCount = z.LongCount() });
// //PUT THE RESULTS INTO MATCHING OBJECTS LIST
// foreach (var SearchMatch in SearchMatches)
// {
// //keep any object that matches *all* the search terms
// if (SearchMatch.ObjectCount >= TotalSearchTermsToMatch)
// MatchingObjects.Add(new AyaTypeId(SearchMatch.AType, SearchMatch.ObjectId));
// }
#endregion old search
StringBuilder q = new StringBuilder();
int termCount = 0;
q.Append("WITH qr AS (SELECT asearchkey.atype, asearchkey.objectid, ");
//EXACT MATCH SEARCH TERMS
foreach (string Term in SearchTerms) foreach (string Term in SearchTerms)
{ q.Append($"COUNT(*) FILTER (WHERE asearchdictionary.word = '{Term}') AS 'st{++termCount}', ");
DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word==Term).Select(z => z.Id).ToListAsync());
}
//WILDCARD SEARCH TERMS
//GET LIST OF DICTIONARY ID'S THAT MATCH WILDCARD SEARCH TERMS
if (PreWildCardedSearchTerms.Count > 0)
{
foreach (string WildCardSearchTerm in PreWildCardedSearchTerms) foreach (string WildCardSearchTerm in PreWildCardedSearchTerms)
q.Append($"COUNT(*) FILTER (WHERE asearchdictionary.word LIKE '{WildCardSearchTerm}') AS 'st{++termCount}', ");
q.Append("FROM asearchdictionary INNER JOIN asearchkey ON asearchdictionary.id = asearchkey.wordid GROUP BY asearchkey.objectid, asearchkey.atype) SELECT objectid, atype FROM qr WHERE ");
for (; termCount > 0; termCount--)
q.Append($"st{termCount} > 0 {(termCount > 1 ? "AND" : "")}");
//execute the query and iterate the results
using (var command = ct.Database.GetDbConnection().CreateCommand())
{ {
//Contains? await ct.Database.OpenConnectionAsync();
if (WildCardSearchTerm.StartsWith("%") && WildCardSearchTerm.EndsWith("%")) command.CommandText = q.ToString();
// try
// {
using (var dr = await command.ExecuteReaderAsync())
{ {
DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word.Contains(WildCardSearchTerm.Replace("%", ""))).Select(z => z.Id).ToListAsync()); while (dr.Read())
}
else if (WildCardSearchTerm.EndsWith("%")) //STARTS WITH?
{ {
DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word.StartsWith(WildCardSearchTerm.Replace("%", ""))).Select(z => z.Id).ToListAsync()); MatchingObjects.Add(new AyaTypeId((AyaType)dr.GetInt32(0), dr.GetInt64(1)));
}
else if (WildCardSearchTerm.StartsWith("%"))//ENDS WITH?
{
DictionaryMatches.AddRange(await ct.SearchDictionary.Where(z => z.Word.EndsWith(WildCardSearchTerm.Replace("%", ""))).Select(z => z.Id).ToListAsync());
} }
} }
// }
// catch (Npgsql.PostgresException e)
// {
// //log out the exception and the query
// log.LogError("DataListFetcher:GetIdListResponseAsync query failed unexpectedly. IDList Query was:");
// log.LogError(qDataQuery);
// log.LogError(e, "DB Exception");
// throw new System.Exception("DataListFetcher:GetIdListResponseAsync - Query failed see log");
// }
// catch (System.Exception e)
// {
// //ensure any other type of exception gets surfaced properly
// //log out the exception and the query
// log.LogError("DataListFetcher:GetIdListResponseAsync unexpected failure. IDList Query was:");
// log.LogError(qDataQuery);
// log.LogError(e, "Exception");
// throw new System.Exception("DataListFetcher:GetIdListResponseAsync - unexpected failure see log");
// }
} }
//============================================== END SEARCH ============================================================
//SEARCH SEARCHKEY FOR MATCHING WORDS AND OPTIONALLY TYPE
var TotalSearchTermsToMatch = PreWildCardedSearchTerms.Count + SearchTerms.Count;
//Build search query based on searchParameters
var q = ct.SearchKey.Distinct().Where(z => DictionaryMatches.Contains(z.WordId));
//Of type?
if (searchParameters.TypeOnly != AyaType.NoType)
q = q.Where(z => z.AType == searchParameters.TypeOnly);
//Find the records that have the search terms in searchkey
var SearchMatches = q.GroupBy(z => new { z.AType, z.ObjectId }).Select(z => new { ObjectId = z.Key.ObjectId, AType = z.Key.AType, ObjectCount = z.LongCount() });
//PUT THE RESULTS INTO MATCHING OBJECTS LIST
foreach (var SearchMatch in SearchMatches)
{
//keep any object that matches *all* the search terms
if (SearchMatch.ObjectCount >= TotalSearchTermsToMatch)
MatchingObjects.Add(new AyaTypeId(SearchMatch.AType, SearchMatch.ObjectId));
}
//REMOVE ANY ITEMS THAT USER IS NOT PERMITTED TO READ //REMOVE ANY ITEMS THAT USER IS NOT PERMITTED TO READ
//list to hold temporary matches //list to hold temporary matches