ES新手在这里(从SolrNet世界过渡,所以我正在寻找具有类似语法的指针)
任何人都可以帮助制定一个看起来像下面的代码片段的查询而不用 lambdas。不幸的是,普通的旧OI语法中只有很少的例子,我被卡住了......
我如何制定一组布尔范围查询,在solrnet中我会做这样的事情来获得属于某个价格范围的项目:
List<ISolrQuery> queryList = new List<ISolrQuery>();
double[] priceList = GetPrices(..) //double array
for (int i = 1; i <= 10; i++)
{
queryList.Add(new solr.Query(new SolrQueryByRange<double>("price", priceList[i] * 0.95, priceList[i] * 1.15));
}
var results = _solr.Query(new SolrMultipleCriteriaQuery(queryList), new QueryOptions
{
Rows = 50,
Fields = new[] { "Item", "Created", "Price" },
});
任何人都可以帮助进行等效的Elasticsearch / Nest查询吗?
答案 0 :(得分:1)
如下所示
void Main()
{
var client = new ElasticClient(connection: new InMemoryConnection());
// prices from your GetPrices method...
var prices = new[] { 1d, 2d };
var searchRequest = new SearchRequest<Document>("index", "document")
{
Fields = new List<Nest.PropertyPathMarker>{ "Item", "Created", "Price" },
Size = 50,
Query = new BoolQuery
{
Should = prices.Select(p => new RangeQuery
{
Field = "price",
GreaterThanOrEqualTo = (p * 0.95).ToString(),
LowerThanOrEqualTo = (p * 1.15).ToString()
}.ToContainer())
}.ToContainer()
};
var result = client.Search<Document>(searchRequest);
Console.WriteLine(Encoding.UTF8.GetString(result.RequestInformation.Request));
}
public class Document
{
public string Item { get; set; }
public DateTime Created { get; set;}
public double Price {get; set;}
}
这会搜索索引"document"
中的"index"
类型(强烈输入您的Document
POCO),使用bool query并根据价格构建范围的条款 - 如果文档的price
字段属于任何一个范围,它将被视为匹配。
上面NEST查询得到的查询DSL是
{
"size": 50,
"fields": [
"Item",
"Created",
"Price"
],
"query": {
"bool": {
"should": [
{
"range": {
"price": {
"gte": "0.95",
"lte": "1.15"
}
}
},
{
"range": {
"price": {
"gte": "1.9",
"lte": "2.3"
}
}
}
]
}
}
}