I have an ElasticSearch server that I want to query using the C# NEST library.
Here is the command I am trying to generate
GET /records/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"available": true
}
},
{
"match": {
"out_scope": false
}
}
]
}
}
}
Here is what I have done, I can't seem to find the right way to specify the field name and the value to search for.
var products = await client.SearchAsync<Product>(x =>
x.Query(q =>
q.Bool(b =>
b.Should(bs1 =>
bs1.Match(bsm =>
bsm.Field(fff => fff.IsAvailable)
.Term(isAvailable.Value) // This does not work!
),
bs2 =>
bs2.Match(bsm =>
bsm.Field(fff => fff.IsAvailable)
.Term(isAvailable.Value) // This does not work!
)
)
);
How can I correctly specify the field's name and the value to match?
Assuming a model like
public class Product
{
[Keyword(Name = "available")]
public bool? IsAvailable { get; set; }
[Keyword(Name = "out_scope")]
public bool? OutScope { get; set; }
}
Then it would be
var searchResponse = await client.SearchAsync<Product>(x => x
.Query(q =>
q.Bool(b =>
b.Should(bs1 =>
bs1.Match(bsm =>
bsm.Field(fff => fff.IsAvailable)
.Query(isAvailable.Value ? "true" : "false")
),
bs2 =>
bs2.Match(bsm =>
bsm.Field(fff => fff.OutScope)
.Query(isAvailable.Value ? "true" : "false")
)
)
)
)
);
Some things to consider:
.Query
field/method. The JSON DSL example shown uses the short form for expressing the query; NEST always generates the long form.var searchResponse = client.Search<Product>(x => x
.Query(q => q
.Match(bsm => bsm
.Field(fff => fff.IsAvailable)
.Query(isAvailable.Value ? "true" : "false")
) || q
.Match(bsm => bsm
.Field(fff => fff.OutScope)
.Query(isAvailable.Value ? "true" : "false")
)
)
);