我必须在同一个查询字符串的多个字段中搜索关键字。
"bool": {
"should": [
{
"match": {
"ABC": "Apple"
}
},
{
"match": {
"XYZ": "Apple"
}
}
]
}
当我编写查询DSL时,它已被翻译为多匹配查询(不确定上述代码和DSL是否相同)
.Bool(b => b
.Should(sh => sh
.MultiMatch(c => c
.Fields(f => f.Field(p => p.ABC).Field("XYZ"))
.Query(keyz)))))
同样我想写一个DSL查询,但我想做match_phrase操作。有人可以帮我解决这个问题。
TIA
答案 0 :(得分:2)
给定文档类型
public class Document
{
public string ABC { get; set; }
public string XYZ { get; set; }
}
这将是
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var defaultIndex = "default-index";
var connectionSettings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex)
.DefaultFieldNameInferrer(p => p);
var client = new ElasticClient(connectionSettings);
var keyz = "Apple";
client.Search<Document>(s => s
.Query(q => q
.Bool(b => b
.Should(sh => sh
.Match(c => c
.Field(p => p.ABC)
.Query(keyz)
),
sh => sh
.Match(c => c
.Field(p => p.XYZ)
.Query(keyz)
)
)
)
)
);
您可以通过 taking advantage of operator overloading
缩短此时间client.Search<Document>(s => s
.Query(q => q
.Match(c => c
.Field(p => p.ABC)
.Query(keyz)
)
|| q.Match(c => c
.Field(p => p.XYZ)
.Query(keyz)
)
)
);
两者都产生
{
"query": {
"bool": {
"should": [
{
"match": {
"ABC": {
"query": "Apple"
}
}
},
{
"match": {
"XYZ": {
"query": "Apple"
}
}
}
]
}
}
}