gpt4 book ai didi

c# - Match 和 Match_phrase 在具有相同查询字符串的多个字段上 - Elastic Search(Nest)

转载 作者:行者123 更新时间:2023-11-30 23:25:18 25 4
gpt4 key购买 nike

我必须在多个字段上搜索同一个查询字符串的关键字。

"bool": {
"should": [
{
"match": {
"ABC": "Apple"
}
},
{
"match": {
"XYZ": "Apple"
}
}
]
}

我写查询DSL的时候,已经翻译成multimatch query(不知道上面的代码和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

最佳答案

给定一个文档类型

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"
}
}
}
]
}
}
}

关于c# - Match 和 Match_phrase 在具有相同查询字符串的多个字段上 - Elastic Search(Nest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37381390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com