How to use aggregation with query?

Hello,

I hope someone can help me.
How do I use aggregation (date range) with query?

I want to be able to add date range to my query with the option of missing value and a condition (greater than).

Thank you in advance.

There is an example in the documentation. What is exactly your problem?

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

I'm currently using query with function score, I want to be able to search for documents with date field greater than the current date. This could be accomplished with the DateRange() in a MUST clause.. however, not all documents have a value in this field, so the ones with the missing values need to have another value.

I read that with aggregation that's possible, but is it possible to use both (query and aggregation) as they are on the same level?

here's my code (I removed some of the parts so it becomes more simple)

     var response = elasticClient.Search<Document>(s => s
            .From(offset)
            .Size(size)
            .Index(name)
            .Sort(so => sortByDate
                ? so.Descending(a => a.Date).Field(f => f.Field("_score").Order(SortOrder.Descending))
                : so.Field(f => f.Field("_score").Order(SortOrder.Descending)))
			 .Aggregations(a => a
					.DateRange("expiry_date", d => d
						.Field(p => p.ExpiryDate)
						.Ranges(
							r => r.From("now")
						)
					).Missing(DateTime.MaxValue.ToString(), x => x.Field(f => f.ExpiryDate))
			)
            .Query(q => q
                .FunctionScore(fs => fs.Query(qy => qy
                .Bool(b => b
                    .Must(m => m
                        .Term(tm => tm.Field(fd => fd.Client)
                            .Value(subscription.Code)
                        )
                        ,
                        m => m.
                        Terms(tm => tm.Field(fd => fd.Info)
                                .Terms(categories)
                        )
                    )
                            
                )
            )

            .ScoreMode(FunctionScoreMode.Sum)
            .BoostMode(FunctionBoostMode.Multiply)
        )
        )
        .MinScore(settings.MinAllowedScore)
        );

Please read the link I shared about how to share a full example.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.