Skip to content

Commit

Permalink
fix: fix less or great by datetime query, update keyword query operat…
Browse files Browse the repository at this point in the history
…e by and
  • Loading branch information
Qinyouzeng committed Jun 28, 2023
1 parent e92039b commit d16c475
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IServiceCollection AddMasaIdentityModel(
private static IServiceCollection AddMasaIdentityCore(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.TryAddSingleton<ICurrentPrincipalAccessor, HttpContextCurrentPrincipalAccessor>();
services.TryAddScoped<ICurrentPrincipalAccessor, HttpContextCurrentPrincipalAccessor>();
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ await client.SearchAsync<TResult>(s => searchDescriptorFunc(s.Index(indexName)).
}
catch (Exception ex)
{
ServiceExtenistion.Logger.LogError(ex, "SearchAsync execute error");
throw new UserFriendlyException($"SearchAsync execute error {ex.Message}");
}
}
Expand All @@ -37,21 +38,19 @@ await client.SearchAsync<TResult>(s => searchDescriptorFunc(s.Index(indexName)).
public static async Task<IEnumerable<MappingResponseDto>> GetMappingAsync(this ICaller caller, string indexName, CancellationToken token = default)
{
var path = $"/{indexName}/_mapping";
var result = await caller.GetAsync<object>(path, false, token);
var result = await caller.GetAsync<object>(path, token);
var json = (JsonElement)result!;
JsonElement mapping = default;
bool findMapping = false;
foreach (var item in json.EnumerateObject())
{
if (!findMapping && item.Value.TryGetProperty("mappings", out mapping))
JsonElement mapping;
if (item.Value.TryGetProperty("mappings", out mapping))
{
findMapping = true;
break;
var mappings = GetRepProperties(mapping, default!)!;
if (mappings != null && mappings.Any())
return mappings;
}
}

if (findMapping)
return GetRepProperties(mapping, default!)!;
throw new UserFriendlyException($"can't find mapping for index: {indexName}");
}

Expand Down Expand Up @@ -197,7 +196,7 @@ private static SearchDescriptor<TResult> AddCondition<TQuery, TResult>(this Sear
}
if (!string.IsNullOrEmpty(query.Keyword))
{
list.Add(queryContainer => queryContainer.QueryString(queryString => queryString.Query(query.Keyword)));
list.Add(queryContainer => queryContainer.QueryString(queryString => queryString.Query(query.Keyword).DefaultOperator(Operator.And)));
}

query.AppendConditions();
Expand Down Expand Up @@ -257,6 +256,7 @@ private static Func<QueryContainerDescriptor<TResult>, QueryContainer> CompareCo
{
CreateFieldKeyword(query.Name, mapping, out var fieldName, out var keyword);
var value = query.Value;
bool isDate = value is DateTime;

switch (query.Type)
{
Expand All @@ -265,12 +265,20 @@ private static Func<QueryContainerDescriptor<TResult>, QueryContainer> CompareCo
case ConditionTypes.NotEqual:
return (container) => !container.Match(f => f.Field(keyword).Query(value?.ToString()));
case ConditionTypes.Great:
if (isDate)
return (container) => container.DateRange(f => f.Field(keyword).GreaterThan((DateTime)value));
return (container) => container.Range(f => f.Field(keyword).GreaterThan(Convert.ToDouble(value)));
case ConditionTypes.Less:
if (isDate)
return (container) => container.DateRange(f => f.Field(keyword).LessThan((DateTime)value));
return (container) => container.Range(f => f.Field(keyword).LessThan(Convert.ToDouble(value)));
case ConditionTypes.GreatEqual:
if (isDate)
return (container) => container.DateRange(f => f.Field(keyword).GreaterThanOrEquals((DateTime)value));
return (container) => container.Range(f => f.Field(keyword).GreaterThanOrEquals(Convert.ToDouble(value)));
case ConditionTypes.LessEqual:
if (isDate)
return (container) => container.DateRange(f => f.Field(keyword).LessThanOrEquals((DateTime)value));
return (container) => container.Range(f => f.Field(keyword).LessThanOrEquals(Convert.ToDouble(value)));
case ConditionTypes.In:
return (container) => container.Terms(f => f.Field(keyword).Terms((IEnumerable<object>)value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceExtenistion
{
internal static ILogger Logger { private set; get; }

public static IServiceCollection AddElasticClientLog(this IServiceCollection services, string[] nodes, string indexName)
{
ElasticConstant.InitLog(indexName, true);
Expand Down Expand Up @@ -72,6 +74,7 @@ public static IServiceCollection AddElasticClientLogAndTrace(this IServiceCollec

private static IServiceCollection AddElasticsearch(IServiceCollection services, string[] nodes, string name)
{
Logger = services.BuildServiceProvider().GetRequiredService<ILogger<LogService>>();
return services.AddElasticsearch(name, options =>
{
options.UseNodes(nodes).UseConnectionSettings(setting => setting.EnableApiVersioningHeader(false));
Expand All @@ -91,7 +94,7 @@ private static IServiceCollection AddElasticsearch(IServiceCollection services,
Action<MasaHttpClient> callerAction, string name)
{
ArgumentNullException.ThrowIfNull(callerAction);

Logger = services.BuildServiceProvider().GetRequiredService<ILogger<LogService>>();
return services.AddElasticsearch(name, elasticsearchConnectionAction)
.AddCaller(name, option => option.UseHttpClient(callerAction).UseAuthentication());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
global using Masa.Utils.Data.Elasticsearch;
global using Masa.Utils.Models;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using Nest;
global using System.Runtime.CompilerServices;
global using System.Text.Json;
Expand Down

0 comments on commit d16c475

Please sign in to comment.