我需要动态创建查询 . 我在散列表中有一个带有权重的术语列表,这些术语的数量会有所不同 . 我想在一定数量的文件内容中搜索这些术语,并根据其重量来提升每个单词 . 类似于下面的代码:
var searchResults = client.Search<Document>(s => s.Index(defaultIndex)
.Query(q => q.Match(m => m.OnField(p => p.File).Query(term1).Boost(term1.weight).Query(term2).Query(term2.weight)...Query(term N ).Boost(termN.weight))))
我找到的唯一解决方案是使用链接http://nest.azurewebsites.net/nest/writing-queries.html中的示例"Raw String"
.QueryRaw(@"{""match_all"": {} }")
.FilterRaw(@"{""match_all"": {} }")
由于我不知道每次有多少条款,我该如何处理这样的问题呢?有没有人知道另一种解决方案而不是“原始字符串”?我正在使用C#Nest Elasticsearch .
以下是针对此类案例的JSON Elasticsearch查询示例:
GET testindex2/document/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"file": {
"query": "kit",
"boost": 3
}
}
},
{
"match": {
"file": {
"query": "motor",
"boost": 2.05
}
}
},
{
"match": {
"file": {
"query": "fuel",
"boost": 1.35
}
}
}
]
}
}}
1 回答
您需要创建
Bool Should
查询并传递可以动态生成的QueryContainer
对象数组 . 我编写了一个小代码片段,它将根据您的要求构建Nest查询 . 只需更新字典boostValues
,你应该好好去 .