diff --git a/server/AyaNova/biz/Search.cs b/server/AyaNova/biz/Search.cs index e19b921f..84a08e03 100644 --- a/server/AyaNova/biz/Search.cs +++ b/server/AyaNova/biz/Search.cs @@ -6,8 +6,6 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using AyaNova.Util; using AyaNova.Models; -//using System.Diagnostics; - namespace AyaNova.Biz { @@ -81,7 +79,6 @@ namespace AyaNova.Biz return ReturnObject; } - //escape literal percentage signs first just in case they are searching for 50% off or something //https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-LIKE //need to get around breaking possibly losing the symbol so make it text @@ -104,92 +101,7 @@ namespace AyaNova.Biz else SearchTerms.Add(PhraseItem.Replace("pctsym", @"\%"));//put back literal percentage symbol if necessary } - - - - /* - this query will do what I want properly. The wildcards here are just for wildcard terms and can also be direct matches doesn't matter the end result is only matches where all terms match the record - however they are desired i.e. AND not OR which is better than v7 which was shit at this as the wildcard search returned all it's results and so did any other terms - So I just need to build this query in this form from the search query terms, put them into the MatchingObjects collection and then the rest will fall into place and can resume as normal - - - WITH qr AS (SELECT aSearchKey.ObjectID, aSearchKey.aType, - count(*) FILTER (WHERE asearchdictionary.word like '%1qt%') AS "st1", - count(*) FILTER (WHERE asearchdictionary.word like '%44%') AS "st2" - FROM aSearchDictionary INNER JOIN aSearchKey ON aSearchDictionary.id = aSearchKey.WordID - group by asearchkey.objectid, asearchkey.atype) - select objectid, atype FROM qr - WHERE st1 > 0 and st2 >0 - order by atype, objectid - - */ - - - - //============================== START SEARCH ========================================================== - #region old search - // //List holder for matching dictionary ID's - // List DictionaryMatches = new List(); - - - - // //GET LIST OF DICTIONARY ID'S THAT MATCH REGULAR SEARCH TERMS - // 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; @@ -215,9 +127,7 @@ namespace AyaNova.Biz using (var command = ct.Database.GetDbConnection().CreateCommand()) { await ct.Database.OpenConnectionAsync(); - command.CommandText = q.ToString(); - // try - // { + command.CommandText = q.ToString(); using (var dr = await command.ExecuteReaderAsync()) { while (dr.Read()) @@ -225,32 +135,9 @@ namespace AyaNova.Biz MatchingObjects.Add(new AyaTypeId((AyaType)dr.GetInt32(0), dr.GetInt64(1))); } } - // } - // 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 ============================================================ - - - - + //REMOVE ANY ITEMS THAT USER IS NOT PERMITTED TO READ //list to hold temporary matches @@ -287,8 +174,6 @@ namespace AyaNova.Biz //Ok, we're here with the list of allowable objects which is now the master matching objects list so... MatchingObjects = CanReadMatchingObjects; - - //TOTAL RESULTS //we have the total results here so set accordingly ReturnObject.TotalResultsFound = MatchingObjects.Count; @@ -299,15 +184,9 @@ namespace AyaNova.Biz if (searchParameters.MaxResults > 0)//0 = all results MatchingObjects = MatchingObjects.Take(searchParameters.MaxResults).ToList(); - //Sort and group the matching objects list in return order - //zCzustomer.OrderBy(z => z.LastName).ThenBy(z => z.FirstName) + //Sort and group the matching objects list in return order var OrderedMatchingObjects = MatchingObjects.OrderBy(z => z.AType).ThenByDescending(z => z.ObjectId); - - - // var watch = new System.Diagnostics.Stopwatch();//###################### PROFILING - // watch.Start();//###################### PROFILING - //Get names using best performing technique using (var command = ct.Database.GetDbConnection().CreateCommand()) { @@ -326,9 +205,6 @@ namespace AyaNova.Biz } } - // watch.Stop();//###################### PROFILING - // var TimeToBuildSearchResultReturnList = watch.ElapsedMilliseconds;//###################### PROFILING - return ReturnObject; }