[英]How do I accurately represent this ElasticSearch query using NEST?
I have a query in ElasticSearch where I'm using filters on a several fields (relatively small data set and we know exactly what values should be in those fields at the time we query). 我在ElasticSearch中有一个查询,我在几个字段上使用过滤器(相对较小的数据集,我们确切地知道在查询时这些字段中应包含哪些值)。 The idea is that we'll perform a full-text query but only after we've filtered on some selections as made by the user.
这个想法是,我们将执行全文查询,但前提是我们已经过滤掉用户做出的某些选择。
I'm putting ElasticSearch behind a WebAPI controller and figured it made sense to use NEST to accomplish the query. 我将ElasticSearch放在WebAPI控制器后面,并认为使用NEST完成查询是有意义的。
We have filters for several fields. 我们有几个领域的过滤器。 Each inner filter is an or filter, but they're together as an AND.
每个内部过滤器都是“或”过滤器,但它们在一起都是“与”号。
In SQL, the pseudo-code equivalent would be select * from table where foo in (1,2,3) AND bar in (4,5,6)
. 在SQL中,伪代码等效项是
select * from table where foo in (1,2,3) AND bar in (4,5,6)
。
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"or": [
{ "term": { "foo": "something" } },
{ "term": { "foo": "somethingElse" } }
]
},
{
"or": [
{ "term": { "bar": "something" } },
{ "term": { "bar": "somethingElse" } }
]
}
]
}
}
}
},
"size": 100
}
This kind of task is quite simple and popular in ES. 这种任务非常简单,在ES中很流行。 You can represent it in NEST like following:
您可以像下面这样在NEST中表示它:
var rs = es.Search<dynamic>(s => s
.Index("your_index").Type("your_type")
.From(0).Size(100)
.Query(q => q
.Filtered(fq => fq
.Filter(ff => ff
.Bool(b => b
.Must(
m1 => m1.Terms("foo", new string[] { "something", "somethingElse" }),
m2 => m2.Terms("bar", new string[] { "something", "somethingElse" })
)
)
)
.Query(qq => qq
.MatchAll()
)
)
)
);
Some notes: 一些注意事项:
foo in ("something", "somethingElse") AND bar in ("something", "somethingElse")
, then query all filtered results ( match_all
). foo in ("something", "somethingElse") AND bar in ("something", "somethingElse")
,然后查询所有过滤后的结果( match_all
)。 You can change match_all
to what you need. match_all
更改为所需的内容。 filtered query
it's for best performance as ES will only need to evaluate scores of documents in query
part (after filtered), not all documents. filtered query
是为了获得最佳性能,因为ES仅需要评估query
部分中的文档分数(过滤后),而不是所有文档。 terms
filter, which more simple and better performance than or
. terms
过滤器,该过滤器比or
更加简单和更好的性能。 Default mode of terms
is OR all input terms, you can refer more in document about available modes (AND, OR, PLAIN, ...). terms
默认模式是“或”所有输入术语,您可以在文档中更多地了解可用模式(“与”,“或”,“普通”等)。 Nest is best choice for .NET in my opinion as it designed for simple & easy to use purposes. 我认为Nest是.NET的最佳选择,因为它是为简单易用的目的而设计的。 I only used lower API if I want to use new features that Nest does not support at that time, or if Nest have bugs in functions I use.
仅当我想使用当时Nest不支持的新功能,或者Nest在使用的功能中存在错误时,才使用较低的API。 You can refer here for a brief NEST tutorial: http://nest.azurewebsites.net/nest/writing-queries.html
您可以在此处参考以获取简短的NEST教程: http : //nest.azurewebsites.net/nest/writing-queries.html
Updated: Building bool filters dynamic: 更新:动态构建布尔过滤器:
var filters = new List<Nest.FilterContainer>();
filters.Add(Nest.Filter<dynamic>.Terms("foo", new string[] { "something", "somethingElse" }));
// ... more filter
then replace .Bool(b => b.Must(...))
with .Bool(b => b.Must(filters.ToArray()))
然后将
.Bool(b => b.Must(...))
替换为.Bool(b => b.Must(...))
.Bool(b => b.Must(filters.ToArray()))
Hope it help 希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.