Convert DSL Query to NEST.net


(Tarki) #1

How can I translate the below query, so it is matching Nest.net

GET /consolidated-capacity/_search
{
"query": {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [

             { "term" : { "Week.keyword": "1712" } },
             { "term" : { "CountAsFailure.keyword": "TRUE" } },
             { "term" : { "Weekday.keyword": "1" } }

             
                ]
            }
        }
    }
}

}


(Russ Cam) #2

What have you tried so far, maybe I can help with where you're having trouble. Did you see the NEST docs on constant score query?


(Tarki) #3

I tried something like this. But coul dnot figure out how to add three terms and passing the value

var response =
elasticClient.Count(
s =>
s.Index("consolidated-capacity")
.Type("StageCapacity")
.Query(
q =>
q.ConstantScore(
f =>
f.Filter(
b =>
b.Bool(
m =>
m.Must(
t1 =>
t1.Term(
c => c
.Field(p => p
.Week))))))));


(Russ Cam) #4

You're close. the Bool() Must() takes a params Func<QueryContainerDescriptor<T>, QueryContainer>[] so can accept multiple lambda expressions

client.Search<StageCapacity>(s => s
    .Index("consolidated-capacity")
    .Type("StageCapacity")
    .Query(q => q
        .ConstantScore(cs => cs
            .Filter(f => f
                .Bool(b => b
                    .Must(
                        m => m.Term("Week.keyword", "1712"),
                        m => m.Term("CountAsFailure.keyword", "TRUE"),
                        m => m.Term("Week.keyword", "1")
                    )
                )
            )
        )
    )
);

or the shorthand

client.Search<StageCapacity>(s => s
    .Index("consolidated-capacity")
    .Type("StageCapacity")
    .Query(q => q
        .ConstantScore(cs => cs
            .Filter(f => f
                .Term("Week.keyword", "1712") && f
                .Term("CountAsFailure.keyword", "TRUE") && f
                .Term("Week.keyword", "1")
            )
        )
    )
);

(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.