我使用C#與的NuGet packeges;彈性搜索5.x中嵌套多個查詢C#
<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" />
我想在這裏做什麼,我想在價格區間2000「白色」項目 - 3000這是針對搜索API一個簡單的請求,對嗎?
所以我寫了一個代碼這一點。這裏是;
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);
}
}
}
但它不工作,因爲請求已經全成但在結果沒有文件(S),但我有。我可以用Sense插件查看文檔。
如果合併兩個查詢,鳥巢將扔在運行時異常(表示:「QueryContainer只能容納一個查詢中已經包含了TermQuery」)。這裏是;
而且,我無法用流利的API,因爲我傳遞參數給我的倉庫般的功能;
EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);
我如何(在描述字段2000-3000之間&價格範圍搜索)在鳥巢的dll SearchRequest結合兩個簡單的查詢。我做錯了什麼?