refine conv state key search
This commit is contained in:
parent
278751c107
commit
172e316c31
|
|
@ -71,5 +71,5 @@ public interface IConversationService
|
|||
/// <param name="convLimit">conversation limit</param>
|
||||
/// <param name="preLoad">if pre-loading, then keys are not filter by the search query</param>
|
||||
/// <returns></returns>
|
||||
Task<List<string>> GetConversationStateSearhKeys(string query, int convlimit = 100, int keyLimit = 10, bool preLoad = false);
|
||||
Task<List<string>> GetConversationStateSearhKeys(string query, int convLimit = 100, int keyLimit = 10, bool preload = false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
=> throw new NotImplementedException();
|
||||
List<string> TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
|
||||
=> throw new NotImplementedException();
|
||||
List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperlimit = 100)
|
||||
List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperLimit = 100)
|
||||
=> throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
|
|
|
|||
|
|
@ -222,17 +222,17 @@ public partial class ConversationService : IConversationService
|
|||
_state.Save();
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetConversationStateSearhKeys(string query, int convlimit = 100, int keyLimit = 10, bool preLoad = false)
|
||||
public async Task<List<string>> GetConversationStateSearhKeys(string query, int convLimit = 100, int keyLimit = 10, bool preload = false)
|
||||
{
|
||||
var keys = new List<string>();
|
||||
if (!preLoad && string.IsNullOrWhiteSpace(query))
|
||||
if (!preload && string.IsNullOrWhiteSpace(query))
|
||||
{
|
||||
return keys;
|
||||
}
|
||||
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
keys = db.GetConversationStateSearchKeys(convUpperlimit: convlimit);
|
||||
keys = preLoad ? keys : keys.Where(x => x.Contains(query, StringComparison.OrdinalIgnoreCase)).ToList();
|
||||
keys = db.GetConversationStateSearchKeys(convUpperLimit: convLimit);
|
||||
keys = preload ? keys : keys.Where(x => x.Contains(query, StringComparison.OrdinalIgnoreCase)).ToList();
|
||||
return keys.OrderBy(x => x).Take(keyLimit).ToList();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -605,7 +605,7 @@ public partial class FileRepository
|
|||
#if !DEBUG
|
||||
[SharpCache(10)]
|
||||
#endif
|
||||
public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperlimit = 100)
|
||||
public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperLimit = 100)
|
||||
{
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
|
||||
if (!Directory.Exists(dir)) return [];
|
||||
|
|
@ -635,7 +635,7 @@ public partial class FileRepository
|
|||
keys.AddRange(stateKeys);
|
||||
count++;
|
||||
|
||||
if (count >= convUpperlimit)
|
||||
if (count >= convUpperLimit)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -555,10 +555,10 @@ public class ConversationController : ControllerBase
|
|||
|
||||
#region Search state keys
|
||||
[HttpGet("/conversation/state/keys")]
|
||||
public async Task<List<string>> GetConversationStateKeys([FromQuery] string query, [FromQuery] int keyLimit = 10, [FromQuery] bool preLoad = false)
|
||||
public async Task<List<string>> GetConversationStateKeys([FromQuery] string query, [FromQuery] int keyLimit = 10, [FromQuery] int convLimit = 100, [FromQuery] bool preload = false)
|
||||
{
|
||||
var convService = _services.GetRequiredService<IConversationService>();
|
||||
var keys = await convService.GetConversationStateSearhKeys(query, keyLimit: keyLimit, preLoad: preLoad);
|
||||
var keys = await convService.GetConversationStateSearhKeys(query, keyLimit: keyLimit, convLimit: convLimit, preload: preload);
|
||||
return keys;
|
||||
}
|
||||
#endregion
|
||||
|
|
|
|||
|
|
@ -614,20 +614,20 @@ public partial class MongoRepository
|
|||
#if !DEBUG
|
||||
[SharpCache(10)]
|
||||
#endif
|
||||
public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperlimit = 100)
|
||||
public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperLimit = 100)
|
||||
{
|
||||
var convFilter = Builders<ConversationDocument>.Filter.Gte(x => x.DialogCount, messageLowerLimit);
|
||||
var conversations = _dc.Conversations.Find(convFilter)
|
||||
.SortByDescending(x => x.UpdatedTime)
|
||||
.Limit(convUpperlimit)
|
||||
.ToList();
|
||||
var stateBuilder = Builders<ConversationStateDocument>.Filter;
|
||||
var sortDef = Builders<ConversationStateDocument>.Sort.Descending(x => x.UpdatedTime);
|
||||
var stateFilters = new List<FilterDefinition<ConversationStateDocument>>()
|
||||
{
|
||||
stateBuilder.Exists(x => x.States),
|
||||
stateBuilder.Ne(x => x.States, [])
|
||||
};
|
||||
|
||||
if (conversations.IsNullOrEmpty()) return [];
|
||||
|
||||
var convIds = conversations.Select(x => x.Id).ToList();
|
||||
var stateFilter = Builders<ConversationStateDocument>.Filter.In(x => x.ConversationId, convIds);
|
||||
|
||||
var states = _dc.ConversationStates.Find(stateFilter).ToList();
|
||||
var states = _dc.ConversationStates.Find(stateBuilder.And(stateFilters))
|
||||
.Sort(sortDef)
|
||||
.Limit(convUpperLimit)
|
||||
.ToList();
|
||||
var keys = states.SelectMany(x => x.States.Select(x => x.Key)).Distinct().ToList();
|
||||
return keys;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue