【问题标题】:Elasticsearch nest initialized TermsQuery object doesn't allow a list of integersElasticsearch 嵌套初始化的 TermsQuery 对象不允许整数列表
【发布时间】:2020-12-03 15:11:21
【问题描述】:

我想使用嵌套及其对象初始化器语法在 Elasticsearch 上构建一个动态查询。我想将表示位置 ID 的整数列表传递给一个 TermsQuery 对象,我将使用它来构建将传递给 SearchRequest 的 BoolQuery。 TermsQUery 对象接收一个字符串列表,没有任何问题,但是当涉及到 integers 时,它会返回一个转换问题。代码如下

        //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'。存在显式转换(您是否缺少演员表?) "

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    Terms 是一个IEnumerable&lt;object&gt;,因此您需要将ints 装箱,因为Int32String 不同,是一个值类型:

    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

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2018-11-06
      • 1970-01-01
      • 2017-02-27
      • 2020-04-14
      • 1970-01-01
      相关资源
      最近更新 更多