elasticsearch嵌套初始化的termsquery对象不允许整数列表

bvuwiixz  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(386)

我想使用nest及其对象初始值设定项语法在elasticsearch上构建一个动态查询。我想把一个表示位置id的整数列表传递给一个termsquery对象,我将用它来构建boolquery,这个boolquery将传递给searchrequest。termsquery对象接收一个没有任何问题的字符串列表,但是当涉及整数时,它返回一个强制转换问题。代码波纹管

//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"));

错误为“无法将类型“system.collections.generic.list”隐式转换为“system.collections.generic.ienumerable”。存在显式转换(是否缺少强制转换?)

0mkxixxg

0mkxixxg1#

Terms 是一个 IEnumerable<object> 所以你得把盒子装起来 intsInt32 ,与 String ,是值类型:

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

为什么我不能在.NET4.0中将list分配给ienumerable

相关问题