Arsalan Valoojerdi
Arsalan Valoojerdi

Reputation: 1026

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

Upvotes: 1

Views: 668

Answers (2)

Muzaffer Galata
Muzaffer Galata

Reputation: 642

Did you try this?

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

Upvotes: 1

Amit
Amit

Reputation: 32386

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.

Upvotes: 1

Related Questions