Adin Sijamija
Adin Sijamija

Reputation: 734

Elasticsearch nest initialized TermsQuery object doesn't allow a list of integers

I want to build a dynamic query on Elasticsearch using nest and their object initializer syntax. I want to pass a list of integers that represent location Ids to a TermsQuery object I will use to build the BoolQuery that will be passed to a SearchRequest. The TermsQUery object receives a list of string without any issues, however it returns a casting issue when it comes to integers . Code bellow

        //term locations DOESNT WORK
        List<int> locationsTest = new List<int>();
        locationsTest.Add(1);
        locationsTest.Add(2);
        locationsTest.Add(3);
        TermsQuery locationstTerm = new TermsQuery()
        {
            Name = "Locations",
            Boost = 1.1,
            Field = "LocationId",
            Terms = locationsTest
        };

        //terms Aggregation type WORKS FINE
        List<string> types = new List<string> { "ParkedBy", "CheckedInBy", "RetrivedBy" };
        TermsQuery aggregationsTerm = new TermsQuery()
        {
            Name = "AggregatorType_Query",
            Boost = 1.1,
            Field = "AggregatorType",
            Terms = types
        };
        queryContainers.Add(aggregationsTerm);
        

        BoolQuery boolQuery = new BoolQuery()
        {
            Filter=queryContainers

        };

        var searchRequest = new SearchRequest();
        searchRequest.SearchType = SearchType.QueryThenFetch;
        searchRequest.From = 0;
        searchRequest.Size = DEFAULT_SCROLL_SIZE;
        searchRequest.Query = boolQuery;


        var searchResponse = Get().SearchAsync<List<AggregationHolder>>(new SearchRequest("0___aggregate"));

The error is " Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) "

Upvotes: 1

Views: 550

Answers (1)

mm8
mm8

Reputation: 169260

Terms is an IEnumerable<object> so you need to box the ints since Int32, unlike String, is a value type:

TermsQuery locationstTerm = new TermsQuery()
{
    Name = "Locations",
    Boost = 1.1,
    Field = "LocationId",
    Terms = locationsTest.Select(x => (object)x).ToArray()
};

Why can't I assign List<int> to IEnumerable<object> in .NET 4.0

Upvotes: 2

Related Questions