使用嵌套按多个值搜索(elasticsearch)

ddarikpa  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(2)|浏览(300)

我有一个叫做“活动”的索引,里面有这些记录:

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

tuwxkamq1#

你试过这个吗?

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)
                        )));
5jdjgkvh

5jdjgkvh2#

我不知道这个词的语法 nest 但由于es是基于rest的,所以提供了json格式的工作示例查询,您可以将其转换为 nest 代码。
索引Map

{
    "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 匹配并引用es中的bool查询以了解查询构造。

相关问题