Home > OS >  How to convert my DSL aggregation query to NEST aggregation elastic client query?
How to convert my DSL aggregation query to NEST aggregation elastic client query?

Time:08-09

Here is a DSL query I wrote from the front side; however, since we need to update the arch, we must query from the 'c#' side.

{
  "query": {
    "query_string": {
      "fields": [
        "siteId"
      ],
      "query": "SE0*"
    }
  },
  "aggs": {
    "total": {
      "cardinality": {
        "field": "linkId.keyword"
      }
    },
    "los_counts": {
      "terms": {
        "field": "linkId.keyword",
        "size": "10000"
      },
      "aggs": {
        "los": {
          "filters": {
            "filters": {
              "los_true": {
                "match": {
                  "losFlag": "Y"
                }
              },
              "los_false": {
                "bool": {
                  "must_not": {
                    "match": {
                      "losFlag": "Y"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "collapse": {
    "field": "linkId.keyword"
  }
}

for the aforementioned DSL client side's payload query, to which I tested the following NEST elastic client 'c#' query.

    await _elasticClient.SearchAsync<T>(s => s
                                 .Index(indexName)
                                 .Take(10000)
                                 .Query(q => (q
                                          .MultiMatch(m => m
                                          .Fields(["siteId"])
                                          .Query("SE0")
                                           )))
                                 .Aggregations(ag => ag
                                    .Cardinality("total", ca => ca.Field("linkId.keyword"))
                                    .Terms("los_counts",tr=>tr.Field("linkId.keyword")
                                    .Aggregations(agg=>agg.Filters("los",fi=>fi.)) 
                                    /*  */
                                    )
                                    )
    
                                 .Collapse(col => col.Field("linkId.keyword"))
                                 ).ConfigureAwait(false);

I was caught at the comment area and was unable to access the "filters" inside of the "filters" once again. then, how do we convert here?

CodePudding user response:

Try this Filters Aggregation

.Aggregations(agg => agg.Filters("projects_by_state",
                     agg => agg.NamedFilters(filters => filters                                                                                  
                                           .Filter("los_true", f => f.Match(p =>p.Field("losFlag").Query("Y")))                                                                                  
                                           .Filter("los_false", f =>f.Bool(p=>p.MustNot(b=>b.Match(p => p.Field("losFlag").Query("Y")))))
                                                                         )
                                                                    )
                                              )
  • Related