【问题标题】:Search by multiple values using NEST(ElasticSearch)使用 NEST(ElasticSearch) 按多个值搜索
【发布时间】:2020-09-20 02:55:57
【问题描述】:

我有一个名为“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"
    }
  }
]

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    我不知道 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 以了解查询构造。

    【讨论】:

      【解决方案2】:

      你试过了吗?

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

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 2018-01-12
        • 1970-01-01
        • 2018-05-29
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        相关资源
        最近更新 更多