Reputation: 17418
I wonder, if someone could be so kind and translate the following raw elastic search query into NEST? Thanks!
var json = @"
{
""from"": 0,
""size"": 50,
""query"": {
""constant_score"": {
""filter"": {
""bool"": {
""must"": [
{
""range"": {
""attachment.content_length"": {
""gt"": 0,
""lte"": 500
}
}
},
{
""term"": {
""ext"": {
""value"": ""pdf""
}
}
},
{
""term"": {
""check"": {
""value"": false
}
}
}
]
}
}
}
}
}
";
Upvotes: 0
Views: 74
Reputation: 9979
var searchResponse = await client.SearchAsync<object>(s => s
.From(0)
.Size(50)
.Query(q => q.ConstantScore(cs => cs
.Filter(f => f.Bool(b => b.Must(
m => m.Range(r => r.Field("attachment.content_length").GreaterThan(0).LessThanOrEquals(500)),
m => m.Term(t => t.Field("ext").Value("pdf")),
m => m.Term(t => t.Field("check").Value(false))
))))));
or slightly more strongly typed version
var searchResponse = await client.SearchAsync<Document>(s => s
.From(0)
.Size(50)
.Query(q => q.ConstantScore(cs => cs
.Filter(f => f.Bool(b => b.Must(
m => m.Range(r => r.Field(field => field.Attachment.ContentLength).GreaterThan(0).LessThanOrEquals(500)),
m => m.Term(t => t.Field(field => field.Ext).Value("pdf")),
m => m.Term(t => t.Field(field => field.Check).Value(false))
))))));
public class Document
{
public int Id { get; set; }
public Nest.Attachment Attachment { get; set; }
public string Ext { get; set; }
public bool Check { get; set; }
}
both producing the following query
{
"from": 0,
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [{
"range": {
"attachment.content_length": {
"gt": 0.0,
"lte": 500.0
}
}
}, {
"term": {
"ext": {
"value": "pdf"
}
}
}, {
"term": {
"check": {
"value": false
}
}
}
]
}
}
}
},
"size": 50
}
Hope that helps.
Upvotes: 1