0

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

"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 ”的广告系列以及“ Approved,Rejected ”之间的任何状态。像这样的东西:

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

2 回答 2

1

我不知道 ES 的语法,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_matchwhich 强制至少一个状态Rejected,并Approved匹配和引用ES中的 bool 查询以理解查询构造。

于 2020-09-19T13:08:29.477 回答
0

你试过这个吗?

            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)
                        )));
于 2020-09-19T18:55:57.547 回答