private async Task <IEnumerable <AuditEventDescriptor> > GetEventsAsync(AuditSearchRequest request) { QueryContainer query = new QueryContainer(); if (!string.IsNullOrWhiteSpace(request.EventType)) { query &= new MatchPhraseQuery { Field = "eventType", Query = request.EventType }; } if (request.StartDate.HasValue || request.EndDate.HasValue) { query &= new DateRangeQuery { GreaterThanOrEqualTo = request.StartDate, LessThanOrEqualTo = request.EndDate }; } var response = await _elasticClient.SearchAsync <AuditEventIntercept>(s => s .Index("demoapi*") .From(request.From) .Size(request.Size) .Query(q => q .Bool(b => b .Filter(f => f .MatchAll() && query)))); return(response.Hits.Select(MapHit)); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { var dateRanges = ctx.Source.GetDateRanges(); if (dateRanges.Count <= 0) { return(Task.CompletedTask); } var resolver = ctx.GetMappingResolver(); foreach (var dateRange in dateRanges.Where(dr => dr.UseDateRange)) { var rangeQuery = new DateRangeQuery { Field = resolver.ResolveFieldName(dateRange.Field) }; if (dateRange.UseStartDate) { rangeQuery.GreaterThanOrEqualTo = dateRange.GetStartDate(); } if (dateRange.UseEndDate) { rangeQuery.LessThanOrEqualTo = dateRange.GetEndDate(); } if (!String.IsNullOrEmpty(dateRange.TimeZone)) { rangeQuery.TimeZone = dateRange.TimeZone; } ctx.Filter &= rangeQuery; } return(Task.CompletedTask); }
public List <T> Search <T>(string url, string search_table_index, DateRangeQuery dq, TermsQuery[] tsqs, int start_index, int each_search_size) where T : class { var node = new Uri($"http://{url}:9200"); var settings = new ConnectionSettings(node).DefaultIndex("default"); settings.DisableDirectStreaming(); var client = new ElasticClient(settings); SearchRequest sr = new SearchRequest($"{search_table_index}*"); sr.From = start_index; sr.Size = each_search_size; var tmpPropertiesAry = typeof(T).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (tsqs != null) { foreach (var tsq in tsqs) { if (tsq != null) { sr.Query &= tsq; } } } sr.Query &= dq; sr.Source = new SourceFilter() { Includes = tmpPropertiesAry, }; var result = client.Search <T>(sr); return(result.Documents.ToList()); }
public void SearchDoc_DateRange(ElasticClient client) { var dateRangeQuery = new DateRangeQuery { Name = "named_query", Boost = 1.1, Field = Infer.Field <Product>(p => p.CreateTime), GreaterThan = new DateTime(2018, 3, 5), LessThan = new DateTime(2018, 9, 18) }; var searchRequest = new SearchRequest <Product>(Indices.Parse("esdemo"), Types.Parse("product")) { From = 0, Size = 10, Query = dateRangeQuery }; var searchResponse = client.Search <Product>(searchRequest); var list = searchResponse.Documents.ToList(); var count = searchResponse.Total; System.Console.WriteLine("DateRangeQuery"); System.Console.WriteLine(list.ToJsonString()); System.Console.WriteLine(count); }
public async Task FindByDateRange_WhenEntersRange_Test() { var startDate = new DateTime(2018, 1, 1); var endDate = new DateTime(2018, 1, 4); var category = CategoryData.CreateCategory(Guid.NewGuid()); var transaction1 = TransactionData.CreateTransaction(Guid.NewGuid(), startDate, category: category); var transaction2 = TransactionData.CreateTransaction(Guid.NewGuid(), startDate.AddDays(1), category: category); var transaction3 = TransactionData.CreateTransaction(Guid.NewGuid(), startDate.AddDays(2), category: category); var transaction4 = TransactionData.CreateTransaction(Guid.NewGuid(), startDate.AddDays(3), category: category); await CommitCreateAsync(category); await CommitCreateAsync(transaction1); await CommitCreateAsync(transaction2); await CommitCreateAsync(transaction3); await CommitCreateAsync(transaction4); var dateRangeQuery = new DateRangeQuery(new DateQuery(startDate), new DateQuery(endDate)); var foundTransactions = await TransactionRepository.FindAsync(new TransactionQuery(dateRangeQuery)); Assert.That(foundTransactions, Is.EquivalentTo(new[] { transaction1, transaction2, transaction3, transaction4 })); }
public QueryContainer GetQuery() { string field = LoadFieldHelper.GetFiledStr <T, TV>(this._expression); if (typeof(TV) == typeof(DateTime)) { var rangQuery = new DateRangeQuery { Field = field, GreaterThanOrEqualTo = this._greaterThanOrEqualTo.HasValue ? DateTime.Parse(this._greaterThanOrEqualTo.Value.ToString()) : default(DateTime?), GreaterThan = this._greaterThan.HasValue ? DateTime.Parse(this._greaterThan.Value.ToString()) : default(DateTime?), LessThan = this._lessThan.HasValue ? DateTime.Parse(this._lessThan.Value.ToString()) : default(DateTime?), LessThanOrEqualTo = this._lessThanOrEqualTo.HasValue ? DateTime.Parse(this._lessThanOrEqualTo.Value.ToString()) : default(DateTime?), TimeZone = "Asia/Shanghai" }; return(rangQuery); } return(new NumericRangeQuery { Field = field, GreaterThanOrEqualTo = this._greaterThanOrEqualTo.HasValue ? double.Parse(this._greaterThanOrEqualTo.Value.ToString()) : default(double?), GreaterThan = this._greaterThan.HasValue ? double.Parse(this._greaterThan.Value.ToString()) : default(double?), LessThan = this._lessThan.HasValue ? double.Parse(this._lessThan.Value.ToString()) : default(double?), LessThanOrEqualTo = this._lessThanOrEqualTo.HasValue ? double.Parse(this._lessThanOrEqualTo.Value.ToString()) : default(double?) }); }
public IEnumerable <HomeEvent> GetEventsWindow(DateTime endDateTime) { var match = new MatchPhraseQuery { Field = new Field("doc"), Query = "event" }; var rangeQuery = new DateRangeQuery { Field = "@timestamp", LessThanOrEqualTo = endDateTime.ToUniversalTime(), GreaterThanOrEqualTo = endDateTime.ToUniversalTime().AddSeconds(-config.EventsWindowsSeconds), }; var boolQuery = new BoolQuery { Must = new List <QueryContainer> { match, rangeQuery } }; var searchRequest = new SearchRequest(Indices.Parse("history-*")) { Query = boolQuery, Sort = new List <ISort>() { new SortField { Field = new Field("@timestamp"), Order = SortOrder.Descending } } }; var result = elastic .Request <ElasticSearchEvent>(searchRequest) .Select(ConvertToLocalDateTime) .ToList(); result.ForEach(r => log.Debug(r.ToString())); return(result.Select(r => new HomeEvent { DateTime = r.timestamp, Id = r.Id, Sensor = r.sensor.display, Status = r.status, SensorType = r.sensor.type, })); }
protected virtual QueryContainer GetCatalogItemQuery <T>(CatalogItemSearchCriteria criteria) where T : class { QueryContainer result = null; if (criteria != null) { result &= new DateRangeQuery { Field = "startdate", LessThanOrEqualTo = criteria.StartDate }; if (criteria.StartDateFrom.HasValue) { result &= new DateRangeQuery { Field = "startdate", GreaterThan = criteria.StartDateFrom.Value }; } if (criteria.EndDate.HasValue) { result &= new DateRangeQuery { Field = "enddate", GreaterThan = criteria.EndDate.Value }; } if (!criteria.WithHidden) { result &= new TermQuery { Field = "status", Value = "visible" } } ; if (criteria.Outlines != null && criteria.Outlines.Count > 0) { result &= CreateQuery("__outline", criteria.Outlines); } if (!string.IsNullOrEmpty(criteria.Catalog)) { result &= CreateQuery("catalog", criteria.Catalog); } if (criteria.ClassTypes != null && criteria.ClassTypes.Count > 0) { result &= CreateQuery("__type", criteria.ClassTypes, false); } } return(result); } }
static void DifferentQuerysInEs(QueryContainer qc) { //匹配查询 qc &= new MatchQuery() { Field = "analyized field name", Query = "关键词", Operator = Operator.Or, MinimumShouldMatch = "100%", Analyzer = "ik_smart" }; //https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax //https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html //query string自定义了一个查询语法 var qsq = new QueryStringQuery() { }; //https://www.elastic.co/guide/cn/elasticsearch/guide/current/_wildcard_and_regexp_queries.html //使用通配符查询,比如name.* var wcq = new WildcardQuery() { }; //精准匹配,不分词 var tq = new TermQuery() { }; //字段存在且不为空 var eq = new ExistsQuery() { }; //https://www.elastic.co/guide/cn/elasticsearch/guide/current/fuzzy-query.html //模糊查询,它会计算关键词和目标字段的“距离”。如果在允许的距离范围内,计算拼写错误也可以匹配到 var fq = new FuzzyQuery() { }; //范围查询 var drq = new DateRangeQuery() { }; var nrq = new NumericRangeQuery() { }; }
public static QueryContainer MakeRangeQuery(string type, string gte, string lte, string field, double boost = 1) { QueryContainer rangeQuery = null; if (type.ToLower() == "long") { rangeQuery = new LongRangeQuery() { Field = field, LessThan = long.Parse(lte), GreaterThan = long.Parse(gte), Boost = boost }; } else if (type.ToLower() == "date") { rangeQuery = new DateRangeQuery() { Field = field, LessThan = DateMath.FromString(lte), GreaterThan = DateMath.FromString(gte), Boost = boost }; } else if (type.ToLower() == "term") { rangeQuery = new TermRangeQuery() { Field = field, LessThan = lte, GreaterThan = gte, Boost = boost }; } else if (type.ToLower() == "numeric") { rangeQuery = new TermRangeQuery() { Field = field, LessThan = lte, GreaterThan = gte, Boost = boost }; } return(rangeQuery); }
/// <summary> /// Count total document by field date range /// </summary> /// <typeparam name="T"></typeparam> /// <param name="field"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="indexName"></param> /// <param name="typeName"></param> /// <returns></returns> public long Count <T>(string field, DateTime from, DateTime to, string indexName, string typeName) where T : class { long total = 0; if (!string.IsNullOrEmpty(indexName)) { indexName = indexName.ToLower(); } if (!string.IsNullOrEmpty(typeName)) { typeName = typeName.ToLower(); } if (!string.IsNullOrEmpty(field)) { field = Char.ToLowerInvariant(field[0]) + field.Substring(1); } var dateRange = new DateRangeQuery() { Field = field }; if (from > DateTime.MinValue) { dateRange.GreaterThanOrEqualTo = from.ToString(FormatDate); } if (to > from) { dateRange.LessThanOrEqualTo = to.ToString(FormatDate); } var indices = Indices.Index(indexName); var types = Types.Type(typeName); var request = new SearchRequest(indices, types) { PostFilter = dateRange, SearchType = SearchType.Count }; ISearchResponse <T> result = _client.Search <T>(request); if (result != null) { total = result.Total; } return(total); }
public QueryContainer CreateQueryForField(SearchField field, UserAccess access) { var startDateKey = "creationPeriod.startDate"; var endDateKey = "creationPeriod.endDate"; var originalQuery = field.Value; var values = originalQuery.Split(new[] { "-" }, StringSplitOptions.None); if (values.Length == 1) { values = new[] { values[0], values[0] }; } if (values.Length != 2) { throw new InvalidOperationException("The creation period must contain zero or one – character"); } var searchFromString = values[0].Trim(); var searchToString = values[1].Trim(); var searchFrom = ConvertFrom(searchFromString); var searchTo = ConvertTo(searchToString); if (searchFrom > searchTo) { searchFrom = ConvertFrom(searchToString); searchTo = ConvertTo(searchFromString); } var startQuery = new DateRangeQuery { Field = startDateKey, Format = "dd.MM.yyyy", LessThanOrEqualTo = searchTo.ToString("dd.MM.yyyy") }; var endQuery = new DateRangeQuery { Field = endDateKey, Format = "dd.MM.yyyy", GreaterThanOrEqualTo = searchFrom.ToString("dd.MM.yyyy") }; return(new BoolQuery { Must = new QueryContainer[] { startQuery, endQuery } }); }
private static IQueryable <Models.Transaction> GetQueryBuilder(IQueryable <Models.Transaction> queryBuilder, DateRangeQuery dateRangeQuery) { if (dateRangeQuery != null) { if (dateRangeQuery.StartDate?.Date != null) { queryBuilder = queryBuilder.Where(t => t.Date >= dateRangeQuery.StartDate.Date.Value); } if (dateRangeQuery.EndDate?.Date != null) { queryBuilder = queryBuilder.Where(t => t.Date <= dateRangeQuery.EndDate.Date.Value); } } return(queryBuilder); }
public List <T> Search <T>(DateRangeQuery dq, TermsQuery[] tsqs, int start_index, int each_search_size) where T : class, new() { T t = new T(); var node = new Uri($"http://{ELASTIC_URL}:9200"); var settings = new ConnectionSettings(node).DefaultIndex("default"); settings.DisableDirectStreaming(); var client = new ElasticClient(settings); var index = GetType().GetFields(BindingFlags.Public | BindingFlags.Static); var TName = t.GetType().Name; var index_tabel = index.Where(x => (x.GetCustomAttribute(typeof(LinkObject), false) as LinkObject).type.Contains(TName)) .FirstOrDefault()?.GetValue(this); SearchRequest sr = new SearchRequest($"{index_tabel}*"); sr.From = start_index; sr.Size = each_search_size; var tmpPropertiesAry = typeof(T).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (tsqs != null) { foreach (var tsq in tsqs) { if (tsq != null) { sr.Query &= tsq; } } } if (dq != null) { sr.Query &= dq; } sr.Source = new SourceFilter() { Includes = tmpPropertiesAry, }; var result = client.Search <T>(sr); return(result.Documents.ToList()); }
public async Task <IActionResult> StructuredDateRangeQuery() { try { var queryContainer = new DateRangeQuery() { Field = "order_date", GreaterThanOrEqualTo = new DateTime(2020, 03, 16), LessThan = DateMath.Now }; IEnumerable <QueryContainer> queryContainers = new List <QueryContainer>() { queryContainer }; var searchRequest = new SearchRequest <CustomerUser>(IndexName) { Query = new BoolQuery() { Filter = queryContainers } }; var queryResponse = await _elasticClient.SearchAsync <CustomerUser>(searchRequest); if (queryResponse.IsValid) { return(Ok(new Entities.SearchResponse() { RecordCount = queryResponse.HitsMetadata.Total.Value, Records = queryResponse.Documents })); } else { return(BadRequest(queryResponse.ServerError.Error)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void Export_Omits_Field_If_Not_Provided() { var start = DateTime.Today; var end = DateTime.Now; var query = new DateRangeQuery() .Start(start) .End(end) .Parser("parser"); var expected = JsonConvert.SerializeObject(new { start = start, inclusive_start = true, end = end, inclusive_end = false, datetime_parser = "parser", }, Formatting.None); Assert.Equal(expected, query.Export().ToString(Formatting.None)); }
private void DeleteWeatherDateRange() { var q = new DateRangeQuery { Name = "named_query", Boost = 1.1, GreaterThanOrEqualTo = DateTime.Now.AddDays(-14), LessThanOrEqualTo = DateTime.Now, Format = "dd/MM/yyyy||yyyy" }; var allDocs = new List <WeatherSummary>(); EsClient.ScanAllDocs <WeatherSummary>(settings.weatherUnderground.IndexName, "timestamp", (doc, id) => { allDocs.Add(new WeatherSummary { Id = id }); }, q); var bulkResponse = EsClient.DeleteMany(allDocs); }
public void Export_Returns_Valid_Json() { var start = DateTime.Today; var end = DateTime.Now; var query = new DateRangeQuery() .Start(start) .End(end) .Field("created_at") .Parser("parser"); var expected = JsonConvert.SerializeObject(new { start = start, inclusive_start = true, end = end, inclusive_end = false, datetime_parser = "parser", field = "created_at" }, Formatting.None); Assert.Equal(expected, query.Export().ToString(Formatting.None)); }
private BoolQuery CreateCommonFilter(CommonSearchRequest searchRequest) { var filteringQuery = new BoolQuery(); var requiredQueryParts = new List <QueryContainer>(); if (searchRequest.ActorIds != null && searchRequest.ActorIds.Any()) { requiredQueryParts.Add(GetOptionalFieldQuery(new TermsQuery() { Field = $"{nameof(MovieSearchItem.Cast)}.{nameof(ActorNestedType.Id)}", Terms = searchRequest.ActorIds.Cast <object>().ToList() })); } if (searchRequest.DateFrom != null || searchRequest.DateTo != null) { var dateRange = new DateRangeQuery() { Field = nameof(MovieSearchItem.Date) }; if (searchRequest.DateFrom != null) { dateRange.GreaterThanOrEqualTo = searchRequest.DateFrom; } if (searchRequest.DateTo != null) { dateRange.LessThanOrEqualTo = searchRequest.DateTo; } requiredQueryParts.Add(GetOptionalFieldQuery(dateRange)); } filteringQuery.Filter = requiredQueryParts; return(filteringQuery); }
private static BoolQuery BuildQuery(LogDataFilter filter) { DateRangeQuery timestampQuery = null; if (filter.StartDate.HasValue && filter.EndDate.HasValue) { timestampQuery = new DateRangeQuery { Field = $"@{nameof(ElasticRawLogRecord.Timestamp).ToLowerCamelCase()}", GreaterThanOrEqualTo = new DateMathExpression(filter.StartDate.Value), LessThanOrEqualTo = new DateMathExpression(filter.EndDate.Value) }; } var query = new BoolQuery(); if (timestampQuery != null) { query.Filter = new QueryContainer[] { timestampQuery }; } return(query); }
/// <summary> /// 搜索日志 /// </summary> /// <param name="highlight"></param> /// <param name="start"></param> /// <param name="end"></param> /// <param name="keyword"></param> /// <param name="logger_name"></param> /// <param name="page"></param> /// <param name="pagesize"></param> /// <returns></returns> public static async Task <PagerData <ESLogLine, QueryExtData> > Search( bool highlight = true, DateTime?start = null, DateTime?end = null, string keyword = null, string logger_name = null, int page = 1, int pagesize = 10) { var sd = new SearchDescriptor <ESLogLine>(); sd = sd.Index(IndexName); var query = new QueryContainer(); if (start != null) { query &= new DateRangeQuery() { Field = nameof(temp.UpdateTime), GreaterThanOrEqualTo = start.Value }; } if (end != null) { query &= new DateRangeQuery() { Field = nameof(temp.UpdateTime), LessThan = end.Value }; } if (ValidateHelper.IsPlumpString(keyword)) { query &= new MatchQuery() { Field = nameof(temp.Message), Query = keyword, Operator = Operator.Or, MinimumShouldMatch = "100%" }; } if (ValidateHelper.IsPlumpString(logger_name)) { query &= new TermQuery() { Field = nameof(temp.LoggerName), Value = logger_name }; } //查询条件 sd = sd.Query(_ => query); //聚合 sd = sd.Aggregations(x => x.Terms(nameof(temp.LoggerName), av => av.Field(nameof(temp.LoggerName)).Size(1000)) .Terms(nameof(temp.Level), av => av.Field(nameof(temp.Level)).Size(1000)) .Terms(nameof(temp.Domain), av => av.Field(nameof(temp.Domain)).Size(1000))); //高亮 if (highlight) { sd = sd.AddHighlightWrapper("<em class='kwd'>", "</em>", x => x.Field(nameof(temp.Message))); } //排序 var sort = new SortDescriptor <ESLogLine>(); sort = sort.Descending(x => x.UpdateTime); sort = sort.Descending(Field.Create("_score", boost: null)); sd = sd.Sort(_ => sort); //分页 sd = sd.QueryPage_(page, pagesize); //请求服务器 var client = new ElasticClient(ElasticsearchClientManager.Instance.DefaultClient); var re = await client.SearchAsync <ESLogLine>(_ => sd); re.ThrowIfException(); var data = new PagerData <ESLogLine, QueryExtData>(); data.ItemCount = (int)re.Total; data.DataList = re.Hits.Select(x => x.Source).ToList(); //聚合数据 data.ExtData = new QueryExtData(); data.ExtData.Ass = re.GetAggs(); data.ExtData.Highlight = re.GetHighlights(); return(data); }
private static Lucene.Net.Search.Query GetDateRangeQuery(DateRangeQuery dateRangeQuery) { var from = dateRangeQuery.From.HasValue ? dateRangeQuery.From.Value : DateTime.MinValue; var to = dateRangeQuery.To.HasValue ? dateRangeQuery.To.Value : DateTime.MaxValue; return new TermRangeQuery(dateRangeQuery.FieldName, from.ToString("yyyyMMddHHmmss"), to.ToString("yyyyMMddHHmmss"), dateRangeQuery.IncludeLower, dateRangeQuery.IncludeUpper); }
public static QueryBase GetDefaultQuery(this TermRangeNode node, IQueryVisitorContext context) { if (!(context is IElasticQueryVisitorContext elasticContext)) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } string field = node.Field; if (elasticContext.MappingResolver.IsDatePropertyType(field)) { var range = new DateRangeQuery { Field = field, TimeZone = node.Boost ?? node.GetTimeZone(elasticContext.DefaultTimeZone) }; if (!String.IsNullOrWhiteSpace(node.UnescapedMin) && node.UnescapedMin != "*") { if (node.MinInclusive.HasValue && !node.MinInclusive.Value) { range.GreaterThan = node.UnescapedMin; } else { range.GreaterThanOrEqualTo = node.UnescapedMin; } } if (!String.IsNullOrWhiteSpace(node.UnescapedMax) && node.UnescapedMax != "*") { if (node.MaxInclusive.HasValue && !node.MaxInclusive.Value) { range.LessThan = node.UnescapedMax; } else { range.LessThanOrEqualTo = node.UnescapedMax; } } return(range); } else { var range = new TermRangeQuery { Field = field }; if (!String.IsNullOrWhiteSpace(node.UnescapedMin) && node.UnescapedMin != "*") { if (node.MinInclusive.HasValue && !node.MinInclusive.Value) { range.GreaterThan = node.UnescapedMin; } else { range.GreaterThanOrEqualTo = node.UnescapedMin; } } if (!String.IsNullOrWhiteSpace(node.UnescapedMax) && node.UnescapedMax != "*") { if (node.MaxInclusive.HasValue && !node.MaxInclusive.Value) { range.LessThan = node.UnescapedMax; } else { range.LessThanOrEqualTo = node.UnescapedMax; } } return(range); } }
// TODO: Refactor so we can use for BetweenQuery as well? private QueryBase GetSingleTermRangeQuery(BinaryNode node, string propertyName) { var fieldName = GetFormattedFieldName(node); // TODO: Don't think we need to format this? //var valueNode = node.GetValueNode<string>(); //var value = ValueFormatter.FormatValueForIndexStorage(valueNode.Value, fieldName); var valueType = node.GetConstantNode().Type; // TODO: This is a bit dodgy, might need testing. var value = node.GetValueNode <object>().Value; // TODO: Use this for type checking instead? Maybe store the types in a list and do DateTimeTypes.Contains(nodeType) //var valueNode = node.GetConstantNode(); // Add method parameter 'nodeType' //var isDate = valueNode.Type == typeof (DateTime) || valueNode.Type == typeof (DateTime?) || // valueNode.Type == typeof (DateTimeOffset) || valueNode.Type == typeof (DateTimeOffset?); // Number double number; if (double.TryParse(value.ToString(), out number)) { var query = new NumericRangeQuery { Field = fieldName, Boost = node.Boost }; return(SetProperty(query, number, propertyName)); } // Date var date = value as DateTime? ?? (value as DateTimeOffset?)?.UtcDateTime; if (date != null) { var query = new DateRangeQuery { Field = fieldName, Boost = node.Boost }; // TODO: We might need to use RoundTo(). Could we maybe specify this on field configuration? return(SetProperty(query, DateMath.Anchored(date.Value), propertyName)); } // String var term = value as string; if (term != null) { var query = new TermRangeQuery { Field = fieldName, Boost = node.Boost }; return(SetProperty(query, term, propertyName)); } // TODO: Handle or throw special exception for null string (when type is string but it is null) throw new NotSupportedException($"The query node type '{valueType}' is not supported in 'greater than' comparisons. Supported types: numeric, DateTime, DateTimeOffset and string"); }
public void Throws_Exception_If_Start_And_End_Are_Not_Provided_When_Export_Is_Called() { var query = new DateRangeQuery(); Assert.Throws <InvalidOperationException>(() => query.Export()); }
internal static QueryContainer SimpleQueryItem(Field field, EsValue query, EnQueryType queryType, double?boost) { switch (queryType) { case EnQueryType.Term: return(new TermQuery { Field = field, IsVerbatim = true, Value = query.Value.ToString(), Boost = boost }); case EnQueryType.Prefix: return(new PrefixQuery { Field = field, Value = query.Value.ToString(), Boost = boost }); case EnQueryType.InWildCard: return(new WildcardQuery { Field = field, Value = string.Format("*{0}*", query.Value.ToString()), Boost = boost }); case EnQueryType.DateRange: { var q = new DateRangeQuery { Field = field, GreaterThan = (DateTime)query.From, Boost = boost }; if (query.To != null) { q.LessThan = (DateTime)query.To; } return(q); } case EnQueryType.GreaterThan: { var q = new NumericRangeQuery { Field = field, GreaterThan = Convert.ToDouble(query.From), Boost = boost }; return(q); } case EnQueryType.LessThan: { var q = new NumericRangeQuery { Field = field, LessThan = Convert.ToDouble(query.From), Boost = boost }; return(q); } case EnQueryType.Range: { var q = new NumericRangeQuery { Field = field, GreaterThan = Convert.ToDouble(query.From), Boost = boost }; if (query.To != null) { q.LessThan = Convert.ToDouble(query.To); } return(q); } case EnQueryType.DatePast: { var q = new DateRangeQuery { Field = field, LessThan = ((DateTime)query.Value), Boost = boost }; return(q); } case EnQueryType.DateFuture: { var q = new DateRangeQuery { Field = field, GreaterThan = (DateTime)query.Value, Boost = boost }; return(q); } case EnQueryType.Distance: { var areaQuery = (LocationArea)query.Value; var q = new GeoDistanceQuery { Field = field, Distance = new Distance(areaQuery.Distance), Location = areaQuery.Center, DistanceType = GeoDistanceType.Plane, Boost = boost }; return(q); } default: return(new MatchQuery { Field = field, Query = query.Value.ToString(), Fuzziness = Fuzziness.Auto, Boost = boost }); } }
public void Boost_WhenBoostIsLessThanZero_ThrowsArgumentOutOfRangeException() { var query = new DateRangeQuery(); Assert.Throws <ArgumentOutOfRangeException>(() => query.Boost(-.1)); }
public void Boost_ReturnsDateRangeQuery() { var query = new DateRangeQuery().Boost(2.2); Assert.IsInstanceOf <DateRangeQuery> (query); }
private void get_glassinterface_data_from_elastic() { try { //var node = new Uri("http://192.168.7.79:9200");//台中 var node = new Uri("http://192.168.39.111:9200");//新竹 var settings = new ConnectionSettings(node).DefaultIndex("default"); var client = new ElasticClient(settings); int size = 10000; SearchRequest sr = new SearchRequest("recodevehiclecstinterface*"); DateTime start_datetime = dtp_start.Value.AddHours(-10); DateTime end_datetime = start_datetime.AddHours(10); DateRangeQuery dq = new DateRangeQuery { Field = "@timestamp", GreaterThan = start_datetime, LessThan = end_datetime, }; TermsQuery tsq = new TermsQuery { Field = new Field("VH_ID.keyword"), Terms = new List <string> { cmb_VH.Text } }; sr.From = 0; sr.Size = size; sr.Query = dq && tsq; //sr.Query = dq; sr.Source = new SourceFilter() { Includes = new string[] { "@timestamp", "VALID", "AM_AVBL", "BUSY", "COMPT", "CONT", "CS_0", "CS_1", "HO_AVBL", "L_REQ", "READY", "TR_REQ", "U_REQ", "VA", "VS_0", "VS_1", "ES", "VH_ID", "PORT_ID" }, //Excludes = new string[] { "CMD_FINISH_TIME" } }; var result = client.Search <recodevehiclecstinterface>(sr); List <recodevehiclecstinterface> resultList = result.Documents.ToList <recodevehiclecstinterface>(); signalList.Clear(); foreach (recodevehiclecstinterface record in resultList) { Signal signal = new Signal(); if (!vehicleList.Contains(record.VH_ID)) { vehicleList.Add(record.VH_ID); } signal.VH_ID = record.VH_ID; signal.PORT_ID = record.PORT_ID; signal.record_time = record.timestamp; signal.L_REQ = record.L_REQ; signal.U_REQ = record.U_REQ; signal.READY = record.READY; signal.VA = record.VA; signal.VS_0 = record.VS_0; signal.VS_1 = record.VS_1; signal.VALID = record.VALID; signal.TR_REQ = record.TR_REQ; signal.BUSY = record.BUSY; signal.COMPT = record.COMPT; signal.AM_AVBL = record.AM_AVBL; signal.HO_AVBL = record.HO_AVBL; signal.ES = record.ES; signalList.Add(signal); } } catch (Exception ex) { logger.Error(ex, "Exception"); } }
/// <summary> /// 构建查询条件 /// </summary> /// <param name="fields"></param> /// <returns>{item1:must,item2:should}</returns> public ESConditionModel BuildQueryContainer(List <ESField> fields, string analyzer) { List <QueryContainer> must = new List <QueryContainer>(), should = new List <QueryContainer>(), mustNot = new List <QueryContainer>(); if (fields != null && fields.Count > 0) { foreach (var item in fields) { QueryContainer container = null; //1.数据类型辨析(暂时不做实现) if (item.DataType == ESDataType.GeoPoint) { //switch (item.QueryType) //{ // case ESQueryType.Geo_Distance: // container = new GeoDistanceQuery() // { // Location = new GeoLocation(0.1, 0.2), // Distance =new Distance(), // }; // break; // case ESQueryType.Geo_Polygon: // container = new GeoPolygonQuery(){ // Points=, // }; // break; // case ESQueryType.Geo_Bounding_Box: // container = new GeoBoundingBoxQuery() // { // BoundingBox = new BoundingBox() // { // BottomRight =, // TopLeft =, // } // }; // break; //} } else { switch (item.QueryType) { case ESQueryType.Match: container = new MatchQuery() { Analyzer = analyzer, Field = item.Key, Query = item.Value.Item1.ToString(), }; break; case ESQueryType.All: container = new MatchAllQuery() { }; break; case ESQueryType.Match_Phrase: container = new MatchPhraseQuery() { Field = item.Key, Analyzer = analyzer, Query = item.Value.Item1.ToString() }; break; case ESQueryType.Match_Phrase_Prefix: container = new MatchPhrasePrefixQuery() { Field = item.Key, Analyzer = analyzer, Query = item.Value.ToString() }; break; case ESQueryType.Term: container = new TermQuery() { Field = item.Key, Value = item.Value.Item1 }; break; case ESQueryType.Range: switch (item.DataType) { case ESDataType.Text: break; case ESDataType.Number: container = new NumericRangeQuery() { Field = item.Key, GreaterThanOrEqualTo = Convert.ToDouble(item.Value.Item1), LessThanOrEqualTo = Convert.ToDouble(item.Value.Item2), }; break; case ESDataType.Date: container = new DateRangeQuery() { Field = item.Key, GreaterThanOrEqualTo = DateMath.FromString(item.Value.Item1.ToString()), LessThanOrEqualTo = DateMath.FromString(item.Value.Item2.ToString()) }; break; default: break; } break; } } //2.条件类型解析 if (container != null) { switch (item.ConditionType) { case ESConditionType.Must: must.Add(container); break; case ESConditionType.Should: should.Add(container); break; case ESConditionType.MustNot: mustNot.Add(container); break; default: break; } } } } return(new ESConditionModel() { Must = must, Should = should, MustNot = mustNot }); }
public static QueryBase GetDefaultQuery(this TermRangeNode node, IQueryVisitorContext context) { if (!(context is IElasticQueryVisitorContext elasticContext)) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } string nodeFullName = node.GetFullName(); if (elasticContext.IsDatePropertyType(nodeFullName)) { string timezone = GetString(context, "TimeZone"); var range = new DateRangeQuery { Field = nodeFullName, TimeZone = timezone }; if (!String.IsNullOrWhiteSpace(node.UnescapedMin)) { if (node.MinInclusive.HasValue && !node.MinInclusive.Value) { range.GreaterThan = node.UnescapedMin; } else { range.GreaterThanOrEqualTo = node.UnescapedMin; } } if (!String.IsNullOrWhiteSpace(node.UnescapedMax)) { if (node.MaxInclusive.HasValue && !node.MaxInclusive.Value) { range.LessThan = node.UnescapedMax; } else { range.LessThanOrEqualTo = node.UnescapedMax; } } return(range); } else { var range = new TermRangeQuery { Field = nodeFullName }; if (!String.IsNullOrWhiteSpace(node.UnescapedMin)) { if (node.MinInclusive.HasValue && !node.MinInclusive.Value) { range.GreaterThan = node.UnescapedMin; } else { range.GreaterThanOrEqualTo = node.UnescapedMin; } } if (!String.IsNullOrWhiteSpace(node.UnescapedMax)) { if (node.MaxInclusive.HasValue && !node.MaxInclusive.Value) { range.LessThan = node.UnescapedMax; } else { range.LessThanOrEqualTo = node.UnescapedMax; } } return(range); } }