如果您正在执行上面的大多数字段 multi_match 查询,您可以通过将多个匹配查询组合在一起来实现您想要的
var indexName = "foo";
var search = "search";
var searchResponse client.Search<JobModelSummary>(s => s
.Index(indexName)
.Query(q => q
.Bool(b => b
.Should(sh => sh
.Match(m => m
.Query(search)
.Fuzziness(Fuzziness.EditDistance(1))
.Field(ff => ff.Name.Suffix("standard"))
.Boost(3)
), sh => sh
.Match(m => m
.Query(search)
.Fuzziness(Fuzziness.EditDistance(1))
.Field(ff => ff.Name.Suffix("edge-ngram"))
.Boost(2)
), sh => sh
.Match(m => m
.Query(search)
.Field(ff => ff.Name.Suffix("phonetic"))
)
)
)
)
);
使用重载运算符进行查询可以使其更加简洁
client.Search<JobModelSummary>(s => s
.Index(indexName)
.Query(q => q
.Match(m => m
.Query(search)
.Fuzziness(Fuzziness.EditDistance(1))
.Field(ff => ff.Name.Suffix("standard"))
.Boost(3)
) || q
.Match(m => m
.Query(search)
.Fuzziness(Fuzziness.EditDistance(1))
.Field(ff => ff.Name.Suffix("edge-ngram"))
.Boost(2)
) || q
.Match(m => m
.Query(search)
.Field(ff => ff.Name.Suffix("phonetic"))
)
)
);
两者都产生
POST http://localhost:9200/foo/jobmodelsummary/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name.standard": {
"boost": 3.0,
"query": "search",
"fuzziness": 1
}
}
},
{
"match": {
"name.edge-ngram": {
"boost": 2.0,
"query": "search",
"fuzziness": 1
}
}
},
{
"match": {
"name.phonetic": {
"query": "search"
}
}
}
]
}
}
}
should 子句中每个查询的分数将加在一起以提供_score
每个文档的最终分数。仅指定 should 子句时,至少必须匹配一个文档才能被视为匹配。