使用 Nest 进行弹性搜索查询,搜索文本,还可以按标志过滤
Elastic search query with Nest, search for text, but also filtering by flags
我有以下查询:
return Q
.MultiMatch(Fu => Fu
.Fields(F => F
.Field(Ff => Ff.Tags)
.Field(Ff => Ff.Title)
)
.Query(Terms)
.Fuzziness(Fuzziness.EditDistance(2))
);
如何向将执行的查询添加次要部分:
"If the document has NO 'score' field OR if 'score' <= 1"
我不知道如何使用 API 添加此部分。
假设 POCO 类似
public class MyDocument
{
public string[] Tags { get; set; }
public string Title { get; set; }
public int Score { get; set; }
}
可以使用overloaded operators on queries构造复合bool查询,满足需求
var client = new ElasticClient(settings);
var terms = "foo bar baz";
var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);
这导致查询
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}
这是更简洁的查询形式
var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);
我有以下查询:
return Q
.MultiMatch(Fu => Fu
.Fields(F => F
.Field(Ff => Ff.Tags)
.Field(Ff => Ff.Title)
)
.Query(Terms)
.Fuzziness(Fuzziness.EditDistance(2))
);
如何向将执行的查询添加次要部分:
"If the document has NO 'score' field OR if 'score' <= 1"
我不知道如何使用 API 添加此部分。
假设 POCO 类似
public class MyDocument
{
public string[] Tags { get; set; }
public string Title { get; set; }
public int Score { get; set; }
}
可以使用overloaded operators on queries构造复合bool查询,满足需求
var client = new ElasticClient(settings);
var terms = "foo bar baz";
var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);
这导致查询
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}
这是更简洁的查询形式
var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);