我正在尝试将下面的弹性搜索DSL转换为NEST查询,我正在使用elasticsearch的5.2版
GET articles/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "FIY",
"fields": [
"title"
]
}
},
{
"nested": {
"path": "tags",
"query": {
"terms": {
"tags.tagName": [
"competition"
]
}
}
}
}
]
}
}
}
到目前为止,我已经得到了以下内容,我知道过滤器部分不应该在那里,但是如果没有它,我似乎无法添加嵌套部分
var result = client.Search<Article>(x => x
.Query(q => q
.Bool(b => b
.Must(m => m
.MultiMatch(mp => mp
.Query(query)
.Fields(f => f
.Fields(f1 => f1.Title, f2 => f2.Content, f3 => f3.Tags))))
.Filter(f => f
.Nested(n => n
.Path("tags")
.Query(q1 => q1
.Terms(t1 => t1.Field(f2 => f2.Tags).Terms(tags))
))))));
不需要使用 Filter。只需将嵌套添加到必须查询中即可
var result = client.Search<Article>(x => x
.Query(q => q
.Bool(b => b
.Must(m => m
.MultiMatch(mp => mp
.Query(query)
.Fields(f => f
.Fields(f1 => f1.Title, f2 => f2.Content, f3 => f3.Tags)),
m=> m.Nested(n => n
.Path("tags")
.Query(q1 => q1
.Terms(t1 => t1.Field(f2 => f2.Tags).Terms(tags))
)))));