gpt4 book ai didi

elasticsearch - 使用NEST(ElasticSearch)按多个值搜索

转载 作者:行者123 更新时间:2023-12-03 01:11:59 25 4
gpt4 key购买 nike

我有一个名为“ campaigns ”的索引,其中包含以下记录:

"hits" : [
{
"_index" : "campaigns",
"_id" : "cf08b05c-c8b5-45cb-bca8-17267c3613fb",
"_source" : {
"PublisherId" : 1,
"CurrentStatus" : "Pending"
}
},
{
"_index" : "campaigns",
"_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
"_source" : {
"PublisherId" : 1,
"CurrentStatus" : "Approved"
}
},
{
"_index" : "campaigns",
"_id" : "21436cb1-583e-4fb4-92e4-4e06ecad23a2",
"_source" : {
"PublisherId" : 1,
"CurrentStatus" : "Rejected"
}
}
]
我想获取所有带有“ PublisherId = 1 ”且状态在“ 已批准,已拒绝”之间的广告系列。像这样:
var statuses = new[] {CampaignStatus.Approved,CampaignStatus.Rejected};
campaigns.Where(c=> c.PublisherId == 1 && statuses.Contains(c.CurrentStatus)).ToList();
如何使用NEST运行此查询?
预期结果:
"hits" : [
{
"_index" : "campaigns",
"_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
"_source" : {
"PublisherId" : 1,
"CurrentStatus" : "Approved"
}
},
{
"_index" : "campaigns",
"_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
"_source" : {
"PublisherId" : 1,
"CurrentStatus" : "Rejected"
}
}
]

最佳答案

我不知道nest的语法,但是由于ES是基于REST的,提供了JSON格式的工作示例查询,您可以将其转换为nest代码。
索引映射

{
"mappings": {
"properties": {
"PublisherId": {
"type": "integer"
},
"CurrentStatus": {
"type": "text"
}
}
}
}
索引所有三个示例文档,并在下面的搜索查询中使用
{
"query": {
"bool": {
"must": {
"term": {
"PublisherId": 1
}
},
"should": [
{
"match": {
"CurrentStatus": "Rejected"
}
},
{
"match": {
"CurrentStatus": "Approved"
}
}
],
"minimum_should_match" : 1
}
}
}
搜索结果
"hits": [
{
"_index": "stof_63968525",
"_type": "_doc",
"_id": "1",
"_score": 1.9808291,
"_source": {
"PublisherId": 1,
"CurrentStatus": "Approved"
}
},
{
"_index": "stof_63968525",
"_type": "_doc",
"_id": "3",
"_score": 1.9808291,
"_source": {
"PublisherId": 1,
"CurrentStatus": "Rejected"
}
}
]
请注意 minimum_should_match的使用,它会迫使状态 RejectedApproved中的至少一个匹配并引用 bool query in ES来理解查询构造。

关于elasticsearch - 使用NEST(ElasticSearch)按多个值搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63968525/

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