使用 NEST(ElasticSearch) 按多个值搜索
Search by multiple values using NEST(ElasticSearch)
我有一个名为“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
的使用,它强制状态 Rejected
和 Approved
中的至少一个匹配并引用 bool query in ES 以了解查询结构。
你试过了吗?
QueryContainer queryAnd = new TermQuery() { Field = "PublisherId", Value = 1 };
QueryContainer queryOr = new TermQuery() { Field = "CurrentStatus", Value = "Approved" };
queryOr |= new TermQuery() { Field = "CurrentStatus", Value = "Rejected" };
QueryContainer queryMain = queryAnd & queryOr;
ISearchResponse<campaigns> searchReponse = elasticClient.Search<campaigns>(s => s
.Query(q2 => q2
.Bool(b => b
.Should(queryMain)
)));
我有一个名为“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
的使用,它强制状态 Rejected
和 Approved
中的至少一个匹配并引用 bool query in ES 以了解查询结构。
你试过了吗?
QueryContainer queryAnd = new TermQuery() { Field = "PublisherId", Value = 1 };
QueryContainer queryOr = new TermQuery() { Field = "CurrentStatus", Value = "Approved" };
queryOr |= new TermQuery() { Field = "CurrentStatus", Value = "Rejected" };
QueryContainer queryMain = queryAnd & queryOr;
ISearchResponse<campaigns> searchReponse = elasticClient.Search<campaigns>(s => s
.Query(q2 => q2
.Bool(b => b
.Should(queryMain)
)));