【发布时间】:2015-07-31 23:07:57
【问题描述】:
我需要动态创建查询。我在哈希表中有一个术语列表及其权重,术语的数量会有所不同。我想在大量文档的内容中搜索这些术语,并根据其权重提升每个单词。类似于下面的代码:
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 中的示例
.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
}
}
}
]
}
} }
【问题讨论】:
-
您能否提及预期的 Elasticsearch JSON 查询?
-
当然,我添加了它。请查看我的问题中的编辑。
-
我发布了您问题的答案。如果它解决了您的问题,请将其标记为答案:)
-
非常感谢 bittusarkar 的回答。
标签: c# elasticsearch nest