I'm using C# with those nuget packeges;
<package id="Elasticsearch.Net" version="5.2.0" targetFramework="net462" />
<package id="NEST" version="5.2.0" targetFramework="net462" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net462" />
What I want to do here, I want to get "white" items in price range 2000 - 3000. It's a simple request for a search api, am I right ?
So I wrote a code for this. Here it is;
private static void Search(IElasticContext elasticContext, string indexName)
{
IQueryContainer termQueryContainer = new QueryContainer();
termQueryContainer.Term = new TermQuery
{
Field = new Field("description"),
Value = "white"
};
IQueryContainer rangeQueryContainer = new QueryContainer();
rangeQueryContainer.Range = new NumericRangeQuery
{
Field = new Field("price"),
LessThanOrEqualTo = 3000,
GreaterThanOrEqualTo = 2000
};
//Should get 2 items.
SearchRequest<Product> searchRequest = new SearchRequest<Product>(indexName, typeof(Product))
{
Size = 10,
From = 0,
Query = (QueryContainer) rangeQueryContainer,
PostFilter = (QueryContainer) termQueryContainer
};
EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);
Console.WriteLine(response.StatusMessage);
if (response.IsValid)
{
foreach (Product product in response.Documents)
{
Console.WriteLine("Id: {0} | Name: {1}", product.Id, product.Name);
}
}
}
But it doesn't work because request has been successfull but there is no document(s) in the result, but I have. I can see the docs with Sense plugin.
If I combine two queries, nest will throw exception in runtime ( Says: "QueryContainer can only hold a single query already contains a TermQuery" ). Here it is;
Also, I can't use fluent api, because I pass the parameters to my repository-like function;
EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);
How can I combine two simple queries ( search in description field & price range between 2000-3000 ) in SearchRequest of Nest dll. And what am I doing wrong?