diff --git a/server/AyaNova/biz/Search.cs b/server/AyaNova/biz/Search.cs index 1447e6e8..54357688 100644 --- a/server/AyaNova/biz/Search.cs +++ b/server/AyaNova/biz/Search.cs @@ -206,29 +206,57 @@ namespace AyaNova.Biz //QUERY FOR ALL TAGMAPS THAT MATCH OBJECT TYPE AND ID FOR EVERY TAG SPECIFIED (UNION) //var tagmatches= await ct.TagMap.Where(m => ).Select(m => m.Id).ToListAsync(); + //ct.TagMap.Where(n => n.Tags.Count(t => tags.Contains(t.DisplayName)) == tags.Count) //algorithm: //1) get counts for each tag specified from tagmap, if any are zero then none match and can bail early foreach (long SearchTagId in searchParameters.Tags) { - var SearchTagCount = await ct.TagMap.Where(m => m.TagId == SearchTagId).LongCountAsync(); + var MatchTagCount = await ct.TagMap.Where(m => m.TagId == SearchTagId).LongCountAsync(); //zero tags matching here at any point means no results for the entire search and we can bail - if (SearchTagCount == 0) + if (MatchTagCount == 0) { //return empty resultlist return ResultList; } //Save the matching count - TagCounts.Add(SearchTagId,SearchTagCount); + TagCounts.Add(SearchTagId, MatchTagCount); } - //2) find smallest count match, fetch the entire list of tagmaps for that term so we are working with the shortest list first + //2) find smallest count match so we are working with the shortest list first var ShortestMatchingTag = TagCounts.OrderBy(x => x.Value).First().Key; - //3) Generate the shortlist of items that match the shortest tag list, then iterate the shortlist and see if each item matches all other tags specified if it does then put it into the matching objects list for return - - //now iterate it and keep each item that matches all other tags + //3) Generate the shortlist of items that match the shortest tag list + var ShortList = await ct.TagMap.Where(x => x.TagId == ShortestMatchingTag).ToListAsync(); + + //4) Iterate the shortlist and see if each item matches all other tags specified if it does then put it into the matching objects list for return + var SearchTagCount = searchParameters.Tags.Count; + //Iterate shortlist + foreach (TagMap t in ShortList) + { + var matchCount = 1; + //Iterate requested tags + foreach (long TagId in searchParameters.Tags) + { + //skipping already matched shortest tag + if (TagId != ShortestMatchingTag) + { + //Ok, does this object have this tag? + bool HasTag = await ct.TagMap.Where(x => x.TagToObjectId == t.TagToObjectId && x.TagId == TagId).AnyAsync(); + if (HasTag) + matchCount++; + } + } + //does it match all tags? + if (matchCount == SearchTagCount) + { + //yes, add it to the results + MatchingObjects.Add(new AyaTypeId(t.TagToObjectType, t.TagToObjectId)); + } + } + + } else