弹性搜索 5.x 嵌套多个查询 C#
Elastic Search 5.x Nest Multiple Queries C#
我将 C# 与那些 nuget 包一起使用;
<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" />
我想在这里做什么,我想获得 "white" 价格范围为 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);
}
}
}
但它不起作用,因为请求已成功但结果中没有文档,但我有。我可以使用 Sense 插件查看文档。
如果我组合两个查询,nest 将在运行时抛出异常(说:"QueryContainer can only hold a single query already contains a TermQuery")。这里是;
此外,我不能使用 fluent api,因为我将参数传递给了类似存储库的函数;
EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);
如何在 Nest dll 的 SearchRequest 中合并两个简单的查询(在描述字段中搜索和价格范围在 2000-3000 之间)。我做错了什么?
您要做的是从两个查询中形成一个复合查询,其中两个查询都必须由文档满足才能被视为匹配项。 A bool
query is used to combine queries in this manner,使用must
子句指定两个查询都必须满足。这是一个示例,使用对象初始化语法
var client = new ElasticClient();
var indexName = "index-name";
var mustClauses = new List<QueryContainer>();
mustClauses.Add(new TermQuery
{
Field = new Field("description"),
Value = "white"
});
mustClauses.Add(new NumericRangeQuery
{
Field = new Field("price"),
LessThanOrEqualTo = 3000,
GreaterThanOrEqualTo = 2000
});
var searchRequest = new SearchRequest<Product>(indexName)
{
Size = 10,
From = 0,
Query = new BoolQuery { Must = mustClauses }
};
var searchResponse = client.Search<Product>(searchRequest);
对于 range
查询,文档与查询子句匹配或不匹配,因此我们可以通过将其添加为 bool
查询来放弃为查询计算的分数 filter
子句
var indexName = "index-name";
var mustClauses = new List<QueryContainer>();
var filterClauses = new List<QueryContainer>();
mustClauses.Add(new TermQuery
{
Field = new Field("description"),
Value = "white"
});
filterClauses.Add(new NumericRangeQuery
{
Field = new Field("price"),
LessThanOrEqualTo = 3000,
GreaterThanOrEqualTo = 2000
});
var searchRequest = new SearchRequest<Product>(indexName)
{
Size = 10,
From = 0,
Query = new BoolQuery
{
Must = mustClauses,
Filter = filterClauses
}
};
var searchResponse = client.Search<Product>(searchRequest);
我将 C# 与那些 nuget 包一起使用;
<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" />
我想在这里做什么,我想获得 "white" 价格范围为 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);
}
}
}
但它不起作用,因为请求已成功但结果中没有文档,但我有。我可以使用 Sense 插件查看文档。
如果我组合两个查询,nest 将在运行时抛出异常(说:"QueryContainer can only hold a single query already contains a TermQuery")。这里是;
此外,我不能使用 fluent api,因为我将参数传递给了类似存储库的函数;
EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);
如何在 Nest dll 的 SearchRequest 中合并两个简单的查询(在描述字段中搜索和价格范围在 2000-3000 之间)。我做错了什么?
您要做的是从两个查询中形成一个复合查询,其中两个查询都必须由文档满足才能被视为匹配项。 A bool
query is used to combine queries in this manner,使用must
子句指定两个查询都必须满足。这是一个示例,使用对象初始化语法
var client = new ElasticClient();
var indexName = "index-name";
var mustClauses = new List<QueryContainer>();
mustClauses.Add(new TermQuery
{
Field = new Field("description"),
Value = "white"
});
mustClauses.Add(new NumericRangeQuery
{
Field = new Field("price"),
LessThanOrEqualTo = 3000,
GreaterThanOrEqualTo = 2000
});
var searchRequest = new SearchRequest<Product>(indexName)
{
Size = 10,
From = 0,
Query = new BoolQuery { Must = mustClauses }
};
var searchResponse = client.Search<Product>(searchRequest);
对于 range
查询,文档与查询子句匹配或不匹配,因此我们可以通过将其添加为 bool
查询来放弃为查询计算的分数 filter
子句
var indexName = "index-name";
var mustClauses = new List<QueryContainer>();
var filterClauses = new List<QueryContainer>();
mustClauses.Add(new TermQuery
{
Field = new Field("description"),
Value = "white"
});
filterClauses.Add(new NumericRangeQuery
{
Field = new Field("price"),
LessThanOrEqualTo = 3000,
GreaterThanOrEqualTo = 2000
});
var searchRequest = new SearchRequest<Product>(indexName)
{
Size = 10,
From = 0,
Query = new BoolQuery
{
Must = mustClauses,
Filter = filterClauses
}
};
var searchResponse = client.Search<Product>(searchRequest);