Is it possible to pass a list of queries to ElasicSearch using Nest?

Viewed 128

I need to pass complex query to Elasticsearch. In this query there will be a lot of range conditions mostly date range.

Example is below:

query = "module : ('mod45') AND code : ('667', '456') AND startTime:[2020-10-18T13:42:17 TO 2021-10-18T13:42:17] AND endTime:[2021-02-18T13:42:17 TO 2021-09-18T13:42:17]"

(module, code, startTime and endTime it's a columns name)

And I have the following Elasticsearch function:

public async Task<IEnumerable<T>> GetQueryStringData<T>(string index, string query) where T : class
{
    var client = GetClient();

    var response = await client.SearchAsync<T>(s => s
        .Index(index)
        .Query(x => x.QueryString(a => a.Query(query))).Size(DefaultResultsSize));

    return response?.Documents;
}

Is it possible to change this function that it could recieve a list of query conditions without "AND" connector?

Something like this:

var queryList = new List<string>();

queryList.Add("module : ('mod45')");
queryList.Add("code : ('667', '456')");
queryList.Add("startTime:[2020-10-18T13:42:17 TO 2021-10-18T13:42:17]");
queryList.Add("endTime:[2021-02-18T13:42:17 TO 2021-09-18T13:42:17]");

EDIT: Let's say I will have 100 queries like this, so lambda isn't works here

1 Answers

You can try using the NEST’s Fluent API which uses lambda expressions to construct a query.

var moduleList = new List<string>();
moduleList.Add("mod45");
var codeList = new List<string>();
codeList.Add("667");
codeList.Add("456");

var response = await client.SearchAsync<T>(s => s
        .Index(index)
        .Query(q => q
                .Bool(b => b
                                .Must(
                                    m => m
                                        .Terms(t => t.Field("module").Terms(moduleList)),
                                    m => m
                                        .Terms(t => t.Field("code").Terms(codeList)),
                                    m => m.Bool(bs => bs
                                            .Must(ms => ms.TermRange(r => r.Field(f => f.startTime)
                                                  .GreaterThanOrEquals(new DateTime(2020, 10, 18))
                                                  .LessThanOrEquals(new DateTime(2021, 10, 18)))
                                            )
                                        ),
                                    m => m.Bool(bs => bs
                                            .Must(ms => ms.TermRange(r => r.Field(f => f.endTime)
                                                  .GreaterThanOrEquals(new DateTime(2021, 02, 18))
                                                  .LessThanOrEquals(new DateTime(2021, 09, 18)))
                                            )
                                        )
                                )
                            )
                        )    
       .DefaultOperator(Operator.And)
       .Size(DefaultResultsSize)); 
Related