使用 NEST(ElasticSearch) 按多个值搜索 [英] Search by multiple values using NEST(ElasticSearch)

查看:85
本文介绍了使用 NEST(ElasticSearch) 按多个值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为广告系列"的索引;使用这些记录:

I have an index called "campaigns" with these records:

"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"的广告系列;以及介于已批准、已拒绝"之间的任何状态.像这样:

I want to get all campaigns with "PublisherId = 1" and with any statuses between "Approved,Rejected". Something like this:

var statuses = new[] {CampaignStatus.Approved,CampaignStatus.Rejected};
campaigns.Where(c=> c.PublisherId == 1 && statuses.Contains(c.CurrentStatus)).ToList();

如何使用 NEST 运行此查询?

How can I run this query using 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代码.

I don't know the syntax of nest but as ES is REST based , providing working example query in JSON format, which you can convert to nest code.

索引映射

{
    "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 之一匹配并引用 ES 中的 bool 查询 以了解查询结构.

Please note the use of minimum_should_match which forces atleast one of status Rejected and Approved to match and refer bool query in ES to understand the query construct.

这篇关于使用 NEST(ElasticSearch) 按多个值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆