嵌套查询等效于以下 ElasticSearch 查询
Nest query equivalent for following ElasticSearch Query
我是 dot net core 的新手。我正在使用 https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html 作为我的弹性搜索客户端。
我有以下用弹性搜索编写的查询:
{
"query": {
"bool":{
"must":[
{
"term":{
"bookId.keyword":"c962ec04-6d25-4823-8e78-6d99d4072032"
}
},
{
"bool":{
"should":[
{
"match_phrase":{
"text":"Plants"
}
}
]
}
}
]
}
}
}
而我的 NEST 代码是:
var searchResponse = await _esClient.SearchAsync<Content>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Term(terms => terms.Field(
field => field.BookId.Suffix("keyword")
).Value(bookId.ToString())
)
)
.Must(should =>
should.Match(mp => mp.Field(f => f.Text).Query(query))
)
)
));
但是我无法获得预期的结果。谁能帮我解决这个问题?
您的 NEST 查询与 DSL 查询不完全匹配。请修改您的查询以匹配以下内容。
var searchResponse = await _esClient.SearchAsync<Content>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Term(terms => terms
.Field(field => field
.BookId.Suffix("keyword"))
.Value(bookId.ToString())
) && m.Bool(mb => mb
.Should(sh => sh
.MatchPhrase(mp => mp
.Field(f => f.Text).Query(query))))
)
)
));
您几乎明白了。 Must()
接受 params[]
次查询,而不是多次调用它
var searchResponse = await _esClient.Search<Content>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Term(terms => terms.Field(
field => field.BookId.Suffix("keyword")
).Value(bookId.ToString())
), m => m
.Bool(mb => mb
.Should(sh => sh
.MatchPhrase(mp => mp
.Field(f => f.Text)
.Query(query)
)
)
)
)
)
)
);
是使用运算符重载编写此查询的更简洁的方式。
我是 dot net core 的新手。我正在使用 https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html 作为我的弹性搜索客户端。
我有以下用弹性搜索编写的查询:
{
"query": {
"bool":{
"must":[
{
"term":{
"bookId.keyword":"c962ec04-6d25-4823-8e78-6d99d4072032"
}
},
{
"bool":{
"should":[
{
"match_phrase":{
"text":"Plants"
}
}
]
}
}
]
}
}
}
而我的 NEST 代码是:
var searchResponse = await _esClient.SearchAsync<Content>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Term(terms => terms.Field(
field => field.BookId.Suffix("keyword")
).Value(bookId.ToString())
)
)
.Must(should =>
should.Match(mp => mp.Field(f => f.Text).Query(query))
)
)
));
但是我无法获得预期的结果。谁能帮我解决这个问题?
您的 NEST 查询与 DSL 查询不完全匹配。请修改您的查询以匹配以下内容。
var searchResponse = await _esClient.SearchAsync<Content>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Term(terms => terms
.Field(field => field
.BookId.Suffix("keyword"))
.Value(bookId.ToString())
) && m.Bool(mb => mb
.Should(sh => sh
.MatchPhrase(mp => mp
.Field(f => f.Text).Query(query))))
)
)
));
您几乎明白了。 Must()
接受 params[]
次查询,而不是多次调用它
var searchResponse = await _esClient.Search<Content>(s => s
.Query(q => q
.Bool(b => b
.Must(m => m
.Term(terms => terms.Field(
field => field.BookId.Suffix("keyword")
).Value(bookId.ToString())
), m => m
.Bool(mb => mb
.Should(sh => sh
.MatchPhrase(mp => mp
.Field(f => f.Text)
.Query(query)
)
)
)
)
)
)
);