Search code examples
elasticsearchnest

Search by multiple values using 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"
    }
  }
]

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

How can I run this query using NEST?

Expected Result:

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

Solution

  • 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.

    Index mapping

    {
        "mappings": {
            "properties": {
                "PublisherId": {
                    "type": "integer"
                },
                "CurrentStatus": {
                    "type": "text"
                }
            }
        }
    }
    

    Index all three sample docs and use below search query

    {
        "query": {
            "bool": {
                "must": {
                    "term": {
                        "PublisherId": 1
                    }
                },
                "should": [
                    {
                        "match": {
                            "CurrentStatus": "Rejected"
                        }
                    },
                    {
                         "match": {
                            "CurrentStatus": "Approved"
                        }
                    }
                ],
                "minimum_should_match" : 1
            }
        }
    }
    

    Search Result

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

    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.