public void UsingStaticPropertyField() { Field fieldString = "name"; /** but for expressions this is still rather involved */ var fieldExpression = Infer.Field <Project>(p => p.Name); /** this can be even shortened even further using a static import. * Now that is much terser then our first example using the constructor! */ fieldExpression = Field <Project>(p => p.Name); Expect("name") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); /** You can specify boosts in the field using a string, as well as using `Nest.Infer.Field` */ fieldString = "name^2.1"; fieldString.Boost.Should().Be(2.1); fieldExpression = Field <Project>(p => p.Name, 2.1); fieldExpression.Boost.Should().Be(2.1); Expect("name^2.1") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); }
public Tuple <string, List <BoardGame> > QueryData_MinPlayTime(string query) { _elasticClient.Flush("boardgames"); var queryContainer = new QueryContainer(); queryContainer &= new MatchAllQuery(); var aggregation = new MinAggregation("min_playing_time_aggregation", Infer.Field <BoardGame>(p => p.PlayingTime)); var searchRequest = new SearchRequest("boardgames", typeof(BoardGame)) { Query = queryContainer, Aggregations = aggregation }; var searchResponse = _elasticClient.Search <BoardGame>(searchRequest); var minPlayingTime = searchResponse.Aggregations.Min("min_playing_time_aggregation"); return(new Tuple <string, List <BoardGame> >( searchResponse.DebugInformation, searchResponse.Documents.ToList() )); }
protected RollupSearchRequest RollupSearchInitializer(string index) => new RollupSearchRequest(CreateRollupSearchIndices(index)) { Size = 0, Query = new MatchAllQuery(), Aggregations = new MaxAggregation("max_temp", Infer.Field <Log>(p => p.Temperature)) && new AverageAggregation("avg_temp", Infer.Field <Log>(p => p.Temperature)) };
protected CreateRollupJobRequest CreateInitializer(string role) => new CreateRollupJobRequest(CreateRollupName(role)) { PageSize = 1000, IndexPattern = TimeSeriesSeeder.IndicesWildCard, RollupIndex = CreateRollupName(role), Cron = CronPeriod, Groups = new RollupGroupings { DateHistogram = new DateHistogramRollupGrouping { Field = Infer.Field <Log>(p => p.Timestamp), Interval = TimeSpan.FromHours(1) }, Terms = new TermsRollupGrouping { Fields = Infer.Field <Log>(p => p.Section).And <Log>(p => p.Tag) }, Histogram = new HistogramRollupGrouping { Fields = Infer.Field <Log>(p => p.Load).And <Log>(p => p.NetIn).And <Log>(p => p.NetOut), Interval = 5 } }, Metrics = new List <IRollupFieldMetric> { new RollupFieldMetric { Field = Infer.Field <Log>(p => p.Temperature), Metrics = new[] { RollupMetric.Average, RollupMetric.Min, RollupMetric.Max } }, } };
public SearchResult <Post> SearchByCategory(string query, IEnumerable <string> tags, int page = 1, int pageSize = 10) { var fieldQuery = new MultiMatchQuery { Query = query, Fields = new[] { Infer.Field <Post>(p => p.Title), Infer.Field <Post>(p => p.Body), Infer.Field <Post>(p => p.Tags) }, Fuzziness = Fuzziness.EditDistance(1) }; var categoryFilter = new BoolQuery { Must = new QueryContainer[] { new TermsQuery { Field = Infer.Field <Post>(p => p.Tags), Terms = tags } } }; var searchRequest = new SearchRequest { From = page - 1, Size = pageSize, Query = new BoolQuery { Must = new QueryContainer[] { fieldQuery }, Filter = new QueryContainer[] { categoryFilter } }, Aggregations = new TermsAggregation("by_tags") { Field = Infer.Field <Post>(p => p.Tags), Size = 10 } }; var result = _client.Search <Post>(searchRequest); return(new SearchResult <Post> { Total = (int)result.Total, Page = page, Results = result.Hits.Select(hit => new SearchResultItem <Post>(hit.Source, hit.Score)), ElapsedMilliseconds = result.Took, AggregationsByTags = result.Aggs.Terms("by_tags").Buckets.ToDictionary(x => x.Key, y => y.DocCount.GetValueOrDefault(0)) }); }
public void UsingStaticPropertyField() { Field fieldString = "name"; /** but for expressions this is still rather involved */ var fieldExpression = Infer.Field <Project>(p => p.Name); /** Using static imports in c# 6 this can be even shortened: * using static Nest.Static; */ fieldExpression = Field <Project>(p => p.Name); /** Now this is much much terser then our first example using the constructor! */ Expect("name") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); fieldString = "name^2.1"; fieldString.Boost.Should().Be(2.1); fieldExpression = Field <Project>(p => p.Name, 2.1); /** Now this is much much terser then our first example using the constructor! */ Expect("name^2.1") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); }
private static void ExcludeUnwantedFields(SearchRequest <ElasticArchiveRecord> searchRequest) { // Exclude content from primarydata. searchRequest.Source = new SourceFilter { Excludes = Infer.Fields("all", "primaryData.items.content") }; }
public IEnumerable <string> Autocomplete(string query) { var searchRequest = new SearchRequest { Suggest = new SuggestContainer { { "tag-suggestions", new SuggestBucket { Prefix = query, Completion = new CompletionSuggester { Field = Infer.Field <Post>(p => p.Suggest), Size = 1 } } } } }; var result = _client.Search <Post>(searchRequest); return(result.Suggest["tag-suggestions"].SelectMany(x => x.Options).Select(y => y.Text)); }
[U] public async Task Urls() { var project = new Project { Name = "NEST" }; var routing = Infer.Route(project); await POST("/project/_explain/NEST?routing=NEST") .Fluent(c => c.Explain <Project>(project, e => e.Routing(routing).Query(q => q.MatchAll()))) .Request(c => c.Explain <Project>(new ExplainRequest <Project>(project) { Routing = routing })) .FluentAsync(c => c.ExplainAsync <Project>(project, e => e.Routing(routing).Query(q => q.MatchAll()))) .RequestAsync(c => c.ExplainAsync <Project>(new ExplainRequest <Project>(project) { Routing = routing })) ; await POST("/project/_explain/NEST") .Fluent(c => c.Explain <Project>("NEST", e => e.Query(q => q.MatchAll()))) .Request(c => c.Explain <Project>(new ExplainRequest <Project>("project", "NEST"))) .FluentAsync(c => c.ExplainAsync <Project>("NEST", e => e.Query(q => q.MatchAll()))) .RequestAsync(c => c.ExplainAsync <Project>(new ExplainRequest <Project>("NEST"))) ; }
private static void LoadMovieGroups(IElasticClient elasticClient) { var searchRequest = new SearchRequest <MovieSearchItem> { Size = 0, Aggregations = new TermsAggregation("by_tags") { Field = Infer.Field <MovieSearchItem>(m => m.Tags) } && new TermsAggregation("by_release_year") { Field = Infer.Field <MovieSearchItem>(m => m.ReleaseYear) } }; Console.WriteLine("Query to run:"); Console.WriteLine(GetQuery(elasticClient, searchRequest)); var searchResult = elasticClient.Search <MovieSearchItem>(searchRequest); var tagsAgg = searchResult.Aggs.Terms("by_tags"); var yearsAgg = searchResult.Aggs.Terms("by_release_year"); Console.WriteLine("tags:"); foreach (var tagsBucket in tagsAgg.Buckets) { Console.WriteLine($"{tagsBucket.Key}-{tagsBucket.DocCount}"); } Console.WriteLine("tags:"); foreach (var yearsBucket in yearsAgg.Buckets) { Console.WriteLine($"{yearsBucket.Key}-{yearsBucket.DocCount}"); } }
public async Task <DeliveryStore> GetDeliveryStoreByFSA(string fsa) { var searchRequest = new SearchRequest <DeliveryStore>() { Query = new MatchQuery { Field = Infer.Field <DeliveryStore>(o => o.FSA), Query = fsa } }; var searchResponse = await _elasticClient.SearchAsync <DeliveryStore>(searchRequest); if (searchResponse.Documents.Count > 0) { var storeResponse = searchResponse.Documents.FirstOrDefault(); DeliveryStore ds = new() { Id = storeResponse.Id, FSA = storeResponse.FSA, StoreNumber = storeResponse.StoreNumber, DeliveryVendorId = storeResponse.DeliveryVendorId, DeliveryVendorName = storeResponse.DeliveryVendorName, DeliveryFeePLU = storeResponse.DeliveryFeePLU, DeliveryFeePromo = storeResponse.DeliveryFeePromo, ClientCode = storeResponse.ClientCode }; return(ds); } else { return(null); } }
public EventStackFilterQueryBuilder(IStackRepository stackRepository, ILoggerFactory loggerFactory) { _stackRepository = stackRepository; _logger = loggerFactory.CreateLogger <EventStackFilterQueryBuilder>(); _inferredEventDateField = Infer.Field <PersistentEvent>(f => f.Date); _inferredStackLastOccurrenceField = Infer.Field <Stack>(f => f.LastOccurrence); }
public void UsingStaticPropertyField() { Field fieldString = "name"; /** but for expressions this is still rather involved */ var fieldExpression = Infer.Field <Project>(p => p.Name); /** this can be even shortened even further using a https://msdn.microsoft.com/en-us/library/sf0df423.aspx#Anchor_0[static import in C# 6] i.e. * `using static Nest.Infer;` */ fieldExpression = Field <Project>(p => p.Name); /** Now that is much terser then our first example using the constructor! */ Expect("name") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); /** You can specify boosts in the field using a string */ fieldString = "name^2.1"; fieldString.Boost.Should().Be(2.1); /** As well as using `Nest.Infer.Field` */ fieldExpression = Field <Project>(p => p.Name, 2.1); Expect("name^2.1") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); }
[U] public void TypedEq() { Field t1 = Infer.Field <Project>(p => p.Name), t2 = Infer.Field <Project>(p => p.Name); (t1 == t2).ShouldBeTrue(t2); t1.Should().Be(t2); }
[U] public void TermsQuery() => Serializes(new TermsQuery { Field = Infer.Field <Project>(p => p.Name), Terms = new object[] { SomeEnum.AnotherValue } }, new { name = new[] { 1 } }, new { name = new[] { "different" } } );
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values) { foreach (var index in values.Values) { this.Client.CreateIndex(index, c => c .Settings(settings => settings .Analysis(Seeder.ProjectAnalysisSettings) ) .Mappings(m => m .Map <Project>(mm => mm.AutoMap() .Properties(Seeder.ProjectProperties) ) .Map <PercolatedQuery>(mm => mm.AutoMap() .Properties(Seeder.PercolatedQueryProperties) ) ) ); this.Client.Index(new PercolatedQuery { Id = PercolatorId, Query = new QueryContainer(new MatchQuery { Field = Infer.Field <Project>(f => f.LeadDeveloper.FirstName), Query = "Martijn" }) }, d => d.Index(index)); this.Client.Refresh(index); } }
[U] public void TypedNotEq() { Field t1 = Infer.Field <Developer>(p => p.Id), t2 = Infer.Field <CommitActivity>(p => p.Id); (t1 != t2).ShouldBeTrue(t2); t1.Should().NotBe(t2); }
public void StoredFields() { /** * When storing fields in this manner, the individual field values to return can be specified using * `.StoredFields` on the search request */ var searchResponse = _client.Search <Project>(s => s .StoredFields(sf => sf .Fields( f => f.Name, f => f.StartedOn, f => f.Branches ) ) .Query(q => q .MatchAll() ) ); /** * And retrieving them is possible using `.Fields` on the response */ foreach (var fieldValues in searchResponse.Fields) { var document = new // <1> Construct a partial document as an anonymous type from the stored fields requested { Name = fieldValues.ValueOf <Project, string>(p => p.Name), StartedOn = fieldValues.Value <DateTime>(Infer.Field <Project>(p => p.StartedOn)), Branches = fieldValues.Values <Project, string>(p => p.Branches.First()) }; } }
public Tuple <string, List <BoardGame> > QueryData_Buckets(string query) { _elasticClient.Flush("boardgames"); var queryContainer = new QueryContainer(); queryContainer &= new MatchAllQuery(); var aggregation = new TermsAggregation("category_aggregation") { Field = Infer.Field <BoardGame>(p => p.Category), }; var searchRequest = new SearchRequest("boardgames", typeof(BoardGame)) { Query = queryContainer, Aggregations = aggregation }; var searchResponse = _elasticClient.Search <BoardGame>(searchRequest); var categories = searchResponse.Aggregations.Terms("category_aggregation").Buckets; return(new Tuple <string, List <BoardGame> >( searchResponse.DebugInformation, searchResponse.Documents.ToList() )); }
public async Task <IPagedResponse <NewsReadModel> > SearchAsync([FromQuery] NewsSearchQuery searchQuery, CancellationToken cancellationToken) { IPagedResponse <NewsReadModel> response = null; await _elasticClient.Indices.FlushAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken); await _elasticClient.Indices.RefreshAsync(indexName, d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken); SearchRequest searchRequest = new SearchRequest <NewsReadModel>(indexName) { Query = new BoolQuery() { Should = new QueryContainer[] { new MatchQuery() { Field = Infer.Field <NewsReadModel>(f => f.Title), Query = searchQuery.Title, Boost = 1.1 }, new MatchQuery() { Field = Infer.Field <NewsReadModel>(f => f.SubTitle), Query = searchQuery.SubTitle, } }, Filter = new QueryContainer[] { new TermQuery() { Field = Infer.Field <NewsReadModel>(f => f.NCategory), Value = searchQuery.Category, }, new TermQuery() { Field = Infer.Field <NewsReadModel>(f => f.Country), Value = searchQuery.Country, }, new MatchQuery() { Field = Infer.Field <NewsReadModel>(f => f.PortalName), Query = searchQuery.Portal, } } } }; var record = await _elasticClient.SearchAsync <NewsReadModel>(searchRequest, cancellationToken); if (record?.Documents?.Count > 0) { response = new PagedResponse <NewsReadModel>() { Records = record.Documents.ToList(), TotalCount = record.Total }; } return(response); }
public const string ParamTake = "size"; // Do not change (ELS reserved) public static void Rebuild() { var request = new SearchRequest { Query = new FunctionScoreQuery { Functions = GetQueryFunctions(), ScoreMode = FunctionScoreMode.Multiply, Query = new BoolQuery { Filter = GetQueryFilters(), Must = new List <QueryContainer> { new MultiMatchQuery { Type = TextQueryType.MostFields, MinimumShouldMatch = MinimumShouldMatch.Percentage(80), Query = $"{{{{{ParamQuery}}}}}", Fields = GetQueryFields() } }, MustNot = new List <QueryContainer> { // Exclude unpublished documents new DateRangeQuery { LessThanOrEqualTo = DateMath.Now, Field = Infer.Field <ElasticEpiDocument>(e => e.StopPublish) }, // Excluded types new MatchQuery { Field = Infer.Field <ElasticEpiDocument>(e => e.TypeName), Query = $"{{{{{ParamExcludedTypeNames}}}}}", ZeroTermsQuery = ZeroTermsQuery.None }, // Excluded ids new MatchQuery { Field = Infer.Field <ElasticEpiDocument>(e => e.ContentGuid), Query = $"{{{{{ParamExcludedIds}}}}}", ZeroTermsQuery = ZeroTermsQuery.None } } } } }; var requestString = ElasticEpiClient.Current.Get().Serializer.SerializeToString(request); var jsonObject = JObject.Parse(requestString); jsonObject.Add(ParamSkip, "{{from}}{{^from}}0{{/from}}"); jsonObject.Add(ParamTake, "{{size}}{{^size}}10{{/size}}"); requestString = ElasticEpiClient.Current.Get().Serializer.SerializeToString(jsonObject); ElasticEpiClient.Current.Get() .PutSearchTemplate(new PutSearchTemplateDescriptor(Name).Template(requestString)); }
[U] public void TypeName() { TypeName fromString = "index-1"; var fromType = Infer.Type <Project>(); DebugFor(fromString).Should().Be("index-1"); DebugFor(fromType).Should().Be("TypeName for typeof: Project"); }
protected override void ConfigureQueryParser(ElasticQueryParserConfiguration config) { string dateFixedFieldName = Configuration.Client.Infer.PropertyName(Infer.Property <Stack>(f => f.DateFixed)); config .SetDefaultFields(new[] { "id", Alias.Title, Alias.Description, Alias.Tags, Alias.References }) .AddVisitor(new StackDateFixedQueryVisitor(dateFixedFieldName)); }
[U] public void FieldsDebug() { Nest.Fields fromString = "field1, field2"; Nest.Fields multiple = Infer.Field("somefield").And <Project>(p => p.Description, 12); DebugFor(fromString).Should().Be($"Count: 2 [(1: field1),(2: field2)]"); DebugFor(multiple).Should().Be($"Count: 2 [(1: somefield),(2: p => p.Description^12 typeof: {nameof(Project)})]"); }
public static void Check(IElasticClient client, string index) { var indices = Infer.Indices(index); var response = client.Indices.Refresh(indices); var search = client.Search <Base>(c => c.Size(1).Index(index)); Console.WriteLine($"The index has {search.Total} logs"); Console.WriteLine($"Last indexed message: {search.Documents.Last().Message}"); }
private Func <QueryContainerDescriptor <BusinessElasticModel>, QueryContainer> TimeRange() { Func <QueryContainerDescriptor <BusinessElasticModel>, QueryContainer> timeFilter = null; var now = DateTime.Now; var time = now.TimeOfDay; Field openingHourDay = null; Field closingHourDay = null; switch (now.DayOfWeek) { case DayOfWeek.Monday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourMonday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourMonday); break; case DayOfWeek.Tuesday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourTuesday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourTuesday); break; case DayOfWeek.Wednesday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourWednesday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourWednesday); break; case DayOfWeek.Thursday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourThursday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourThursday); break; case DayOfWeek.Friday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourFriday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourFriday); break; case DayOfWeek.Saturday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourSaturday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourSaturday); break; case DayOfWeek.Sunday: openingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.OpeningHourSunday); closingHourDay = Infer.Field <BusinessElasticModel>(p => p.OpeningHours.ClosingHourSunday); break; } if (openingHourDay != null && closingHourDay != null) { timeFilter = fq => (fq.LongRange(d => d.Field(openingHourDay).LessThanOrEquals(time.Ticks)) && fq.LongRange(d => d.Field(closingHourDay).GreaterThanOrEquals(time.Ticks))) || (fq.Terms(t => t.Field(f => f.OpeningHours.AlwaysOpen).Terms(true)) || fq.Terms(t => t.Field(f => f.OpeningHours.HideOpeningHours).Terms(true))); } return(timeFilter); }
public void ReversSearchingTest() { Console.WriteLine($"Try create index:'{this.Index}'"); CreatePercolatorIndex(this.Index); Console.WriteLine($"Index created:'{this.IndexExists(this.Index)}'"); Console.WriteLine($"Try add to index document"); var result = this.AddToIndex(new OfferQuery { Id = "1", Query = new MatchQuery { Field = Infer.Field <Employee>(entry => entry.Skills), Query = "skill" } }, this.Index); if (!result.IsValid) { Console.WriteLine($"Error: {result.DebugInformation}"); } Console.WriteLine($"Try search"); var search = Search(new Employee { Skills = new List <string> { "skill" } }); if (!search.IsValid) { Console.WriteLine($"Error: {result.DebugInformation}"); } foreach (var doc in search.Documents) { Console.WriteLine($"Doc Id: {doc.Id}"); } Console.WriteLine($"Try search"); search = Search(new Employee { Skills = new List <string> { "skin" } }); if (!search.IsValid) { Console.WriteLine($"Error: {result.DebugInformation}"); } foreach (var doc in search.Documents) { Console.WriteLine($"Doc Id: {doc.Id}"); } Console.WriteLine($"Try remove index:'{this.Index}'"); this.RemoveIndex(this.Index); Console.WriteLine($"Index removed:'{!this.IndexExists(this.Index)}'"); }
public EventStackFilterQueryBuilder(IStackRepository stackRepository, ICacheClient cacheClient, ILoggerFactory loggerFactory) { _stackRepository = stackRepository; _cacheClient = new ScopedCacheClient(cacheClient, "stack-filter"); _logger = loggerFactory.CreateLogger <EventStackFilterQueryBuilder>(); _inferredEventDateField = Infer.Field <PersistentEvent>(f => f.Date); _inferredStackLastOccurrenceField = Infer.Field <Stack>(f => f.LastOccurrence); _eventStackFilter = new EventStackFilter(); }
[U] public void PropertyName() { var fieldFromString = new PropertyName("name"); var fieldFromExpressionWithBoost = Infer.Property <Project>(p => p.Name); PropertyName propertyInfoField = typeof(Project).GetProperty(nameof(Project.Name)); DebugFor(fieldFromString).Should().Be("name"); DebugFor(fieldFromExpressionWithBoost).Should().Be("p => p.Name typeof: Project"); DebugFor(propertyInfoField).Should().Be("PropertyInfo: Name typeof: Project"); }
[U] public void Field() { var fieldFromString = new Field("name^2"); var fieldFromExpressionWithBoost = Infer.Field <Project>(p => p.Name, 14); var propertyInfoField = Infer.Field(typeof(Project).GetProperty(nameof(Project.Name))); DebugFor(fieldFromString).Should().Be("name^2"); DebugFor(fieldFromExpressionWithBoost).Should().Be("p => p.Name^14 typeof: Project"); DebugFor(propertyInfoField).Should().Be("PropertyInfo: Name typeof: Project"); }