如何使用嵌套布尔创建布尔过滤器查询应使用NEST OIS查询

时间:2019-05-10 13:39:09

标签: elasticsearch nest

我无法使用NEST创建此查询:

{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {"term": {"status": 0}},
        {"term": {"freeRound": false }},
        {"term": {"playerId": 189149 }},
        {
          "range": {
            "eventTime": {
              "format": "strict_date_optional_time",
              "gte": "2019-05-02T00:00:00",
              "lte": "2019-05-02T23:59:59"
            }
          }
        },
        {
          "bool": {
            "should": [
              {"term": {"extGameId": 527 }},
              {"term": {"extGameId": 804 }},
              {"term": {"extGameId": 9503 }}
            ],
            "minimum_should_match": 1
          }
        },
        {"term": {"transactionType": 3 }}
      ]
    }
  },
  "aggs": {
    "byGames": {
      "terms": {"field": "extGameId" }
    },
    "sumAgg": {
        "sum": {"field": "amount"}
    }
  }
}

可以看出,我的查询使用的是布尔过滤器,因为我不需要评分。里面有TermQueryDateRange部分,它们应该满足(状态,freeRound,playerId和Date范围),然后是子布尔值,要求必须满足extGamesId之一。

现在这是我使用NEST尝试实现的代码:

//playerId, gameList, startDate and endDate are passed as a parameters

var filters = new List<QueryContainer>();

filters.Add(new TermQuery { Field = new Field("status"), Value = "0" });//status=0
filters.Add(new TermQuery {Field = new Field("freeRound"), Value = false});//freeRound = false
filters.Add(new TermQuery { Field = new Field("playerId"), Value = playerId });//playerId
filters.Add(endDate != null ? 
    new DateRangeQuery
    {
        Field = new Field("eventTime"),
        GreaterThanOrEqualTo = startDate,
        LessThanOrEqualTo = endDate
    } : 
    new DateRangeQuery
    {
        Field = new Field("eventTime"),
        GreaterThanOrEqualTo = startDate
    });

if (gameList.Count > 0)
{
    var shouldFilter = new List<QueryContainer>();
    gameList.ForEach(g => shouldFilter.Add(new TermQuery { Field = new Field("extGameId"), Value = g }));//extGameIds

    filters.Add(new BoolQuery { Should = shouldFilter, MinimumShouldMatch = 1 });
}

filters.Add(new TermQuery { Field = new Field("transactionType"), Value = 3 });//transactionType

var searchRequest = new SearchRequest<SomethingHere>()
{
    Query = new BoolQuery
    {
        Filter = filters
    },
    Aggregations = new SumAggregation("sum_agg", "amount")

};

var responsesResult = _client.Search<SomethingHere>(searchRequest);

但是,我得到“输入字符串的格式不正确”。错误。 请帮助我了解我在这里做错了什么。预先感谢。

编辑: 以下是相关的映射:

  "amount": {
    "type": "double"
  },
  "eventTime": {
    "type": "date"
  },
  "extGameId": {
    "type": "keyword"
  },
  "freeRound": {
    "type": "boolean"
  },
  "playerId": {
    "type": "long"
  },
  "status": {
    "type": "keyword"
  }
  "transactionType": {
    "type": "short"
  }

这是POCO:

[Number(NumberType.Double)]
public decimal Amount { get; set; }
[Date]
public DateTime eventTime { get; set; }
[Keyword]
public string ExtGameId { get; set; }
[Boolean]
public decimal FreeRound { get; set; }
[Number(NumberType.Long)]
public long PlayerId { get; set; }
[Keyword]
public string Status { get; set; }
[Number(NumberType.Short)]
public int TransactionType { get; set; }

0 个答案:

没有答案