This commit is contained in:
2018-12-07 16:06:58 +00:00
parent 5fc348e3cf
commit 45d7051804
5 changed files with 113 additions and 139 deletions

View File

@@ -86,7 +86,7 @@ asearchkey
- objectid (long id of source object) - objectid (long id of source object)
- objecttype (AyaType as int of source object) - objecttype (AyaType as int of source object)
- inname (bool indicates the search word was in the name of the object) - inname (bool indicates the search word was in the name of the object)
- intags (bool indicates teh search word was found in the tags collection for the object)

View File

@@ -31,8 +31,7 @@ namespace AyaNova.Biz
- Can be empty if tags are specified, no tags and no phrase is an error condition - Can be empty if tags are specified, no tags and no phrase is an error condition
- ObjectType: only return results for objects of this type - ObjectType: only return results for objects of this type
- InName: flag that indicates only search in names - InName: flag that indicates only search in names
- Tags that are also on result objects
- Can be empty if a phrase is specified
ACTION ACTION
@@ -65,7 +64,7 @@ namespace AyaNova.Biz
public string Phrase { get; set; } public string Phrase { get; set; }
public bool NameOnly { get; set; } public bool NameOnly { get; set; }
public AyaType TypeOnly { get; set; } public AyaType TypeOnly { get; set; }
public List<string> Tags { get; set; }
//Note: maxresults of 0 will get all results //Note: maxresults of 0 will get all results
public int MaxResults { get; set; } public int MaxResults { get; set; }
@@ -73,7 +72,6 @@ namespace AyaNova.Biz
{ {
NameOnly = false; NameOnly = false;
TypeOnly = AyaType.NoType; TypeOnly = AyaType.NoType;
Tags = new List<string>();
MaxResults = 500; MaxResults = 500;
} }
@@ -84,11 +82,6 @@ namespace AyaNova.Biz
//has a phrase? //has a phrase?
if (!string.IsNullOrWhiteSpace(this.Phrase)) if (!string.IsNullOrWhiteSpace(this.Phrase))
return true; return true;
//has tags?
if (this.Tags.Count > 0)
return true;
return false; return false;
} }
} }
@@ -197,7 +190,6 @@ namespace AyaNova.Biz
q = q.Where(m => m.ObjectType == searchParameters.TypeOnly); q = q.Where(m => m.ObjectType == searchParameters.TypeOnly);
//Find the records that have the search terms in searchkey //Find the records that have the search terms in searchkey
var SearchMatches = q.GroupBy(x => new { x.ObjectType, x.ObjectId }).Select(x => new { ObjectId = x.Key.ObjectId, ObjectType = x.Key.ObjectType, ObjectCount = x.LongCount() }); var SearchMatches = q.GroupBy(x => new { x.ObjectType, x.ObjectId }).Select(x => new { ObjectId = x.Key.ObjectId, ObjectType = x.Key.ObjectType, ObjectCount = x.LongCount() });
@@ -211,102 +203,104 @@ namespace AyaNova.Biz
} }
#region TAGS DEPRECATED
// //--------------- TAGS ARE DEPRECATED AS BEING SOMETHING YOU CAN SEARCH FOR SEPARATELY (INTAGS) INSTEAD THEY ARE TREATED AS PART OF THE TEXT OF THE OBJECT like any other
// //IF TAGS SPECIFIED
// if (searchParameters.Tags.Count > 0)
// {
// //get a count of the search tags (used by both paths below)
// var SearchTagCount = searchParameters.Tags.Count;
//--------------- TAGS // if (string.IsNullOrWhiteSpace(searchParameters.Phrase))
//IF TAGS SPECIFIED // {
if (searchParameters.Tags.Count > 0)
{
//get a count of the search tags (used by both paths below)
var SearchTagCount = searchParameters.Tags.Count;
if (string.IsNullOrWhiteSpace(searchParameters.Phrase)) // #region TAGS ONLY SEARCH (NO PHRASE) ALL FULL MATCHES ARE INCLUSIVE
{ // Dictionary<long, long> TagCounts = new Dictionary<long, long>();
#region TAGS ONLY SEARCH (NO PHRASE) ALL FULL MATCHES ARE INCLUSIVE // //QUERY FOR ALL TAGMAPS THAT MATCH OBJECT TYPE AND ID FOR EVERY TAG SPECIFIED (UNION)
Dictionary<long, long> TagCounts = new Dictionary<long, long>(); // //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)
//QUERY FOR ALL TAGMAPS THAT MATCH OBJECT TYPE AND ID FOR EVERY TAG SPECIFIED (UNION) // //algorithm:
//var tagmatches= await ct.TagMap.Where(m => ).Select(m => m.Id).ToListAsync(); // //1) get counts for each tag specified from tagmap, if any are zero then none match and can bail early
//ct.TagMap.Where(n => n.Tags.Count(t => tags.Contains(t.DisplayName)) == tags.Count) // foreach (long SearchTagId in searchParameters.Tags)
// {
// 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 (MatchTagCount == 0)
// {
// //return empty resultlist
// return ReturnObject;
// }
//algorithm: // //Save the matching count
//1) get counts for each tag specified from tagmap, if any are zero then none match and can bail early // TagCounts.Add(SearchTagId, MatchTagCount);
foreach (long SearchTagId in searchParameters.Tags) // }
{
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 (MatchTagCount == 0)
{
//return empty resultlist
return ReturnObject;
}
//Save the matching count // //2) find smallest count match so we are working with the shortest list first
TagCounts.Add(SearchTagId, MatchTagCount); // var ShortestMatchingTag = TagCounts.OrderBy(x => x.Value).First().Key;
}
//2) find smallest count match so we are working with the shortest list first // //3) Generate the shortlist of items that match the shortest tag list
var ShortestMatchingTag = TagCounts.OrderBy(x => x.Value).First().Key; // var ShortList = await ct.TagMap.Where(x => x.TagId == ShortestMatchingTag).ToListAsync();
//3) Generate the shortlist of items that match the shortest tag list // //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 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 // //Iterate shortlist
// foreach (TagMap t in ShortList)
//Iterate shortlist // {
foreach (TagMap t in ShortList) // var matchCount = 1;
{ // //Iterate requested tags
var matchCount = 1; // foreach (long TagId in searchParameters.Tags)
//Iterate requested tags // {
foreach (long TagId in searchParameters.Tags) // //skipping already matched shortest tag
{ // if (TagId != ShortestMatchingTag)
//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.TagToObjectType == t.TagToObjectType && x.TagId == TagId).AnyAsync();
//Ok, does this object have this tag? // if (HasTag)
bool HasTag = await ct.TagMap.Where(x => x.TagToObjectId == t.TagToObjectId && x.TagToObjectType == t.TagToObjectType && x.TagId == TagId).AnyAsync(); // matchCount++;
if (HasTag) // }
matchCount++; // }
} // //does it match all tags?
} // if (matchCount == SearchTagCount)
//does it match all tags? // {
if (matchCount == SearchTagCount) // //yes, add it to the results
{ // MatchingObjects.Add(new AyaTypeId(t.TagToObjectType, t.TagToObjectId));
//yes, add it to the results // }
MatchingObjects.Add(new AyaTypeId(t.TagToObjectType, t.TagToObjectId)); // }
} // #endregion
}
#endregion
} // }
else // else
{ // {
#region TAGS PLUS PHRASE SEARCH WITH NON MATCHING TAGS EXCLUSIVE // #region TAGS PLUS PHRASE SEARCH WITH NON MATCHING TAGS EXCLUSIVE
//list to hold temporary matches // //list to hold temporary matches
List<AyaTypeId> TagMatchingObjects = new List<AyaTypeId>(); // List<AyaTypeId> TagMatchingObjects = new List<AyaTypeId>();
//LOOP THROUGH MATCHING OBJECTS LIST // //LOOP THROUGH MATCHING OBJECTS LIST
foreach (AyaTypeId i in MatchingObjects) // foreach (AyaTypeId i in MatchingObjects)
{ // {
var matchCount = await ct.TagMap.Where(x => x.TagToObjectId == i.ObjectId && x.TagToObjectType == i.ObjectType && searchParameters.Tags.Contains(x.TagId)).LongCountAsync(); // var matchCount = await ct.TagMap.Where(x => x.TagToObjectId == i.ObjectId && x.TagToObjectType == i.ObjectType && searchParameters.Tags.Contains(x.TagId)).LongCountAsync();
if (matchCount == SearchTagCount) // if (matchCount == SearchTagCount)
{ // {
TagMatchingObjects.Add(i); // TagMatchingObjects.Add(i);
} // }
} // }
//Ok here we have all the MatchingObjects that had all the tags in the TagMatchingObjects list so that's actually now our defacto return list // //Ok here we have all the MatchingObjects that had all the tags in the TagMatchingObjects list so that's actually now our defacto return list
MatchingObjects = TagMatchingObjects; // MatchingObjects = TagMatchingObjects;
#endregion // #endregion
} // }
} // }
//--------------------- END TAGS ------------- //--------------------- END TAGS -------------
#endregion tags DEPRECATED
//REMOVE ANY ITEMS THAT USER IS NOT PERMITTED TO READ //REMOVE ANY ITEMS THAT USER IS NOT PERMITTED TO READ
//If it's a name only search then all is allowed //If it's a name only search then all is allowed
@@ -386,16 +380,13 @@ namespace AyaNova.Biz
public AyaType ObjectType { get; set; } public AyaType ObjectType { get; set; }
public string Name { get; set; } public string Name { get; set; }
public List<string> Words { get; set; } public List<string> Words { get; set; }
public List<string> Tags { get; set; }
public SearchIndexProcessObjectParameters(long localeId, long objectID, AyaType objectType, string name, List<string> tags = null)
public SearchIndexProcessObjectParameters(long localeId, long objectID, AyaType objectType, string name)
{ {
Words = new List<string>(); Words = new List<string>();
if (tags != null)
Tags = tags;
else
Tags = new List<string>();
LocaleId = localeId; LocaleId = localeId;
ObjectId = objectID; ObjectId = objectID;
ObjectType = objectType; ObjectType = objectType;
@@ -413,6 +404,15 @@ namespace AyaNova.Biz
Words.Add(u.ToString()); Words.Add(u.ToString());
return this; return this;
} }
public SearchIndexProcessObjectParameters AddWord(List<string> lWords)
{
foreach (string s in lWords)
{
Words.Add(s);
}
return this;
}
} }
@@ -465,10 +465,10 @@ namespace AyaNova.Biz
List<string> NameKeyWordList = Break(p.LocaleId, p.Name); List<string> NameKeyWordList = Break(p.LocaleId, p.Name);
//BUILD ALL KEYWORDS LIST //BUILD ALL KEYWORDS LIST
List<string> tempList = new List<string>(); // List<string> tempList = new List<string>();
tempList.AddRange(KeyWordList); // tempList.AddRange(KeyWordList);
tempList.AddRange(p.Tags); // tempList.AddRange(p.Tags);
var DistinctAllKeywordsCombinedList = tempList.Distinct(); // var DistinctAllKeywordsCombinedList = tempList.Distinct();
// AllKeyWordsCombinedList=((IEnumerable<string>)AllKeyWordsCombinedList).Distinct(); // AllKeyWordsCombinedList=((IEnumerable<string>)AllKeyWordsCombinedList).Distinct();
//EARLY EXIT IF NO KEYWORDS OR NAME RECORD OR TAGS TO PROCESS //EARLY EXIT IF NO KEYWORDS OR NAME RECORD OR TAGS TO PROCESS
@@ -482,19 +482,14 @@ namespace AyaNova.Biz
List<MatchingDictionaryEntry> MatchingKeywordIdList = new List<MatchingDictionaryEntry>(); List<MatchingDictionaryEntry> MatchingKeywordIdList = new List<MatchingDictionaryEntry>();
//ITERATE ALL THE KEYWORDS, SEARCH IN THE SEARCHDICTIONARY TABLE AND COLLECT ID'S OF ANY PRE-EXISTING IN DB KEYWORDS //ITERATE ALL THE KEYWORDS, SEARCH IN THE SEARCHDICTIONARY TABLE AND COLLECT ID'S OF ANY PRE-EXISTING IN DB KEYWORDS
// var ExistingKeywordMatches = ServiceProviderProvider.DBContext.SearchDictionary.AsNoTracking().Where(m => KeyWordList.Contains(m.Word)).ToDictionary(m => m.Id, m => m.Word); var ExistingKeywordMatches = ServiceProviderProvider.DBContext.SearchDictionary.AsNoTracking().Where(m => KeyWordList.Contains(m.Word)).ToDictionary(m => m.Id, m => m.Word);
var ExistingKeywordMatches = ServiceProviderProvider.DBContext.SearchDictionary.AsNoTracking().Where(m => DistinctAllKeywordsCombinedList.Contains(m.Word)).ToDictionary(m => m.Id, m => m.Word); //var ExistingKeywordMatches = ServiceProviderProvider.DBContext.SearchDictionary.AsNoTracking().Where(m => DistinctAllKeywordsCombinedList.Contains(m.Word)).ToDictionary(m => m.Id, m => m.Word);
//Put the matching keyword ID's into the list //Put the matching keyword ID's into the list
foreach (KeyValuePair<long, string> K in ExistingKeywordMatches) foreach (KeyValuePair<long, string> K in ExistingKeywordMatches)
{ {
//Name or regular word match? //Name or regular word match?
bool IsName = NameKeyWordList.Contains(K.Value); bool IsName = NameKeyWordList.Contains(K.Value);
if (IsName || KeyWordList.Contains(K.Value)) MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = K.Key, InName = IsName });
MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = K.Key, InName = IsName });
//Tag match?
if (p.Tags.Contains(K.Value))
MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = K.Key, InTags = true });
} }
@@ -505,7 +500,7 @@ namespace AyaNova.Biz
var log = AyaNova.Util.ApplicationLogging.CreateLogger("### Search::ProcessKeywords ###"); var log = AyaNova.Util.ApplicationLogging.CreateLogger("### Search::ProcessKeywords ###");
#endif #endif
foreach (string KeyWord in DistinctAllKeywordsCombinedList) foreach (string KeyWord in KeyWordList)
{ {
if (!ExistingKeywordMatches.ContainsValue(KeyWord)) if (!ExistingKeywordMatches.ContainsValue(KeyWord))
{ {
@@ -524,16 +519,7 @@ namespace AyaNova.Biz
//------- //-------
//Add to matching keywords //Add to matching keywords
//was this before tags: MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = NewWord.Id, InName = NameKeyWordList.Contains(KeyWord) }); MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = NewWord.Id, InName = NameKeyWordList.Contains(KeyWord) });
//Name or regular word match?
bool IsName = NameKeyWordList.Contains(KeyWord);
if (IsName || KeyWordList.Contains(KeyWord))
MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = NewWord.Id, InName = IsName });
//Tag match?
if (p.Tags.Contains(KeyWord))
MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = NewWord.Id, InTags = true });
//------- //-------
@@ -561,19 +547,7 @@ namespace AyaNova.Biz
var SearchDictionaryMatchFoundInDB = ServiceProviderProvider.DBContext.SearchDictionary.AsNoTracking().Where(x => x.Word == KeyWord).FirstOrDefault(); var SearchDictionaryMatchFoundInDB = ServiceProviderProvider.DBContext.SearchDictionary.AsNoTracking().Where(x => x.Word == KeyWord).FirstOrDefault();
if (SearchDictionaryMatchFoundInDB != null) if (SearchDictionaryMatchFoundInDB != null)
{ {
//was before tags: MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = SearchDictionaryMatchFoundInDB.Id, InName = NameKeyWordList.Contains(KeyWord) }); MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = SearchDictionaryMatchFoundInDB.Id, InName = NameKeyWordList.Contains(KeyWord) });
//-------
//Add to matching keywords
//Name or regular word match?
bool IsName = NameKeyWordList.Contains(KeyWord);
if (IsName || KeyWordList.Contains(KeyWord))
MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = SearchDictionaryMatchFoundInDB.Id, InName = IsName });
//Tag match?
if (p.Tags.Contains(KeyWord))
MatchingKeywordIdList.Add(new MatchingDictionaryEntry() { DictionaryId = SearchDictionaryMatchFoundInDB.Id, InTags = true });
//-------
//It exists now //It exists now
ExistingKeywordMatches.Add(SearchDictionaryMatchFoundInDB.Id, SearchDictionaryMatchFoundInDB.Word); ExistingKeywordMatches.Add(SearchDictionaryMatchFoundInDB.Id, SearchDictionaryMatchFoundInDB.Word);
} }
@@ -605,7 +579,7 @@ namespace AyaNova.Biz
var NewSearchKeyList = new List<SearchKey>(); var NewSearchKeyList = new List<SearchKey>();
foreach (MatchingDictionaryEntry E in MatchingKeywordIdList) foreach (MatchingDictionaryEntry E in MatchingKeywordIdList)
{ {
NewSearchKeyList.Add(new SearchKey() { WordId = E.DictionaryId, InName = E.InName, InTags = E.InTags, ObjectId = p.ObjectId, ObjectType = p.ObjectType }); NewSearchKeyList.Add(new SearchKey() { WordId = E.DictionaryId, InName = E.InName, ObjectId = p.ObjectId, ObjectType = p.ObjectType });
} }
var CtSearchKeyAdd = ServiceProviderProvider.DBContext; var CtSearchKeyAdd = ServiceProviderProvider.DBContext;
CtSearchKeyAdd.SearchKey.AddRange(NewSearchKeyList); CtSearchKeyAdd.SearchKey.AddRange(NewSearchKeyList);
@@ -619,12 +593,12 @@ namespace AyaNova.Biz
public class MatchingDictionaryEntry public class MatchingDictionaryEntry
{ {
public bool InName { get; set; } public bool InName { get; set; }
public bool InTags { get; set; }
public long DictionaryId { get; set; } public long DictionaryId { get; set; }
public MatchingDictionaryEntry() public MatchingDictionaryEntry()
{ {
InName = false; InName = false;
InTags = false;
DictionaryId = -1; DictionaryId = -1;
} }
} }

View File

@@ -133,8 +133,8 @@ namespace AyaNova.Biz
EventLogProcessor.LogEventToDatabase(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TempContext); EventLogProcessor.LogEventToDatabase(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), TempContext);
//SEARCH INDEXING //SEARCH INDEXING
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserLocaleId, outObj.Id, BizType, outObj.Name, outObj.Tags); var SearchParams = new Search.SearchIndexProcessObjectParameters(UserLocaleId, outObj.Id, BizType, outObj.Name);
SearchParams.AddWord(outObj.Notes).AddWord(outObj.Name).AddWord(outObj.Serial); SearchParams.AddWord(outObj.Notes).AddWord(outObj.Name).AddWord(outObj.Serial).AddWord(outObj.Tags);
Search.ProcessNewObjectKeywords(SearchParams); Search.ProcessNewObjectKeywords(SearchParams);
return outObj; return outObj;

View File

@@ -20,7 +20,7 @@ namespace AyaNova.Models
[Required] [Required]
public AyaType ObjectType { get; set; } public AyaType ObjectType { get; set; }
public bool InName { get; set; } public bool InName { get; set; }
public bool InTags { get; set; }
} }
} }

View File

@@ -22,7 +22,7 @@ namespace AyaNova.Util
//!!!!WARNING: BE SURE TO UPDATE THE DbUtil::PrepareDatabaseForSeeding WHEN NEW TABLES ADDED!!!! //!!!!WARNING: BE SURE TO UPDATE THE DbUtil::PrepareDatabaseForSeeding WHEN NEW TABLES ADDED!!!!
private const int DESIRED_SCHEMA_LEVEL = 8; private const int DESIRED_SCHEMA_LEVEL = 8;
internal const long EXPECTED_COLUMN_COUNT = 95; internal const long EXPECTED_COLUMN_COUNT = 94;
internal const long EXPECTED_INDEX_COUNT = 20; internal const long EXPECTED_INDEX_COUNT = 20;
//!!!!WARNING: BE SURE TO UPDATE THE DbUtil::PrepareDatabaseForSeeding WHEN NEW TABLES ADDED!!!! //!!!!WARNING: BE SURE TO UPDATE THE DbUtil::PrepareDatabaseForSeeding WHEN NEW TABLES ADDED!!!!
@@ -145,7 +145,7 @@ namespace AyaNova.Util
//LOOKAT: this index is periodically being violated during testing //LOOKAT: this index is periodically being violated during testing
exec("CREATE UNIQUE INDEX asearchdictionary_word_idx ON asearchdictionary (word);"); exec("CREATE UNIQUE INDEX asearchdictionary_word_idx ON asearchdictionary (word);");
exec("CREATE TABLE asearchkey (id BIGSERIAL PRIMARY KEY, wordid bigint not null REFERENCES asearchdictionary (id), objectid bigint not null, objecttype integer not null, inname bool not null, intags bool not null)"); exec("CREATE TABLE asearchkey (id BIGSERIAL PRIMARY KEY, wordid bigint not null REFERENCES asearchdictionary (id), objectid bigint not null, objecttype integer not null, inname bool not null)");
//create locale text tables //create locale text tables
exec("CREATE TABLE alocale (id BIGSERIAL PRIMARY KEY, ownerid bigint not null, name varchar(255) not null, stock bool, cjkindex bool default false)"); exec("CREATE TABLE alocale (id BIGSERIAL PRIMARY KEY, ownerid bigint not null, name varchar(255) not null, stock bool, cjkindex bool default false)");
@@ -299,7 +299,7 @@ namespace AyaNova.Util
if (currentSchema < 8) if (currentSchema < 8)
{ {
LogUpdateMessage(log); LogUpdateMessage(log);
exec("CREATE TABLE adatafilter (id BIGSERIAL PRIMARY KEY, ownerid bigint not null, name varchar(255) not null, public bool not null," + exec("CREATE TABLE adatafilter (id BIGSERIAL PRIMARY KEY, ownerid bigint not null, name varchar(255) not null, public bool not null," +
"listkey varchar(255) not null, filter text, UNIQUE(name))"); "listkey varchar(255) not null, filter text, UNIQUE(name))");